Skip to content

Commit

Permalink
Merge pull request #211 from zaquestion/transformer_valid_destination
Browse files Browse the repository at this point in the history
fix: gate transformers on valid non-nil destinations
  • Loading branch information
darccio committed May 24, 2022
2 parents fd3dfc9 + 8109749 commit fd7d2bc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion merge.go
Expand Up @@ -79,7 +79,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
visited[h] = &visit{addr, typ, seen}
}

if config.Transformers != nil && !isEmptyValue(dst) {
if config.Transformers != nil && !isReflectNil(dst) && dst.IsValid() {
if fn := config.Transformers.Transformer(dst.Type()); fn != nil {
err = fn(dst, src)
return
Expand Down
25 changes: 25 additions & 0 deletions pr211_2_test.go
@@ -0,0 +1,25 @@
package mergo

import (
"reflect"
"testing"
"time"
)

type transformer struct {
}

func (s *transformer) Transformer(t reflect.Type) func(dst, src reflect.Value) error {
return nil
}

func Test_deepMergeTransformerInvalidDestination(t *testing.T) {
foo := time.Time{}
src := reflect.ValueOf(foo)
deepMerge(reflect.Value{}, src, make(map[uintptr]*visit), 0, &Config{
Transformers: &transformer{},
})
// this test is intentionally not asserting on anything, it's sole
// purpose to verify deepMerge doesn't panic when a transformer is
// passed and the destination is invalid.
}
36 changes: 36 additions & 0 deletions pr211_test.go
@@ -0,0 +1,36 @@
package mergo_test

import (
"reflect"
"testing"

"github.com/imdario/mergo"
)

func TestMergeWithTransformerZeroValue(t *testing.T) {
// This test specifically tests that a transformer can be used to
// prevent overwriting a zero value (in this case a bool). This would fail prior to #211
type fooWithBoolPtr struct {
b *bool
}
var Bool = func(b bool) *bool { return &b }
a := fooWithBoolPtr{b: Bool(false)}
b := fooWithBoolPtr{b: Bool(true)}

if err := mergo.Merge(&a, &b, mergo.WithTransformers(&transformer{
m: map[reflect.Type]func(dst, src reflect.Value) error{
reflect.TypeOf(Bool(false)): func(dst, src reflect.Value) error {
if dst.CanSet() && dst.IsNil() {
dst.Set(src)
}
return nil
},
},
})); err != nil {
t.Error(err)
}

if *a.b != false {
t.Errorf("b not merged in properly: a.b(%v) != expected(%v)", a.b, false)
}
}

0 comments on commit fd7d2bc

Please sign in to comment.