-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
105 lines (92 loc) · 2.13 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
package envconf
import (
"errors"
"reflect"
"strconv"
"strings"
)
var (
errNoSettable = errors.New("is not settable")
errInvalidValue = errors.New("has invalid type")
)
type decoder struct {
separator string
}
func (d *decoder) decode(v reflect.Value, s string) error {
if !v.CanSet() {
return errNoSettable
}
v = d.indirect(v)
switch v.Kind() {
case reflect.Bool:
return d.decodeBool(v, s)
case reflect.String:
return d.decodeString(v, s)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return d.decodeUint(v, s)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return d.decodeInt(v, s)
case reflect.Float32, reflect.Float64:
return d.decodeFloat(v, s)
case reflect.Array, reflect.Slice:
return d.decodeArray(v, s)
default:
return errInvalidValue
}
}
// indirect walks down v allocating pointers as needed, until it gets to a non-pointer.
func (d *decoder) indirect(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}
func (d *decoder) decodeArray(v reflect.Value, s string) error {
t := strings.Split(s, d.separator)
v.Set(reflect.MakeSlice(v.Type(), len(t), len(t)))
for i := 0; i < len(t); i++ {
if err := d.decode(v.Index(i), t[i]); err != nil {
return err
}
}
return nil
}
func (d *decoder) decodeString(v reflect.Value, s string) error {
v.SetString(s)
return nil
}
func (d *decoder) decodeBool(v reflect.Value, s string) error {
b, err := strconv.ParseBool(s)
if err != nil {
return err
}
v.SetBool(b)
return nil
}
func (d *decoder) decodeInt(v reflect.Value, s string) error {
i, err := strconv.ParseInt(s, 0, v.Type().Bits())
if err != nil {
return err
}
v.SetInt(i)
return nil
}
func (d *decoder) decodeUint(v reflect.Value, s string) error {
i, err := strconv.ParseUint(s, 0, v.Type().Bits())
if err != nil {
return err
}
v.SetUint(i)
return nil
}
func (d *decoder) decodeFloat(v reflect.Value, s string) error {
f, err := strconv.ParseFloat(s, v.Type().Bits())
if err != nil {
return err
}
v.SetFloat(f)
return nil
}