Skip to content

Commit

Permalink
function/stdlib: flatten allows objects containing unknown types
Browse files Browse the repository at this point in the history
Using HasDynamicTypes to see if the Flatten argument's type will be
unknown is a little too conservative, and will also catch dynamic values
within maps and objects. Check each value as we're iterating to find any
dynamic values.
  • Loading branch information
jbardin authored May 14, 2021
1 parent 7f88b46 commit 9d0deca
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
12 changes: 7 additions & 5 deletions cty/function/stdlib/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,16 +530,18 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
// predict the length of our result yet either.
return nil, markses, false
}
// Any dynamic types could result in more collection that need to be
// flattened, so the type cannot be known.
if flattenList.Type().HasDynamicTypes() {
return nil, markses, false
}

out := make([]cty.Value, 0)
isKnown := true
for it := flattenList.ElementIterator(); it.Next(); {
_, val := it.Element()

// 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) {
isKnown = false
}

if val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType() {
if !val.IsKnown() {
isKnown = false
Expand Down
52 changes: 51 additions & 1 deletion cty/function/stdlib/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,57 @@ func TestFlatten(t *testing.T) {
}).Mark("marked"),
}),
}),
cty.DynamicVal,
cty.DynamicVal.Mark("marked"),
"",
},
{
cty.TupleVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"blop": cty.ListVal([]cty.Value{
cty.DynamicVal,
}),
}),
}),
cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.DynamicVal,
}),
}),
}),
cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"blop": cty.ListVal([]cty.Value{
cty.DynamicVal,
}),
}),
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.DynamicVal,
}),
}),
"",
},
{
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.DynamicVal,
}),
}),
cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.DynamicVal,
}),
}),
}),
cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.DynamicVal,
}),
cty.ObjectVal(map[string]cty.Value{
"bloop": cty.DynamicVal,
}),
}),
"",
},
{
Expand Down

0 comments on commit 9d0deca

Please sign in to comment.