Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gate transformers on valid non-nil destinations #211

Merged
merged 3 commits into from May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}