Skip to content

Commit

Permalink
Merge pull request #4910 from ColinHebert/trim
Browse files Browse the repository at this point in the history
Add the trimspace() interpolation function
  • Loading branch information
radeksimko committed Jan 30, 2016
2 parents 6680397 + 61a40dc commit f9e369c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit f9e369c

Please sign in to comment.