Add the trim() interpolation function

This commit is contained in:
Colin Hebert 2016-01-30 10:28:04 +11:00
parent 66803970f0
commit f5074cd521
3 changed files with 26 additions and 0 deletions

View File

@ -41,6 +41,7 @@ func Funcs() map[string]ast.Function {
"split": interpolationFuncSplit(), "split": interpolationFuncSplit(),
"sha1": interpolationFuncSha1(), "sha1": interpolationFuncSha1(),
"sha256": interpolationFuncSha256(), "sha256": interpolationFuncSha256(),
"strip": interpolationFuncStrip(),
"base64encode": interpolationFuncBase64Encode(), "base64encode": interpolationFuncBase64Encode(),
"base64decode": interpolationFuncBase64Decode(), "base64decode": interpolationFuncBase64Decode(),
"upper": interpolationFuncUpper(), "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
},
}
}

View File

@ -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 { type testFunctionConfig struct {
Cases []testFunctionCase Cases []testFunctionCase
Vars map[string]ast.Variable Vars map[string]ast.Variable

View File

@ -176,6 +176,8 @@ The supported built-in functions are:
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`. `a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
Example: `split(",", module.amod.server_ids)` 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. * `upper(string)` - Returns a copy of the string with all Unicode letters mapped to their upper case.
## Templates ## Templates