config: new "abs" interpolation function
This new function returns the absolute value for a given number.
This commit is contained in:
parent
b86967099f
commit
6e7e03f4ea
|
@ -58,6 +58,7 @@ func listVariableValueToStringSlice(values []ast.Variable) ([]string, error) {
|
||||||
// Funcs is the mapping of built-in functions for configuration.
|
// Funcs is the mapping of built-in functions for configuration.
|
||||||
func Funcs() map[string]ast.Function {
|
func Funcs() map[string]ast.Function {
|
||||||
return map[string]ast.Function{
|
return map[string]ast.Function{
|
||||||
|
"abs": interpolationFuncAbs(),
|
||||||
"basename": interpolationFuncBasename(),
|
"basename": interpolationFuncBasename(),
|
||||||
"base64decode": interpolationFuncBase64Decode(),
|
"base64decode": interpolationFuncBase64Decode(),
|
||||||
"base64encode": interpolationFuncBase64Encode(),
|
"base64encode": interpolationFuncBase64Encode(),
|
||||||
|
@ -1546,3 +1547,14 @@ func interpolationFuncURLEncode() ast.Function {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// interpolationFuncAbs returns the absolute value of a given float.
|
||||||
|
func interpolationFuncAbs() ast.Function {
|
||||||
|
return ast.Function{
|
||||||
|
ArgTypes: []ast.Type{ast.TypeFloat},
|
||||||
|
ReturnType: ast.TypeFloat,
|
||||||
|
Callback: func(args []interface{}) (interface{}, error) {
|
||||||
|
return math.Abs(args[0].(float64)), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2612,3 +2612,55 @@ 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,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`${abs(1.0)}`,
|
||||||
|
"1",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`${abs(-1.0)}`,
|
||||||
|
"1",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`${abs(-3.14)}`,
|
||||||
|
"3.14",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`${abs(-42.001)}`,
|
||||||
|
"42.001",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -145,6 +145,10 @@ syntax `name(arg, arg2, ...)`. For example, to read a file:
|
||||||
|
|
||||||
The supported built-in functions are:
|
The supported built-in functions are:
|
||||||
|
|
||||||
|
* `abs(float)` - Returns the absolute value of a given float.
|
||||||
|
Example: `abs(1)` returns `1`, and `abs(-1)` would also return `1`,
|
||||||
|
whereas `abs(-3.14)` would return `3.14`. See also the `signum` function.
|
||||||
|
|
||||||
* `basename(path)` - Returns the last element of a path.
|
* `basename(path)` - Returns the last element of a path.
|
||||||
|
|
||||||
* `base64decode(string)` - Given a base64-encoded string, decodes it and
|
* `base64decode(string)` - Given a base64-encoded string, decodes it and
|
||||||
|
@ -358,7 +362,7 @@ The supported built-in functions are:
|
||||||
SHA-512 hash of the given string.
|
SHA-512 hash of the given string.
|
||||||
Example: `"${sha512("${aws_vpc.default.tags.customer}-s3-bucket")}"`
|
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
|
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.
|
a different value for the rest of the resources.
|
||||||
Example: `element(split(",", var.r53_failover_policy), signum(count.index))`
|
Example: `element(split(",", var.r53_failover_policy), signum(count.index))`
|
||||||
|
|
Loading…
Reference in New Issue