Skip to content

Commit

Permalink
Concat func.
Browse files Browse the repository at this point in the history
  • Loading branch information
cam-stitt committed Oct 29, 2017
1 parent 4540b57 commit 49411c2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
17 changes: 17 additions & 0 deletions interpolation/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@ func interpolationFuncList() ast.Function {
},
}
}

// interpolationFuncConcat will concat multiple lists into a single list
func interpolationFuncConcat() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{},
ReturnType: ast.TypeList,
Variadic: true,
VariadicType: ast.TypeList,
Callback: func(inputs []interface{}) (interface{}, error) {
result := make([]ast.Variable, 0)
for _, input := range inputs {
result = append(result, input.([]ast.Variable)...)
}
return result, nil
},
}
}
44 changes: 43 additions & 1 deletion interpolation/lists_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package interpolation

import "testing"
import (
"testing"

"github.com/hashicorp/hil/ast"
)

func TestInterpolationFuncList(t *testing.T) {
testCases := []functionTestCase{
Expand Down Expand Up @@ -29,3 +33,41 @@ func TestInterpolationFuncList(t *testing.T) {
})
}
}

func TestInterpoloationFuncConcat(t *testing.T) {
testCases := []functionTestCase{
{
description: "Two lists combine",
text: `${concat(foo, bar)}`,
expectation: []interface{}{"test1", "test2"},
vars: map[string]ast.Variable{
"foo": ast.Variable{
Type: ast.TypeList,
Value: []ast.Variable{
{
Type: ast.TypeString,
Value: "test1",
},
},
},
"bar": ast.Variable{
Type: ast.TypeList,
Value: []ast.Variable{
{
Type: ast.TypeString,
Value: "test2",
},
},
},
},
},
}

concatTestFunc := testInterpolationFunc("concat", interpolationFuncConcat)

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
concatTestFunc(t, tc)
})
}
}

0 comments on commit 49411c2

Please sign in to comment.