From 92af4801a1e7bd3051a8de58bfea7a56b1c54a2d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Jan 2015 12:47:54 -0800 Subject: [PATCH] config: reintroduce concat --- config/interpolate_funcs.go | 23 +++++++++++++++++++++++ config/interpolate_funcs_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 8f19d3b95f..65cebbfa13 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -1,6 +1,7 @@ package config import ( + "bytes" "fmt" "io/ioutil" "strconv" @@ -15,12 +16,34 @@ var Funcs map[string]lang.Function func init() { Funcs = map[string]lang.Function{ + "concat": interpolationFuncConcat(), "file": interpolationFuncFile(), "join": interpolationFuncJoin(), "element": interpolationFuncElement(), } } +// interpolationFuncConcat implements the "concat" function that +// concatenates multiple strings. This isn't actually necessary anymore +// since our language supports string concat natively, but for backwards +// compat we do this. +func interpolationFuncConcat() lang.Function { + return lang.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Variadic: true, + VariadicType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + var b bytes.Buffer + for _, v := range args { + b.WriteString(v.(string)) + } + + return b.String(), nil + }, + } +} + // interpolationFuncFile implements the "file" function that allows // loading contents from a file. func interpolationFuncFile() lang.Function { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 9ea2ac1cb7..757f8aa5bb 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -10,6 +10,30 @@ import ( "github.com/hashicorp/terraform/config/lang" ) +func TestInterpolateFuncConcat(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${concat("foo", "bar")}`, + "foobar", + false, + }, + + { + `${concat("foo")}`, + "foo", + false, + }, + + { + `${concat()}`, + nil, + true, + }, + }, + }) +} + func TestInterpolateFuncFile(t *testing.T) { tf, err := ioutil.TempFile("", "tf") if err != nil {