config: interpolationWalker detects functions

This commit is contained in:
Mitchell Hashimoto 2014-07-22 06:43:04 -07:00
parent 8b5cc5d534
commit 7578fb8bdc
3 changed files with 39 additions and 1 deletions

View File

@ -98,6 +98,12 @@ func NewInterpolation(v string) (Interpolation, error) {
args := make([]InterpolatedVariable, 0, len(match)-2)
for i := 2; i < len(match); i++ {
// This can be empty if we have a single argument
// due to the format of the regexp.
if match[i] == "" {
continue
}
v, err := NewInterpolatedVariable(match[i])
if err != nil {
return nil, err
@ -196,6 +202,12 @@ func (i *VariableInterpolation) Variables() map[string]InterpolatedVariable {
func NewResourceVariable(key string) (*ResourceVariable, error) {
parts := strings.SplitN(key, ".", 3)
if len(parts) < 3 {
return nil, fmt.Errorf(
"%s: resource variables must be three parts: type.name.attr",
key)
}
field := parts[2]
multi := false
var index int

View File

@ -10,7 +10,7 @@ import (
// interpRegexp is a regexp that matches interpolations such as ${foo.bar}
var interpRegexp *regexp.Regexp = regexp.MustCompile(
`(?i)(\$+)\{([*-.a-z0-9_]+)\}`)
`(?i)(\$+)\{([\s*-.,\(\)a-z0-9_]+)\}`)
// interpolationWalker implements interfaces for the reflectwalk package
// (github.com/mitchellh/reflectwalk) that can be used to automatically

View File

@ -33,6 +33,24 @@ func TestInterpolationWalker_detect(t *testing.T) {
},
},
},
{
Input: map[string]interface{}{
"foo": "${lookup(var.foo)}",
},
Result: []Interpolation{
&FunctionInterpolation{
Func: nil,
Args: []InterpolatedVariable{
&UserVariable{
Name: "foo",
key: "var.foo",
},
},
key: "lookup(var.foo)",
},
},
},
}
for i, tc := range cases {
@ -48,6 +66,14 @@ func TestInterpolationWalker_detect(t *testing.T) {
t.Fatalf("err: %s", err)
}
for _, a := range actual {
// This is jank, but reflect.DeepEqual never has functions
// being the same.
if f, ok := a.(*FunctionInterpolation); ok {
f.Func = nil
}
}
if !reflect.DeepEqual(actual, tc.Result) {
t.Fatalf("%d: bad:\n\n%#v", i, actual)
}