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():
|
case wantTy.IsMapType():
|
||||||
newVals := map[string]cty.Value{}
|
newVals := map[string]cty.Value{}
|
||||||
|
|
||||||
for it := input.ElementIterator(); it.Next(); {
|
if !input.IsNull() {
|
||||||
k, v := it.Element()
|
for it := input.ElementIterator(); it.Next(); {
|
||||||
newVals[k.AsString()] = defaultsApply(v, fallback)
|
k, v := it.Element()
|
||||||
|
newVals[k.AsString()] = defaultsApply(v, fallback)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(newVals) == 0 {
|
if len(newVals) == 0 {
|
||||||
|
@ -139,10 +141,12 @@ func defaultsApply(input, fallback cty.Value) cty.Value {
|
||||||
case wantTy.IsListType(), wantTy.IsSetType():
|
case wantTy.IsListType(), wantTy.IsSetType():
|
||||||
var newVals []cty.Value
|
var newVals []cty.Value
|
||||||
|
|
||||||
for it := input.ElementIterator(); it.Next(); {
|
if !input.IsNull() {
|
||||||
_, v := it.Element()
|
for it := input.ElementIterator(); it.Next(); {
|
||||||
newV := defaultsApply(v, fallback)
|
_, v := it.Element()
|
||||||
newVals = append(newVals, newV)
|
newV := defaultsApply(v, fallback)
|
||||||
|
newVals = append(newVals, newV)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(newVals) == 0 {
|
if len(newVals) == 0 {
|
||||||
|
|
|
@ -370,6 +370,25 @@ func TestDefaults(t *testing.T) {
|
||||||
Defaults: cty.StringVal("hello"),
|
Defaults: cty.StringVal("hello"),
|
||||||
WantErr: `only object types and collections of object types can have defaults applied`,
|
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 {
|
for _, test := range tests {
|
||||||
|
|
Loading…
Reference in New Issue