Merge pull request #29928 from hashicorp/jbardin/ignore_null_map

`ignore_changes` converting null maps to empty maps, and applying marks.
This commit is contained in:
James Bardin 2021-11-12 14:32:31 -05:00 committed by GitHub
commit 33bcc715a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 4 deletions

View File

@ -1289,14 +1289,19 @@ func processIgnoreChangesIndividual(prior, config cty.Value, ignoreChangesPath [
} }
var newVal cty.Value var newVal cty.Value
if len(configMap) == 0 { switch {
newVal = cty.MapValEmpty(v.Type().ElementType()) case len(configMap) > 0:
} else {
newVal = cty.MapVal(configMap) newVal = cty.MapVal(configMap)
case v.IsNull():
// if the config value was null, and no values remain in the map,
// reset the value to null.
newVal = v
default:
newVal = cty.MapValEmpty(v.Type().ElementType())
} }
if len(vMarks) > 0 { if len(vMarks) > 0 {
newVal = v.WithMarks(vMarks) newVal = newVal.WithMarks(vMarks)
} }
return newVal, nil return newVal, nil

View File

@ -369,6 +369,54 @@ func TestProcessIgnoreChangesIndividual(t *testing.T) {
"b": cty.StringVal("new b value"), "b": cty.StringVal("new b value"),
}), }),
}, },
"null_map": {
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("ok"),
"list": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"s": cty.StringVal("ok"),
"map": cty.NullVal(cty.Map(cty.String)),
}),
}),
}),
cty.ObjectVal(map[string]cty.Value{
"a": cty.NullVal(cty.String),
"list": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"s": cty.StringVal("ok"),
"map": cty.NullVal(cty.Map(cty.String)),
}),
}),
}),
[]string{"a"},
cty.ObjectVal(map[string]cty.Value{
"a": cty.StringVal("ok"),
"list": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"s": cty.StringVal("ok"),
"map": cty.NullVal(cty.Map(cty.String)),
}),
}),
}),
},
"marked_map": {
cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key": cty.StringVal("val"),
}).Mark("marked"),
}),
cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key": cty.StringVal("new val"),
}).Mark("marked"),
}),
[]string{`map["key"]`},
cty.ObjectVal(map[string]cty.Value{
"map": cty.MapVal(map[string]cty.Value{
"key": cty.StringVal("val"),
}).Mark("marked"),
}),
},
} }
for name, test := range tests { for name, test := range tests {