@@ -103,7 +103,42 @@ func encodeTomlString(value string) string {
103103 return b .String ()
104104}
105105
106- func tomlValueStringRepresentation (v interface {}, commented string , indent string , arraysOneElementPerLine bool ) (string , error ) {
106+ func tomlTreeStringRepresentation (t * Tree , ord marshalOrder ) (string , error ) {
107+ var orderedVals []sortNode
108+
109+ switch ord {
110+ case OrderPreserve :
111+ orderedVals = sortByLines (t )
112+ default :
113+ orderedVals = sortAlphabetical (t )
114+ }
115+
116+ stringBuffer := bytes.Buffer {}
117+ stringBuffer .WriteString (`{` )
118+ first := true
119+ for i := range orderedVals {
120+ v := t .values [orderedVals [i ].key ]
121+ quotedKey := quoteKeyIfNeeded (orderedVals [i ].key )
122+ valueStr , err := tomlValueStringRepresentation (v , "" , "" , ord , false )
123+ if err != nil {
124+ return "" , err
125+ }
126+ if first {
127+ first = false
128+ } else {
129+ stringBuffer .WriteString (`,` )
130+ }
131+
132+ stringBuffer .WriteString (quotedKey )
133+ stringBuffer .WriteString (" = " )
134+ stringBuffer .WriteString (valueStr )
135+ }
136+ stringBuffer .WriteString (`}` )
137+
138+ return stringBuffer .String (), nil
139+ }
140+
141+ func tomlValueStringRepresentation (v interface {}, commented string , indent string , ord marshalOrder , arraysOneElementPerLine bool ) (string , error ) {
107142 // this interface check is added to dereference the change made in the writeTo function.
108143 // That change was made to allow this function to see formatting options.
109144 tv , ok := v .(* tomlValue )
@@ -140,7 +175,7 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
140175 return "\" " + encodeTomlString (value ) + "\" " , nil
141176 case []byte :
142177 b , _ := v .([]byte )
143- return tomlValueStringRepresentation (string (b ), commented , indent , arraysOneElementPerLine )
178+ return tomlValueStringRepresentation (string (b ), commented , indent , ord , arraysOneElementPerLine )
144179 case bool :
145180 if value {
146181 return "true" , nil
@@ -154,6 +189,8 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
154189 return value .String (), nil
155190 case LocalTime :
156191 return value .String (), nil
192+ case * Tree :
193+ return tomlTreeStringRepresentation (value , ord )
157194 case nil :
158195 return "" , nil
159196 }
@@ -164,7 +201,7 @@ func tomlValueStringRepresentation(v interface{}, commented string, indent strin
164201 var values []string
165202 for i := 0 ; i < rv .Len (); i ++ {
166203 item := rv .Index (i ).Interface ()
167- itemRepr , err := tomlValueStringRepresentation (item , commented , indent , arraysOneElementPerLine )
204+ itemRepr , err := tomlValueStringRepresentation (item , commented , indent , ord , arraysOneElementPerLine )
168205 if err != nil {
169206 return "" , err
170207 }
@@ -368,7 +405,7 @@ func (t *Tree) writeToOrdered(w io.Writer, indent, keyspace string, bytesCount i
368405 if parentCommented || t .commented || v .commented {
369406 commented = "# "
370407 }
371- repr , err := tomlValueStringRepresentation (v , commented , indent , arraysOneElementPerLine )
408+ repr , err := tomlValueStringRepresentation (v , commented , indent , ord , arraysOneElementPerLine )
372409 if err != nil {
373410 return bytesCount , err
374411 }
0 commit comments