-
Notifications
You must be signed in to change notification settings - Fork 63
/
encode.go
247 lines (209 loc) · 5.94 KB
/
encode.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
package csvutil
import (
"encoding"
"encoding/base64"
"reflect"
"strconv"
)
var (
textMarshaler = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
csvMarshaler = reflect.TypeOf((*Marshaler)(nil)).Elem()
)
var (
encodeFloat32 = encodeFloatN(32)
encodeFloat64 = encodeFloatN(64)
)
type encodeFunc func(buf []byte, v reflect.Value, omitempty bool) ([]byte, error)
func nopEncode(buf []byte, _ reflect.Value, _ bool) ([]byte, error) {
return buf, nil
}
func encodeFuncValue(fn marshalFunc) encodeFunc {
return func(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
b, err := fn.f(v.Interface())
if err != nil {
return nil, err
}
return append(buf, b...), nil
}
}
func encodeFuncValuePtr(fn marshalFunc) encodeFunc {
return func(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
if !v.CanAddr() {
fallback, err := encodeFn(v.Type(), false, nil, nil)
if err != nil {
return nil, err
}
return fallback(buf, v, omitempty)
}
b, err := fn.f(v.Addr().Interface())
if err != nil {
return nil, err
}
return append(buf, b...), nil
}
}
func encodeString(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
return append(buf, v.String()...), nil
}
func encodeInt(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
n := v.Int()
if n == 0 && omitempty {
return buf, nil
}
return strconv.AppendInt(buf, n, 10), nil
}
func encodeUint(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
n := v.Uint()
if n == 0 && omitempty {
return buf, nil
}
return strconv.AppendUint(buf, n, 10), nil
}
func encodeFloatN(bits int) encodeFunc {
return func(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
f := v.Float()
if f == 0 && omitempty {
return buf, nil
}
return strconv.AppendFloat(buf, f, 'G', -1, bits), nil
}
}
func encodeBool(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
t := v.Bool()
if !t && omitempty {
return buf, nil
}
return strconv.AppendBool(buf, t), nil
}
func encodeInterface(funcMap map[reflect.Type]marshalFunc, funcs []marshalFunc) encodeFunc {
return func(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
if !v.IsValid() || v.IsNil() || !v.Elem().IsValid() {
return buf, nil
}
v = v.Elem()
canAddr := v.Kind() == reflect.Ptr
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
return buf, nil
}
default:
}
enc, err := encodeFn(v.Type(), canAddr, funcMap, funcs)
if err != nil {
return nil, err
}
return enc(buf, v, omitempty)
}
}
func encodePtrMarshaler(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
if v.CanAddr() {
return encodeMarshaler(buf, v.Addr(), omitempty)
}
fallback, err := encodeFn(v.Type(), false, nil, nil)
if err != nil {
return nil, err
}
return fallback(buf, v, omitempty)
}
func encodeTextMarshaler(buf []byte, v reflect.Value, _ bool) ([]byte, error) {
if v.Kind() == reflect.Ptr && v.IsNil() {
return buf, nil
}
b, err := v.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil {
return nil, &MarshalerError{Type: v.Type(), MarshalerType: "MarshalText", Err: err}
}
return append(buf, b...), nil
}
func encodePtrTextMarshaler(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
if v.CanAddr() {
return encodeTextMarshaler(buf, v.Addr(), omitempty)
}
fallback, err := encodeFn(v.Type(), false, nil, nil)
if err != nil {
return nil, err
}
return fallback(buf, v, omitempty)
}
func encodeMarshaler(buf []byte, v reflect.Value, _ bool) ([]byte, error) {
if v.Kind() == reflect.Ptr && v.IsNil() {
return buf, nil
}
b, err := v.Interface().(Marshaler).MarshalCSV()
if err != nil {
return nil, &MarshalerError{Type: v.Type(), MarshalerType: "MarshalCSV", Err: err}
}
return append(buf, b...), nil
}
func encodePtr(typ reflect.Type, canAddr bool, funcMap map[reflect.Type]marshalFunc, funcs []marshalFunc) (encodeFunc, error) {
next, err := encodeFn(typ.Elem(), canAddr, funcMap, funcs)
if err != nil {
return nil, err
}
return func(buf []byte, v reflect.Value, omitempty bool) ([]byte, error) {
if v.IsNil() {
return buf, nil
}
return next(buf, v.Elem(), omitempty)
}, nil
}
func encodeBytes(buf []byte, v reflect.Value, _ bool) ([]byte, error) {
data := v.Bytes()
l := len(buf)
buf = append(buf, make([]byte, base64.StdEncoding.EncodedLen(len(data)))...)
base64.StdEncoding.Encode(buf[l:], data)
return buf, nil
}
func encodeFn(typ reflect.Type, canAddr bool, funcMap map[reflect.Type]marshalFunc, funcs []marshalFunc) (encodeFunc, error) {
if v, ok := funcMap[typ]; ok {
return encodeFuncValue(v), nil
}
if v, ok := funcMap[reflect.PtrTo(typ)]; ok && canAddr {
return encodeFuncValuePtr(v), nil
}
for _, v := range funcs {
argType := v.argType
if typ.AssignableTo(argType) {
return encodeFuncValue(v), nil
}
if canAddr && reflect.PtrTo(typ).AssignableTo(argType) {
return encodeFuncValuePtr(v), nil
}
}
if typ.Implements(csvMarshaler) {
return encodeMarshaler, nil
}
if canAddr && reflect.PtrTo(typ).Implements(csvMarshaler) {
return encodePtrMarshaler, nil
}
if typ.Implements(textMarshaler) {
return encodeTextMarshaler, nil
}
if canAddr && reflect.PtrTo(typ).Implements(textMarshaler) {
return encodePtrTextMarshaler, nil
}
switch typ.Kind() {
case reflect.String:
return encodeString, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return encodeInt, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return encodeUint, nil
case reflect.Float32:
return encodeFloat32, nil
case reflect.Float64:
return encodeFloat64, nil
case reflect.Bool:
return encodeBool, nil
case reflect.Interface:
return encodeInterface(funcMap, funcs), nil
case reflect.Ptr:
return encodePtr(typ, canAddr, funcMap, funcs)
case reflect.Slice:
if typ.Elem().Kind() == reflect.Uint8 {
return encodeBytes, nil
}
}
return nil, &UnsupportedTypeError{Type: typ}
}