fix bug in ignore_changes Transform
The cty.Transform for ignore_changes could return early when building a map that had multiple ignored keys. Refactor the function to try and separate the fast-path a little better, and hopefully make it easier to follow.
This commit is contained in:
parent
2c2ed53ff3
commit
aa5c8add2e
|
@ -664,47 +664,58 @@ func processIgnoreChangesIndividual(prior, config cty.Value, ignoreChanges []hcl
|
|||
}
|
||||
|
||||
ret, _ := cty.Transform(config, func(path cty.Path, v cty.Value) (cty.Value, error) {
|
||||
// Easy path for when we are only matching the entire value. The only
|
||||
// values we break up for inspection are maps.
|
||||
if !v.Type().IsMapType() {
|
||||
for _, ignored := range ignoredValues {
|
||||
if path.Equals(ignored.path) {
|
||||
return ignored.value, nil
|
||||
}
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
// We now know this must be a map, so we need to accumulate the values
|
||||
// key-by-key.
|
||||
|
||||
if !v.IsNull() && !v.IsKnown() {
|
||||
// since v is not known, we cannot ignore individual keys
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// The configMap is the current configuration value, which we will
|
||||
// mutate based on the ignored paths and the prior map value.
|
||||
var configMap map[string]cty.Value
|
||||
switch {
|
||||
case v.IsNull() || v.LengthInt() == 0:
|
||||
configMap = map[string]cty.Value{}
|
||||
default:
|
||||
configMap = v.AsValueMap()
|
||||
}
|
||||
|
||||
for _, ignored := range ignoredValues {
|
||||
if !path.Equals(ignored.path) {
|
||||
return v, nil
|
||||
continue
|
||||
}
|
||||
|
||||
// no index, so we can return the entire value
|
||||
if ignored.key.IsNull() {
|
||||
// The map address is confirmed to match at this point,
|
||||
// so if there is no key, we want the entire map and can
|
||||
// stop accumulating values.
|
||||
return ignored.value, nil
|
||||
}
|
||||
|
||||
// we have an index key, so make sure we have a map
|
||||
if !v.Type().IsMapType() {
|
||||
// we'll let other validation catch any type mismatch
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// Now we know we are ignoring a specific index of this map, so get
|
||||
// the config map and modify, add, or remove the desired key.
|
||||
var configMap map[string]cty.Value
|
||||
var priorMap map[string]cty.Value
|
||||
|
||||
if !v.IsNull() {
|
||||
if !v.IsKnown() {
|
||||
// if the entire map is not known, we can't ignore any
|
||||
// specific keys yet.
|
||||
continue
|
||||
}
|
||||
configMap = v.AsValueMap()
|
||||
}
|
||||
if configMap == nil {
|
||||
configMap = map[string]cty.Value{}
|
||||
}
|
||||
|
||||
// We also need to create a prior map, so we can check for
|
||||
// existence while getting the value. Value.Index will always
|
||||
// return null.
|
||||
if !ignored.value.IsNull() {
|
||||
priorMap = ignored.value.AsValueMap()
|
||||
}
|
||||
if priorMap == nil {
|
||||
// existence while getting the value, because Value.Index will
|
||||
// return null for a key with a null value and for a non-existent
|
||||
// key.
|
||||
var priorMap map[string]cty.Value
|
||||
switch {
|
||||
case ignored.value.IsNull() || ignored.value.LengthInt() == 0:
|
||||
priorMap = map[string]cty.Value{}
|
||||
default:
|
||||
priorMap = ignored.value.AsValueMap()
|
||||
}
|
||||
|
||||
key := ignored.key.AsString()
|
||||
|
@ -718,14 +729,13 @@ func processIgnoreChangesIndividual(prior, config cty.Value, ignoreChanges []hcl
|
|||
default:
|
||||
configMap[key] = priorElem
|
||||
}
|
||||
|
||||
if len(configMap) == 0 {
|
||||
return cty.MapValEmpty(v.Type().ElementType()), nil
|
||||
}
|
||||
|
||||
return cty.MapVal(configMap), nil
|
||||
}
|
||||
return v, nil
|
||||
|
||||
if len(configMap) == 0 {
|
||||
return cty.MapValEmpty(v.Type().ElementType()), nil
|
||||
}
|
||||
|
||||
return cty.MapVal(configMap), nil
|
||||
})
|
||||
return ret, nil
|
||||
}
|
||||
|
|
|
@ -136,6 +136,53 @@ func TestProcessIgnoreChangesIndividual(t *testing.T) {
|
|||
"b": cty.StringVal("b value"),
|
||||
}),
|
||||
},
|
||||
"map_index_multiple_keys": {
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.MapVal(map[string]cty.Value{
|
||||
"a0": cty.StringVal("a0 value"),
|
||||
"a1": cty.StringVal("a1 value"),
|
||||
"a2": cty.StringVal("a2 value"),
|
||||
"a3": cty.StringVal("a3 value"),
|
||||
}),
|
||||
"b": cty.StringVal("b value"),
|
||||
}),
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.NullVal(cty.Map(cty.String)),
|
||||
"b": cty.StringVal("new b value"),
|
||||
}),
|
||||
[]string{`a["a1"]`, `a["a2"]`, `a["a3"]`, `b`},
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.MapVal(map[string]cty.Value{
|
||||
"a1": cty.StringVal("a1 value"),
|
||||
"a2": cty.StringVal("a2 value"),
|
||||
"a3": cty.StringVal("a3 value"),
|
||||
}),
|
||||
"b": cty.StringVal("b value"),
|
||||
}),
|
||||
},
|
||||
"map_index_redundant": {
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.MapVal(map[string]cty.Value{
|
||||
"a0": cty.StringVal("a0 value"),
|
||||
"a1": cty.StringVal("a1 value"),
|
||||
"a2": cty.StringVal("a2 value"),
|
||||
}),
|
||||
"b": cty.StringVal("b value"),
|
||||
}),
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.NullVal(cty.Map(cty.String)),
|
||||
"b": cty.StringVal("new b value"),
|
||||
}),
|
||||
[]string{`a["a1"]`, `a`, `b`},
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.MapVal(map[string]cty.Value{
|
||||
"a0": cty.StringVal("a0 value"),
|
||||
"a1": cty.StringVal("a1 value"),
|
||||
"a2": cty.StringVal("a2 value"),
|
||||
}),
|
||||
"b": cty.StringVal("b value"),
|
||||
}),
|
||||
},
|
||||
"missing_map_index": {
|
||||
cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.MapVal(map[string]cty.Value{
|
||||
|
|
Loading…
Reference in New Issue