Commit 178a9b32 authored by Alisdair McDiarmid's avatar Alisdair McDiarmid
Browse files

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.
parent e9c7f37b
No related merge requests found
Showing with 30 additions and 7 deletions
+30 -7
......@@ -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 {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment