fix(maputil): prevent nil value overwrite (#1755)
Signed-off-by: zhengbayi <ban11111@qq.com> Co-authored-by: zhengbayi <zhengbaiyi@sensetime.com>
This commit is contained in:
		
							parent
							
								
									d1416ec7b4
								
							
						
					
					
						commit
						5837672bfa
					
				|  | @ -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 { | ||||
|  |  | |||
|  | @ -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) | ||||
| 	} | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue