Skip to content

Commit

Permalink
core: Fix panic on concat() w/ list complex types
Browse files Browse the repository at this point in the history
The `concat()` interpolation function does not yet support types other
than strings / lists of strings. Make it an error message instead of a
panic when a list of non-primitives is supplied.

Fixes the panic in #7030
  • Loading branch information
phinze committed Jun 12, 2016
1 parent 2127466 commit 3fc98d9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,13 @@ func interpolationFuncConcat() ast.Function {
// Otherwise variables
if argument, ok := arg.([]ast.Variable); ok {
for _, element := range argument {
finalListElements = append(finalListElements, element.Value.(string))
t := element.Type
switch t {
case ast.TypeString:
finalListElements = append(finalListElements, element.Value.(string))
default:
return nil, fmt.Errorf("concat() does not support lists of %s", t.Printable())
}
}
continue
}
Expand Down
35 changes: 35 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"reflect"
"strings"
"testing"

"github.com/hashicorp/hil"
Expand Down Expand Up @@ -226,6 +227,40 @@ func TestInterpolateFuncConcat(t *testing.T) {
})
}

// TODO: This test is split out and calls a private function
// because there's no good way to get a list of maps into the unit
// tests due to GH-7142 - once lists of maps can be expressed properly as
// literals this unit test can be wrapped back into the suite above.
//
// Reproduces crash reported in GH-7030.
func TestInterpolationFuncConcatListOfMaps(t *testing.T) {
listOfMapsOne := ast.Variable{
Type: ast.TypeList,
Value: []ast.Variable{
{
Type: ast.TypeMap,
Value: map[string]interface{}{"one": "foo"},
},
},
}
listOfMapsTwo := ast.Variable{
Type: ast.TypeList,
Value: []ast.Variable{
{
Type: ast.TypeMap,
Value: map[string]interface{}{"two": "bar"},
},
},
}
args := []interface{}{listOfMapsOne.Value, listOfMapsTwo.Value}

_, err := interpolationFuncConcat().Callback(args)

if err == nil || !strings.Contains(err.Error(), "concat() does not support lists of type map") {
t.Fatalf("Expected err, got: %v", err)
}
}

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

0 comments on commit 3fc98d9

Please sign in to comment.