functions: Fix defaults null collections panic
When applying default values to collection types, null collections in the input should result in empty collections in the output.
This commit is contained in:
parent
e9c7f37b8c
commit
178a9b32d7
|
@ -127,9 +127,11 @@ func defaultsApply(input, fallback cty.Value) cty.Value {
|
|||
case wantTy.IsMapType():
|
||||
newVals := map[string]cty.Value{}
|
||||
|
||||
for it := input.ElementIterator(); it.Next(); {
|
||||
k, v := it.Element()
|
||||
newVals[k.AsString()] = defaultsApply(v, fallback)
|
||||
if !input.IsNull() {
|
||||
for it := input.ElementIterator(); it.Next(); {
|
||||
k, v := it.Element()
|
||||
newVals[k.AsString()] = defaultsApply(v, fallback)
|
||||
}
|
||||
}
|
||||
|
||||
if len(newVals) == 0 {
|
||||
|
@ -139,10 +141,12 @@ func defaultsApply(input, fallback cty.Value) cty.Value {
|
|||
case wantTy.IsListType(), wantTy.IsSetType():
|
||||
var newVals []cty.Value
|
||||
|
||||
for it := input.ElementIterator(); it.Next(); {
|
||||
_, v := it.Element()
|
||||
newV := defaultsApply(v, fallback)
|
||||
newVals = append(newVals, newV)
|
||||
if !input.IsNull() {
|
||||
for it := input.ElementIterator(); it.Next(); {
|
||||
_, v := it.Element()
|
||||
newV := defaultsApply(v, fallback)
|
||||
newVals = append(newVals, newV)
|
||||
}
|
||||
}
|
||||
|
||||
if len(newVals) == 0 {
|
||||
|
|
|
@ -370,6 +370,25 @@ func TestDefaults(t *testing.T) {
|
|||
Defaults: cty.StringVal("hello"),
|
||||
WantErr: `only object types and collections of object types can have defaults applied`,
|
||||
},
|
||||
// When applying default values to collection types, null collections in the
|
||||
// input should result in empty collections in the output.
|
||||
{
|
||||
Input: cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.NullVal(cty.List(cty.String)),
|
||||
"b": cty.NullVal(cty.Map(cty.String)),
|
||||
"c": cty.NullVal(cty.Set(cty.String)),
|
||||
}),
|
||||
Defaults: cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.StringVal("hello"),
|
||||
"b": cty.StringVal("hi"),
|
||||
"c": cty.StringVal("greetings"),
|
||||
}),
|
||||
Want: cty.ObjectVal(map[string]cty.Value{
|
||||
"a": cty.ListValEmpty(cty.String),
|
||||
"b": cty.MapValEmpty(cty.String),
|
||||
"c": cty.SetValEmpty(cty.String),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
|
Loading…
Reference in New Issue