diff --git a/config/variable.go b/config/variable.go index edf8b671c8..e26fa85bb6 100644 --- a/config/variable.go +++ b/config/variable.go @@ -1,6 +1,7 @@ package config import ( + "fmt" "reflect" "regexp" "strings" @@ -53,6 +54,13 @@ func (w *variableDetectWalker) Primitive(v reflect.Value) error { var err error var iv InterpolatedVariable + if strings.Index(key, ".") == -1 { + return fmt.Errorf( + "Interpolated variable '%s' has bad format. "+ + "Did you mean 'var.%s'?", + key, key) + } + if strings.HasPrefix(key, "var.") { iv, err = NewUserVariable(key) } else { diff --git a/config/variable_test.go b/config/variable_test.go new file mode 100644 index 0000000000..bef0f60af7 --- /dev/null +++ b/config/variable_test.go @@ -0,0 +1,58 @@ +package config + +import ( + "reflect" + "testing" +) + +func TestVariableDetectWalker(t *testing.T) { + w := new(variableDetectWalker) + + str := `foo ${var.bar}` + if err := w.Primitive(reflect.ValueOf(str)); err != nil { + t.Fatalf("err: %s", err) + } + + if len(w.Variables) != 1 { + t.Fatalf("bad: %#v", w.Variables) + } + if w.Variables["var.bar"].(*UserVariable).FullKey() != "var.bar" { + t.Fatalf("bad: %#v", w.Variables) + } +} + +func TestVariableDetectWalker_bad(t *testing.T) { + w := new(variableDetectWalker) + + str := `foo ${bar}` + if err := w.Primitive(reflect.ValueOf(str)); err == nil { + t.Fatal("should error") + } +} + +func TestVariableDetectWalker_escaped(t *testing.T) { + w := new(variableDetectWalker) + + str := `foo $${var.bar}` + if err := w.Primitive(reflect.ValueOf(str)); err != nil { + t.Fatalf("err: %s", err) + } + + if len(w.Variables) > 0 { + t.Fatalf("bad: %#v", w.Variables) + } +} + +func TestVariableDetectWalker_empty(t *testing.T) { + w := new(variableDetectWalker) + + str := `foo` + if err := w.Primitive(reflect.ValueOf(str)); err != nil { + t.Fatalf("err: %s", err) + } + + if len(w.Variables) > 0 { + t.Fatalf("bad: %#v", w.Variables) + } +} +