Skip to content

Commit

Permalink
Add new interpolation function abs.
Browse files Browse the repository at this point in the history
This commit adds new interpolation function `abs` which returns an absolute
value for a given integer. Having an ability to obtain an absolute value remove
the need to use and abuse the `signum` function to achieve the same gaol.

Signed-off-by: Krzysztof Wilczynski <kw@linux.com>
  • Loading branch information
kwilczynski committed Sep 24, 2017
1 parent f846bee commit 704f688
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
12 changes: 12 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func listVariableValueToStringSlice(values []ast.Variable) ([]string, error) {
// Funcs is the mapping of built-in functions for configuration.
func Funcs() map[string]ast.Function {
return map[string]ast.Function{
"abs": interpolationFuncAbs(),
"basename": interpolationFuncBasename(),
"base64decode": interpolationFuncBase64Decode(),
"base64encode": interpolationFuncBase64Encode(),
Expand Down Expand Up @@ -1546,3 +1547,14 @@ func interpolationFuncURLEncode() ast.Function {
},
}
}

// interpolationFuncAbs returns the absolute value of a given integer.
func interpolationFuncAbs() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeInt},
ReturnType: ast.TypeInt,
Callback: func(args []interface{}) (interface{}, error) {
return int(math.Abs(float64(args[0].(int)))), nil
},
}
}
32 changes: 32 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2612,3 +2612,35 @@ func TestInterpolateFuncURLEncode(t *testing.T) {
},
})
}

func TestInterpolateFuncAbs(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${abs()}`,
nil,
true,
},
{
`${abs("")}`,
nil,
true,
},
{
`${abs(0)}`,
"0",
false,
},
{
`${abs(1)}`,
"1",
false,
},
{
`${abs(-1)}`,
"1",
false,
},
},
})
}
6 changes: 5 additions & 1 deletion website/docs/configuration/interpolation.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ syntax `name(arg, arg2, ...)`. For example, to read a file:

The supported built-in functions are:

* `abs(integer)` - Returns the absolute value of a given integer.
Example: `abs(1)` returns `1`, and `abs(-1)` would also return `1`.
See also the `signum` function.

* `basename(path)` - Returns the last element of a path.

* `base64decode(string)` - Given a base64-encoded string, decodes it and
Expand Down Expand Up @@ -358,7 +362,7 @@ The supported built-in functions are:
SHA-512 hash of the given string.
Example: `"${sha512("${aws_vpc.default.tags.customer}-s3-bucket")}"`

* `signum(int)` - Returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers.
* `signum(integer)` - Returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers.
This function is useful when you need to set a value for the first resource and
a different value for the rest of the resources.
Example: `element(split(",", var.r53_failover_policy), signum(count.index))`
Expand Down

0 comments on commit 704f688

Please sign in to comment.