-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathstructencoder.go
executable file
·540 lines (446 loc) · 14.6 KB
/
structencoder.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package jingo
// structencoder.go manages StructEncoder and its responsibilities.
// The general goal of the approach is to do as much of the necessary work as possible inside
// the 'compile' stage upon instantiation. This includes any logic, type assertions, buffering
// or otherwise. Changes made should consider first their ns/op impact and then their allocation
// profile also. Allocations should essentially remain at zero - albeit with the exclusion of the
// `.String()` stringer functionality which is somewhat out of our control.
import (
"fmt"
"io"
"reflect"
"strings"
"time"
"unsafe"
)
// instruction describes the different ways we can execute a single instruction at runtime.
// static, leapFun/offset, fun are mutually exclusive. we've used a concrete type for speed.
type instruction struct {
static []byte // provides a fast path for writing static chunks without needing an instruction function
kind int // used to switch special paths in Marshal, like string fast path
offset uintptr // used in conjunction with leapFun
leapFun func(unsafe.Pointer, *Buffer) // provides a fast path for simple write & avoids wrapping function to capture offset
fun func(unsafe.Pointer, *Buffer) // full instruction function for when the approaches above fail
}
const (
kindNormal = iota
kindStringField
kindStatic
kindInt
)
// iface describes the memory footprint of interface{}
type iface struct {
Type, Data unsafe.Pointer
}
// StructEncoder stores a set of instructions for converting a struct to a json document. It's
// useless to create an instance of this outside of `NewStructEncoder`.
type StructEncoder struct {
instructions []instruction // the instructionset to be executed during Marshal
f reflect.StructField // current field
t interface{} // type
i int // iter
cb Buffer // side buffer for static data
cpos int // side buffer position
}
// Marshal executes the instructions for a given type and writes the resulting
// json document to the io.Writer provided
func (e *StructEncoder) Marshal(s interface{}, w *Buffer) {
p := (*(*iface)(unsafe.Pointer(&s))).Data
for i := 0; i < len(e.instructions); i++ {
if e.instructions[i].kind == kindStatic { // static data fast path
w.Write(e.instructions[i].static)
continue
} else if e.instructions[i].kind == kindStringField { // string fields fast path, allows inlining of whole write
ptrStringToBuf(unsafe.Pointer(uintptr(p)+e.instructions[i].offset), w)
continue
} else if e.instructions[i].kind == kindInt { // int fields fast path, allows inlining of whole write
ptrIntToBuf(unsafe.Pointer(uintptr(p)+e.instructions[i].offset), w)
continue
} else if e.instructions[i].leapFun != nil { // simple 'conv' function fast path
e.instructions[i].leapFun(unsafe.Pointer(uintptr(p)+e.instructions[i].offset), w)
continue
}
e.instructions[i].fun(p, w) // all other instruction types
}
}
// NewStructEncoder compiles a set of instructions for marhsaling a struct shape to a JSON document.
func NewStructEncoder(t interface{}) *StructEncoder {
e := &StructEncoder{}
e.t = t
tt := reflect.TypeOf(t)
e.chunk("{")
emit := 0 // track number of fields we emit
// pass over each field in the struct to build up our instruction set for each
for e.i = 0; e.i < tt.NumField(); e.i++ {
e.f = tt.Field(e.i)
tag, opts := parseTag(e.f.Tag.Get("json")) // we're using tags to nominate inclusion
if tag == "" {
continue
}
emit++
// write the key
if emit > 1 {
e.chunk(",")
}
e.chunk(`"` + tag + `":`)
switch {
/// support calling .String() when the 'stringer' option is passed
case opts.Contains("stringer") && reflect.ValueOf(e.t).Field(e.i).MethodByName("String").Kind() != reflect.Invalid:
e.optInstrStringer()
/// support calling .JSONEncode(*Buffer) when the 'encoder' option is passed
case opts.Contains("encoder"):
// requrie explicit opt-in for JSONMarshaler implementation
t := reflect.ValueOf(e.t).Field(e.i).Type()
if t.Kind() != reflect.Ptr {
t = reflect.PtrTo(t)
}
if _, ok := t.MethodByName("EncodeJSON"); ok {
e.optInstrEncoderWriter()
break
}
// default to JSONEncoder implementation for any other encoder fields
e.optInstrEncoder()
/// support writing byteslice-like items using 'raw' option.
case opts.Contains("raw"):
e.optInstrRaw()
/// suport escaping reserved json characters from byteslice-like items and slices
case opts.Contains("escape"):
e.optInstrEscape()
/// time is a type of struct, not a kind, so somewhat of a special case here.
case e.f.Type == timeType:
e.chunk(`"`)
e.val(ptrTimeToBuf)
e.chunk(`"`)
case e.f.Type.Kind() == reflect.Ptr && timeType == reflect.TypeOf(e.t).Field(e.i).Type.Elem():
e.ptrstringval(ptrTimeToBuf)
// write the value instruction depending on type
case e.f.Type.Kind() == reflect.Ptr:
// create an instruction which can read from a pointer field
e.valueInst(e.f.Type.Elem().Kind(), e.ptrval)
default:
// create an instruction which reads from a standard field
e.valueInst(e.f.Type.Kind(), e.val)
}
}
e.chunk("}")
e.flunk()
return e
}
func (e *StructEncoder) appendInstructionFun(fun func(unsafe.Pointer, *Buffer)) {
e.instructions = append(e.instructions, instruction{fun: fun})
}
func (e *StructEncoder) optInstrStringer() {
e.chunk(`"`)
t := reflect.ValueOf(e.t).Field(e.i).Type()
if e.f.Type.Kind() == reflect.Ptr {
t = t.Elem()
}
conv := func(v unsafe.Pointer, w *Buffer) {
e, ok := reflect.NewAt(t, v).Interface().(fmt.Stringer)
if !ok {
return
}
w.WriteString(e.String())
}
if e.f.Type.Kind() == reflect.Ptr {
e.ptrval(conv)
} else {
e.val(conv)
}
e.chunk(`"`)
}
func (e *StructEncoder) optInstrEncoder() {
t := reflect.ValueOf(e.t).Field(e.i).Type()
if e.f.Type.Kind() == reflect.Ptr {
t = t.Elem()
}
conv := func(v unsafe.Pointer, w *Buffer) {
e, ok := reflect.NewAt(t, v).Interface().(JSONEncoder)
if !ok {
w.Write(null)
return
}
e.JSONEncode(w)
}
if e.f.Type.Kind() == reflect.Ptr {
e.ptrval(conv)
} else {
e.val(conv)
}
}
func (e *StructEncoder) optInstrEncoderWriter() {
t := reflect.ValueOf(e.t).Field(e.i).Type()
if e.f.Type.Kind() == reflect.Ptr {
t = t.Elem()
}
conv := func(v unsafe.Pointer, w *Buffer) {
e, ok := reflect.NewAt(t, v).Interface().(JSONMarshaler)
if !ok {
w.Write(null)
return
}
e.EncodeJSON(w)
}
if e.f.Type.Kind() == reflect.Ptr {
e.ptrval(conv)
} else {
e.val(conv)
}
}
func (e *StructEncoder) optInstrRaw() {
conv := func(v unsafe.Pointer, w *Buffer) {
s := *(*string)(v)
if len(s) == 0 {
w.Write(null)
return
}
w.WriteString(s)
}
if e.f.Type.Kind() == reflect.Ptr {
e.ptrval(conv)
} else {
e.val(conv)
}
}
func (e *StructEncoder) optInstrEscape() {
if e.f.Type.Kind() == reflect.Slice {
e.flunk()
/// create an escape string encoder internally instead of mirroring the struct, so people only need to pass the ,escape opt instead
enc := NewSliceEncoder([]EscapeString{})
f := e.f
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
var em interface{} = unsafe.Pointer(uintptr(v) + f.Offset)
enc.Marshal(em, w)
})
return
}
if e.f.Type.Kind() == reflect.Ptr {
e.ptrstringval(ptrEscapeStringToBuf)
} else {
e.chunk(`"`)
e.val(ptrEscapeStringToBuf)
e.chunk(`"`)
}
}
// chunk writes a chunk of body data to the chunk buffer. only for writing static
//
// structure and not dynamic values.
func (e *StructEncoder) chunk(b string) {
e.cb.Write([]byte(b))
}
// flunk flushes whatever chunk data we've got buffered into a single instruction
func (e *StructEncoder) flunk() {
b := e.cb.Bytes
bs := b[e.cpos:]
e.cpos = len(b)
if len(bs) == 0 {
return
}
e.instructions = append(e.instructions, instruction{static: bs, kind: kindStatic})
}
// valueInst works out the conversion function we need for `k` and creates an instruction to write it to the buffer
func (e *StructEncoder) valueInst(k reflect.Kind, instr func(func(unsafe.Pointer, *Buffer))) {
switch k {
case reflect.Int:
/// fast path for int fields
if e.f.Type.Kind() == reflect.Ptr {
instr(ptrIntToBuf)
return
}
e.flunk()
e.instructions = append(e.instructions, instruction{offset: e.f.Offset, kind: kindInt})
case reflect.Bool,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64:
/// standard print
conv, ok := typeconv[k]
if !ok {
return
}
instr(conv)
case reflect.Array:
/// support for primitives in arrays (proabbly need arrayencoder.go here if we want to take this further)
e.chunk("[")
conv, ok := typeconv[e.f.Type.Elem().Kind()]
if !ok {
return
}
offset := e.f.Type.Elem().Size()
for i := 0; i < e.f.Type.Len(); i++ {
if i > 0 {
e.chunk(", ")
}
e.flunk()
f := e.f
i := i
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
conv(unsafe.Pointer(uintptr(v)+f.Offset+(uintptr(i)*offset)), w)
})
}
e.chunk("]")
case reflect.Slice:
e.flunk()
enc := NewSliceEncoder(reflect.ValueOf(e.t).Field(e.i).Interface())
f := e.f
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
var em interface{} = unsafe.Pointer(uintptr(v) + f.Offset)
enc.Marshal(em, w)
})
case reflect.String:
/// for strings to be nullable they need a special instruction to write quotes conditionally.
if e.f.Type.Kind() == reflect.Ptr {
e.ptrstringval(ptrStringToBuf)
return
}
// otherwise a standard quoted print instruction
e.chunk(`"`)
/// fast path for strings
e.flunk() // flush any chunk data we've buffered
e.instructions = append(e.instructions, instruction{offset: e.f.Offset, kind: kindStringField})
e.chunk(`"`)
case reflect.Struct:
// create an instruction for the field name (as per val)
e.flunk()
if e.f.Type.Kind() == reflect.Ptr {
/// now cater for it being a pointer to a struct
var inf = reflect.New(reflect.TypeOf(e.t).Field(e.i).Type.Elem()).Elem().Interface()
var enc *StructEncoder
if e.t == inf {
// handle recursive structs by re-using the current encoder
enc = e
} else {
enc = NewStructEncoder(inf)
}
// now create an instruction to marshal the field
f := e.f
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
var em interface{} = unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(v) + f.Offset)))
if em == unsafe.Pointer(nil) {
w.Write(null)
return
}
enc.Marshal(em, w)
})
return
}
// build a new StructEncoder for the type
enc := NewStructEncoder(reflect.ValueOf(e.t).Field(e.i).Interface())
// now create another instruction which calls marshal on the struct, passing our writer
f := e.f
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
var em interface{} = unsafe.Pointer(uintptr(v) + f.Offset)
enc.Marshal(em, w)
})
return
case reflect.Invalid,
reflect.Map,
reflect.Interface,
reflect.Complex64,
reflect.Complex128,
reflect.Chan,
reflect.Func,
reflect.Uintptr,
reflect.UnsafePointer:
// no
panic(fmt.Sprint("unsupported type ", e.f.Type.Kind(), e.f.Name))
}
}
// val creates an instruction to read from a field we're marshaling
func (e *StructEncoder) val(conv func(unsafe.Pointer, *Buffer)) {
e.flunk() // flush any chunk data we've buffered
e.instructions = append(e.instructions, instruction{leapFun: conv, offset: e.f.Offset})
}
// ptrval creates an instruction to read from a pointer field we're marshaling
func (e *StructEncoder) ptrval(conv func(unsafe.Pointer, *Buffer)) {
e.flunk() // flush any chunk data we've buffered
// avoids allocs at runtime
null := []byte("null")
f := e.f
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
p := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(v) + f.Offset)))
if p == unsafe.Pointer(nil) {
w.Write(null)
return
}
conv(p, w)
})
}
// ptrstringval is essentially the same as ptrval but quotes strings if not nil
func (e *StructEncoder) ptrstringval(conv func(unsafe.Pointer, *Buffer)) {
e.flunk() // flush any chunk data we've buffered
// avoids allocs at runtime
null := []byte("null")
f := e.f
e.appendInstructionFun(func(v unsafe.Pointer, w *Buffer) {
p := unsafe.Pointer(*(*unsafe.Pointer)(unsafe.Pointer(uintptr(v) + f.Offset)))
if p == unsafe.Pointer(nil) {
w.Write(null)
return
}
// quotes need to be at runtime here because we don't know if we're going to have to null the field
w.WriteByte('"')
conv(p, w)
w.WriteByte('"')
})
}
// JSONEncoder works with the `.encoder` option. Fields can implement this to encode their own JSON string straight
// into the working buffer. This can be useful if you're working with interface fields at runtime.
type JSONEncoder interface {
JSONEncode(*Buffer)
}
// JSONMarshaler works with the `.encoder` option. Fields can implement this to encode their own JSON string straight
// into the provided `io.Writer`. This is useful if you require the functionality of `JSONEncoder` but don't want the hard
// dependency on `Buffer`.
type JSONMarshaler interface {
EncodeJSON(io.Writer)
}
// tagOptions is the string following a comma in a struct field's "json"
// tag, or the empty string. It does not include the leading comma.
//
// this is jacked from the stdlib to remain compatible with that syntax.
type tagOptions string
// parseTag splits a struct field's json tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
// Contains reports whether a comma-separated list of options
// contains a particular substr flag. substr must be surrounded by a
// string boundary or commas.
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, ",")
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}
var timeType = reflect.TypeOf(time.Time{})
// EscapeString can be used to cast your string slice encoders in replacement of `[]string` when using SliceEncoder directly.
// This is only necessary if you wish for the slice elements to be escaped of control sequences.
// e.g var mySliceEncoder = NewSliceEncoder([]jingo.EscapeString{})
// You can and should just use the `,escape` option on your struct fields when using StructEncoder.
type EscapeString string
var escapeStringType = reflect.TypeOf(EscapeString(""))