diff --git a/decoder_test.go b/decoder_test.go index 39ea046..b982627 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -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\""}`, }, }, @@ -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 { diff --git a/hcl/scanner/scanner_test.go b/hcl/scanner/scanner_test.go index b167811..e92f650 100644 --- a/hcl/scanner/scanner_test.go +++ b/hcl/scanner/scanner_test.go @@ -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" { @@ -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"`}, diff --git a/hcl/strconv/quote.go b/hcl/strconv/quote.go index 956c899..d578769 100644 --- a/hcl/strconv/quote.go +++ b/hcl/strconv/quote.go @@ -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:] @@ -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]...) diff --git a/hcl/strconv/quote_test.go b/hcl/strconv/quote_test.go index af2d848..9de8c9f 100644 --- a/hcl/strconv/quote_test.go +++ b/hcl/strconv/quote_test.go @@ -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{ diff --git a/hcl/token/token_test.go b/hcl/token/token_test.go index 932951c..e4b4af2 100644 --- a/hcl/token/token_test.go +++ b/hcl/token/token_test.go @@ -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: "<