Skip to content

Commit 6a60fa4

Browse files
committed
config: concat function supports lists (combines more lists into one)
1 parent e9e41df commit 6a60fa4

3 files changed

Lines changed: 109 additions & 10 deletions

File tree

config/interpolate_funcs.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var Funcs map[string]ast.Function
1919

2020
func init() {
2121
Funcs = map[string]ast.Function{
22+
"concat": interpolationFuncConcat(),
2223
"element": interpolationFuncElement(),
2324
"file": interpolationFuncFile(),
2425
"format": interpolationFuncFormat(),
@@ -27,10 +28,6 @@ func init() {
2728
"length": interpolationFuncLength(),
2829
"replace": interpolationFuncReplace(),
2930
"split": interpolationFuncSplit(),
30-
31-
// Concat is a little useless now since we supported embeddded
32-
// interpolations but we keep it around for backwards compat reasons.
33-
"concat": interpolationFuncConcat(),
3431
}
3532
}
3633

@@ -46,11 +43,32 @@ func interpolationFuncConcat() ast.Function {
4643
VariadicType: ast.TypeString,
4744
Callback: func(args []interface{}) (interface{}, error) {
4845
var b bytes.Buffer
49-
for _, v := range args {
50-
b.WriteString(v.(string))
46+
var finalList []string
47+
48+
var isDeprecated = true
49+
50+
for _, arg := range args {
51+
argument := arg.(string)
52+
53+
if len(argument) == 0 {
54+
continue
55+
}
56+
57+
if strings.Contains(argument, InterpSplitDelim) {
58+
isDeprecated = false
59+
}
60+
61+
finalList = append(finalList, argument)
62+
63+
// Deprecated concat behaviour
64+
b.WriteString(argument)
65+
}
66+
67+
if isDeprecated {
68+
return b.String(), nil
5169
}
5270

53-
return b.String(), nil
71+
return strings.Join(finalList, InterpSplitDelim), nil
5472
},
5573
}
5674
}

config/interpolate_funcs_test.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import (
55
"io/ioutil"
66
"os"
77
"reflect"
8+
"strings"
89
"testing"
910

1011
"github.com/hashicorp/terraform/config/lang"
1112
"github.com/hashicorp/terraform/config/lang/ast"
1213
)
1314

14-
func TestInterpolateFuncConcat(t *testing.T) {
15+
func TestInterpolateFuncDeprecatedConcat(t *testing.T) {
1516
testFunction(t, testFunctionConfig{
1617
Cases: []testFunctionCase{
1718
{
@@ -35,6 +36,86 @@ func TestInterpolateFuncConcat(t *testing.T) {
3536
})
3637
}
3738

39+
func TestInterpolateFuncConcat(t *testing.T) {
40+
testFunction(t, testFunctionConfig{
41+
Cases: []testFunctionCase{
42+
// String + list
43+
{
44+
`${concat("a", split(",", "b,c"))}`,
45+
fmt.Sprintf(
46+
"%s%s%s%s%s",
47+
"a",
48+
InterpSplitDelim,
49+
"b",
50+
InterpSplitDelim,
51+
"c"),
52+
false,
53+
},
54+
55+
// List + string
56+
{
57+
`${concat(split(",", "a,b"), "c")}`,
58+
fmt.Sprintf(
59+
"%s%s%s%s%s",
60+
"a",
61+
InterpSplitDelim,
62+
"b",
63+
InterpSplitDelim,
64+
"c"),
65+
false,
66+
},
67+
68+
// Single list
69+
{
70+
`${concat(split(",", ",foo,"))}`,
71+
fmt.Sprintf(
72+
"%s%s%s",
73+
InterpSplitDelim,
74+
"foo",
75+
InterpSplitDelim),
76+
false,
77+
},
78+
{
79+
`${concat(split(",", "a,b,c"))}`,
80+
fmt.Sprintf(
81+
"%s%s%s%s%s",
82+
"a",
83+
InterpSplitDelim,
84+
"b",
85+
InterpSplitDelim,
86+
"c"),
87+
false,
88+
},
89+
90+
// Two lists
91+
{
92+
`${concat(split(",", "a,b,c"), split(",", "d,e"))}`,
93+
strings.Join([]string{
94+
"a", "b", "c", "d", "e",
95+
}, InterpSplitDelim),
96+
false,
97+
},
98+
// Two lists with different separators
99+
{
100+
`${concat(split(",", "a,b,c"), split(" ", "d e"))}`,
101+
strings.Join([]string{
102+
"a", "b", "c", "d", "e",
103+
}, InterpSplitDelim),
104+
false,
105+
},
106+
107+
// More lists
108+
{
109+
`${concat(split(",", "a,b"), split(",", "c,d"), split(",", "e,f"), split(",", "0,1"))}`,
110+
strings.Join([]string{
111+
"a", "b", "c", "d", "e", "f", "0", "1",
112+
}, InterpSplitDelim),
113+
false,
114+
},
115+
},
116+
})
117+
}
118+
38119
func TestInterpolateFuncFile(t *testing.T) {
39120
tf, err := ioutil.TempFile("", "tf")
40121
if err != nil {

website/source/docs/configuration/interpolation.html.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ are documented below.
7272

7373
The supported built-in functions are:
7474

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

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

0 commit comments

Comments
 (0)