@@ -32,6 +32,9 @@ const (
3232 // a list.
3333 Blocks string = "blocks"
3434
35+ // Expression indicates that this field should not be quoted.
36+ Expression string = "expr"
37+
3538 // UnusedKeysTag is a flag that indicates any unused keys found by the
3639 // decoder are stored in this field of type []string. This has the same
3740 // behavior as the OmitTag and is not encoded.
@@ -61,6 +64,7 @@ type fieldMeta struct {
6164 key bool
6265 squash bool
6366 repeatBlock bool
67+ expression bool
6468 unusedKeys bool
6569 decodedFields bool
6670 omit bool
@@ -83,7 +87,7 @@ func encodeField(in reflect.Value, meta fieldMeta) (node ast.Node, key []*ast.Ob
8387 case reflect .Bool , reflect .Float64 , reflect .String ,
8488 reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 ,
8589 reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
86- return encodePrimitive (in )
90+ return encodePrimitive (in , meta . expression )
8791
8892 case reflect .Slice :
8993 return encodeList (in , meta .repeatBlock )
@@ -101,8 +105,8 @@ func encodeField(in reflect.Value, meta fieldMeta) (node ast.Node, key []*ast.Ob
101105
102106// encodePrimitive converts a primitive value into an ast.LiteralType. An
103107// ast.ObjectKey is never returned.
104- func encodePrimitive (in reflect.Value ) (ast.Node , []* ast.ObjectKey , error ) {
105- tkn , err := tokenize (in , false )
108+ func encodePrimitive (in reflect.Value , expr bool ) (ast.Node , []* ast.ObjectKey , error ) {
109+ tkn , err := tokenize (in , expr )
106110 if err != nil {
107111 return nil , nil , err
108112 }
@@ -328,7 +332,7 @@ func encodeStruct(in reflect.Value) (ast.Node, []*ast.ObjectKey, error) {
328332
329333// tokenize converts a primitive type into an token.Token. IDENT tokens (unquoted strings)
330334// can be optionally triggered for any string types.
331- func tokenize (in reflect.Value , ident bool ) (t token.Token , err error ) {
335+ func tokenize (in reflect.Value , unquotedString bool ) (t token.Token , err error ) {
332336 switch in .Kind () {
333337 case reflect .Bool :
334338 return token.Token {
@@ -355,7 +359,7 @@ func tokenize(in reflect.Value, ident bool) (t token.Token, err error) {
355359 }, nil
356360
357361 case reflect .String :
358- if ident {
362+ if unquotedString {
359363 return token.Token {
360364 Type : token .IDENT ,
361365 Text : in .String (),
@@ -397,6 +401,8 @@ func extractFieldMeta(f reflect.StructField) (meta fieldMeta) {
397401 meta .unusedKeys = true
398402 case Blocks :
399403 meta .repeatBlock = true
404+ case Expression :
405+ meta .expression = true
400406 }
401407 }
402408 }
0 commit comments