From adccaad252963f8bfb08b1699a787d00a8ce8066 Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Wed, 28 Jan 2015 16:59:16 -0500 Subject: [PATCH 1/2] Add split function --- config/interpolate_funcs.go | 13 +++++++++++++ config/interpolate_funcs_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index c529adb8d9..69e327e370 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -19,6 +19,7 @@ func init() { "file": interpolationFuncFile(), "join": interpolationFuncJoin(), "element": interpolationFuncElement(), + "split": interpolationFuncSplit(), } } @@ -78,6 +79,18 @@ func interpolationFuncJoin() ast.Function { } } +// interpolationFuncSplit implements the "split" function that allows +// strings to split into multi-variable values +func interpolationFuncSplit() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString, ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + return strings.Replace(args[1].(string), args[0].(string), InterpSplitDelim, -1), nil + }, + } +} + // interpolationFuncLookup implements the "lookup" function that allows // dynamic lookups of map types within a Terraform configuration. func interpolationFuncLookup(vs map[string]ast.Variable) ast.Function { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 1a4a59057a..9710852db8 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -107,6 +107,33 @@ func TestInterpolateFuncJoin(t *testing.T) { }) } +func TestInterpolateFuncSplit(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${split(",")}`, + nil, + true, + }, + + { + `${split(",", "foo")}`, + "foo", + false, + }, + + { + `${split(".", "foo.bar.baz")}`, + fmt.Sprintf( + "foo%sbar%sbaz", + InterpSplitDelim, + InterpSplitDelim), + false, + }, + }, + }) +} + func TestInterpolateFuncLookup(t *testing.T) { testFunction(t, testFunctionConfig{ Vars: map[string]ast.Variable{ From fa8280a56ca819b0d046ea3f48669d9cf598117e Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Tue, 17 Feb 2015 13:24:34 -0500 Subject: [PATCH 2/2] Add split docs --- website/source/docs/configuration/interpolation.html.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index 326d155fb6..87f8d47dc8 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -72,6 +72,11 @@ The supported built-in functions are: only possible with splat variables from resources with a count greater than one. Example: `join(",", aws_instance.foo.*.id)` + * `split(delim, string)` - Splits the string previously created by `join` + back into a list. This is useful for pushing lists through module + outputs since they currently only support string values. + Example: `split(",", module.amod.server_ids)` + * `lookup(map, key)` - Performs a dynamic lookup into a mapping variable.