buildr/modules/helpers/hclhelpers/collections.go
Peter 786578bc6f
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
feat: adapt new wire format
update to Go 1.21
2023-08-15 21:47:19 +02:00

51 lines
1.1 KiB
Go

package hclhelpers
import (
"fmt"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"code.icb4dc0.de/buildr/buildr/internal/maps"
)
func HclToSet() function.Function {
return function.New(&function.Spec{
Description: "Transform a list to a set",
Params: []function.Parameter{
{
Name: "in",
Description: "Input list",
Type: cty.List(cty.DynamicPseudoType),
},
},
Type: func(args []cty.Value) (cty.Type, error) {
if len(args) < 1 {
return cty.Type{}, fmt.Errorf("cannot determine type with no argument")
}
return cty.Set(args[0].Type().ElementType()), nil
},
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
if len(args) < 1 {
return cty.ListVal(nil), nil
}
values := args[0].AsValueSlice()
if len(values) == 0 {
return cty.SetValEmpty(retType.ElementType()), nil
}
mapped := make(map[int]cty.Value, len(values))
for i := range values {
val := values[i]
mapped[val.Hash()] = val
}
return cty.SetVal(maps.Values(mapped)), nil
},
})
}