config: lookup function works + tests

This commit is contained in:
Mitchell Hashimoto 2014-07-21 13:12:43 -07:00
parent 6a191d7395
commit 8b5cc5d534
2 changed files with 73 additions and 1 deletions

View File

@ -1,5 +1,10 @@
package config
import (
"fmt"
"strings"
)
// Funcs is the mapping of built-in functions for configuration.
var Funcs map[string]InterpolationFunc
@ -13,5 +18,18 @@ func init() {
// dynamic lookups of map types within a Terraform configuration.
func interpolationFuncLookup(
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
}

View File

@ -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)
}
}
}