hcl: don't escape ANYTHING in ${}, let lower layer handle it

This commit is contained in:
Mitchell Hashimoto 2016-09-09 19:45:36 -06:00
parent 99df0eb941
commit 62ebf9354f
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
6 changed files with 30 additions and 15 deletions

View File

@ -63,7 +63,7 @@ func TestDecode_interface(t *testing.T) {
"qux": "back\\slash", "qux": "back\\slash",
"bar": "new\nline", "bar": "new\nline",
"qax": `slash\:colon`, "qax": `slash\:colon`,
"nested": `${HH\:mm\:ss}`, "nested": `${HH\\:mm\\:ss}`,
"nestedquotes": `${"\"stringwrappedinquotes\""}`, "nestedquotes": `${"\"stringwrappedinquotes\""}`,
}, },
}, },
@ -356,6 +356,20 @@ func TestDecode_interface(t *testing.T) {
true, true,
nil, 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 { for _, tc := range cases {

View File

@ -363,7 +363,7 @@ func TestRealExample(t *testing.T) {
provider "aws" { provider "aws" {
access_key = "foo" access_key = "foo"
secret_key = "bar" secret_key = "${replace(var.foo, ".", "\\.")}"
} }
resource "aws_security_group" "firewall" { resource "aws_security_group" "firewall" {
@ -416,7 +416,7 @@ EOF
{token.STRING, `"foo"`}, {token.STRING, `"foo"`},
{token.IDENT, `secret_key`}, {token.IDENT, `secret_key`},
{token.ASSIGN, `=`}, {token.ASSIGN, `=`},
{token.STRING, `"bar"`}, {token.STRING, `"${replace(var.foo, ".", "\\.")}"`},
{token.RBRACE, `}`}, {token.RBRACE, `}`},
{token.IDENT, `resource`}, {token.IDENT, `resource`},
{token.STRING, `"aws_security_group"`}, {token.STRING, `"aws_security_group"`},

View File

@ -46,7 +46,7 @@ func Unquote(s string) (t string, err error) {
for len(s) > 0 { for len(s) > 0 {
// If we're starting a '${}' then let it through un-unquoted. // If we're starting a '${}' then let it through un-unquoted.
// Specifically: we don't unquote any characters within the `${}` // 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] == '{' { if s[0] == '$' && len(s) > 1 && s[1] == '{' {
buf = append(buf, '$', '{') buf = append(buf, '$', '{')
s = s[2:] s = s[2:]
@ -61,16 +61,6 @@ func Unquote(s string) (t string, err error) {
s = s[size:] 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) n := utf8.EncodeRune(runeTmp[:], r)
buf = append(buf, runeTmp[:n]...) buf = append(buf, runeTmp[:n]...)

View File

@ -39,7 +39,7 @@ var unquotetests = []unQuoteTest{
{`"${file("\"foo\"")}"`, `${file("\"foo\"")}`}, {`"${file("\"foo\"")}"`, `${file("\"foo\"")}`},
{`"echo ${var.region}${element(split(",",var.zones),0)}"`, {`"echo ${var.region}${element(split(",",var.zones),0)}"`,
`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{ var misquoted = []string{

View File

@ -51,6 +51,12 @@ func TestTokenValue(t *testing.T) {
{Token{Type: STRING, Text: `"foo"`}, "foo"}, {Token{Type: STRING, Text: `"foo"`}, "foo"},
{Token{Type: STRING, Text: `"foo\nbar"`}, "foo\nbar"}, {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: STRING,
Text: `"${replace("foo", ".", "\\.")}"`,
},
`${replace("foo", ".", "\\.")}`},
{Token{Type: HEREDOC, Text: "<<EOF\nfoo\nbar\nEOF"}, "foo\nbar"}, {Token{Type: HEREDOC, Text: "<<EOF\nfoo\nbar\nEOF"}, "foo\nbar"},
} }

View File

@ -0,0 +1,5 @@
output {
one = "${replace(var.sub_domain, ".", "\\.")}"
two = "${replace(var.sub_domain, ".", "\\\\.")}"
many = "${replace(var.sub_domain, ".", "\\\\\\\\.")}"
}