Skip to content

Commit ae61226

Browse files
Completely escape non-expr strings
1 parent cb91e76 commit ae61226

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

_tests/escaped-strings.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
EscapedString = "\n\t\r\\\""
2-
TemplateString = "\n\t\r\\\" ${"\\ \""}"
2+
TemplateString = "test-A${"\\ \""}"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ require (
66
github.com/hashicorp/hcl v1.0.0
77
github.com/hashicorp/hcl/v2 v2.11.1
88
github.com/stretchr/testify v1.6.1
9-
github.com/zclconf/go-cty v1.8.0 // indirect
9+
github.com/zclconf/go-cty v1.8.0
1010
)

hclencoder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func TestEncoder(t *testing.T) {
6868
ID: "escaped strings",
6969
Input: struct {
7070
EscapedString string
71-
TemplateString string
71+
TemplateString string `hcl:",expr"`
7272
}{
7373
"\n\t\r\\\"",
74-
"\n\t\r\\\" ${\"\\\\ \\\"\"}",
74+
"\"test-\u0041${\"\\\\ \\\"\"}\"",
7575
},
7676
Output: "escaped-strings",
7777
},

tokens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func tokenize(in reflect.Value, meta fieldMeta) (tkns hclwrite.Tokens, err error
5050
case reflect.String:
5151
val := in.String()
5252
if !meta.expression {
53-
val = fmt.Sprintf(`"%s"`, EscapeString(val))
53+
return hclwrite.TokensForValue(cty.StringVal(val)), nil
5454
}
5555
// Unfortunately hcl escapes template expressions (${...}) when using hclwrite.TokensForValue. So we escape
5656
// everything but template expressions and then parse the expression into tokens.

0 commit comments

Comments
 (0)