-
Notifications
You must be signed in to change notification settings - Fork 62
/
decode.go
231 lines (202 loc) · 5.38 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
package csvutil
import (
"encoding"
"encoding/base64"
"reflect"
"strconv"
)
var (
textUnmarshaler = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
csvUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
)
var intDecoders = map[int]decodeFunc{
8: decodeIntN(8),
16: decodeIntN(16),
32: decodeIntN(32),
64: decodeIntN(64),
}
var uintDecoders = map[int]decodeFunc{
8: decodeUintN(8),
16: decodeUintN(16),
32: decodeUintN(32),
64: decodeUintN(64),
}
var (
decodeFloat32 = decodeFloatN(32)
decodeFloat64 = decodeFloatN(64)
)
type decodeFunc func(s string, v reflect.Value) error
func decodeFuncValue(f func([]byte, any) error) decodeFunc {
return func(s string, v reflect.Value) error {
return f([]byte(s), v.Interface())
}
}
func decodeFuncValuePtr(f func([]byte, any) error) decodeFunc {
return func(s string, v reflect.Value) error {
v = v.Addr()
return f([]byte(s), v.Interface())
}
}
func decodeString(s string, v reflect.Value) error {
v.SetString(s)
return nil
}
func decodeIntN(bits int) decodeFunc {
return func(s string, v reflect.Value) error {
n, err := strconv.ParseInt(s, 10, bits)
if err != nil {
return &UnmarshalTypeError{Value: s, Type: v.Type()}
}
v.SetInt(n)
return nil
}
}
func decodeUintN(bits int) decodeFunc {
return func(s string, v reflect.Value) error {
n, err := strconv.ParseUint(s, 10, bits)
if err != nil {
return &UnmarshalTypeError{Value: s, Type: v.Type()}
}
v.SetUint(n)
return nil
}
}
func decodeFloatN(bits int) decodeFunc {
return func(s string, v reflect.Value) error {
n, err := strconv.ParseFloat(s, bits)
if err != nil {
return &UnmarshalTypeError{Value: s, Type: v.Type()}
}
v.SetFloat(n)
return nil
}
}
func decodeBool(s string, v reflect.Value) error {
b, err := strconv.ParseBool(s)
if err != nil {
return &UnmarshalTypeError{Value: s, Type: v.Type()}
}
v.SetBool(b)
return nil
}
func decodePtrTextUnmarshaler(s string, v reflect.Value) error {
return decodeTextUnmarshaler(s, v.Addr())
}
func decodeTextUnmarshaler(s string, v reflect.Value) error {
return v.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(s))
}
func decodePtrFieldUnmarshaler(s string, v reflect.Value) error {
return decodeFieldUnmarshaler(s, v.Addr())
}
func decodeFieldUnmarshaler(s string, v reflect.Value) error {
return v.Interface().(Unmarshaler).UnmarshalCSV([]byte(s))
}
func decodePtr(typ reflect.Type, funcMap map[reflect.Type]func([]byte, any) error, ifaceFuncs []ifaceDecodeFunc) (decodeFunc, error) {
next, err := decodeFn(typ.Elem(), funcMap, ifaceFuncs)
if err != nil {
return nil, err
}
return func(s string, v reflect.Value) error {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
return next(s, v.Elem())
}, nil
}
func decodeInterface(funcMap map[reflect.Type]func([]byte, any) error, ifaceFuncs []ifaceDecodeFunc) decodeFunc {
return func(s string, v reflect.Value) error {
if v.NumMethod() != 0 {
return &UnmarshalTypeError{
Value: s,
Type: v.Type(),
}
}
if v.IsNil() {
v.Set(reflect.ValueOf(s))
return nil
}
el := walkValue(v)
if !el.CanSet() {
if el.IsValid() {
// we may get a value receiver unmarshalers or registered funcs
// underneath the interface in which case we should call
// Unmarshal/Registered func.
typ := el.Type()
if f, ok := funcMap[typ]; ok {
return decodeFuncValue(f)(s, el)
}
for _, f := range ifaceFuncs {
if typ.AssignableTo(f.argType) {
return decodeFuncValue(f.f)(s, el)
}
}
if typ.Implements(csvUnmarshaler) {
return decodeFieldUnmarshaler(s, el)
}
if typ.Implements(textUnmarshaler) {
return decodeTextUnmarshaler(s, el)
}
}
v.Set(reflect.ValueOf(s))
return nil
}
fn, err := decodeFn(el.Type(), funcMap, ifaceFuncs)
if err != nil {
return err
}
return fn(s, el)
}
}
func decodeBytes(s string, v reflect.Value) error {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
v.SetBytes(b)
return nil
}
func decodeFn(typ reflect.Type, funcMap map[reflect.Type]func([]byte, any) error, ifaceFuncs []ifaceDecodeFunc) (decodeFunc, error) {
if f, ok := funcMap[typ]; ok {
return decodeFuncValue(f), nil
}
if f, ok := funcMap[reflect.PtrTo(typ)]; ok {
return decodeFuncValuePtr(f), nil
}
for _, f := range ifaceFuncs {
if typ.AssignableTo(f.argType) {
return decodeFuncValue(f.f), nil
}
if reflect.PtrTo(typ).AssignableTo(f.argType) {
return decodeFuncValuePtr(f.f), nil
}
}
if reflect.PtrTo(typ).Implements(csvUnmarshaler) {
return decodePtrFieldUnmarshaler, nil
}
if reflect.PtrTo(typ).Implements(textUnmarshaler) {
return decodePtrTextUnmarshaler, nil
}
switch typ.Kind() {
case reflect.Ptr:
return decodePtr(typ, funcMap, ifaceFuncs)
case reflect.Interface:
return decodeInterface(funcMap, ifaceFuncs), nil
case reflect.String:
return decodeString, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intDecoders[typ.Bits()], nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return uintDecoders[typ.Bits()], nil
case reflect.Float32:
return decodeFloat32, nil
case reflect.Float64:
return decodeFloat64, nil
case reflect.Bool:
return decodeBool, nil
case reflect.Slice:
if typ.Elem().Kind() == reflect.Uint8 {
return decodeBytes, nil
}
}
return nil, &UnsupportedTypeError{Type: typ}
}