-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from imdario/fix/pr-56
Pull request #56 merged after passing tests in Travis CI
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package mergo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type transformer struct { | ||
m map[reflect.Type]func(dst, src reflect.Value) error | ||
} | ||
|
||
func (s *transformer) Transformer(t reflect.Type) func(dst, src reflect.Value) error { | ||
if fn, ok := s.m[t]; ok { | ||
return fn | ||
} | ||
return nil | ||
} | ||
|
||
type foo struct { | ||
s string | ||
Bar *bar | ||
} | ||
|
||
type bar struct { | ||
i int | ||
s map[string]string | ||
} | ||
|
||
func TestMergeWithTransformerNilStruct(t *testing.T) { | ||
a := foo{s: "foo"} | ||
b := foo{Bar: &bar{i: 2, s: map[string]string{"foo": "bar"}}} | ||
if err := Merge(&a, &b, WithOverride, WithTransformers(&transformer{ | ||
m: map[reflect.Type]func(dst, src reflect.Value) error{ | ||
reflect.TypeOf(&bar{}): func(dst, src reflect.Value) error { | ||
// Do sthg with Elem | ||
t.Log(dst.Elem().FieldByName("i")) | ||
t.Log(src.Elem()) | ||
return nil | ||
}, | ||
}, | ||
})); err != nil { | ||
t.Fatal(err) | ||
} | ||
if a.s != "foo" { | ||
t.Fatalf("b not merged in properly: a.s.Value(%s) != expected(%s)", a.s, "foo") | ||
} | ||
if a.Bar == nil { | ||
t.Fatalf("b not merged in properly: a.Bar shouldn't be nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters