buildr/modules/helpers/strings/join.go

39 lines
891 B
Go

package strings
import (
"strings"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
func Join() function.Function {
return function.New(&function.Spec{
Description: "join a list of strings",
Params: []function.Parameter{
{
Name: "separator",
Description: "separator to be used to join the strings",
Type: cty.String,
},
{
Name: "list",
Description: "list of strings to join",
Type: cty.List(cty.String),
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
separator := args[0].AsString()
rawElems := args[1].AsValueSlice()
elems := make([]string, len(rawElems))
for i := range rawElems {
elems[i] = rawElems[i].AsString()
}
return cty.StringVal(strings.Join(elems, separator)), nil
},
})
}