-
Notifications
You must be signed in to change notification settings - Fork 5
/
encode.go
309 lines (269 loc) · 7.15 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package qs
import (
"github.com/pkg/errors"
"net/url"
"reflect"
"strings"
"sync"
"time"
)
const (
tagOmitEmpty = "omitempty"
)
var (
timeType = reflect.TypeOf(time.Time{})
encoderType = reflect.TypeOf(new(QueryParamEncoder)).Elem()
zeroerType = reflect.TypeOf(new(Zeroer)).Elem()
)
// EncoderOption provides option for Encoder
type EncoderOption func(encoder *Encoder)
// Encoder is the main instance
// Apply options by using WithTagAlias, WithCustomType
type Encoder struct {
tagAlias string
cache *cacheStore
dataPool *sync.Pool
}
type encoder struct {
e *Encoder
values url.Values
tags [][]byte
scope []byte
}
// WithTagAlias create a option to set custom tag alias instead of `qs`
func WithTagAlias(tagAlias string) EncoderOption {
return func(encoder *Encoder) {
encoder.tagAlias = tagAlias
}
}
// NewEncoder init new *Encoder instance
// Use EncoderOption to apply options
func NewEncoder(options ...EncoderOption) *Encoder {
e := &Encoder{
tagAlias: "qs",
}
// Apply options
for _, opt := range options {
opt(e)
}
e.cache = newCacheStore()
e.dataPool = &sync.Pool{New: func() interface{} {
tagSize := 5
tags := make([][]byte, 0, tagSize)
for i := 0; i < tagSize; i++ {
tags = append(tags, make([]byte, 0, 56))
}
return &encoder{
e: e,
tags: tags,
scope: make([]byte, 0, 64),
}
}}
return e
}
// Values encodes a struct into url.Values
// v must be struct data type
func (e *Encoder) Values(v interface{}) (url.Values, error) {
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
}
val = val.Elem()
}
switch val.Kind() {
case reflect.Invalid:
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
case reflect.Struct:
enc := e.dataPool.Get().(*encoder)
enc.values = make(url.Values)
err := enc.encodeStruct(val, enc.values, nil)
if err != nil {
return nil, err
}
values := enc.values
e.dataPool.Put(enc)
return values, nil
default:
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
}
}
// Encode encodes a struct into the given url.Values
// v must be struct data type
func (e *Encoder) Encode(v interface{}, values url.Values) error {
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return errors.Errorf("expects struct input, got %v", val.Kind())
}
val = val.Elem()
}
switch val.Kind() {
case reflect.Invalid:
return errors.Errorf("expects struct input, got %v", val.Kind())
case reflect.Struct:
enc := e.dataPool.Get().(*encoder)
err := enc.encodeStruct(val, values, nil)
if err != nil {
return err
}
return nil
default:
return errors.Errorf("expects struct input, got %v", val.Kind())
}
}
func (e *encoder) encodeStruct(stVal reflect.Value, values url.Values, scope []byte) error {
stTyp := stVal.Type()
cachedFlds := e.e.cache.Retrieve(stTyp)
if cachedFlds == nil {
cachedFlds = make(cachedFields, 0, stTyp.NumField())
e.structCaching(&cachedFlds, stVal, scope)
e.e.cache.Store(stTyp, cachedFlds)
}
for i, cachedFld := range cachedFlds {
stFldVal := stVal.Field(i)
switch cachedFld := cachedFld.(type) {
case nil:
// skip field
continue
case *mapField:
if cachedFld.cachedKeyField == nil || cachedFld.cachedValueField == nil {
//data type is not supported
continue
}
for stFldVal.Kind() == reflect.Ptr {
stFldVal = stFldVal.Elem()
}
if !stFldVal.IsValid() {
continue
}
if stFldVal.Len() == 0 {
continue
}
case *listField:
if cachedFld.cachedField == nil {
//data type is not supported
continue
}
if cachedFld.arrayFormat <= arrayFormatBracket {
// With cachedFld type is slice/array, only accept non-nil value
for stFldVal.Kind() == reflect.Ptr {
stFldVal = stFldVal.Elem()
}
if !stFldVal.IsValid() {
continue
}
if stFldVal.Len() == 0 {
continue
}
if count := countElem(stFldVal); count > 0 {
// preallocate slice
values[cachedFld.name] = make([]string, 0, countElem(stFldVal))
} else {
continue
}
}
}
// format value
err := cachedFld.formatFnc(stFldVal, func(name string, val string) {
values[name] = append(values[name], val)
})
if err != nil {
return err
}
}
return nil
}
func (e *encoder) structCaching(fields *cachedFields, stVal reflect.Value, scope []byte) {
structTyp := getType(stVal)
for i := 0; i < structTyp.NumField(); i++ {
structField := structTyp.Field(i)
if structField.PkgPath != "" && !structField.Anonymous { // unexported field
*fields = append(*fields, nil)
continue
}
e.getTagNameAndOpts(structField)
if string(e.tags[0]) == "-" { // ignored field
continue
}
if string(scope) != "" {
scopedName := strings.Builder{}
scopedName.Write(scope)
scopedName.WriteRune('[')
scopedName.Write(e.tags[0])
scopedName.WriteRune(']')
e.tags[0] = e.tags[0][:0]
e.tags[0] = append(e.tags[0], scopedName.String()...)
}
fieldVal := stVal.Field(i)
if fieldVal.Type().Implements(encoderType) {
*fields = append(*fields, newCustomField(fieldVal.Type(), e.tags[0], e.tags[1:]))
continue
}
fieldTyp := getType(fieldVal)
if fieldTyp == timeType {
*fields = append(*fields, newTimeField(e.tags[0], e.tags[1:]))
continue
}
switch fieldTyp.Kind() {
case reflect.Struct:
fieldVal = reflect.Zero(fieldTyp)
// Clear and set new scope
e.scope = e.scope[:0]
e.scope = append(e.scope, e.tags[0]...)
// New embed field
field := newEmbedField(fieldVal.NumField(), e.tags[0], e.tags[1:])
*fields = append(*fields, field)
// Recursive
e.structCaching(&field.cachedFields, fieldVal, e.scope)
case reflect.Slice, reflect.Array:
//Slice element type
elemType := fieldTyp.Elem()
if elemType.Implements(encoderType) {
*fields = append(*fields, e.newListField(elemType, e.tags[0], e.tags[1:]))
continue
}
for elemType.Kind() == reflect.Ptr {
elemType = elemType.Elem()
}
*fields = append(*fields, e.newListField(elemType, e.tags[0], e.tags[1:]))
case reflect.Map:
keyType := fieldTyp.Key()
/*for keyType.Kind() == reflect.Ptr {
keyType = keyType.Elem()
}*/
valueType := fieldTyp.Elem()
/*for valueType.Kind() == reflect.Ptr {
valueType = valueType.Elem()
}*/
*fields = append(*fields, newMapField(keyType, valueType, e.tags[0], e.tags[1:]))
default:
*fields = append(*fields, newCachedFieldByKind(fieldTyp.Kind(), e.tags[0], e.tags[1:]))
}
}
}
func (e *encoder) getTagNameAndOpts(f reflect.StructField) {
// Get tag by alias
tag := f.Tag.Get(e.e.tagAlias)
// Clear first tag in slice
e.tags[0] = e.tags[0][:0]
if len(tag) == 0 {
// no tag, using struct field name
e.tags[0] = append(e.tags[0][:0], f.Name...)
e.tags = e.tags[:1]
} else {
// Use first tag as temp
e.tags[0] = append(e.tags[0][:0], tag...)
splitTags := strings.Split(tag, ",")
e.tags = e.tags[:len(splitTags)]
for i := 0; i < len(splitTags); i++ {
if i == 0 {
if len(splitTags[0]) == 0 {
e.tags[0] = append(e.tags[i][:0], f.Name...)
continue
}
}
e.tags[i] = append(e.tags[i][:0], splitTags[i]...)
}
}
}