Skip to content

Commit 8551ed3

Browse files
Escape strings
1 parent 7b0372d commit 8551ed3

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

_tests/basic.hcl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
String = "bar"
22

3+
EscapedString = "\"\\"
4+
35
Int = 123
46

57
Bool = true

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func Example() {
111111
// says = "meow"
112112
// }
113113
//
114-
// buildings {
114+
// buildings = {
115115
// Barn = "456 Digits Drive"
116116
// House = "123 Numbers Lane"
117117
// }

hclencoder_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ func TestEncoder(t *testing.T) {
2525
{
2626
ID: "basic struct",
2727
Input: struct {
28-
String string
29-
Int int
30-
Bool bool
31-
Float float64
28+
String string
29+
EscapedString string
30+
Int int
31+
Bool bool
32+
Float float64
3233
}{
3334
"bar",
35+
`"\`,
3436
123,
3537
true,
3638
4.56,
@@ -222,7 +224,8 @@ func TestEncoder(t *testing.T) {
222224
}
223225

224226
assert.NoError(t, err, test.ID)
225-
assert.EqualValues(t,
227+
assert.EqualValues(
228+
t,
226229
string(expected),
227230
string(actual),
228231
fmt.Sprintf("%s\nExpected:\n%s\nActual:\n%s", test.ID, expected, actual),

nodes.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ func encodeMap(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
207207
itemKey := &ast.ObjectKey{Token: tkn}
208208
for _, obj := range typ.Items {
209209
keys := append([]*ast.ObjectKey{itemKey}, obj.Keys...)
210-
l = append(l, &ast.ObjectItem{
211-
Keys: keys,
212-
Val: obj.Val,
213-
})
210+
l = append(
211+
l, &ast.ObjectItem{
212+
Keys: keys,
213+
Val: obj.Val,
214+
},
215+
)
214216
}
215217

216218
default:
@@ -306,10 +308,12 @@ func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
306308
if objectList, ok := val.(*ast.ObjectList); ok {
307309
for _, obj := range objectList.Items {
308310
objectKeys := append([]*ast.ObjectKey{itemKey}, obj.Keys...)
309-
list.Add(&ast.ObjectItem{
310-
Keys: objectKeys,
311-
Val: obj.Val,
312-
})
311+
list.Add(
312+
&ast.ObjectItem{
313+
Keys: objectKeys,
314+
Val: obj.Val,
315+
},
316+
)
313317
}
314318
continue
315319
}
@@ -366,13 +370,18 @@ func tokenize(in reflect.Value, unquotedString bool) (t token.Token, err error)
366370
}
367371
return token.Token{
368372
Type: token.STRING,
369-
Text: fmt.Sprintf(`"%s"`, in.String()),
373+
Text: fmt.Sprintf(`"%s"`, escapeString(in.String())),
370374
}, nil
371375
}
372376

373377
return t, fmt.Errorf("cannot encode primitive kind %s to token", in.Kind())
374378
}
375379

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

nodes_test.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ func TestEncodePrimitive(t *testing.T) {
120120
},
121121
}
122122

123-
RunAll(tests, func(value reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
124-
return encodePrimitive(value, false)
125-
}, t)
123+
RunAll(
124+
tests, func(value reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
125+
return encodePrimitive(value, false)
126+
}, t,
127+
)
126128
}
127129

128130
func TestEncodeList(t *testing.T) {
@@ -239,9 +241,11 @@ func TestEncodeList(t *testing.T) {
239241
},
240242
}
241243

242-
RunAll(tests, func(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
243-
return encodeList(in, false)
244-
}, t)
244+
RunAll(
245+
tests, func(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
246+
return encodeList(in, fieldMeta{})
247+
}, t,
248+
)
245249
}
246250

247251
func TestEncodeMap(t *testing.T) {
@@ -290,16 +294,18 @@ func TestEncodeMap(t *testing.T) {
290294
},
291295
{
292296
ID: "keyed list",
293-
Input: reflect.ValueOf(map[string][]map[string]interface{}{
294-
"obj1": {
295-
{"foo": "bar"},
296-
{"boo": "hoo"},
297-
},
298-
"obj2": {
299-
{"foo": "bar"},
300-
{"boo": "hoo"},
297+
Input: reflect.ValueOf(
298+
map[string][]map[string]interface{}{
299+
"obj1": {
300+
{"foo": "bar"},
301+
{"boo": "hoo"},
302+
},
303+
"obj2": {
304+
{"foo": "bar"},
305+
{"boo": "hoo"},
306+
},
301307
},
302-
}),
308+
),
303309
Expected: &ast.ObjectType{List: &ast.ObjectList{Items: []*ast.ObjectItem{
304310
&ast.ObjectItem{
305311
Keys: []*ast.ObjectKey{{Token: token.Token{Type: token.IDENT, Text: "obj1"}}},

0 commit comments

Comments
 (0)