Added chomp interpolation function.

This commit is contained in:
Joern Barthel 2017-04-06 13:14:09 +02:00
parent 182e6c5f4e
commit 059a1b2c0f
3 changed files with 40 additions and 0 deletions

View File

@ -58,6 +58,7 @@ func Funcs() map[string]ast.Function {
"base64encode": interpolationFuncBase64Encode(),
"base64sha256": interpolationFuncBase64Sha256(),
"ceil": interpolationFuncCeil(),
"chomp": interpolationFuncChomp(),
"cidrhost": interpolationFuncCidrHost(),
"cidrnetmask": interpolationFuncCidrNetmask(),
"cidrsubnet": interpolationFuncCidrSubnet(),
@ -459,6 +460,17 @@ func interpolationFuncCeil() ast.Function {
}
}
// interpolationFuncChomp removes trailing newlines from the given string
func interpolationFuncChomp() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
return strings.TrimRight(args[0].(string), "\n"), nil
},
}
}
// interpolationFuncFloorreturns returns the greatest integer value less than or equal to the argument
func interpolationFuncFloor() ast.Function {
return ast.Function{

View File

@ -370,6 +370,32 @@ func TestInterpolateFuncCeil(t *testing.T) {
})
}
func TestInterpolateFuncChomp(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${chomp()}`,
nil,
true,
},
{
`${chomp("hello world")}`,
"hello world",
false,
},
{
`${chomp("goodbye\ncruel\nworld\n")}`,
`goodbye
cruel
world`,
false,
},
},
})
}
func TestInterpolateFuncMap(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{

View File

@ -156,6 +156,8 @@ The supported built-in functions are:
* `ceil(float)` - Returns the least integer value greater than or equal
to the argument.
* `chomp(string)` - Removes trailing newlines from the given string.
* `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation
and creates an IP address with the given host number. For example,
`cidrhost("10.0.0.0/8", 2)` returns `10.0.0.2`.