Merge pull request #6884 from daveadams/f-lookup-default-value
Allow specifying a default value to lookup()
This commit is contained in:
commit
3500dcfc1d
|
@ -548,17 +548,32 @@ func interpolationFuncSplit() ast.Function {
|
||||||
// dynamic lookups of map types within a Terraform configuration.
|
// dynamic lookups of map types within a Terraform configuration.
|
||||||
func interpolationFuncLookup(vs map[string]ast.Variable) ast.Function {
|
func interpolationFuncLookup(vs map[string]ast.Variable) ast.Function {
|
||||||
return ast.Function{
|
return ast.Function{
|
||||||
ArgTypes: []ast.Type{ast.TypeMap, ast.TypeString},
|
ArgTypes: []ast.Type{ast.TypeMap, ast.TypeString},
|
||||||
ReturnType: ast.TypeString,
|
ReturnType: ast.TypeString,
|
||||||
|
Variadic: true,
|
||||||
|
VariadicType: ast.TypeString,
|
||||||
Callback: func(args []interface{}) (interface{}, error) {
|
Callback: func(args []interface{}) (interface{}, error) {
|
||||||
|
defaultValue := ""
|
||||||
|
defaultValueSet := false
|
||||||
|
if len(args) > 2 {
|
||||||
|
defaultValue = args[2].(string)
|
||||||
|
defaultValueSet = true
|
||||||
|
}
|
||||||
|
if len(args) > 3 {
|
||||||
|
return "", fmt.Errorf("lookup() takes no more than three arguments")
|
||||||
|
}
|
||||||
index := args[1].(string)
|
index := args[1].(string)
|
||||||
mapVar := args[0].(map[string]ast.Variable)
|
mapVar := args[0].(map[string]ast.Variable)
|
||||||
|
|
||||||
v, ok := mapVar[index]
|
v, ok := mapVar[index]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf(
|
if defaultValueSet {
|
||||||
"lookup failed to find '%s'",
|
return defaultValue, nil
|
||||||
args[1].(string))
|
} else {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"lookup failed to find '%s'",
|
||||||
|
args[1].(string))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if v.Type != ast.TypeString {
|
if v.Type != ast.TypeString {
|
||||||
return "", fmt.Errorf(
|
return "", fmt.Errorf(
|
||||||
|
|
|
@ -713,12 +713,33 @@ func TestInterpolateFuncLookup(t *testing.T) {
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Supplied default with valid key
|
||||||
|
{
|
||||||
|
`${lookup(var.foo, "bar", "")}`,
|
||||||
|
"baz",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Supplied default with invalid key
|
||||||
|
{
|
||||||
|
`${lookup(var.foo, "zip", "")}`,
|
||||||
|
"",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
// Too many args
|
// Too many args
|
||||||
{
|
{
|
||||||
`${lookup(var.foo, "bar", "baz")}`,
|
`${lookup(var.foo, "bar", "", "abc")}`,
|
||||||
nil,
|
nil,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Non-empty default
|
||||||
|
{
|
||||||
|
`${lookup(var.foo, "zap", "xyz")}`,
|
||||||
|
"xyz",
|
||||||
|
false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,9 +160,11 @@ The supported built-in functions are:
|
||||||
* `${length(split(",", "a,b,c"))}` = 3
|
* `${length(split(",", "a,b,c"))}` = 3
|
||||||
* `${length("a,b,c")}` = 5
|
* `${length("a,b,c")}` = 5
|
||||||
|
|
||||||
* `lookup(map, key)` - Performs a dynamic lookup into a mapping
|
* `lookup(map, key [, default])` - Performs a dynamic lookup into a mapping
|
||||||
variable. The `map` parameter should be another variable, such
|
variable. The `map` parameter should be another variable, such
|
||||||
as `var.amis`.
|
as `var.amis`. If `key` does not exist in `map`, the interpolation will
|
||||||
|
fail unless you specify a third argument, `default`, which should be a
|
||||||
|
string value to return if no `key` is found in `map.
|
||||||
|
|
||||||
* `lower(string)` - Returns a copy of the string with all Unicode letters mapped to their lower case.
|
* `lower(string)` - Returns a copy of the string with all Unicode letters mapped to their lower case.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue