@@ -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
377386func extractFieldMeta (f reflect.StructField ) (meta fieldMeta ) {
378387 if f .Anonymous {
0 commit comments