Skip to content

Commit

Permalink
bugfix: fix map type can not be merged
Browse files Browse the repository at this point in the history
add more merge map test

Signed-off-by: Ace-Tang <aceapril@126.com>
  • Loading branch information
Ace-Tang committed May 22, 2018
1 parent 1a982bf commit 558d4d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ func doMerge(src, dest reflect.Value) error {

case reflect.Map:
for _, key := range src.MapKeys() {
if err := doMerge(src.MapIndex(key), dest.MapIndex(key)); err != nil {
return err
srcElem := src.MapIndex(key)
if !srcElem.IsValid() || isEmptyValue(srcElem) {
continue
}
if dest.IsNil() {
dest.Set(reflect.MakeMap(dest.Type()))
}
dest.SetMapIndex(key, srcElem)
}

case reflect.Slice:
Expand Down
12 changes: 11 additions & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,19 @@ func TestMerge(t *testing.T) {
ok: true,
}, {
src: &simple{Sa: 1, Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo"}},
dest: &simple{Sa: 2, Sb: "world", Sc: false, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
dest: &simple{Sa: 2, Sb: "world", Sc: false, Sd: map[string]string{"go": "old"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
expected: &simple{Sa: 1, Sb: "world", Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo", "foo"}},
ok: true,
}, {
src: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
dest: &simple{Sd: map[string]string{"go": "old"}},
expected: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
ok: true,
}, {
src: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
dest: &simple{},
expected: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
ok: true,
},
} {
err := Merge(tm.src, tm.dest)
Expand Down

0 comments on commit 558d4d2

Please sign in to comment.