don't retain removed map values

Make sure values removed from a map during apply are not copied into the
new map. The broken test is no longer valid in this case, and the
updated diff.Apply should prevent the case it used to cover.
This commit is contained in:
James Bardin 2019-04-23 12:47:19 -04:00
parent 0f883b118a
commit 0696cf7245
2 changed files with 26 additions and 22 deletions

View File

@ -1110,7 +1110,6 @@ func stripSchema(s *schema.Schema) *schema.Schema {
// very uncommon nor was it reliable before 0.12 either. // very uncommon nor was it reliable before 0.12 either.
func normalizeNullValues(dst, src cty.Value, preferDst bool) cty.Value { func normalizeNullValues(dst, src cty.Value, preferDst bool) cty.Value {
ty := dst.Type() ty := dst.Type()
if !src.IsNull() && !src.IsKnown() { if !src.IsNull() && !src.IsKnown() {
// While this seems backwards to return src when preferDst is set, it // While this seems backwards to return src when preferDst is set, it
// means this might be a plan scenario, and it must retain unknown // means this might be a plan scenario, and it must retain unknown
@ -1154,7 +1153,12 @@ func normalizeNullValues(dst, src cty.Value, preferDst bool) cty.Value {
srcMap := src.AsValueMap() srcMap := src.AsValueMap()
for key, v := range srcMap { for key, v := range srcMap {
dstVal := dstMap[key] dstVal, ok := dstMap[key]
if !ok && !preferDst && ty.IsMapType() {
// don't transfer old map values to dst during apply
continue
}
if dstVal == cty.NilVal { if dstVal == cty.NilVal {
if preferDst && ty.IsMapType() { if preferDst && ty.IsMapType() {
// let plan shape this map however it wants // let plan shape this map however it wants

View File

@ -795,26 +795,6 @@ func TestNormalizeNullValues(t *testing.T) {
}))), }))),
}), }),
}, },
{
// Retain the zero value within the map
Src: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("a"),
"b": cty.StringVal(""),
}),
}),
Dst: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("a"),
}),
}),
Expect: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("a"),
"b": cty.StringVal(""),
}),
}),
},
{ {
// Retain don't re-add unexpected planned values in a map // Retain don't re-add unexpected planned values in a map
Src: cty.ObjectVal(map[string]cty.Value{ Src: cty.ObjectVal(map[string]cty.Value{
@ -835,6 +815,26 @@ func TestNormalizeNullValues(t *testing.T) {
}), }),
Plan: true, Plan: true,
}, },
{
// Remove extra values after apply
Src: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("a"),
"b": cty.StringVal("b"),
}),
}),
Dst: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("a"),
}),
}),
Expect: cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("a"),
}),
}),
Plan: false,
},
{ {
Src: cty.ObjectVal(map[string]cty.Value{ Src: cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("a"), "a": cty.StringVal("a"),