/
decoder.go
538 lines (449 loc) · 13.4 KB
/
decoder.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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config
import (
"bytes"
"encoding"
"fmt"
"math"
"reflect"
"strconv"
"github.com/go-validator/validator"
"github.com/pkg/errors"
)
type fieldInfo struct {
FieldName string
DefaultValue string
Required bool
}
func getFieldInfo(field reflect.StructField) fieldInfo {
return fieldInfo{
FieldName: field.Tag.Get("yaml"),
DefaultValue: field.Tag.Get("default"),
}
}
func derefType(t reflect.Type) reflect.Type {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}
func convertSignedInts(src interface{}, dst *reflect.Value) error {
switch t := src.(type) {
case int, uint, int8, uint8, int16, uint16, int32, uint32, int64:
i, err := strconv.ParseInt(fmt.Sprint(t), 10, 64)
if err != nil {
return err
}
if !dst.OverflowInt(i) {
dst.SetInt(i)
return nil
}
case uint64:
if t <= math.MaxInt64 {
dst.SetInt(int64(t))
return nil
}
case uintptr:
if t <= math.MaxInt64 && !dst.OverflowInt(int64(t)) {
dst.SetInt(int64(t))
return nil
}
case float32:
if t >= math.MinInt64 && t <= math.MaxInt64 && !dst.OverflowInt(int64(t)) {
dst.SetInt(int64(t))
return nil
}
case float64:
if t >= math.MinInt64 && t <= math.MaxInt64 && !dst.OverflowInt(int64(t)) {
dst.SetInt(int64(t))
return nil
}
case string:
i, err := strconv.ParseInt(t, 10, 64)
if err != nil {
return err
}
if !dst.OverflowInt(i) {
dst.SetInt(i)
return nil
}
}
return fmt.Errorf("can't convert %q to integer type %q", fmt.Sprint(src), dst.Type())
}
func convertUnsignedInts(src interface{}, dst *reflect.Value) error {
switch t := src.(type) {
case int, uint, int8, uint8, int16, uint16, int32, uint32, int64:
i, err := strconv.ParseInt(fmt.Sprint(t), 10, 64)
if err != nil {
return err
}
if i >= 0 && !dst.OverflowUint(uint64(i)) {
dst.SetUint(uint64(i))
return nil
}
case uint64:
if !dst.OverflowUint(t) {
dst.SetUint(t)
return nil
}
case uintptr:
if t <= math.MaxUint64 && !dst.OverflowUint(uint64(t)) {
dst.SetUint(uint64(t))
return nil
}
case float32:
if t >= 0 && t <= math.MaxUint64 && !dst.OverflowUint(uint64(t)) {
dst.SetUint(uint64(t))
return nil
}
case float64:
if t >= 0 && t <= math.MaxUint64 && !dst.OverflowUint(uint64(t)) {
dst.SetUint(uint64(t))
return nil
}
case string:
i, err := strconv.ParseUint(t, 10, 64)
if err != nil {
return err
}
if !dst.OverflowUint(i) {
dst.SetUint(i)
return nil
}
}
return fmt.Errorf("can't convert %q to unsigned integer type %q", fmt.Sprint(src), dst.Type())
}
func convertFloats(src interface{}, dst *reflect.Value) error {
switch t := src.(type) {
case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, uintptr, float32:
f, err := strconv.ParseFloat(fmt.Sprint(t), 64)
dst.SetFloat(f)
return err
case float64:
v := float64(t)
if !dst.OverflowFloat(v) {
dst.SetFloat(v)
return nil
}
case string:
f, err := strconv.ParseFloat(t, 64)
if err != nil {
return err
}
if !dst.OverflowFloat(f) {
dst.SetFloat(f)
return nil
}
}
return fmt.Errorf("can't convert %q to float type %q", fmt.Sprint(src), dst.Type())
}
func convertValueFromStruct(src interface{}, dst *reflect.Value) error {
// The fieldType is probably a custom type here. We will try and set the fieldValue by
// the custom type
switch dst.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return convertSignedInts(src, dst)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return convertUnsignedInts(src, dst)
case reflect.Float32, reflect.Float64:
return convertFloats(src, dst)
case reflect.Bool:
if v, err := strconv.ParseBool(fmt.Sprint(src)); err == nil {
dst.SetBool(v)
}
case reflect.String:
dst.SetString(fmt.Sprint(src))
default:
return fmt.Errorf("can't convert %q to %q", fmt.Sprint(src), dst.Type())
}
return nil
}
func addSeparator(key string) string {
if key != "" {
key += _separator
}
return key
}
func errorWithKey(err error, key string) error {
return errors.Wrap(err, fmt.Sprintf("for key %q", key))
}
type decoder struct {
*Value
m map[interface{}]struct{}
}
func (d *decoder) getGlobalProvider() Provider {
if d.root == nil {
return d.provider
}
return d.root
}
// Sets value to a primitive type.
func (d *decoder) scalar(childKey string, value reflect.Value, def string) error {
global := d.getGlobalProvider()
var val interface{}
// For primitive values, just get the value and set it into the field
if v2 := global.Get(childKey); v2.HasValue() {
val = v2.Value()
} else if def != "" {
val = def
}
if val != nil {
// First try to convert primitive type values, if convertValue wasn't able
// to convert to primitive,try converting the value as a struct value
if ret, err := convertValue(val, value.Type()); ret != nil {
if err != nil {
return errorWithKey(err, childKey)
}
value.Set(reflect.ValueOf(ret))
} else {
return errorWithKey(convertValueFromStruct(val, &value), childKey)
}
}
return nil
}
// Set value for a sequence type
// TODO(alsam) We stop populating sequence on a first nil value. Can we do better?
func (d *decoder) sequence(childKey string, value reflect.Value) error {
valueType := value.Type()
global := d.getGlobalProvider()
destSlice := reflect.MakeSlice(valueType, 0, 4)
// start looking for child values.
elementType := derefType(valueType).Elem()
childKey = addSeparator(childKey)
for ai := 0; ; ai++ {
arrayKey := childKey + strconv.Itoa(ai)
// Iterate until we find first missing value.
if v2 := global.Get(arrayKey); v2.HasValue() {
val := reflect.New(elementType).Elem()
// Unmarshal current element.
if err := d.unmarshal(arrayKey, val, ""); err != nil {
return err
}
// Append element to the slice
if destSlice.Len() <= ai {
destSlice = reflect.Append(destSlice, reflect.Zero(elementType))
}
destSlice.Index(ai).Set(val)
} else {
break
}
}
if destSlice.Len() > 0 {
value.Set(destSlice)
}
return nil
}
// Set value for the array type
func (d *decoder) array(childKey string, value reflect.Value) error {
valueType := value.Type()
global := d.getGlobalProvider()
// start looking for child values.
elementType := derefType(valueType).Elem()
childKey = addSeparator(childKey)
for ai := 0; ai < value.Len(); ai++ {
arrayKey := childKey + strconv.Itoa(ai)
// Iterate until we find first missing value.
if v2 := global.Get(arrayKey); v2.HasValue() {
val := reflect.New(elementType).Elem()
// Unmarshal current element.
if err := d.unmarshal(arrayKey, val, ""); err != nil {
return err
}
value.Index(ai).Set(val)
} else if value.Index(ai).Kind() == reflect.Struct {
if err := d.valueStruct(arrayKey, value.Index(ai).Addr().Interface()); err != nil {
return err
}
}
}
return nil
}
// Sets value to a map type.
func (d *decoder) mapping(childKey string, value reflect.Value, def string) error {
valueType := value.Type()
global := d.getGlobalProvider()
v := global.Get(childKey)
if !v.HasValue() || v.Value() == nil {
return nil
}
val := v.Value()
destMap := reflect.ValueOf(reflect.MakeMap(valueType).Interface())
// child yamlNode parsed from yaml file is of type map[interface{}]interface{}
// type casting here makes sure that we are iterating over a parsed map.
if v, ok := val.(map[interface{}]interface{}); ok {
childKey = addSeparator(childKey)
for key := range v {
subKey := fmt.Sprintf("%v", key)
if subKey == "" {
// We can confuse an empty map key with a root element.
return errorWithKey(errors.New("empty map key is ambigious"), childKey)
}
itemValue := reflect.New(valueType.Elem()).Elem()
// Try to unmarshal value and save it in the map.
if err := d.unmarshal(childKey+subKey, itemValue, def); err != nil {
return err
}
destMap.SetMapIndex(reflect.ValueOf(key), itemValue)
}
value.Set(destMap)
}
return nil
}
// Sets value to an interface type.
func (d *decoder) iface(key string, value reflect.Value, def string) error {
v := d.getGlobalProvider().Get(key)
if !v.HasValue() || v.Value() == nil {
return nil
}
src := reflect.ValueOf(v.Value())
if src.Type().Implements(value.Type()) {
value.Set(src)
return nil
}
return errorWithKey(fmt.Errorf("%q doesn't implement %q", src.Type(), value.Type()), key)
}
// Sets value to an object type.
func (d *decoder) object(childKey string, value reflect.Value) error {
value = value.Addr()
if value.IsNil() {
tmp := reflect.New(value.Type().Elem())
value.Set(tmp)
}
return d.valueStruct(childKey, value.Interface())
}
// Walk through the struct and start asking the providers for values at each key.
//
// - for individual values, we terminate
// - for array values, we start asking for indexes
// - for object values, we recurse.
func (d *decoder) valueStruct(key string, target interface{}) error {
tarGet := reflect.Indirect(reflect.ValueOf(target))
targetType := tarGet.Type()
for i := 0; i < targetType.NumField(); i++ {
field := targetType.Field(i)
// Check for the private field
if field.PkgPath != "" && !field.Anonymous {
continue
}
fieldName := field.Name
fieldInfo := getFieldInfo(field)
if fieldInfo.FieldName != "" {
fieldName = fieldInfo.FieldName
}
if key != "" {
fieldName = key + _separator + fieldName
}
fieldValue := tarGet.Field(i)
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
fieldValue.Set(reflect.New(fieldValue.Type()).Elem())
}
if err := d.unmarshal(fieldName, fieldValue, getFieldInfo(field).DefaultValue); err != nil {
return err
}
}
return errorWithKey(validator.Validate(target), key)
}
// If there is no value with name - leave it nil, otherwise allocate memory and set the value.
func (d *decoder) pointer(name string, value reflect.Value, def string) error {
if !d.getGlobalProvider().Get(name).HasValue() {
return nil
}
if value.IsNil() {
value.Set(reflect.New(value.Type().Elem()))
}
return d.unmarshal(name, value.Elem(), def)
}
// Sets value to a channel type.
func (d *decoder) channel(key string, value reflect.Value, def string) error {
return d.textUnmarshaller(key, value, def)
}
// Sets value to a function type.
func (d *decoder) function(key string, value reflect.Value, def string) error {
return d.textUnmarshaller(key, value, def)
}
func (d *decoder) textUnmarshaller(key string, value reflect.Value, str string) error {
v := d.getGlobalProvider().Get(key)
if v.HasValue() {
str = v.String()
} else if str == "" {
return nil
}
if value.IsNil() {
value.Set(reflect.New(value.Type()).Elem())
}
// Value has to have a pointer receiver to be able to modify itself with TextUnmarshaller
if !value.CanAddr() {
return errorWithKey(errors.New("can't use TextUnmarshaller because value is not addressable"), key)
}
switch t := value.Addr().Interface().(type) {
case encoding.TextUnmarshaler:
return errorWithKey(t.UnmarshalText([]byte(str)), key)
}
return nil
}
// Check if a value is a pointer and decoder set it before.
// TODO(alsam) print only elements in the loop, not all elements.
func (d *decoder) checkCycles(value reflect.Value) error {
if value.Type().Comparable() {
val := value.Interface()
kind := value.Kind()
if _, ok := d.m[val]; ok {
if kind == reflect.Ptr && !value.IsNil() {
buf := &bytes.Buffer{}
for k := range d.m {
fmt.Fprintf(buf, "%+v -> ", k)
}
fmt.Fprintf(buf, "%+v", value.Interface())
return fmt.Errorf("cycles detected: %s", buf.String())
}
}
d.m[val] = struct{}{}
}
return nil
}
// Dispatch un-marshalling functions based on the value type.
func (d *decoder) unmarshal(name string, value reflect.Value, def string) error {
if err := d.checkCycles(value); err != nil {
return errorWithKey(err, name)
}
switch value.Kind() {
case reflect.Invalid:
return fmt.Errorf("invalid value type for key %s", name)
case reflect.Ptr:
return d.pointer(name, value, def)
case reflect.Struct:
return d.object(name, value)
case reflect.Array:
return d.array(name, value)
case reflect.Slice:
return d.sequence(name, value)
case reflect.Map:
return d.mapping(name, value, def)
case reflect.Interface:
return d.iface(name, value, def)
case reflect.Chan:
return d.channel(name, value, def)
case reflect.Func:
return d.function(name, value, def)
default:
return d.scalar(name, value, def)
}
}