Skip to content

Commit

Permalink
hcl: don't escape ANYTHING in ${}, let lower layer handle it
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Sep 10, 2016
1 parent 99df0eb commit 62ebf93
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
16 changes: 15 additions & 1 deletion decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestDecode_interface(t *testing.T) {
"qux": "back\\slash",
"bar": "new\nline",
"qax": `slash\:colon`,
"nested": `${HH\:mm\:ss}`,
"nested": `${HH\\:mm\\:ss}`,
"nestedquotes": `${"\"stringwrappedinquotes\""}`,
},
},
Expand Down Expand Up @@ -356,6 +356,20 @@ func TestDecode_interface(t *testing.T) {
true,
nil,
},

{
"escape_backslash.hcl",
false,
map[string]interface{}{
"output": []map[string]interface{}{
map[string]interface{}{
"one": `${replace(var.sub_domain, ".", "\\.")}`,
"two": `${replace(var.sub_domain, ".", "\\\\.")}`,
"many": `${replace(var.sub_domain, ".", "\\\\\\\\.")}`,
},
},
},
},
}

for _, tc := range cases {
Expand Down
4 changes: 2 additions & 2 deletions hcl/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func TestRealExample(t *testing.T) {
provider "aws" {
access_key = "foo"
secret_key = "bar"
secret_key = "${replace(var.foo, ".", "\\.")}"
}
resource "aws_security_group" "firewall" {
Expand Down Expand Up @@ -416,7 +416,7 @@ EOF
{token.STRING, `"foo"`},
{token.IDENT, `secret_key`},
{token.ASSIGN, `=`},
{token.STRING, `"bar"`},
{token.STRING, `"${replace(var.foo, ".", "\\.")}"`},
{token.RBRACE, `}`},
{token.IDENT, `resource`},
{token.STRING, `"aws_security_group"`},
Expand Down
12 changes: 1 addition & 11 deletions hcl/strconv/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Unquote(s string) (t string, err error) {
for len(s) > 0 {
// If we're starting a '${}' then let it through un-unquoted.
// Specifically: we don't unquote any characters within the `${}`
// section, except for escaped backslashes, which we handle specifically.
// section.
if s[0] == '$' && len(s) > 1 && s[1] == '{' {
buf = append(buf, '$', '{')
s = s[2:]
Expand All @@ -61,16 +61,6 @@ func Unquote(s string) (t string, err error) {

s = s[size:]

// We special case escaped backslashes in interpolations, converting
// them to their unescaped equivalents.
if r == '\\' {
q, _ := utf8.DecodeRuneInString(s)
switch q {
case '\\':
continue
}
}

n := utf8.EncodeRune(runeTmp[:], r)
buf = append(buf, runeTmp[:n]...)

Expand Down
2 changes: 1 addition & 1 deletion hcl/strconv/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var unquotetests = []unQuoteTest{
{`"${file("\"foo\"")}"`, `${file("\"foo\"")}`},
{`"echo ${var.region}${element(split(",",var.zones),0)}"`,
`echo ${var.region}${element(split(",",var.zones),0)}`},
{`"${HH\\:mm\\:ss}"`, `${HH\:mm\:ss}`},
{`"${HH\\:mm\\:ss}"`, `${HH\\:mm\\:ss}`},
}

var misquoted = []string{
Expand Down
6 changes: 6 additions & 0 deletions hcl/token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func TestTokenValue(t *testing.T) {
{Token{Type: STRING, Text: `"foo"`}, "foo"},
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"},
{Token{Type: STRING, Text: `"${file("foo")}"`}, `${file("foo")}`},
{
Token{
Type: STRING,
Text: `"${replace("foo", ".", "\\.")}"`,
},
`${replace("foo", ".", "\\.")}`},
{Token{Type: HEREDOC, Text: "<<EOF\nfoo\nbar\nEOF"}, "foo\nbar"},
}

Expand Down
5 changes: 5 additions & 0 deletions test-fixtures/escape_backslash.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output {
one = "${replace(var.sub_domain, ".", "\\.")}"
two = "${replace(var.sub_domain, ".", "\\\\.")}"
many = "${replace(var.sub_domain, ".", "\\\\\\\\.")}"
}

0 comments on commit 62ebf93

Please sign in to comment.