Skip to content

Commit f06dcfd

Browse files
Use strconv.Quote instead
1 parent 8551ed3 commit f06dcfd

File tree

3 files changed

+4
-8
lines changed

3 files changed

+4
-8
lines changed

_tests/basic.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
String = "bar"
22

3-
EscapedString = "\"\\"
3+
EscapedString = "\"\\\n\t"
44

55
Int = 123
66

hclencoder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ func TestEncoder(t *testing.T) {
3232
Float float64
3333
}{
3434
"bar",
35-
`"\`,
35+
`"\
36+
`,
3637
123,
3738
true,
3839
4.56,

nodes.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,13 @@ func tokenize(in reflect.Value, unquotedString bool) (t token.Token, err error)
370370
}
371371
return token.Token{
372372
Type: token.STRING,
373-
Text: fmt.Sprintf(`"%s"`, escapeString(in.String())),
373+
Text: strconv.Quote(in.String()),
374374
}, nil
375375
}
376376

377377
return t, fmt.Errorf("cannot encode primitive kind %s to token", in.Kind())
378378
}
379379

380-
// escapes \ and "
381-
func escapeString(s string) string {
382-
return strings.ReplaceAll(strings.ReplaceAll(s, `\`, `\\`), `"`, `\"`)
383-
}
384-
385380
// extractFieldMeta pulls information about struct fields and the optional HCL tags
386381
func extractFieldMeta(f reflect.StructField) (meta fieldMeta) {
387382
if f.Anonymous {

0 commit comments

Comments
 (0)