Skip to content

Commit

Permalink
fixed issue with not overriding of existing value of map, and added e…
Browse files Browse the repository at this point in the history
…xpect test
  • Loading branch information
nl5887 committed Jan 29, 2015
1 parent 67b9c0a commit 86cdca0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions merge.go
Expand Up @@ -53,8 +53,10 @@ func deepMerge(dst, src reflect.Value, visited map[uintptr]*visit, depth int) (e
if err = deepMerge(dstElement, srcElement, visited, depth+1); err != nil {
return
}
}
if !dstElement.IsValid() {
if !dstElement.IsValid() {
dst.SetMapIndex(key, srcElement)
}
default:
dst.SetMapIndex(key, srcElement)
}
}
Expand Down
15 changes: 13 additions & 2 deletions mergo_test.go
Expand Up @@ -136,17 +136,28 @@ func TestMaps(t *testing.T) {
m := map[string]simpleTest{
"a": simpleTest{},
"b": simpleTest{42},
"d": simpleTest{61},
}
n := map[string]simpleTest{
"a": simpleTest{16},
"b": simpleTest{},
"c": simpleTest{12},
"e": simpleTest{14},
}
expect := map[string]simpleTest{
"a": simpleTest{0},
"b": simpleTest{42},
"c": simpleTest{12},
"d": simpleTest{61},
"e": simpleTest{14},
}

if err := Merge(&m, n); err != nil {
t.Fatalf(err.Error())
}
if len(m) != 3 {
t.Fatalf(`n not merged in m properly, m must have 3 elements instead of %d`, len(m))

if !reflect.DeepEqual(m, expect) {
t.Fatalf("Test failed:\ngot :\n%#v\n\nwant :\n%#v\n\n", m, expect)
}
if m["a"].Value != 0 {
t.Fatalf(`n merged in m because I solved non-addressable map values TODO: m["a"].Value(%d) != n["a"].Value(%d)`, m["a"].Value, n["a"].Value)
Expand Down

0 comments on commit 86cdca0

Please sign in to comment.