Skip to content

Commit

Permalink
Merge pull request #65 from nbrownus/nil-maps
Browse files Browse the repository at this point in the history
Handle nil maps in dst
  • Loading branch information
darccio committed Apr 2, 2018
2 parents b4c3521 + 2c0ce3a commit 0919279
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ install:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
script:
- $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN
- $HOME/gopath/bin/goveralls -service=travis-ci
22 changes: 22 additions & 0 deletions issue61_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mergo

import (
"reflect"
"testing"
)

func TestIssue61MergeNilMap(t *testing.T) {
type T struct {
I map[string][]string
}

t1 := T{}
t2 := T{I: map[string][]string{"hi": {"there"}}}
if err := Merge(&t1, t2); err != nil {
t.Fail()
}

if !reflect.DeepEqual(t2, T{I: map[string][]string{"hi": {"there"}}}) {
t.FailNow()
}
}
3 changes: 1 addition & 2 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int, co
}
}
case reflect.Map:
if len(src.MapKeys()) == 0 && !src.IsNil() && len(dst.MapKeys()) == 0 {
if dst.IsNil() && !src.IsNil() {
dst.Set(reflect.MakeMap(dst.Type()))
return
}
for _, key := range src.MapKeys() {
srcElement := src.MapIndex(key)
Expand Down

0 comments on commit 0919279

Please sign in to comment.