diff --git a/pkg/maputil/maputil.go b/pkg/maputil/maputil.go index 8df924ee..bf0c5c7a 100644 --- a/pkg/maputil/maputil.go +++ b/pkg/maputil/maputil.go @@ -231,6 +231,9 @@ func MergeMaps(a, b map[string]interface{}) map[string]interface{} { out[k] = v } for k, v := range b { + if v == nil { + continue + } if v, ok := v.(map[string]interface{}); ok { if bv, ok := out[k]; ok { if bv, ok := bv.(map[string]interface{}); ok { diff --git a/pkg/maputil/maputil_test.go b/pkg/maputil/maputil_test.go index 837c5b4e..74bd9b0a 100644 --- a/pkg/maputil/maputil_test.go +++ b/pkg/maputil/maputil_test.go @@ -215,6 +215,10 @@ func TestMapUtil_MergeMaps(t *testing.T) { "app1": 3, }, } + map5 := map[string]interface{}{ + "logLevel": "error", + "replicaCount": nil, + } testMap := MergeMaps(map2, map4) equal := reflect.DeepEqual(testMap, map4) @@ -247,4 +251,17 @@ func TestMapUtil_MergeMaps(t *testing.T) { if !equal { t.Errorf("Expected a map with different keys to merge properly with another map. Expected: %v, got %v", expectedMap, testMap) } + + testMap = MergeMaps(map3, map5) + expectedMap = map[string]interface{}{ + "logLevel": "error", + "replicaCount": map[string]any{ + "app1": 3, + "awesome": 4, + }, + } + equal = reflect.DeepEqual(testMap, expectedMap) + if !equal { + t.Errorf("Expected a map with empty value not to overwrite another map's value. Expected: %v, got %v", expectedMap, testMap) + } }