Merge pull request #125 from hashicorp/b-escapes

strconv: Fix escaped backslashes \\ in braces ${}
This commit is contained in:
Paul Hinze 2016-06-06 19:19:40 -05:00
commit d7400db714
4 changed files with 19 additions and 5 deletions

View File

@ -59,7 +59,12 @@ func TestDecode_interface(t *testing.T) {
"escape.hcl",
false,
map[string]interface{}{
"foo": "bar\"baz\\n",
"foo": "bar\"baz\\n",
"qux": "back\\slash",
"bar": "new\nline",
"qax": `slash\:colon`,
"nested": `${HH\:mm\:ss}`,
"nestedquotes": `${"okay"}`,
},
},
{

View File

@ -49,7 +49,8 @@ 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, which we handle specifically.
// section, except for escaped quotes and escaped backslashes, which we
// handle specifically.
if s[0] == '$' && len(s) > 1 && s[1] == '{' {
buf = append(buf, '$', '{')
s = s[2:]
@ -64,10 +65,12 @@ func Unquote(s string) (t string, err error) {
s = s[size:]
// We special case escaped double quotes in interpolations, converting
// them to straight double quotes.
// We special case escaped double quotes and escaped backslashes in
// interpolations, converting them to their unescaped equivalents.
if r == '\\' {
if q, _ := utf8.DecodeRuneInString(s); q == '"' {
q, _ := utf8.DecodeRuneInString(s)
switch q {
case '"', '\\':
continue
}
}

View File

@ -39,6 +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}`},
}
var misquoted = []string{

View File

@ -1 +1,6 @@
foo = "bar\"baz\\n"
bar = "new\nline"
qux = "back\\slash"
qax = "slash\\:colon"
nested = "${HH\\:mm\\:ss}"
nestedquotes = "${\"okay\"}"