Skip to content

Commit

Permalink
Issue #149 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Jul 17, 2020
1 parent 431e3df commit 258c11b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
37 changes: 37 additions & 0 deletions issue149_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
12 changes: 12 additions & 0 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion mergo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
Expand Down

0 comments on commit 258c11b

Please sign in to comment.