Skip to content

Commit

Permalink
Merge pull request #29928 from hashicorp/jbardin/ignore_null_map
Browse files Browse the repository at this point in the history
`ignore_changes` converting null maps to empty maps, and applying marks.
  • Loading branch information
jbardin committed Nov 12, 2021
2 parents e07a7c4 + 9d19086 commit 33bcc71
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/terraform/node_resource_abstract_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,14 +1289,19 @@ func processIgnoreChangesIndividual(prior, config cty.Value, ignoreChangesPath [
}

var newVal cty.Value
if len(configMap) == 0 {
newVal = cty.MapValEmpty(v.Type().ElementType())
} else {
switch {
case len(configMap) > 0:
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 {
newVal = v.WithMarks(vMarks)
newVal = newVal.WithMarks(vMarks)
}

return newVal, nil
Expand Down
48 changes: 48 additions & 0 deletions internal/terraform/reduce_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,54 @@ func TestProcessIgnoreChangesIndividual(t *testing.T) {
"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 {
Expand Down

0 comments on commit 33bcc71

Please sign in to comment.