-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
200 lines (177 loc) · 4.81 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
package csvlib
import (
"encoding"
"fmt"
"reflect"
"strconv"
)
var (
textUnmarshaler = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
csvUnmarshaler = reflect.TypeOf((*CSVUnmarshaler)(nil)).Elem()
)
func getDecodeFunc(typ reflect.Type) (DecodeFunc, error) {
if typ.Implements(csvUnmarshaler) {
return decodeCSVUnmarshaler, nil
}
if reflect.PointerTo(typ).Implements(csvUnmarshaler) {
return decodePtrCSVUnmarshaler, nil
}
if typ.Implements(textUnmarshaler) {
return decodeTextUnmarshaler, nil
}
if reflect.PointerTo(typ).Implements(textUnmarshaler) {
return decodePtrTextUnmarshaler, nil
}
return getDecodeFuncBaseType(typ)
}
func getDecodeFuncBaseType(typ reflect.Type) (DecodeFunc, error) {
typeIsPtr := false
if typ.Kind() == reflect.Pointer {
typeIsPtr = true
typ = typ.Elem()
}
switch typ.Kind() { // nolint: exhaustive
case reflect.String:
if typeIsPtr {
return decodePtrStr, nil
}
return decodeStr, nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
if typeIsPtr {
return decodePtrIntFunc(typ.Bits()), nil
}
return decodeIntFunc(typ.Bits()), nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
if typeIsPtr {
return decodePtrUintFunc(typ.Bits()), nil
}
return decodeUintFunc(typ.Bits()), nil
case reflect.Bool:
if typeIsPtr {
return decodePtrBool, nil
}
return decodeBool, nil
case reflect.Float32, reflect.Float64:
if typeIsPtr {
return decodePtrFloatFunc(typ.Bits()), nil
}
return decodeFloatFunc(typ.Bits()), nil
case reflect.Interface:
if typeIsPtr {
return decodePtrInterface, nil
}
return decodeInterface, nil
default:
return nil, fmt.Errorf("%w: %v", ErrTypeUnsupported, typ.Kind())
}
}
func decodeTextUnmarshaler(s string, v reflect.Value) error {
initAndIndirectValue(v)
return v.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(s)) // nolint: forcetypeassert
}
func decodePtrTextUnmarshaler(s string, v reflect.Value) error {
if v.CanAddr() {
return decodeTextUnmarshaler(s, v.Addr())
}
// Fallback to process the value dynamically
decodeFn, err := getDecodeFuncBaseType(v.Type())
if err != nil {
return err
}
return decodeFn(s, v)
}
func decodeCSVUnmarshaler(s string, v reflect.Value) error {
initAndIndirectValue(v)
return v.Interface().(CSVUnmarshaler).UnmarshalCSV([]byte(s)) // nolint: forcetypeassert
}
func decodePtrCSVUnmarshaler(s string, v reflect.Value) error {
if v.CanAddr() {
return decodeCSVUnmarshaler(s, v.Addr())
}
// Fallback to process the value dynamically
decodeFn, err := getDecodeFuncBaseType(v.Type())
if err != nil {
return err
}
return decodeFn(s, v)
}
func decodeStr(s string, v reflect.Value) error {
v.SetString(s)
return nil
}
func decodePtrStr(s string, v reflect.Value) error {
return decodeStr(s, initAndIndirectValue(v))
}
func decodeBool(s string, v reflect.Value) error {
b, err := strconv.ParseBool(s)
if err != nil {
return fmt.Errorf("%w: %v (%s)", ErrDecodeValueType, v.Type(), s)
}
v.SetBool(b)
return nil
}
func decodePtrBool(s string, v reflect.Value) error {
return decodeBool(s, initAndIndirectValue(v))
}
func decodeInt(s string, v reflect.Value, bits int) error {
n, err := strconv.ParseInt(s, 10, bits)
if err != nil {
return fmt.Errorf("%w: %v (%s)", ErrDecodeValueType, v.Type(), s)
}
v.SetInt(n)
return nil
}
func decodeIntFunc(bits int) DecodeFunc {
return func(s string, v reflect.Value) error {
return decodeInt(s, v, bits)
}
}
func decodePtrIntFunc(bits int) DecodeFunc {
return func(s string, v reflect.Value) error {
return decodeInt(s, initAndIndirectValue(v), bits)
}
}
func decodeUint(s string, v reflect.Value, bits int) error {
n, err := strconv.ParseUint(s, 10, bits)
if err != nil {
return fmt.Errorf("%w: %v (%s)", ErrDecodeValueType, v.Type(), s)
}
v.SetUint(n)
return nil
}
func decodeUintFunc(bits int) DecodeFunc {
return func(s string, v reflect.Value) error {
return decodeUint(s, v, bits)
}
}
func decodePtrUintFunc(bits int) DecodeFunc {
return func(s string, v reflect.Value) error {
return decodeUint(s, initAndIndirectValue(v), bits)
}
}
func decodeFloat(s string, v reflect.Value, bits int) error {
n, err := strconv.ParseFloat(s, bits)
if err != nil {
return fmt.Errorf("%w: %v (%s)", ErrDecodeValueType, v.Type(), s)
}
v.SetFloat(n)
return nil
}
func decodeFloatFunc(bits int) DecodeFunc {
return func(s string, v reflect.Value) error {
return decodeFloat(s, v, bits)
}
}
func decodePtrFloatFunc(bits int) DecodeFunc {
return func(s string, v reflect.Value) error {
return decodeFloat(s, initAndIndirectValue(v), bits)
}
}
func decodeInterface(s string, v reflect.Value) error {
v.Set(reflect.ValueOf(s))
return nil
}
func decodePtrInterface(s string, v reflect.Value) error {
initAndIndirectValue(v).Set(reflect.ValueOf(s))
return nil
}