github.com/hashicorp/packer@v1.14.3/hcl2template/function/alltrue.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  // AllTrue constructs a function that returns true if all elements of the
    12  // list are true. If the list is empty, return true.
    13  var AllTrue = 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.True
    24  		for it := args[0].ElementIterator(); it.Next(); {
    25  			_, v := it.Element()
    26  			if !v.IsKnown() {
    27  				return cty.UnknownVal(cty.Bool), nil
    28  			}
    29  			if v.IsNull() {
    30  				return cty.False, nil
    31  			}
    32  			result = result.And(v)
    33  			if result.False() {
    34  				return cty.False, nil
    35  			}
    36  		}
    37  		return result, nil
    38  	},
    39  })