mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-13 10:40:05 -04:00
* Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at https://hashi.co/license-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package function
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
"github.com/zclconf/go-cty/cty/function"
|
|
"github.com/zclconf/go-cty/cty/function/stdlib"
|
|
)
|
|
|
|
// IndexFunc constructs a function that finds the element index for a given value in a list.
|
|
var IndexFunc = function.New(&function.Spec{
|
|
Params: []function.Parameter{
|
|
{
|
|
Name: "list",
|
|
Type: cty.DynamicPseudoType,
|
|
},
|
|
{
|
|
Name: "value",
|
|
Type: cty.DynamicPseudoType,
|
|
},
|
|
},
|
|
Type: function.StaticReturnType(cty.Number),
|
|
Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
|
|
if !(args[0].Type().IsListType() || args[0].Type().IsTupleType()) {
|
|
return cty.NilVal, errors.New("argument must be a list or tuple")
|
|
}
|
|
|
|
if !args[0].IsKnown() {
|
|
return cty.UnknownVal(cty.Number), nil
|
|
}
|
|
|
|
if args[0].LengthInt() == 0 { // Easy path
|
|
return cty.NilVal, errors.New("cannot search an empty list")
|
|
}
|
|
|
|
for it := args[0].ElementIterator(); it.Next(); {
|
|
i, v := it.Element()
|
|
eq, err := stdlib.Equal(v, args[1])
|
|
if err != nil {
|
|
return cty.NilVal, err
|
|
}
|
|
if !eq.IsKnown() {
|
|
return cty.UnknownVal(cty.Number), nil
|
|
}
|
|
if eq.True() {
|
|
return i, nil
|
|
}
|
|
}
|
|
return cty.NilVal, errors.New("item not found")
|
|
|
|
},
|
|
})
|
|
|
|
// Index finds the element index for a given value in a list.
|
|
func Index(list, value cty.Value) (cty.Value, error) {
|
|
return IndexFunc.Call([]cty.Value{list, value})
|
|
}
|