From 7578fb8bdc3c1744861b78c002800a0fa2e8cb7a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jul 2014 06:43:04 -0700 Subject: [PATCH] config: interpolationWalker detects functions --- config/interpolate.go | 12 ++++++++++++ config/interpolate_walk.go | 2 +- config/interpolate_walk_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/config/interpolate.go b/config/interpolate.go index ab2cd26a4..1c6aeefc9 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -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 diff --git a/config/interpolate_walk.go b/config/interpolate_walk.go index 90b98e1ec..fd8a70833 100644 --- a/config/interpolate_walk.go +++ b/config/interpolate_walk.go @@ -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 diff --git a/config/interpolate_walk_test.go b/config/interpolate_walk_test.go index 255e0de5b..a731f8615 100644 --- a/config/interpolate_walk_test.go +++ b/config/interpolate_walk_test.go @@ -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) }