Skip to content

Commit

Permalink
Merge pull request hashicorp#27237 from hashicorp/alisdair/alltrue-an…
Browse files Browse the repository at this point in the history
…ytrue-unknown

lang/funcs: Fix alltrue/anytrue with unknowns
  • Loading branch information
alisdair authored Dec 10, 2020
2 parents c8216a8 + df626e8 commit 67f4134
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lang/funcs/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ var AllTrueFunc = function.New(&function.Spec{
result := cty.True
for it := args[0].ElementIterator(); it.Next(); {
_, v := it.Element()
if !v.IsKnown() {
return cty.UnknownVal(cty.Bool), nil
}
if v.IsNull() {
return cty.False, nil
}
Expand All @@ -94,8 +97,13 @@ var AnyTrueFunc = function.New(&function.Spec{
Type: function.StaticReturnType(cty.Bool),
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
result := cty.False
var hasUnknown bool
for it := args[0].ElementIterator(); it.Next(); {
_, v := it.Element()
if !v.IsKnown() {
hasUnknown = true
continue
}
if v.IsNull() {
continue
}
Expand All @@ -104,6 +112,9 @@ var AnyTrueFunc = function.New(&function.Spec{
return cty.True, nil
}
}
if hasUnknown {
return cty.UnknownVal(cty.Bool), nil
}
return result, nil
},
})
Expand Down
48 changes: 46 additions & 2 deletions lang/funcs/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,28 @@ func TestAllTrue(t *testing.T) {
cty.False,
false,
},
{
cty.ListVal([]cty.Value{cty.True, cty.NullVal(cty.Bool)}),
cty.False,
false,
},
{
cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}),
cty.UnknownVal(cty.Bool),
true,
false,
},
{
cty.ListVal([]cty.Value{
cty.UnknownVal(cty.Bool),
cty.UnknownVal(cty.Bool),
}),
cty.UnknownVal(cty.Bool),
false,
},
{
cty.UnknownVal(cty.List(cty.Bool)),
cty.UnknownVal(cty.Bool),
false,
},
{
cty.NullVal(cty.List(cty.Bool)),
Expand Down Expand Up @@ -232,10 +250,36 @@ func TestAnyTrue(t *testing.T) {
cty.True,
false,
},
{
cty.ListVal([]cty.Value{cty.NullVal(cty.Bool), cty.True}),
cty.True,
false,
},
{
cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}),
cty.UnknownVal(cty.Bool),
true,
false,
},
{
cty.ListVal([]cty.Value{
cty.UnknownVal(cty.Bool),
cty.False,
}),
cty.UnknownVal(cty.Bool),
false,
},
{
cty.ListVal([]cty.Value{
cty.UnknownVal(cty.Bool),
cty.True,
}),
cty.True,
false,
},
{
cty.UnknownVal(cty.List(cty.Bool)),
cty.UnknownVal(cty.Bool),
false,
},
{
cty.NullVal(cty.List(cty.Bool)),
Expand Down

0 comments on commit 67f4134

Please sign in to comment.