config: ReplaceVariables is public
This commit is contained in:
parent
686b563428
commit
d1cfb38bb8
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue