Merge pull request #27237 from hashicorp/alisdair/alltrue-anytrue-unknown

lang/funcs: Fix alltrue/anytrue with unknowns
This commit is contained in:
Alisdair McDiarmid 2020-12-10 12:44:48 -05:00 committed by GitHub
commit 67f4134628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -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
}
@ -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
}
@ -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
},
})

View File

@ -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)),
@ -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)),