51 lines
1.1 KiB
Go
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
|
|
},
|
|
})
|
|
}
|