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

config: Add support for lists to concat #1790

Merged
merged 3 commits into from
Jun 23, 2015
Merged
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
36 changes: 27 additions & 9 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ var Funcs map[string]ast.Function

func init() {
Funcs = map[string]ast.Function{
"concat": interpolationFuncConcat(),
"element": interpolationFuncElement(),
"file": interpolationFuncFile(),
"format": interpolationFuncFormat(),
"formatlist": interpolationFuncFormatList(),
"join": interpolationFuncJoin(),
"element": interpolationFuncElement(),
"length": interpolationFuncLength(),
"replace": interpolationFuncReplace(),
"split": interpolationFuncSplit(),
"length": interpolationFuncLength(),

// Concat is a little useless now since we supported embeddded
// interpolations but we keep it around for backwards compat reasons.
"concat": interpolationFuncConcat(),
}
}

Expand All @@ -46,11 +43,32 @@ func interpolationFuncConcat() ast.Function {
VariadicType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
var b bytes.Buffer
for _, v := range args {
b.WriteString(v.(string))
var finalList []string

var isDeprecated = true

for _, arg := range args {
argument := arg.(string)

if len(argument) == 0 {
continue
}

if strings.Contains(argument, InterpSplitDelim) {
isDeprecated = false
}

finalList = append(finalList, argument)

// Deprecated concat behaviour
b.WriteString(argument)
}

if isDeprecated {
return b.String(), nil
}

return b.String(), nil
return strings.Join(finalList, InterpSplitDelim), nil
},
}
}
Expand Down
87 changes: 84 additions & 3 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"io/ioutil"
"os"
"reflect"
"strings"
"testing"

"github.com/hashicorp/terraform/config/lang"
"github.com/hashicorp/terraform/config/lang/ast"
)

func TestInterpolateFuncConcat(t *testing.T) {
func TestInterpolateFuncDeprecatedConcat(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
Expand All @@ -35,6 +36,86 @@ func TestInterpolateFuncConcat(t *testing.T) {
})
}

func TestInterpolateFuncConcat(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
// String + list
{
`${concat("a", split(",", "b,c"))}`,
fmt.Sprintf(
"%s%s%s%s%s",
"a",
InterpSplitDelim,
"b",
InterpSplitDelim,
"c"),
false,
},

// List + string
{
`${concat(split(",", "a,b"), "c")}`,
fmt.Sprintf(
"%s%s%s%s%s",
"a",
InterpSplitDelim,
"b",
InterpSplitDelim,
"c"),
false,
},

// Single list
{
`${concat(split(",", ",foo,"))}`,
fmt.Sprintf(
"%s%s%s",
InterpSplitDelim,
"foo",
InterpSplitDelim),
false,
},
{
`${concat(split(",", "a,b,c"))}`,
fmt.Sprintf(
"%s%s%s%s%s",
"a",
InterpSplitDelim,
"b",
InterpSplitDelim,
"c"),
false,
},

// Two lists
{
`${concat(split(",", "a,b,c"), split(",", "d,e"))}`,
strings.Join([]string{
"a", "b", "c", "d", "e",
}, InterpSplitDelim),
false,
},
// Two lists with different separators
{
`${concat(split(",", "a,b,c"), split(" ", "d e"))}`,
strings.Join([]string{
"a", "b", "c", "d", "e",
}, InterpSplitDelim),
false,
},

// More lists
{
`${concat(split(",", "a,b"), split(",", "c,d"), split(",", "e,f"), split(",", "0,1"))}`,
strings.Join([]string{
"a", "b", "c", "d", "e", "f", "0", "1",
}, InterpSplitDelim),
false,
},
},
})
}

func TestInterpolateFuncFile(t *testing.T) {
tf, err := ioutil.TempFile("", "tf")
if err != nil {
Expand Down Expand Up @@ -532,12 +613,12 @@ func testFunction(t *testing.T, config testFunctionConfig) {
for i, tc := range config.Cases {
ast, err := lang.Parse(tc.Input)
if err != nil {
t.Fatalf("%d: err: %s", i, err)
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err)
}

out, _, err := lang.Eval(ast, langEvalConfig(config.Vars))
if (err != nil) != tc.Error {
t.Fatalf("%d: err: %s", i, err)
t.Fatalf("Case #%d:\ninput: %#v\nerr: %s", i, tc.Input, err)
}

if !reflect.DeepEqual(out, tc.Result) {
Expand Down
4 changes: 2 additions & 2 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ are documented below.

The supported built-in functions are:

* `concat(args...)` - Concatenates the values of multiple arguments into
a single string.
* `concat(list1, list2)` - Combines two or more lists into a single list.
Example: `combine(aws_instance.db.*.tags.Name, aws_instance.web.*.tags.Name)`

* `element(list, index)` - Returns a single element from a list
at the given index. If the index is greater than the number of
Expand Down