Skip to content

Commit

Permalink
Merge pull request #1367 from Ace-Tang/fix_merge_map
Browse files Browse the repository at this point in the history
bugfix: fix map type can not be merged
  • Loading branch information
Ace-Tang authored May 24, 2018
2 parents 1adfb78 + 558d4d2 commit feb98c0
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 feb98c0

Please sign in to comment.