diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c87a4d8ba..a20f0c047 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -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: diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 12be73d80..43dc3acf3 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -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)