From f5074cd521d777a5350731f1548b9cdcd90e7f43 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sat, 30 Jan 2016 10:28:04 +1100 Subject: [PATCH 1/4] Add the trim() interpolation function --- config/interpolate_funcs.go | 12 ++++++++++++ config/interpolate_funcs_test.go | 12 ++++++++++++ .../source/docs/configuration/interpolation.html.md | 2 ++ 3 files changed, 26 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index a0ceeeec4..1757da79a 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -41,6 +41,7 @@ func Funcs() map[string]ast.Function { "split": interpolationFuncSplit(), "sha1": interpolationFuncSha1(), "sha256": interpolationFuncSha256(), + "strip": interpolationFuncStrip(), "base64encode": interpolationFuncBase64Encode(), "base64decode": interpolationFuncBase64Decode(), "upper": interpolationFuncUpper(), @@ -617,3 +618,14 @@ func interpolationFuncSha256() ast.Function { }, } } + +func interpolationFuncTrim() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + trim := args[0].(string) + return strings.TrimSpace(trim), nil + }, + } +} diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 78139aa2d..b80d7456b 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -858,6 +858,18 @@ func TestInterpolateFuncSha256(t *testing.T) { }) } +func TestInterpolateFuncTrim(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${trim(" test ")}`, + "test", + false, + }, + }, + }) +} + type testFunctionConfig struct { Cases []testFunctionCase Vars map[string]ast.Variable diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index 77c40e59e..e43f524b3 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -176,6 +176,8 @@ The supported built-in functions are: `a_resource_param = ["${split(",", var.CSV_STRING)}"]`. Example: `split(",", module.amod.server_ids)` + * `trim(string)` - Returns a copy of the string with all leading and trailing white spaces removed. + * `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case. ## Templates From d45b7b2ddc1541ae402471a1ccdbefa640a1511b Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sat, 30 Jan 2016 10:32:18 +1100 Subject: [PATCH 2/4] Fix name from strip to trim --- config/interpolate_funcs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 1757da79a..5d5e6f6e2 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -41,7 +41,7 @@ func Funcs() map[string]ast.Function { "split": interpolationFuncSplit(), "sha1": interpolationFuncSha1(), "sha256": interpolationFuncSha256(), - "strip": interpolationFuncStrip(), + "trim": interpolationFuncTrim(), "base64encode": interpolationFuncBase64Encode(), "base64decode": interpolationFuncBase64Decode(), "upper": interpolationFuncUpper(), From d92d205dd939fda2f02380ecb0782225942a34da Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sat, 30 Jan 2016 20:51:28 +1100 Subject: [PATCH 3/4] rename trim to trimspace --- config/interpolate_funcs.go | 8 ++++---- website/source/docs/configuration/interpolation.html.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 5d5e6f6e2..85e69e14c 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -41,7 +41,7 @@ func Funcs() map[string]ast.Function { "split": interpolationFuncSplit(), "sha1": interpolationFuncSha1(), "sha256": interpolationFuncSha256(), - "trim": interpolationFuncTrim(), + "trimspace": interpolationFuncTrimSpace(), "base64encode": interpolationFuncBase64Encode(), "base64decode": interpolationFuncBase64Decode(), "upper": interpolationFuncUpper(), @@ -619,13 +619,13 @@ func interpolationFuncSha256() ast.Function { } } -func interpolationFuncTrim() ast.Function { +func interpolationFuncTrimSpace() ast.Function { return ast.Function{ ArgTypes: []ast.Type{ast.TypeString}, ReturnType: ast.TypeString, Callback: func(args []interface{}) (interface{}, error) { - trim := args[0].(string) - return strings.TrimSpace(trim), nil + trimSpace := args[0].(string) + return strings.TrimSpace(trimSpace), nil }, } } diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index e43f524b3..b1f2b9fe6 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -176,7 +176,7 @@ The supported built-in functions are: `a_resource_param = ["${split(",", var.CSV_STRING)}"]`. Example: `split(",", module.amod.server_ids)` - * `trim(string)` - Returns a copy of the string with all leading and trailing white spaces removed. + * `trimspace(string)` - Returns a copy of the string with all leading and trailing white spaces removed. * `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case. From 61a40dce139fa2d6ebf4514b7ed0cfa33b61c914 Mon Sep 17 00:00:00 2001 From: Colin Hebert Date: Sat, 30 Jan 2016 20:52:45 +1100 Subject: [PATCH 4/4] Update the test file --- config/interpolate_funcs_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index b80d7456b..a2c2a0fdd 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -858,11 +858,11 @@ func TestInterpolateFuncSha256(t *testing.T) { }) } -func TestInterpolateFuncTrim(t *testing.T) { +func TestInterpolateFuncTrimSpace(t *testing.T) { testFunction(t, testFunctionConfig{ Cases: []testFunctionCase{ { - `${trim(" test ")}`, + `${trimspace(" test ")}`, "test", false, },