/
decode.go
147 lines (128 loc) · 3.26 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
package csvutil
import (
"encoding"
"encoding/base64"
"reflect"
"strconv"
)
var (
textUnmarshaler = reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
csvUnmarshaler = reflect.TypeOf(new(Unmarshaler)).Elem()
)
type decodeFunc func(s string, v reflect.Value) error
func decodeString(s string, v reflect.Value) error {
v.SetString(s)
return nil
}
func decodeInt(t reflect.Type) decodeFunc {
bits := t.Bits()
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 decodeUint(t reflect.Type) decodeFunc {
bits := t.Bits()
return func(s string, v reflect.Value) error {
n, err := strconv.ParseUint(s, 10, bits)
if err != nil {
return &UnmarshalTypeError{Value: s, Type: t}
}
v.SetUint(n)
return nil
}
}
func decodeFloat(t reflect.Type) decodeFunc {
bits := t.Bits()
return func(s string, v reflect.Value) error {
n, err := strconv.ParseFloat(s, bits)
if err != nil {
return &UnmarshalTypeError{Value: s, Type: t}
}
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) (decodeFunc, error) {
next, err := decodeFn(typ.Elem())
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(s string, v reflect.Value) error {
if v.NumMethod() == 0 {
v.Set(reflect.ValueOf(s))
return nil
}
return &UnmarshalTypeError{
Value: s,
Type: v.Type(),
}
}
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) (decodeFunc, error) {
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)
case reflect.Interface:
return decodeInterface, nil
case reflect.String:
return decodeString, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return decodeInt(typ), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return decodeUint(typ), nil
case reflect.Float32, reflect.Float64:
return decodeFloat(typ), nil
case reflect.Bool:
return decodeBool, nil
case reflect.Slice:
if typ.Elem().Kind() == reflect.Uint8 {
return decodeBytes, nil
}
}
return nil, &UnsupportedTypeError{Type: typ}
}