Skip to content

Commit feb98c0

Browse files
authored
Merge pull request #1367 from Ace-Tang/fix_merge_map
bugfix: fix map type can not be merged
2 parents 1adfb78 + 558d4d2 commit feb98c0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pkg/utils/utils.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,14 @@ func doMerge(src, dest reflect.Value) error {
176176

177177
case reflect.Map:
178178
for _, key := range src.MapKeys() {
179-
if err := doMerge(src.MapIndex(key), dest.MapIndex(key)); err != nil {
180-
return err
179+
srcElem := src.MapIndex(key)
180+
if !srcElem.IsValid() || isEmptyValue(srcElem) {
181+
continue
182+
}
183+
if dest.IsNil() {
184+
dest.Set(reflect.MakeMap(dest.Type()))
181185
}
186+
dest.SetMapIndex(key, srcElem)
182187
}
183188

184189
case reflect.Slice:

pkg/utils/utils_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,19 @@ func TestMerge(t *testing.T) {
191191
ok: true,
192192
}, {
193193
src: &simple{Sa: 1, Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo"}},
194-
dest: &simple{Sa: 2, Sb: "world", Sc: false, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
194+
dest: &simple{Sa: 2, Sb: "world", Sc: false, Sd: map[string]string{"go": "old"}, Se: nestS{Na: 22}, Sf: []string{"foo"}},
195195
expected: &simple{Sa: 1, Sb: "world", Sc: true, Sd: map[string]string{"go": "gogo"}, Se: nestS{Na: 11}, Sf: []string{"foo", "foo"}},
196196
ok: true,
197+
}, {
198+
src: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
199+
dest: &simple{Sd: map[string]string{"go": "old"}},
200+
expected: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
201+
ok: true,
202+
}, {
203+
src: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
204+
dest: &simple{},
205+
expected: &simple{Sd: map[string]string{"go": "gogo", "a": "b"}},
206+
ok: true,
197207
},
198208
} {
199209
err := Merge(tm.src, tm.dest)

0 commit comments

Comments
 (0)