(t *testing.T)
| 8 | ) |
| 9 | |
| 10 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…