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

Add the trimspace() interpolation function #4910

Merged
merged 4 commits into from
Jan 30, 2016
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
12 changes: 12 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func Funcs() map[string]ast.Function {
"split": interpolationFuncSplit(),
"sha1": interpolationFuncSha1(),
"sha256": interpolationFuncSha256(),
"trimspace": interpolationFuncTrimSpace(),
"base64encode": interpolationFuncBase64Encode(),
"base64decode": interpolationFuncBase64Decode(),
"upper": interpolationFuncUpper(),
Expand Down Expand Up @@ -617,3 +618,14 @@ func interpolationFuncSha256() ast.Function {
},
}
}

func interpolationFuncTrimSpace() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
trimSpace := args[0].(string)
return strings.TrimSpace(trimSpace), nil
},
}
}
12 changes: 12 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,18 @@ func TestInterpolateFuncSha256(t *testing.T) {
})
}

func TestInterpolateFuncTrimSpace(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${trimspace(" test ")}`,
"test",
false,
},
},
})
}

type testFunctionConfig struct {
Cases []testFunctionCase
Vars map[string]ast.Variable
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ The supported built-in functions are:
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
Example: `split(",", module.amod.server_ids)`

* `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.

## Templates
Expand Down