forked from darccio/mergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_test.go
88 lines (75 loc) · 1.62 KB
/
merge_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package mergo_test
import (
"reflect"
"testing"
"github.com/imdario/mergo"
)
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 := mergo.Merge(&a, &b, mergo.WithOverride, mergo.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.Error(err)
}
if a.s != "foo" {
t.Errorf("b not merged in properly: a.s.Value(%s) != expected(%s)", a.s, "foo")
}
if a.Bar == nil {
t.Errorf("b not merged in properly: a.Bar shouldn't be nil")
}
}
func TestMergeNonPointer(t *testing.T) {
dst := bar{
i: 1,
}
src := bar{
i: 2,
s: map[string]string{
"a": "1",
},
}
want := mergo.ErrNonPointerAgument
if got := mergo.Merge(dst, src); got != want {
t.Errorf("want: %s, got: %s", want, got)
}
}
func TestMapNonPointer(t *testing.T) {
dst := make(map[string]bar)
src := map[string]bar{
"a": {
i: 2,
s: map[string]string{
"a": "1",
},
},
}
want := mergo.ErrNonPointerAgument
if got := mergo.Merge(dst, src); got != want {
t.Errorf("want: %s, got: %s", want, got)
}
}