Skip to content

Commit

Permalink
skip null values in flatten
Browse files Browse the repository at this point in the history
When flattening a list of collections, we can always skip null values,
even if the type of that value was unknown.
  • Loading branch information
jbardin committed Jul 6, 2021
1 parent f40c78a commit 8fdc150
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cty/function/stdlib/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
for it := flattenList.ElementIterator(); it.Next(); {
_, val := it.Element()

if val.IsNull() {
// If the collection is null, there is nothing to append even if
// the type was unknown.
continue
}

// Any dynamic types could result in more collections that need to be
// flattened, so the type cannot be known.
if val.Type().Equals(cty.DynamicPseudoType) {
Expand Down
18 changes: 18 additions & 0 deletions cty/function/stdlib/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,24 @@ func TestFlatten(t *testing.T) {
}),
"",
},
{
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
}),
cty.NullVal(cty.DynamicPseudoType),
cty.TupleVal([]cty.Value{
cty.StringVal("c"),
}),
}),
cty.TupleVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
"",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 8fdc150

Please sign in to comment.