Merge pull request #7899 from hashicorp/jbardin/concat

core: Disallow strings as arguments to concat
This commit is contained in:
James Bardin 2016-08-01 15:34:47 -04:00 committed by GitHub
commit 8c4b4edd2f
2 changed files with 30 additions and 27 deletions

View File

@ -311,19 +311,15 @@ func interpolationFuncCoalesce() ast.Function {
// multiple lists. // multiple lists.
func interpolationFuncConcat() ast.Function { func interpolationFuncConcat() ast.Function {
return ast.Function{ return ast.Function{
ArgTypes: []ast.Type{ast.TypeAny}, ArgTypes: []ast.Type{ast.TypeList},
ReturnType: ast.TypeList, ReturnType: ast.TypeList,
Variadic: true, Variadic: true,
VariadicType: ast.TypeAny, VariadicType: ast.TypeList,
Callback: func(args []interface{}) (interface{}, error) { Callback: func(args []interface{}) (interface{}, error) {
var outputList []ast.Variable var outputList []ast.Variable
for _, arg := range args { for _, arg := range args {
switch arg := arg.(type) { for _, v := range arg.([]ast.Variable) {
case string:
outputList = append(outputList, ast.Variable{Type: ast.TypeString, Value: arg})
case []ast.Variable:
for _, v := range arg {
switch v.Type { switch v.Type {
case ast.TypeString: case ast.TypeString:
outputList = append(outputList, v) outputList = append(outputList, v)
@ -335,10 +331,6 @@ func interpolationFuncConcat() ast.Function {
return nil, fmt.Errorf("concat() does not support lists of %s", v.Type.Printable()) return nil, fmt.Errorf("concat() does not support lists of %s", v.Type.Printable())
} }
} }
default:
return nil, fmt.Errorf("concat() does not support %T", arg)
}
} }
// we don't support heterogeneous types, so make sure all types match the first // we don't support heterogeneous types, so make sure all types match the first

View File

@ -362,17 +362,19 @@ func TestInterpolateFuncConcat(t *testing.T) {
testFunction(t, testFunctionConfig{ testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{ Cases: []testFunctionCase{
// String + list // String + list
// no longer supported, now returns an error
{ {
`${concat("a", split(",", "b,c"))}`, `${concat("a", split(",", "b,c"))}`,
[]interface{}{"a", "b", "c"}, nil,
false, true,
}, },
// List + string // List + string
// no longer supported, now returns an error
{ {
`${concat(split(",", "a,b"), "c")}`, `${concat(split(",", "a,b"), "c")}`,
[]interface{}{"a", "b", "c"}, nil,
false, true,
}, },
// Single list // Single list
@ -427,6 +429,14 @@ func TestInterpolateFuncConcat(t *testing.T) {
false, false,
}, },
// multiple strings
// no longer supported, now returns an error
{
`${concat("string1", "string2")}`,
nil,
true,
},
// mismatched types // mismatched types
{ {
`${concat("${var.lists}", "${var.maps}")}`, `${concat("${var.lists}", "${var.maps}")}`,
@ -1532,15 +1542,16 @@ type testFunctionCase struct {
func testFunction(t *testing.T, config testFunctionConfig) { func testFunction(t *testing.T, config testFunctionConfig) {
for i, tc := range config.Cases { for i, tc := range config.Cases {
fmt.Println("running", i)
ast, err := hil.Parse(tc.Input) ast, err := hil.Parse(tc.Input)
if err != nil { if err != nil {
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err) t.Fatalf("Case #%d: input: %#v\nerr: %v", i, tc.Input, err)
} }
result, err := hil.Eval(ast, langEvalConfig(config.Vars)) result, err := hil.Eval(ast, langEvalConfig(config.Vars))
t.Logf("err: %s", err) t.Logf("err: %v", err)
if err != nil != tc.Error { if err != nil != tc.Error {
t.Fatalf("Case #%d:\ninput: %#v\nerr: %s", i, tc.Input, err) t.Fatalf("Case #%d:\ninput: %#v\nerr: %v", i, tc.Input, err)
} }
if !reflect.DeepEqual(result.Value, tc.Result) { if !reflect.DeepEqual(result.Value, tc.Result) {