config: eval HEL as long as the result changes [GH-2909]

This commit is contained in:
Mitchell Hashimoto 2016-01-19 12:51:56 -08:00
parent 54de9057ba
commit f223be15cd
3 changed files with 49 additions and 3 deletions

View File

@ -118,10 +118,16 @@ func (w *interpolationWalker) Primitive(v reflect.Value) error {
return err
}
// If the AST we got is just a literal string value, then we ignore it
if _, ok := astRoot.(*ast.LiteralNode); ok {
// If the AST we got is just a literal string value with the same
// value then we ignore it. We have to check if its the same value
// because it is possible to input a string, get out a string, and
// have it be different. For example: "foo-$${bar}" turns into
// "foo-${bar}"
if n, ok := astRoot.(*ast.LiteralNode); ok {
if s, ok := n.Value.(string); ok && s == v.String() {
return nil
}
}
if w.ContextF != nil {
w.ContextF(w.loc, astRoot)

View File

@ -24,6 +24,14 @@ func TestEval(t *testing.T) {
ast.TypeString,
},
{
"foo $${bar}",
nil,
false,
"foo ${bar}",
ast.TypeString,
},
{
"foo ${bar}",
&ast.BasicScope{

View File

@ -114,6 +114,38 @@ func TestRawConfig_double(t *testing.T) {
}
}
func TestRawConfigInterpolate_escaped(t *testing.T) {
raw := map[string]interface{}{
"foo": "bar-$${baz}",
}
rc, err := NewRawConfig(raw)
if err != nil {
t.Fatalf("err: %s", err)
}
// Before interpolate, Config() should be the raw
if !reflect.DeepEqual(rc.Config(), raw) {
t.Fatalf("bad: %#v", rc.Config())
}
if err := rc.Interpolate(nil); err != nil {
t.Fatalf("err: %s", err)
}
actual := rc.Config()
expected := map[string]interface{}{
"foo": "bar-${baz}",
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
if len(rc.UnknownKeys()) != 0 {
t.Fatalf("bad: %#v", rc.UnknownKeys())
}
}
func TestRawConfig_merge(t *testing.T) {
raw1 := map[string]interface{}{
"foo": "${var.foo}",