-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
239 lines (217 loc) · 5.32 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
package csvlib
import (
"encoding"
"fmt"
"reflect"
"strconv"
)
var (
textMarshaler = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
csvMarshaler = reflect.TypeOf((*CSVMarshaler)(nil)).Elem()
)
func getEncodeFunc(typ reflect.Type) (EncodeFunc, error) {
if typ.Implements(csvMarshaler) {
return encodeCSVMarshaler, nil
}
if reflect.PointerTo(typ).Implements(csvMarshaler) {
return encodePtrCSVMarshaler, nil
}
if typ.Implements(textMarshaler) {
return encodeTextMarshaler, nil
}
if reflect.PointerTo(typ).Implements(textMarshaler) {
return encodePtrTextMarshaler, nil
}
return getEncodeFuncBaseType(typ)
}
func getEncodeFuncBaseType(typ reflect.Type) (EncodeFunc, error) {
typeIsPtr := false
if typ.Kind() == reflect.Pointer {
typeIsPtr = true
typ = typ.Elem()
}
switch typ.Kind() { // nolint: exhaustive
case reflect.String:
if typeIsPtr {
return encodePtrStr, nil
}
return encodeStr, nil
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
if typeIsPtr {
return encodePtrInt, nil
}
return encodeInt, nil
case reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8:
if typeIsPtr {
return encodePtrUint, nil
}
return encodeUint, nil
case reflect.Bool:
if typeIsPtr {
return encodePtrBool, nil
}
return encodeBool, nil
case reflect.Float32, reflect.Float64:
if typeIsPtr {
return encodePtrFloatFunc(typ.Bits()), nil
}
return encodeFloatFunc(typ.Bits()), nil
case reflect.Interface:
if typeIsPtr {
return encodePtrInterface, nil
}
return encodeInterface, nil
default:
return nil, fmt.Errorf("%w: %v", ErrTypeUnsupported, typ.Kind())
}
}
func encodeCSVMarshaler(v reflect.Value, _ bool) (string, error) {
if !v.IsValid() {
return "", nil
}
if v.Kind() == reflect.Pointer && v.IsNil() {
return "", nil
}
b, err := v.Interface().(CSVMarshaler).MarshalCSV()
if err != nil {
return "", fmt.Errorf("%w: %v", ErrEncodeValueType, v.Type())
}
return string(b), nil
}
func encodePtrCSVMarshaler(v reflect.Value, omitempty bool) (string, error) {
if v.CanAddr() {
return encodeCSVMarshaler(v.Addr(), omitempty)
}
// Fallback to process the value dynamically
encodeFn, err := getEncodeFuncBaseType(v.Type())
if err != nil {
return "", err
}
return encodeFn(v, omitempty)
}
func encodeTextMarshaler(v reflect.Value, _ bool) (string, error) {
if !v.IsValid() {
return "", nil
}
if v.Kind() == reflect.Pointer && v.IsNil() {
return "", nil
}
b, err := v.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil {
return "", fmt.Errorf("%w: %v", ErrEncodeValueType, v.Type())
}
return string(b), nil
}
func encodePtrTextMarshaler(v reflect.Value, omitempty bool) (string, error) {
if v.CanAddr() {
return encodeTextMarshaler(v.Addr(), omitempty)
}
// Fallback to process the value dynamically
encodeFn, err := getEncodeFuncBaseType(v.Type())
if err != nil {
return "", err
}
return encodeFn(v, omitempty)
}
func encodeStr(v reflect.Value, _ bool) (string, error) {
return v.String(), nil
}
func encodePtrStr(v reflect.Value, _ bool) (string, error) {
v = v.Elem()
if !v.IsValid() {
return "", nil
}
return v.String(), nil
}
func encodeBool(v reflect.Value, omitempty bool) (string, error) {
t := v.Bool()
if !t && omitempty {
return "", nil
}
return strconv.FormatBool(t), nil
}
func encodePtrBool(v reflect.Value, omitempty bool) (string, error) {
v = v.Elem()
if !v.IsValid() {
return "", nil
}
return encodeBool(v, omitempty)
}
func encodeInt(v reflect.Value, omitempty bool) (string, error) {
n := v.Int()
if n == 0 && omitempty {
return "", nil
}
return strconv.FormatInt(n, 10), nil
}
func encodePtrInt(v reflect.Value, omitempty bool) (string, error) {
v = v.Elem()
if !v.IsValid() {
return "", nil
}
return encodeInt(v, omitempty)
}
func encodeUint(v reflect.Value, omitempty bool) (string, error) {
n := v.Uint()
if n == 0 && omitempty {
return "", nil
}
return strconv.FormatUint(n, 10), nil
}
func encodePtrUint(v reflect.Value, omitempty bool) (string, error) {
v = v.Elem()
if !v.IsValid() {
return "", nil
}
return encodeUint(v, omitempty)
}
func encodeFloat(v reflect.Value, omitempty bool, bits int) (string, error) {
f := v.Float()
if f == 0 && omitempty {
return "", nil
}
return strconv.FormatFloat(f, 'f', -1, bits), nil
}
func encodePtrFloat(v reflect.Value, omitempty bool, bits int) (string, error) {
v = v.Elem()
if !v.IsValid() {
return "", nil
}
return encodeFloat(v, omitempty, bits)
}
func encodeFloatFunc(bits int) EncodeFunc {
return func(v reflect.Value, omitempty bool) (string, error) {
return encodeFloat(v, omitempty, bits)
}
}
func encodePtrFloatFunc(bits int) EncodeFunc {
return func(v reflect.Value, omitempty bool) (string, error) {
return encodePtrFloat(v, omitempty, bits)
}
}
func encodeInterface(v reflect.Value, omitempty bool) (string, error) {
val := v.Elem()
if !val.IsValid() {
return "", nil
}
encodeFn, err := getEncodeFunc(val.Type())
if err != nil {
return "", err
}
return encodeFn(val, omitempty)
}
func encodePtrInterface(v reflect.Value, omitempty bool) (string, error) {
val := v.Elem()
if !val.IsValid() {
return "", nil
}
val = val.Elem()
if !val.IsValid() {
return "", nil
}
encodeFn, err := getEncodeFunc(val.Type())
if err != nil {
return "", err
}
return encodeFn(val, omitempty)
}