Merge #3814: 'coalesce' interpolation func
This commit is contained in:
commit
988baa584b
|
@ -15,6 +15,7 @@ FEATURES:
|
|||
* **New resource: `google_compute_target_https_proxy`** [GH-3728]
|
||||
* **New resource: `google_compute_global_forwarding_rule`** [GH-3702]
|
||||
* **New resource: `openstack_networking_port_v2`** [GH-3731]
|
||||
* New interpolation function: `coalesce` [GH-3814]
|
||||
|
||||
IMPROVEMENTS:
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ func init() {
|
|||
"cidrhost": interpolationFuncCidrHost(),
|
||||
"cidrnetmask": interpolationFuncCidrNetmask(),
|
||||
"cidrsubnet": interpolationFuncCidrSubnet(),
|
||||
"coalesce": interpolationFuncCoalesce(),
|
||||
"compact": interpolationFuncCompact(),
|
||||
"concat": interpolationFuncConcat(),
|
||||
"element": interpolationFuncElement(),
|
||||
|
@ -145,6 +146,30 @@ func interpolationFuncCidrSubnet() ast.Function {
|
|||
}
|
||||
}
|
||||
|
||||
// interpolationFuncCoalesce implements the "coalesce" function that
|
||||
// returns the first non null / empty string from the provided input
|
||||
func interpolationFuncCoalesce() ast.Function {
|
||||
return ast.Function{
|
||||
ArgTypes: []ast.Type{ast.TypeString},
|
||||
ReturnType: ast.TypeString,
|
||||
Variadic: true,
|
||||
VariadicType: ast.TypeString,
|
||||
Callback: func(args []interface{}) (interface{}, error) {
|
||||
if len(args) < 2 {
|
||||
return nil, fmt.Errorf("must provide at least two arguments")
|
||||
}
|
||||
for _, arg := range args {
|
||||
argument := arg.(string)
|
||||
|
||||
if argument != "" {
|
||||
return argument, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// interpolationFuncConcat implements the "concat" function that
|
||||
// concatenates multiple strings. This isn't actually necessary anymore
|
||||
// since our language supports string concat natively, but for backwards
|
||||
|
|
|
@ -147,6 +147,33 @@ func TestInterpolateFuncCidrSubnet(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncCoalesce(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
{
|
||||
`${coalesce("first", "second", "third")}`,
|
||||
"first",
|
||||
false,
|
||||
},
|
||||
{
|
||||
`${coalesce("", "second", "third")}`,
|
||||
"second",
|
||||
false,
|
||||
},
|
||||
{
|
||||
`${coalesce("", "", "")}`,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
`${coalesce("foo")}`,
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncDeprecatedConcat(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
|
|
|
@ -95,6 +95,9 @@ The supported built-in functions are:
|
|||
CIDR notation (like ``10.0.0.0/8``) and extends its prefix to include an
|
||||
additional subnet number. For example,
|
||||
``cidrsubnet("10.0.0.0/8", 8, 2)`` returns ``10.2.0.0/16``.
|
||||
|
||||
* `coalesce(string1, string2, ...)` - Returns the first non-empty value from
|
||||
the given arguments. At least two arguments must be provided.
|
||||
|
||||
* `compact(list)` - Removes empty string elements from a list. This can be
|
||||
useful in some cases, for example when passing joined lists as module
|
||||
|
|
Loading…
Reference in New Issue