config: lookup function works + tests
This commit is contained in:
parent
6a191d7395
commit
8b5cc5d534
|
@ -1,5 +1,10 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Funcs is the mapping of built-in functions for configuration.
|
// Funcs is the mapping of built-in functions for configuration.
|
||||||
var Funcs map[string]InterpolationFunc
|
var Funcs map[string]InterpolationFunc
|
||||||
|
|
||||||
|
@ -13,5 +18,18 @@ func init() {
|
||||||
// dynamic lookups of map types within a Terraform configuration.
|
// dynamic lookups of map types within a Terraform configuration.
|
||||||
func interpolationFuncLookup(
|
func interpolationFuncLookup(
|
||||||
vs map[string]string, args ...string) (string, error) {
|
vs map[string]string, args ...string) (string, error) {
|
||||||
return "", nil
|
if len(args) != 2 {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"lookup expects 2 arguments, got %d", len(args))
|
||||||
|
}
|
||||||
|
|
||||||
|
k := fmt.Sprintf("var.%s", strings.Join(args, "."))
|
||||||
|
v, ok := vs[k]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf(
|
||||||
|
"lookup in '%s' failed to find '%s'",
|
||||||
|
args[0], args[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInterpolateFuncLookup(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
M map[string]string
|
||||||
|
Args []string
|
||||||
|
Result string
|
||||||
|
Error bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
map[string]string{
|
||||||
|
"var.foo.bar": "baz",
|
||||||
|
},
|
||||||
|
[]string{"foo", "bar"},
|
||||||
|
"baz",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Invalid key
|
||||||
|
{
|
||||||
|
map[string]string{
|
||||||
|
"var.foo.bar": "baz",
|
||||||
|
},
|
||||||
|
[]string{"foo", "baz"},
|
||||||
|
"",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
|
||||||
|
// Too many args
|
||||||
|
{
|
||||||
|
map[string]string{
|
||||||
|
"var.foo.bar": "baz",
|
||||||
|
},
|
||||||
|
[]string{"foo", "bar", "baz"},
|
||||||
|
"",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range cases {
|
||||||
|
actual, err := interpolationFuncLookup(tc.M, tc.Args...)
|
||||||
|
if (err != nil) != tc.Error {
|
||||||
|
t.Fatalf("%d: err: %s", i, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if actual != tc.Result {
|
||||||
|
t.Fatalf("%d: bad: %#v", i, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue