Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding concatlist for concatenating lists #2113

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Funcs map[string]ast.Function

func init() {
Funcs = map[string]ast.Function{
"concatlist": interpolationFuncConcatlist(),
"file": interpolationFuncFile(),
"format": interpolationFuncFormat(),
"formatlist": interpolationFuncFormatList(),
Expand Down Expand Up @@ -54,6 +55,27 @@ func interpolationFuncConcat() ast.Function {
}
}

// interpolationFuncConcatList implements the "concatlist" function that
// concatenates multiple lists together.
func interpolationFuncConcatlist() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeAny},
ReturnType: ast.TypeString,
Variadic: true,
VariadicType: ast.TypeAny,
Callback: func(args []interface{}) (interface{}, error) {
var list []string
for _, arg := range args {
parts := strings.Split(arg.(string), InterpSplitDelim)
if len(parts) > 0 {
list = append(list, parts...)
}
}
return strings.Join(list, InterpSplitDelim), nil
},
}
}

// interpolationFuncFile implements the "file" function that allows
// loading contents from a file.
func interpolationFuncFile() ast.Function {
Expand Down
27 changes: 27 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ func TestInterpolateFuncConcat(t *testing.T) {
})
}

func TestInterpolateFuncConcatLists(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${join(",", concatlist(split(",", "A,B"), split(",", "C,D")))}`,
"A,B,C,D",
false,
},
{
`${join(",", concatlist(split(",", "A,B"), split(",", "C,D"), split(",", "E")))}`,
"A,B,C,D,E",
false,
},
{
`${join(",", concatlist(split(",", "A,B"), "C"))}`,
"A,B,C",
false,
},
{
`${join(",", concatlist("A", "B"))}`,
"A,B",
false,
},
},
})
}

func TestInterpolateFuncFile(t *testing.T) {
tf, err := ioutil.TempFile("", "tf")
if err != nil {
Expand Down