-
Notifications
You must be signed in to change notification settings - Fork 32
/
sliceencoder.go
executable file
·326 lines (262 loc) · 6.78 KB
/
sliceencoder.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
package jingo
// sliceencoder.go manages SliceEncoder and its responsibilities.
// SliceEncoder follows the same principle of structencoder.go in that it generates lightweight
// instructions as part of its compile stage which are executed later during the Marshal. The
// slight difference here is that instruction is singular rather than plural. This is due to the
// need for slice iteration to be managed from the instruction itself (at 'runtime') as a result
// of slices being of variable length.
import (
"reflect"
"unsafe"
)
// SliceEncoder stores a set of instructions for building a JSON document from a slice at runtime.
type SliceEncoder struct {
instruction func(t unsafe.Pointer, w *Buffer)
tt reflect.Type
offset uintptr
}
// Marshal executes the instruction set built up by NewSliceEncoder
func (e *SliceEncoder) Marshal(s interface{}, w *Buffer) {
p := unsafe.Pointer(reflect.ValueOf(s).Pointer())
e.instruction(p, w)
}
// NewSliceEncoder builds a new SliceEncoder
func NewSliceEncoder(t interface{}) *SliceEncoder {
e := &SliceEncoder{}
e.tt = reflect.TypeOf(t)
e.offset = e.tt.Elem().Size()
// see if we can select based on a specific type
switch e.tt.Elem() {
case timeType:
e.timeInstr()
return e
case escapeStringType:
e.stringInstr(ptrEscapeStringToBuf)
return e
}
// what type of encoding do we need
switch e.tt.Elem().Kind() {
case reflect.Slice:
e.sliceInstr()
case reflect.Struct:
e.structInstr()
case reflect.String:
e.stringInstr(ptrStringToBuf)
case reflect.Ptr:
/// which pointer type
switch e.tt.Elem().Elem() {
case timeType:
e.ptrTimeInstr()
return e
case escapeStringType:
e.ptrStringInstr(ptrEscapeStringToBuf)
return e
}
switch e.tt.Elem().Elem().Kind() {
case reflect.Slice:
e.ptrSliceInstr()
case reflect.Struct:
e.ptrStrctInstr()
case reflect.String:
e.ptrStringInstr(ptrStringToBuf)
default:
e.ptrOtherInstr()
}
default:
e.otherInstr()
}
return e
}
// // avoid allocs in the instruction
var (
null = []byte("null")
zero = uintptr(0)
)
// sliceHeader is a replacement for reflect.SliceHeader which forces the uintptr conversion to be done inline to
// play nice with vet and the unsafe conversion rules
type sliceHeader struct {
Data unsafe.Pointer
Len int
Cap int
}
func (e *SliceEncoder) sliceInstr() {
enc := NewSliceEncoder(reflect.New(e.tt.Elem()).Elem().Interface())
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))
enc.Marshal(s, w)
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) structInstr() {
enc := NewStructEncoder(reflect.New(e.tt.Elem()).Elem().Interface())
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))
enc.Marshal(s, w)
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) stringInstr(conv func(unsafe.Pointer, *Buffer)) {
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i == 0 {
w.WriteByte('"')
}
if i > zero {
w.Write([]byte(`","`))
}
conv(unsafe.Pointer(uintptr(sl.Data)+(i*e.offset)), w)
if i == uintptr(sl.Len)-1 {
w.WriteByte('"')
}
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) otherInstr() {
conv, ok := typeconv[e.tt.Elem().Kind()]
if !ok {
return
}
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
conv(unsafe.Pointer(uintptr(sl.Data)+(i*e.offset)), w)
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) timeInstr() {
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
w.WriteByte('"')
ptrTimeToBuf(unsafe.Pointer(uintptr(sl.Data)+(i*e.offset)), w)
w.WriteByte('"')
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) ptrSliceInstr() {
enc := NewSliceEncoder(reflect.New(e.tt.Elem()).Elem().Elem().Interface())
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))))
if s == unsafe.Pointer(nil) {
w.Write(null)
continue
}
enc.Marshal(s, w)
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) ptrStrctInstr() {
enc := NewStructEncoder(reflect.New(e.tt.Elem().Elem()).Elem().Interface())
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))))
if s == unsafe.Pointer(nil) {
w.Write(null)
continue
}
enc.Marshal(s, w)
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) ptrStringInstr(conv func(unsafe.Pointer, *Buffer)) {
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))))
if s == unsafe.Pointer(nil) {
w.Write(null)
continue
}
w.WriteByte('"')
conv(s, w)
w.WriteByte('"')
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) ptrOtherInstr() {
conv, ok := typeconv[e.tt.Elem().Elem().Kind()]
if !ok {
return
}
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))))
if s == unsafe.Pointer(nil) {
w.Write(null)
continue
}
conv(s, w)
}
w.WriteByte(']')
}
}
func (e *SliceEncoder) ptrTimeInstr() {
e.instruction = func(v unsafe.Pointer, w *Buffer) {
w.WriteByte('[')
sl := *(*sliceHeader)(v)
for i := uintptr(0); i < uintptr(sl.Len); i++ {
if i > zero {
w.WriteByte(',')
}
s := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(sl.Data) + (i * e.offset))))
if s == unsafe.Pointer(nil) {
w.Write(null)
continue
}
w.WriteByte('"')
ptrTimeToBuf(s, w)
w.WriteByte('"')
}
w.WriteByte(']')
}
}