MCPcopy
hub / github.com/darccio/mergo / TestMergeWithTransformerZeroValue

Function TestMergeWithTransformerZeroValue

pr211_test.go:10–36  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8)
9
10func TestMergeWithTransformerZeroValue(t *testing.T) {
11 // This test specifically tests that a transformer can be used to
12 // prevent overwriting a zero value (in this case a bool). This would fail prior to #211
13 type fooWithBoolPtr struct {
14 b *bool
15 }
16 var Bool = func(b bool) *bool { return &b }
17 a := fooWithBoolPtr{b: Bool(false)}
18 b := fooWithBoolPtr{b: Bool(true)}
19
20 if err := mergo.Merge(&a, &b, mergo.WithTransformers(&transformer{
21 m: map[reflect.Type]func(dst, src reflect.Value) error{
22 reflect.TypeOf(Bool(false)): func(dst, src reflect.Value) error {
23 if dst.CanSet() && dst.IsNil() {
24 dst.Set(src)
25 }
26 return nil
27 },
28 },
29 })); err != nil {
30 t.Error(err)
31 }
32
33 if *a.b != false {
34 t.Errorf("b not merged in properly: a.b(%v) != expected(%v)", a.b, false)
35 }
36}

Callers

nothing calls this directly

Calls 2

MergeFunction · 0.92
WithTransformersFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…