@@ -44,7 +44,7 @@ func encodeTomlString(value string) string {
44
44
return b .String ()
45
45
}
46
46
47
- func tomlValueStringRepresentation (v interface {}) (string , error ) {
47
+ func tomlValueStringRepresentation (v interface {}, indent string , arraysOneElementPerLine bool ) (string , error ) {
48
48
switch value := v .(type ) {
49
49
case uint64 :
50
50
return strconv .FormatUint (value , 10 ), nil
@@ -61,7 +61,7 @@ func tomlValueStringRepresentation(v interface{}) (string, error) {
61
61
return "\" " + encodeTomlString (value ) + "\" " , nil
62
62
case []byte :
63
63
b , _ := v .([]byte )
64
- return tomlValueStringRepresentation (string (b ))
64
+ return tomlValueStringRepresentation (string (b ), indent , arraysOneElementPerLine )
65
65
case bool :
66
66
if value {
67
67
return "true" , nil
@@ -76,21 +76,40 @@ func tomlValueStringRepresentation(v interface{}) (string, error) {
76
76
rv := reflect .ValueOf (v )
77
77
78
78
if rv .Kind () == reflect .Slice {
79
- values := []string {}
79
+ var values []string
80
80
for i := 0 ; i < rv .Len (); i ++ {
81
81
item := rv .Index (i ).Interface ()
82
- itemRepr , err := tomlValueStringRepresentation (item )
82
+ itemRepr , err := tomlValueStringRepresentation (item , indent , arraysOneElementPerLine )
83
83
if err != nil {
84
84
return "" , err
85
85
}
86
86
values = append (values , itemRepr )
87
87
}
88
+ if arraysOneElementPerLine && len (values ) > 1 {
89
+ stringBuffer := bytes.Buffer {}
90
+ valueIndent := indent + ` ` // TODO: move that to a shared encoder state
91
+
92
+ stringBuffer .WriteString ("[\n " )
93
+
94
+ for i , value := range values {
95
+ stringBuffer .WriteString (valueIndent )
96
+ stringBuffer .WriteString (value )
97
+ if i != len (values ) - 1 {
98
+ stringBuffer .WriteString (`,` )
99
+ }
100
+ stringBuffer .WriteString ("\n " )
101
+ }
102
+
103
+ stringBuffer .WriteString (indent + "]" )
104
+
105
+ return stringBuffer .String (), nil
106
+ }
88
107
return "[" + strings .Join (values , "," ) + "]" , nil
89
108
}
90
109
return "" , fmt .Errorf ("unsupported value type %T: %v" , v , v )
91
110
}
92
111
93
- func (t * Tree ) writeTo (w io.Writer , indent , keyspace string , bytesCount int64 ) (int64 , error ) {
112
+ func (t * Tree ) writeTo (w io.Writer , indent , keyspace string , bytesCount int64 , arraysOneElementPerLine bool ) (int64 , error ) {
94
113
simpleValuesKeys := make ([]string , 0 )
95
114
complexValuesKeys := make ([]string , 0 )
96
115
@@ -113,7 +132,7 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
113
132
return bytesCount , fmt .Errorf ("invalid value type at %s: %T" , k , t .values [k ])
114
133
}
115
134
116
- repr , err := tomlValueStringRepresentation (v .value )
135
+ repr , err := tomlValueStringRepresentation (v .value , indent , arraysOneElementPerLine )
117
136
if err != nil {
118
137
return bytesCount , err
119
138
}
@@ -178,7 +197,7 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
178
197
if err != nil {
179
198
return bytesCount , err
180
199
}
181
- bytesCount , err = node .writeTo (w , indent + " " , combinedKey , bytesCount )
200
+ bytesCount , err = node .writeTo (w , indent + " " , combinedKey , bytesCount , arraysOneElementPerLine )
182
201
if err != nil {
183
202
return bytesCount , err
184
203
}
@@ -190,7 +209,7 @@ func (t *Tree) writeTo(w io.Writer, indent, keyspace string, bytesCount int64) (
190
209
return bytesCount , err
191
210
}
192
211
193
- bytesCount , err = subTree .writeTo (w , indent + " " , combinedKey , bytesCount )
212
+ bytesCount , err = subTree .writeTo (w , indent + " " , combinedKey , bytesCount , arraysOneElementPerLine )
194
213
if err != nil {
195
214
return bytesCount , err
196
215
}
@@ -216,7 +235,7 @@ func writeStrings(w io.Writer, s ...string) (int, error) {
216
235
// WriteTo encode the Tree as Toml and writes it to the writer w.
217
236
// Returns the number of bytes written in case of success, or an error if anything happened.
218
237
func (t * Tree ) WriteTo (w io.Writer ) (int64 , error ) {
219
- return t .writeTo (w , "" , "" , 0 )
238
+ return t .writeTo (w , "" , "" , 0 , false )
220
239
}
221
240
222
241
// ToTomlString generates a human-readable representation of the current tree.
0 commit comments