Fix nested null value overrides

Signed-off-by: Mingxiang Xue <mingxiangxue@gmail.com>
pull/7743/head
Mingxiang Xue 6 years ago
parent 1940f500fd
commit c0058b53ec

@ -186,19 +186,18 @@ func CoalesceTables(dst, src map[string]interface{}) map[string]interface{} {
// Because dest has higher precedence than src, dest values override src
// values.
for key, val := range src {
if istable(val) {
switch innerdst, ok := dst[key]; {
case !ok:
if dv, ok := dst[key]; ok && dv == nil {
delete(dst, key)
} else if !ok {
dst[key] = val
case istable(innerdst):
CoalesceTables(innerdst.(map[string]interface{}), val.(map[string]interface{}))
default:
} else if istable(val) {
if istable(dv) {
CoalesceTables(dv.(map[string]interface{}), val.(map[string]interface{}))
} else {
log.Printf("warning: cannot overwrite table with non table for %s (%v)", key, val)
}
} else if dv, ok := dst[key]; ok && istable(dv) {
} else if istable(dv) {
log.Printf("warning: destination for %s is a table. Ignoring non-table value %v", key, val)
} else if !ok { // <- ok is still in scope from preceding conditional.
dst[key] = val
}
}
return dst

@ -31,6 +31,8 @@ right: Null
left: NULL
front: ~
back: ""
nested:
boat: null
global:
name: Ishmael
@ -114,6 +116,10 @@ func TestCoalesceValues(t *testing.T) {
}
}
if _, ok := v["nested"].(map[string]interface{})["boat"]; ok {
t.Error("Expected nested boat key to be removed, still present")
}
// CoalesceValues should not mutate the passed arguments
is.Equal(valsCopy, vals)
}
@ -124,22 +130,26 @@ func TestCoalesceTables(t *testing.T) {
"address": map[string]interface{}{
"street": "123 Spouter Inn Ct.",
"city": "Nantucket",
"country": nil,
},
"details": map[string]interface{}{
"friends": []string{"Tashtego"},
},
"boat": "pequod",
"hole": nil,
}
src := map[string]interface{}{
"occupation": "whaler",
"address": map[string]interface{}{
"state": "MA",
"street": "234 Spouter Inn Ct.",
"country": "US",
},
"details": "empty",
"boat": map[string]interface{}{
"mast": true,
},
"hole": "black",
}
// What we expect is that anything in dst overrides anything in src, but that
@ -170,6 +180,10 @@ func TestCoalesceTables(t *testing.T) {
t.Errorf("Unexpected state: %v", addr["state"])
}
if _, ok = addr["country"]; ok {
t.Error("The country is not left out.")
}
if det, ok := dst["details"].(map[string]interface{}); !ok {
t.Fatalf("Details is the wrong type: %v", dst["details"])
} else if _, ok := det["friends"]; !ok {
@ -179,4 +193,8 @@ func TestCoalesceTables(t *testing.T) {
if dst["boat"].(string) != "pequod" {
t.Errorf("Expected boat string, got %v", dst["boat"])
}
if _, ok = dst["hole"]; ok {
t.Error("The hole still exists.")
}
}

@ -7,3 +7,5 @@ right: exists
left: exists
front: exists
back: exists
nested:
boat: true

Loading…
Cancel
Save