Add lower / upper interpolation functions

This commit is contained in:
Matt Morrison 2015-10-20 14:49:51 +13:00 committed by Martin Atkins
parent 77847b1572
commit cccc5d03e3
3 changed files with 80 additions and 0 deletions

View File

@ -29,10 +29,12 @@ func init() {
"index": interpolationFuncIndex(),
"join": interpolationFuncJoin(),
"length": interpolationFuncLength(),
"lower": interpolationFuncLower(),
"replace": interpolationFuncReplace(),
"split": interpolationFuncSplit(),
"base64encode": interpolationFuncBase64Encode(),
"base64decode": interpolationFuncBase64Decode(),
"upper": interpolationFuncUpper(),
}
}
@ -442,3 +444,29 @@ func interpolationFuncBase64Decode() ast.Function {
},
}
}
// interpolationFuncLower implements the "lower" function that does
// string lower casing.
func interpolationFuncLower() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
toLower := args[0].(string)
return strings.ToLower(toLower), nil
},
}
}
// interpolationFuncUpper implements the "upper" function that does
// string upper casing.
func interpolationFuncUpper() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
toUpper := args[0].(string)
return strings.ToUpper(toUpper), nil
},
}
}

View File

@ -644,6 +644,54 @@ func TestInterpolateFuncBase64Decode(t *testing.T) {
})
}
func TestInterpolateFuncLower(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${lower("HELLO")}`,
"hello",
false,
},
{
`${lower("")}`,
"",
false,
},
{
`${lower()}`,
nil,
true,
},
},
})
}
func TestInterpolateFuncUpper(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${upper("hello")}`,
"HELLO",
false,
},
{
`${upper("")}`,
"",
false,
},
{
`${upper()}`,
nil,
true,
},
},
})
}
type testFunctionConfig struct {
Cases []testFunctionCase
Vars map[string]ast.Variable

View File

@ -131,6 +131,8 @@ The supported built-in functions are:
variable. The `map` parameter should be another variable, such
as `var.amis`.
* `lower(string)` - returns a copy of the string with all Unicode letters mapped to their lower case.
* `replace(string, search, replace)` - Does a search and replace on the
given string. All instances of `search` are replaced with the value
of `replace`. If `search` is wrapped in forward slashes, it is treated
@ -147,6 +149,8 @@ The supported built-in functions are:
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
Example: `split(",", module.amod.server_ids)`
* `upper(string)` - returns a copy of the string with all Unicode letters mapped to their upper case.
## Templates
Long strings can be managed using templates. [Templates](/docs/providers/template/index.html) are [resources](/docs/configuration/resources.html) defined by a filename and some variables to use during interpolation. They have a computed `rendered` attribute containing the result.