-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
491 lines (460 loc) · 12 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package toml
import (
"encoding"
"encoding/base64"
"fmt"
"go/ast"
"reflect"
"runtime"
"strconv"
"strings"
"time"
"github.com/kezhuw/toml/internal/types"
)
// An InvalidUnmarshalError describes that an invalid argment was passed
// to Unmarshal. The argument passed to Unmarshal must be non-nil pointer.
type InvalidUnmarshalError struct {
Type reflect.Type
}
func (e *InvalidUnmarshalError) Error() string {
if e.Type == nil {
return "toml: Unmarshal(nil)"
}
if e.Type.Kind() != reflect.Ptr {
return "toml: Unmarshal(non-pointer " + e.Type.String() + ")"
}
return "toml: Unmarshal(nil " + e.Type.String() + ")"
}
// An UnmarshalTypeError describes that a TOML value is not appropriate
// to be stored in specified Go type.
type UnmarshalTypeError struct {
Value string
Type reflect.Type
}
func (e *UnmarshalTypeError) Error() string {
return "toml: cannot unmarshal " + e.Value + " to Go value of type " + e.Type.String()
}
// An UnmarshalOverflowError describes that a TOML number value overflows
// specified Go type.
type UnmarshalOverflowError struct {
Value string
Type reflect.Type
}
func (e *UnmarshalOverflowError) Error() string {
return "toml: " + e.Value + " overflow Go value of type " + e.Type.String()
}
func indirectType(t reflect.Type) reflect.Type {
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}
func indirectValue(v reflect.Value) (encoding.TextUnmarshaler, reflect.Value) {
var u encoding.TextUnmarshaler
for {
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() {
v = e
continue
}
}
if v.Kind() != reflect.Ptr {
break
}
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
if v.NumMethod() > 0 {
// TOML has native Datetime support, while time.Time implements
// encoding.TextUnmarshaler. For native Datetime, we need settable
// time.Time struct, so continue here.
if i, ok := v.Interface().(encoding.TextUnmarshaler); ok {
u = i
}
}
v = v.Elem()
}
return u, v
}
func findField(t *types.Table, field *reflect.StructField, tagname string) (string, types.Value) {
if tagname != "" {
return tagname, t.Elems[tagname]
}
if value, ok := t.Elems[field.Name]; ok {
return field.Name, value
}
lowerName := strings.ToLower(field.Name)
return lowerName, t.Elems[lowerName]
}
func unmarshalBoolean(b bool, v reflect.Value) {
switch v.Kind() {
case reflect.Bool:
v.SetBool(b)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(b))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"boolean " + strconv.FormatBool(b), v.Type()})
}
}
func unmarshalQuoted(s string, v reflect.Value) {
switch v.Kind() {
case reflect.Bool:
b, err := strconv.ParseBool(s)
if err != nil {
panic(err)
}
v.SetBool(b)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
panic(err)
}
if v.OverflowInt(i) {
goto overflowError
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u, err := strconv.ParseUint(s, 10, 64)
if err != nil {
panic(err)
}
if v.OverflowUint(u) {
goto overflowError
}
v.SetUint(u)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(s, 64)
if err != nil {
panic(err)
}
if v.OverflowFloat(f) {
goto overflowError
}
v.SetFloat(f)
default:
panic("toml: unexpected type for quoted string")
}
return
overflowError:
panic(&UnmarshalOverflowError{"string " + strconv.Quote(s), v.Type()})
}
func unmarshalString(s string, v reflect.Value, options tagOptions) {
if v.Kind() != reflect.Ptr && v.CanAddr() {
v = v.Addr()
}
u, v := indirectValue(v)
if u != nil {
err := u.UnmarshalText([]byte(s))
if err != nil {
panic(err)
}
if v.Type().ConvertibleTo(datetimeType) {
t := v.Addr().Convert(reflect.PtrTo(datetimeType)).Interface().(*time.Time)
if t.IsZero() {
*t = time.Time{}
}
}
return
}
switch v.Kind() {
case reflect.String:
if options.Has("string") {
t, err := strconv.Unquote(s)
if err != nil {
panic(err)
}
v.SetString(t)
break
}
v.SetString(s)
case reflect.Slice:
if v.Type().Elem().Kind() != reflect.Uint8 {
goto typeError
}
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
n, err := base64.StdEncoding.Decode(b, []byte(s))
if err != nil {
panic(err)
}
v.SetBytes(b[:n])
case reflect.Bool,
reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if !options.Has("string") {
goto typeError
}
unmarshalQuoted(s, v)
case reflect.Interface:
if v.NumMethod() != 0 {
goto typeError
}
v.Set(reflect.ValueOf(s))
default:
goto typeError
}
return
typeError:
panic(&UnmarshalTypeError{fmt.Sprintf("string: %q", s), v.Type()})
}
func unmarshalDatetime(t time.Time, v reflect.Value) {
if t.IsZero() {
t = time.Time{}
}
if !reflect.TypeOf(t).ConvertibleTo(v.Type()) {
panic(&UnmarshalTypeError{"datetime " + t.Format(time.RFC3339Nano), v.Type()})
}
v.Set(reflect.ValueOf(t).Convert(v.Type()))
}
func unmarshalFloat(f float64, v reflect.Value) {
switch v.Kind() {
case reflect.Float32, reflect.Float64:
if v.OverflowFloat(f) {
panic(&UnmarshalOverflowError{"float " + strconv.FormatFloat(f, 'g', -1, 64), v.Type()})
}
v.SetFloat(f)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(f))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"float " + strconv.FormatFloat(f, 'g', -1, 64), v.Type()})
}
}
func unmarshalInteger(i int64, v reflect.Value) {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.OverflowInt(i) {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if i < 0 {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
u := uint64(i)
if v.OverflowUint(u) {
panic(&UnmarshalOverflowError{"integer " + strconv.FormatUint(u, 10), v.Type()})
}
v.SetUint(u)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(i))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"integer " + strconv.FormatInt(i, 10), v.Type()})
}
}
var emptyInterfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
func unmarshalMap(t *types.Table, v reflect.Value) {
keyType := v.Type().Key()
if keyType.Kind() != reflect.String {
panic(&UnmarshalTypeError{"table", v.Type()})
}
m := reflect.MakeMap(v.Type())
elemType := v.Type().Elem()
elemZero := reflect.Zero(elemType)
elemValue := reflect.New(elemType).Elem()
for key, value := range t.Elems {
elemValue.Set(elemZero)
unmarshalValue(value, elemValue, nil)
m.SetMapIndex(reflect.ValueOf(key).Convert(keyType), elemValue)
}
v.Set(m)
}
func unmarshalStructNested(t *types.Table, v reflect.Value, matchs map[string]struct{}) {
_, v = indirectValue(v)
vType := v.Type()
for i := 0; i < v.NumField(); i++ {
field := vType.Field(i)
name, options := parseTag(field.Tag.Get("toml"))
if name == "-" {
continue
}
// Don't look inside explicitly tagged anonymous struct fields.
if field.Anonymous && name == "" {
fieldValue := v.Field(i)
switch field.Type.Kind() {
case reflect.Struct:
unmarshalStructNested(t, v.Field(i), matchs)
continue
case reflect.Ptr:
if field.Type.Elem().Kind() != reflect.Struct {
break
}
if fieldValue.IsNil() {
fieldNew := reflect.New(field.Type.Elem())
n := len(matchs)
unmarshalStructNested(t, fieldNew.Elem(), matchs)
if n != len(matchs) {
fieldValue.Set(fieldNew)
}
} else {
unmarshalStructNested(t, fieldValue, matchs)
}
continue
}
}
// There is a bug which cause unexported embedded struct fields
// settable in Go 1.5 and prior. See https://github.com/golang/go/issues/12367.
// Use ast.IsExported to circumvent it.
if !ast.IsExported(field.Name) {
continue
}
name, value := findField(t, &field, name)
if value == nil {
if options.Has("omitempty") {
v.Field(i).Set(reflect.Zero(field.Type))
}
continue
}
if _, ok := matchs[name]; ok {
continue
}
unmarshalValue(value, v.Field(i), options)
matchs[name] = struct{}{}
}
}
func unmarshalStruct(t *types.Table, v reflect.Value) {
unmarshalStructNested(t, v, make(map[string]struct{}, len(t.Elems)))
}
func unmarshalTable(t *types.Table, v reflect.Value) {
switch v.Kind() {
case reflect.Map:
unmarshalMap(t, v)
case reflect.Struct:
unmarshalStruct(t, v)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(t.Interface()))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"table", v.Type()})
}
}
func unmarshalSlice(a *types.Array, v reflect.Value) {
n := len(a.Elems)
slice := reflect.MakeSlice(v.Type(), n, n)
for i, value := range a.Elems {
unmarshalValue(value, slice.Index(i), nil)
}
v.Set(slice)
}
func unmarshalGoArray(a *types.Array, v reflect.Value) {
if len(a.Elems) != v.Type().Len() {
panic(&UnmarshalTypeError{fmt.Sprintf("[%d]array", len(a.Elems)), v.Type()})
}
if v.IsNil() {
v.Set(reflect.Zero(v.Type()))
}
for i, value := range a.Elems {
unmarshalValue(value, v.Index(i), nil)
}
}
func unmarshalArray(a *types.Array, v reflect.Value) {
switch v.Kind() {
case reflect.Array:
unmarshalGoArray(a, v)
case reflect.Slice:
unmarshalSlice(a, v)
case reflect.Interface:
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(a.Interface()))
return
}
fallthrough
default:
panic(&UnmarshalTypeError{"array", v.Type()})
}
}
func unmarshalValue(tv types.Value, rv reflect.Value, options tagOptions) {
_, rv = indirectValue(rv)
switch tv := tv.(type) {
case types.Boolean:
unmarshalBoolean(bool(tv), rv)
case types.Float:
unmarshalFloat(float64(tv), rv)
case types.String:
unmarshalString(string(tv), rv, options)
case types.Integer:
unmarshalInteger(int64(tv), rv)
case types.Datetime:
unmarshalDatetime(time.Time(tv), rv)
case *types.Array:
unmarshalArray(tv, rv)
case *types.Table:
unmarshalTable(tv, rv)
}
}
func catchError(errp *error) {
if r := recover(); r != nil {
switch err := r.(type) {
default:
panic(r)
case runtime.Error:
panic(r)
case error:
*errp = err
}
}
}
// Unmarshal parses TOML data and stores the result in the value pointed
// by v.
//
// To unmarshal TOML into a struct, Unmarshal uses TOML tagged name to
// find matching item in TOML table. Field name and its lower case will
// got tried in sequence if TOML tagged name is absent. Options can be
// specified after tag name separated by comma. Examples:
//
// // Field is ignored by this package.
// Field int `toml:"-"`
//
// // "Field" and "field" will be used to find key in TOML table.
// Field int `toml:"myName"`
//
// // "myName" will be used to find matching item in TOML table, and
// // if it is absent, it will be set to zero value.
// Field int `toml:"myName,omitempty"`
//
// // "Field" and "field" will be used to find key in TOML table and
// // this field can be unmarshalled from TOML string.
// Field int `toml:",string"
//
// To unmarshal TOML into an interface value, Unmarshal stores TOML
// value in following types:
//
// bool, for TOML Boolean
// int64, for TOML Integer
// float64, for TOML Float
// string, for TOML String
// time.Time, for TOML Datetime
// []interface{}, for TOML Array
// map[string]interface{}, for TOML Table
//
// There is no guarantee that origin data in Go value will be preserved
// after a failure or success Unmarshal().
func Unmarshal(data []byte, v interface{}) (err error) {
defer catchError(&err)
t, err := parse(data)
if err != nil {
return err
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}
_, rv = indirectValue(rv)
unmarshalTable(t, rv)
return nil
}