Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for typed nulls in flatten func #129

Merged
merged 1 commit into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion cty/function/stdlib/collection.go
Expand Up @@ -525,6 +525,7 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
if len(flattenListMarks) > 0 {
markses = append(markses, flattenListMarks)
}

if !flattenList.Length().IsKnown() {
// If we don't know the length of what we're flattening then we can't
// predict the length of our result yet either.
Expand All @@ -542,7 +543,7 @@ func flattener(flattenList cty.Value) ([]cty.Value, []cty.ValueMarks, bool) {
isKnown = false
}

if val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType() {
if !val.IsNull() && (val.Type().IsListType() || val.Type().IsSetType() || val.Type().IsTupleType()) {
if !val.IsKnown() {
isKnown = false
_, unknownMarks := val.Unmark()
Expand Down
104 changes: 104 additions & 0 deletions cty/function/stdlib/collection_test.go
Expand Up @@ -2064,6 +2064,110 @@ func TestFlatten(t *testing.T) {
cty.UnknownVal(cty.DynamicPseudoType),
"",
},
{
// null of an unknown type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.True,
}),
"",
},
{
// null of a string type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
cty.True,
}),
"",
},
{
// null of a list type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
cty.True,
}),
"",
},
{
// null of a tuple type
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
cty.True,
}),
"",
},
{
// nested null of an unknown type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.DynamicPseudoType),
cty.True,
}),
"",
},
{
// nested null of a string type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.String),
cty.True,
}),
"",
},
{
// nested null of a list type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.List(cty.String)),
cty.True,
}),
"",
},
{
// nested null of a tuple type
cty.TupleVal([]cty.Value{
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
}),
cty.True,
}),
cty.TupleVal([]cty.Value{
cty.NullVal(cty.EmptyTuple),
cty.True,
}),
"",
},
}

for _, test := range tests {
Expand Down