mirror of https://github.com/hashicorp/packer
this function add the hcl2 anytrue function which takes a collection and return true if any of the element is true.pull/13263/head
parent
476ddc3810
commit
cb4965d53a
@ -0,0 +1,44 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package function
|
||||
|
||||
import (
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/function"
|
||||
)
|
||||
|
||||
// AnyTrue constructs a function that returns true if a single element of
|
||||
// the list is true. If the list is empty, return false.
|
||||
var AnyTrue = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "list",
|
||||
Type: cty.List(cty.Bool),
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.Bool),
|
||||
RefineResult: refineNotNull,
|
||||
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
|
||||
}
|
||||
result = result.Or(v)
|
||||
if result.True() {
|
||||
return cty.True, nil
|
||||
}
|
||||
}
|
||||
if hasUnknown {
|
||||
return cty.UnknownVal(cty.Bool), nil
|
||||
}
|
||||
return result, nil
|
||||
},
|
||||
})
|
||||
@ -0,0 +1,89 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package function
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
func TestAnyTrue(t *testing.T) {
|
||||
tests := []struct {
|
||||
Collection cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.ListValEmpty(cty.Bool),
|
||||
cty.False,
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{cty.True}),
|
||||
cty.True,
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{cty.False}),
|
||||
cty.False,
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{cty.True, cty.False}),
|
||||
cty.True,
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{cty.False, cty.True}),
|
||||
cty.True,
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{cty.True, cty.NullVal(cty.Bool)}),
|
||||
cty.True,
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}),
|
||||
cty.UnknownVal(cty.Bool).RefineNotNull(),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.ListVal([]cty.Value{
|
||||
cty.UnknownVal(cty.Bool),
|
||||
cty.UnknownVal(cty.Bool),
|
||||
}),
|
||||
cty.UnknownVal(cty.Bool).RefineNotNull(),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.UnknownVal(cty.List(cty.Bool)),
|
||||
cty.UnknownVal(cty.Bool).RefineNotNull(),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.NullVal(cty.List(cty.Bool)),
|
||||
cty.NilVal,
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(fmt.Sprintf("anytrue(%#v)", tc.Collection), func(t *testing.T) {
|
||||
got, err := AnyTrue.Call([]cty.Value{tc.Collection})
|
||||
|
||||
if tc.Err && err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
if !tc.Err && err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
if !got.RawEquals(tc.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, tc.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
---
|
||||
page_title: anytrue - Functions - Configuration Language
|
||||
description: |-
|
||||
The anytrue function determines whether any element of a collection
|
||||
is true or "true". If the collection is empty, it returns false.
|
||||
---
|
||||
|
||||
# `anytrue` Function
|
||||
|
||||
`anytrue` returns `true` if any element in a given collection is `true`
|
||||
or `"true"`. It also returns `false` if the collection is empty.
|
||||
|
||||
```hcl
|
||||
anytrue(list)
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
```command
|
||||
> anytrue(["true"])
|
||||
true
|
||||
> anytrue([true])
|
||||
true
|
||||
> anytrue([true, false])
|
||||
true
|
||||
> anytrue([])
|
||||
false
|
||||
```
|
||||
Loading…
Reference in new issue