-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoder_config.go
374 lines (351 loc) · 9.53 KB
/
encoder_config.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package jzon
import (
"io"
"reflect"
"sync"
"sync/atomic"
)
var (
// DefaultEncoderConfig is compatible with standard lib
DefaultEncoderConfig = NewEncoderConfig(nil)
)
// EncoderOption can be used to customize the encoder config
type EncoderOption struct {
ValEncoders map[reflect.Type]ValEncoder
EscapeHTML bool
Tag string
OnlyTaggedField bool
}
type encoderCache map[rtype]ValEncoder
func (cache encoderCache) has(rtype rtype) bool {
_, ok := cache[rtype]
return ok
}
// make sure that the pointer encoders has already been rebuilt
// before calling, so it's safe to use it's internal encoder
func (cache encoderCache) preferPtrEncoder(typ reflect.Type) ValEncoder {
ptrType := reflect.PtrTo(typ)
ptrEncoder := cache[rtypeOfType(ptrType)]
if pe, ok := ptrEncoder.(*pointerEncoder); ok {
return pe.encoder
}
// the element has a special pointer encoder
return &directEncoder{ptrEncoder}
}
// EncoderConfig is a frozen config for encoding
type EncoderConfig struct {
cacheMu sync.Mutex
// the encoder cache, or root encoder cache
encoderCache atomic.Value
// the internal cache
internalCache encoderCache
tag string
onlyTaggedField bool
// can override during runtime
escapeHTML bool
}
// NewEncoderConfig returns a new encoder config
// If the input option is nil, the default option will be applied
func NewEncoderConfig(opt *EncoderOption) *EncoderConfig {
encCfg := EncoderConfig{
tag: "json",
escapeHTML: true,
}
cache := encoderCache{}
internalCache := encoderCache{}
if opt != nil {
for typ, valEnc := range opt.ValEncoders {
rtype := rtypeOfType(typ)
cache[rtype] = valEnc
internalCache[rtype] = valEnc
}
encCfg.escapeHTML = opt.EscapeHTML
if opt.Tag != "" {
encCfg.tag = opt.Tag
}
encCfg.onlyTaggedField = opt.OnlyTaggedField
}
encCfg.encoderCache.Store(cache)
encCfg.internalCache = internalCache
return &encCfg
}
// Marshal behave like json.Marshal
func (encCfg *EncoderConfig) Marshal(obj interface{}) ([]byte, error) {
s := encCfg.NewStreamer()
defer s.Release()
s.Value(obj)
if s.Error != nil {
return nil, s.Error
}
// we make a new slice with explicit size,
// 1. the internal buffer may be much longer than the output one,
// it can be used for longer output
// 2. avoid calling bytes buffer pool (sync.Pool)
b := make([]byte, len(s.buffer))
copy(b, s.buffer)
return b, nil
}
// NewEncoder returns a new encoder that writes to w.
func (encCfg *EncoderConfig) NewEncoder(w io.Writer) *Encoder {
s := encCfg.NewStreamer()
s.Reset(w)
return &Encoder{
s: s,
}
}
func (encCfg *EncoderConfig) getEncoderFromCache(rtype rtype) ValEncoder {
return encCfg.encoderCache.Load().(encoderCache)[rtype]
}
func (encCfg *EncoderConfig) createEncoder(rtype rtype, typ reflect.Type) ValEncoder {
encCfg.cacheMu.Lock()
defer encCfg.cacheMu.Unlock()
cache := encCfg.encoderCache.Load().(encoderCache)
// double check
if ve := cache[rtype]; ve != nil {
return ve
}
newCache := encoderCache{}
for k, v := range cache {
newCache[k] = v
}
var q typeQueue
q.push(typ)
encCfg.createEncoderInternal(newCache, encCfg.internalCache, q)
encCfg.encoderCache.Store(newCache)
return newCache[rtype]
}
func (encCfg *EncoderConfig) createEncoderInternal(cache, internalCache encoderCache, typesToCreate typeQueue) {
rebuildMap := map[rtype]interface{}{}
for typ := typesToCreate.pop(); typ != nil; typ = typesToCreate.pop() {
rType := rtypeOfType(typ)
if internalCache.has(rType) { // check if visited
continue
}
// check global encoders
if v, ok := globalValEncoders[rType]; ok {
internalCache[rType] = v
cache[rType] = v
continue
}
kind := typ.Kind()
// check json.Marshaler interface
if typ.Implements(jsonMarshalerType) {
if ifaceIndir(rType) {
v := &jsonMarshalerEncoder{
isEmpty: isEmptyFunctions[kind],
rtype: rType,
}
internalCache[rType] = v
cache[rType] = v
continue
}
if typ.Kind() == reflect.Ptr {
elemType := typ.Elem()
if elemType.Implements(jsonMarshalerType) {
// treat as a pointer encoder
typesToCreate.push(elemType)
w := newPointerEncoder(elemType)
internalCache[rType] = w.encoder
rebuildMap[rType] = w
} else {
v := pointerJSONMarshalerEncoder(rType)
internalCache[rType] = v
cache[rType] = &directEncoder{v}
}
continue
}
v := &directJSONMarshalerEncoder{
isEmpty: isEmptyFunctions[kind],
rtype: rType,
}
internalCache[rType] = v
cache[rType] = &directEncoder{v}
continue
}
// check encoding.TextMarshaler interface
if typ.Implements(textMarshalerType) {
if ifaceIndir(rType) {
v := &textMarshalerEncoder{
isEmpty: isEmptyFunctions[kind],
rtype: rType,
}
internalCache[rType] = v
cache[rType] = v
continue
}
if typ.Kind() == reflect.Ptr {
elemType := typ.Elem()
if elemType.Implements(textMarshalerType) {
// treat as a pointer encoder
typesToCreate.push(elemType)
w := newPointerEncoder(elemType)
internalCache[rType] = w.encoder
rebuildMap[rType] = w
} else {
v := pointerTextMarshalerEncoder(rType)
internalCache[rType] = v
cache[rType] = &directEncoder{v}
}
continue
}
v := &directTextMarshalerEncoder{
isEmpty: isEmptyFunctions[kind],
rtype: rType,
}
internalCache[rType] = v
cache[rType] = &directEncoder{v}
continue
}
if kindRType := encoderKindMap[kind]; kindRType != 0 {
// TODO: shall we make this an option?
// TODO: so that only the native type is affected?
// check if the native type has a custom encoder
if v, ok := internalCache[kindRType]; ok {
internalCache[rType] = v
cache[rType] = v
continue
}
if v := kindEncoders[kind]; v != nil {
internalCache[rType] = v
cache[rType] = v
continue
}
}
switch kind {
case reflect.Ptr:
elemType := typ.Elem()
typesToCreate.push(elemType)
w := newPointerEncoder(elemType)
internalCache[rType] = w.encoder
rebuildMap[rType] = w
case reflect.Array:
elemType := typ.Elem()
typesToCreate.push(reflect.PtrTo(elemType))
if typ.Len() == 0 {
v := (*emptyArrayEncoder)(nil)
internalCache[rType] = v
cache[rType] = v
} else {
w := newArrayEncoder(typ)
internalCache[rType] = w.encoder
rebuildMap[rType] = w
}
case reflect.Interface:
var v ValEncoder
if typ.NumMethod() == 0 {
v = (*efaceEncoder)(nil)
} else {
v = (*ifaceEncoder)(nil)
}
internalCache[rType] = v
cache[rType] = v
case reflect.Map:
w := newMapEncoder(typ)
if w == nil {
v := notSupportedEncoder(typ.String())
internalCache[rType] = v
cache[rType] = v
} else {
typesToCreate.push(typ.Elem())
// pointer decoder is a reverse of direct encoder
internalCache[rType] = w.encoder
rebuildMap[rType] = w
}
case reflect.Slice:
w := newSliceEncoder(typ)
typesToCreate.push(reflect.PtrTo(typ.Elem()))
internalCache[rType] = w.encoder
rebuildMap[rType] = w
case reflect.Struct:
w := encCfg.newStructEncoder(typ)
if w == nil {
// no fields to marshal
v := (*emptyStructEncoder)(nil)
internalCache[rType] = v
cache[rType] = v
} else {
for i := range w.fields {
fi := &w.fields[i]
typesToCreate.push(fi.ptrType)
}
internalCache[rType] = w.encoder
rebuildMap[rType] = w
}
default:
v := notSupportedEncoder(typ.String())
internalCache[rType] = v
cache[rType] = v
}
}
// rebuild base64 encoders
for rType, builder := range rebuildMap {
switch x := builder.(type) {
case *sliceEncoderBuilder:
if x.elemType.Kind() != reflect.Uint8 {
continue
}
elemPtrType := reflect.PtrTo(x.elemType)
elemPtrEncoder := internalCache[rtypeOfType(elemPtrType)]
if _, ok := elemPtrEncoder.(*pointerEncoder); !ok {
// the element has a special pointer encoder
continue
}
// the pointer decoder has not been rebuilt yet
// we need to use the explicit element rtype
elemEncoder := internalCache[rtypeOfType(x.elemType)]
if elemEncoder != (*uint8Encoder)(nil) {
// the element has a special value encoder
continue
}
v := (*base64Encoder)(nil)
internalCache[rType] = v
cache[rType] = v
delete(rebuildMap, rType)
}
}
// rebuild ptr encoders
for rType, builder := range rebuildMap {
switch x := builder.(type) {
case *pointerEncoderBuilder:
v := internalCache[x.elemRType]
x.encoder.encoder = v
cache[rType] = v
delete(rebuildMap, rType)
}
}
// rebuild other encoders
for rType, builder := range rebuildMap {
switch x := builder.(type) {
case *arrayEncoderBuilder:
x.encoder.encoder = internalCache.preferPtrEncoder(x.elemType)
if ifaceIndir(rType) {
cache[rType] = x.encoder
} else {
// (see reflect.ArrayOf)
// when the array is stored in interface directly, it means:
// 1. the length of array is 1
// 2. the element of the array is also directly saved
cache[rType] = &directEncoder{x.encoder}
}
case *mapEncoderBuilder:
// TODO: key encoder
x.encoder.elemEncoder = internalCache[x.elemRType]
cache[rType] = &directEncoder{x.encoder}
case *sliceEncoderBuilder:
x.encoder.elemEncoder = internalCache.preferPtrEncoder(x.elemType)
cache[rType] = x.encoder
case *structEncoderBuilder:
x.encoder.fields.init(len(x.fields))
for i := range x.fields {
fi := &x.fields[i]
v := internalCache.preferPtrEncoder(fi.ptrType.Elem())
x.encoder.fields.add(fi, v)
}
if ifaceIndir(rType) {
cache[rType] = x.encoder
} else {
cache[rType] = &directEncoder{x.encoder}
}
}
}
}