config: ReplaceVariables is public

This commit is contained in:
Mitchell Hashimoto 2014-06-20 11:41:12 -07:00
parent 686b563428
commit d1cfb38bb8
2 changed files with 28 additions and 5 deletions

View File

@ -16,14 +16,18 @@ func init() {
varRegexp = regexp.MustCompile(`(?i)(\$+)\{([-.a-z0-9_]+)\}`) 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 // and performs the structure walking necessary to properly replace
// all the variables. // all the variables.
func replaceVariables( func ReplaceVariables(
c map[string]interface{}, c interface{},
vs map[string]string) error { vs map[string]string) ([]string, error) {
w := &variableReplaceWalker{Values: vs} 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 // variableDetectWalker implements interfaces for the reflectwalk package

View File

@ -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) { func TestVariableDetectWalker(t *testing.T) {
w := new(variableDetectWalker) w := new(variableDetectWalker)