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

     1  package function
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  	"github.com/zclconf/go-cty/cty/function"
     8  )
     9  
    10  // StartsWithFunc constructs a function that checks if a string starts with
    11  // a specific prefix using strings.HasPrefix
    12  var StartsWithFunc = function.New(&function.Spec{
    13  	Params: []function.Parameter{
    14  		{
    15  			Name:         "str",
    16  			Type:         cty.String,
    17  			AllowUnknown: false,
    18  		},
    19  		{
    20  			Name: "prefix",
    21  			Type: cty.String,
    22  		},
    23  	},
    24  	Type:         function.StaticReturnType(cty.Bool),
    25  	RefineResult: refineNotNull,
    26  	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
    27  		str := args[0].AsString()
    28  		prefix := args[1].AsString()
    29  
    30  		return cty.BoolVal(strings.HasPrefix(str, prefix)), nil
    31  	},
    32  })