From 258c11b15ea3d315885636876c5926ea1a229f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Casta=C3=B1=C3=A9?= Date: Sat, 18 Jul 2020 00:52:43 +0200 Subject: [PATCH] Issue #149 fixed --- go.mod | 5 ++++- go.sum | 2 ++ issue149_test.go | 37 +++++++++++++++++++++++++++++++++++++ merge.go | 12 ++++++++++++ mergo_test.go | 2 +- 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 issue149_test.go diff --git a/go.mod b/go.mod index 11768bf..51a8630 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/aivetech/mergo go 1.13 -require gopkg.in/yaml.v2 v2.3.0 +require ( + github.com/imdario/mergo v0.3.9 // indirect + gopkg.in/yaml.v2 v2.3.0 +) diff --git a/go.sum b/go.sum index 8fabe8d..597e808 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= +github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/issue149_test.go b/issue149_test.go new file mode 100644 index 0000000..c6b5c0c --- /dev/null +++ b/issue149_test.go @@ -0,0 +1,37 @@ +package mergo + +import ( + "testing" +) + +type user struct { + Name string +} + +type token struct { + User *user + Token *string +} + +func TestIssue149(t *testing.T) { + dest := &token{ + User: &user{ + Name: "destination", + }, + Token: nil, + } + tokenValue := "Issue149" + src := &token{ + User: nil, + Token: &tokenValue, + } + if err := Merge(dest, src, WithOverwriteWithEmptyValue); err != nil { + t.Error(err) + } + if dest.User != nil { + t.Errorf("expected nil User, got %q", dest.User) + } + if dest.Token == nil { + t.Errorf("expected not nil Token, got %q", *dest.Token) + } +} diff --git a/merge.go b/merge.go index ac904f2..c35fd53 100644 --- a/merge.go +++ b/merge.go @@ -102,6 +102,14 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co if dst.IsNil() && !src.IsNil() { dst.Set(reflect.MakeMap(dst.Type())) } + + if src.Kind() != reflect.Map { + if overwrite { + dst.Set(src) + } + return + } + for _, key := range src.MapKeys() { srcElement := src.MapIndex(key) if !srcElement.IsValid() { @@ -219,6 +227,9 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co fallthrough case reflect.Interface: if src.IsNil() { + if overwriteWithEmptySrc && dst.CanSet() && src.Type().AssignableTo(dst.Type()) { + dst.Set(src) + } break } @@ -249,6 +260,7 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co } break } + if dst.IsNil() || overwrite { if dst.CanSet() && (overwrite || isEmptyValue(dst)) { dst.Set(src) diff --git a/mergo_test.go b/mergo_test.go index ca608bb..d63c876 100644 --- a/mergo_test.go +++ b/mergo_test.go @@ -529,7 +529,7 @@ func TestMergeUsingStructAndMap(t *testing.T) { t.Error(err) } if !reflect.DeepEqual(tc.target, tc.output) { - t.Errorf("Test failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", tc.target, tc.output) + t.Errorf("Test failed:\ngot :\n%+v\n\nwant :\n%+v\n\n", tc.target.Params, tc.output.Params) } }) }