From 2fb7c957a4e8b0b412125840480ac1a7e1accd04 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Thu, 16 Jun 2016 12:30:20 -0500 Subject: [PATCH] Remove BC escaped double quote behavior When we switched to the current edition of the HCL parser, we inadvertently broke some undocumented behavior where users could successfully used escaped double quotes in a nested interpolation like this: foo = "${somefunc(\"somearg\")}" The proper syntax here is: foo = "${somefunc("somearg")}" Because once you are inside the interpolation braces, the "quoting context" is different. At the time, while we didn't like the fact that the non-standard syntax was in the wild, we didn't want to break BC, so we treated it as a bug and fixed it in #62. Now that we are at the moment of a major Terraform release, we can yank the BC compatible behavior, which fixes a currently broken use case - there is no way to express strings with double quotes in them from inside interpolation braces. foo = "${somefunc("\"inquotes\"")}" After merge and a dep upgrade, this will take care of https://github.com/hashicorp/terraform/issues/5550 --- decoder_test.go | 9 +-------- hcl/strconv/quote.go | 9 ++++----- hcl/strconv/quote_test.go | 2 +- hcl/token/token_test.go | 2 +- test-fixtures/escape.hcl | 2 +- test-fixtures/interpolate_escape.hcl | 1 - 6 files changed, 8 insertions(+), 17 deletions(-) delete mode 100644 test-fixtures/interpolate_escape.hcl diff --git a/decoder_test.go b/decoder_test.go index 961c545..79994cb 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -64,14 +64,7 @@ func TestDecode_interface(t *testing.T) { "bar": "new\nline", "qax": `slash\:colon`, "nested": `${HH\:mm\:ss}`, - "nestedquotes": `${"okay"}`, - }, - }, - { - "interpolate_escape.hcl", - false, - map[string]interface{}{ - "foo": "${file(\"bing/bong.txt\")}", + "nestedquotes": `${"\"stringwrappedinquotes\""}`, }, }, { diff --git a/hcl/strconv/quote.go b/hcl/strconv/quote.go index 6cfd8c3..74e232e 100644 --- a/hcl/strconv/quote.go +++ b/hcl/strconv/quote.go @@ -49,8 +49,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 quotes and escaped backslashes, which we - // handle specifically. + // section, except for escaped backslashes, which we handle specifically. if s[0] == '$' && len(s) > 1 && s[1] == '{' { buf = append(buf, '$', '{') s = s[2:] @@ -65,12 +64,12 @@ func Unquote(s string) (t string, err error) { s = s[size:] - // We special case escaped double quotes and escaped backslashes in - // interpolations, converting them to their unescaped equivalents. + // We special case escaped backslashes in interpolations, converting + // them to their unescaped equivalents. if r == '\\' { q, _ := utf8.DecodeRuneInString(s) switch q { - case '"', '\\': + case '\\': continue } } diff --git a/hcl/strconv/quote_test.go b/hcl/strconv/quote_test.go index bee3916..c369144 100644 --- a/hcl/strconv/quote_test.go +++ b/hcl/strconv/quote_test.go @@ -36,7 +36,7 @@ var unquotetests = []unQuoteTest{ {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""}, {`"'"`, "'"}, {`"${file("foo")}"`, `${file("foo")}`}, - {`"${file(\"foo\")}"`, `${file("foo")}`}, + {`"${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}`}, diff --git a/hcl/token/token_test.go b/hcl/token/token_test.go index b5b766c..932951c 100644 --- a/hcl/token/token_test.go +++ b/hcl/token/token_test.go @@ -50,7 +50,7 @@ func TestTokenValue(t *testing.T) { {Token{Type: IDENT, Text: `foo`}, "foo"}, {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: `"${file("foo")}"`}, `${file("foo")}`}, {Token{Type: HEREDOC, Text: "<