From d1cfb38bb8da5c09be23e13d35168e7e69c4293a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 20 Jun 2014 11:41:12 -0700 Subject: [PATCH] config: ReplaceVariables is public --- config/variable.go | 14 +++++++++----- config/variable_test.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/config/variable.go b/config/variable.go index 0fd0ebd84..841981a39 100644 --- a/config/variable.go +++ b/config/variable.go @@ -16,14 +16,18 @@ func init() { varRegexp = regexp.MustCompile(`(?i)(\$+)\{([-.a-z0-9_]+)\}`) } -// replaceVariables takes a configuration and a mapping of variables +// ReplaceVariables takes a configuration and a mapping of variables // and performs the structure walking necessary to properly replace // all the variables. -func replaceVariables( - c map[string]interface{}, - vs map[string]string) error { +func ReplaceVariables( + c interface{}, + vs map[string]string) ([]string, error) { w := &variableReplaceWalker{Values: vs} - return reflectwalk.Walk(c, w) + if err := reflectwalk.Walk(c, w); err != nil { + return nil, err + } + + return w.UnknownKeys, nil } // variableDetectWalker implements interfaces for the reflectwalk package diff --git a/config/variable_test.go b/config/variable_test.go index bd3a6a96e..88fdb23e1 100644 --- a/config/variable_test.go +++ b/config/variable_test.go @@ -36,6 +36,25 @@ func BenchmarkVariableReplaceWalker(b *testing.B) { } } +func TestReplaceVariables(t *testing.T) { + input := "foo-${var.bar}" + expected := "foo-bar" + + unk, err := ReplaceVariables(&input, map[string]string{ + "var.bar": "bar", + }) + if err != nil { + t.Fatalf("err: %s", err) + } + if len(unk) > 0 { + t.Fatal("bad: %#v", unk) + } + + if input != expected { + t.Fatalf("bad: %#v", input) + } +} + func TestVariableDetectWalker(t *testing.T) { w := new(variableDetectWalker)