github.com/hashicorp/packer@v1.14.3/hcl2template/function/anytrue.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package function
     5  
     6  import (
     7  	"github.com/zclconf/go-cty/cty"
     8  	"github.com/zclconf/go-cty/cty/function"
     9  )
    10  
    11  // AnyTrue constructs a function that returns true if a single element of
    12  // the list is true. If the list is empty, return false.
    13  var AnyTrue = function.New(&function.Spec{
    14  	Params: []function.Parameter{
    15  		{
    16  			Name: "list",
    17  			Type: cty.List(cty.Bool),
    18  		},
    19  	},
    20  	Type:         function.StaticReturnType(cty.Bool),
    21  	RefineResult: refineNotNull,
    22  	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
    23  		result := cty.False
    24  		var hasUnknown bool
    25  		for it := args[0].ElementIterator(); it.Next(); {
    26  			_, v := it.Element()
    27  			if !v.IsKnown() {
    28  				hasUnknown = true
    29  				continue
    30  			}
    31  			if v.IsNull() {
    32  				continue
    33  			}
    34  			result = result.Or(v)
    35  			if result.True() {
    36  				return cty.True, nil
    37  			}
    38  		}
    39  		if hasUnknown {
    40  			return cty.UnknownVal(cty.Bool), nil
    41  		}
    42  		return result, nil
    43  	},
    44  })