Debian Codegraph @

codec/encode.go

raw symbols SCIP
1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
3
4 package codec
5
6 import (
7 "encoding"
8 "errors"
9 "io"
10 "reflect"
11 "sort"
12 "strconv"
13 "time"
14 )
15
16 // defEncByteBufSize is the default size of []byte used
17 // for bufio buffer or []byte (when nil passed)
18 const defEncByteBufSize = 1 << 10 // 4:16, 6:64, 8:256, 10:1024
19
20 var errEncoderNotInitialized = errors.New("Encoder not initialized")
21
22 // encDriver abstracts the actual codec (binc vs msgpack, etc)
23 type encDriver interface {
24 EncodeNil()
25 EncodeInt(i int64)
26 EncodeUint(i uint64)
27 EncodeBool(b bool)
28 EncodeFloat32(f float32)
29 EncodeFloat64(f float64)
30 EncodeRawExt(re *RawExt)
31 EncodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext)
32 // EncodeString using cUTF8, honor'ing StringToRaw flag
33 EncodeString(v string)
34 EncodeStringBytesRaw(v []byte)
35 EncodeTime(time.Time)
36 WriteArrayStart(length int)
37 WriteArrayEnd()
38 WriteMapStart(length int)
39 WriteMapEnd()
40
41 // reset will reset current encoding runtime state, and cached information from the handle
42 reset()
43
44 encoder() *Encoder
45
46 driverStateManager
47 }
48
49 type encDriverContainerTracker interface {
50 WriteArrayElem()
51 WriteMapElemKey()
52 WriteMapElemValue()
53 }
54
55 type encDriverNoState struct{}
56
57 func (encDriverNoState) captureState() interface{} { return nil }
58 func (encDriverNoState) reset() {}
59 func (encDriverNoState) resetState() {}
60 func (encDriverNoState) restoreState(v interface{}) {}
61
62 type encDriverNoopContainerWriter struct{}
63
64 func (encDriverNoopContainerWriter) WriteArrayStart(length int) {}
65 func (encDriverNoopContainerWriter) WriteArrayEnd() {}
66 func (encDriverNoopContainerWriter) WriteMapStart(length int) {}
67 func (encDriverNoopContainerWriter) WriteMapEnd() {}
68
69 // encStructFieldObj[Slice] is used for sorting when there are missing fields and canonical flag is set
70 type encStructFieldObj struct {
71 key string
72 rv reflect.Value
73 intf interface{}
74 ascii bool
75 isRv bool
76 }
77
78 type encStructFieldObjSlice []encStructFieldObj
79
80 func (p encStructFieldObjSlice) Len() int { return len(p) }
81 func (p encStructFieldObjSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] }
82 func (p encStructFieldObjSlice) Less(i, j int) bool {
83 return p[uint(i)].key < p[uint(j)].key
84 }
85
86 // EncodeOptions captures configuration options during encode.
87 type EncodeOptions struct {
88 // WriterBufferSize is the size of the buffer used when writing.
89 //
90 // if > 0, we use a smart buffer internally for performance purposes.
91 WriterBufferSize int
92
93 // ChanRecvTimeout is the timeout used when selecting from a chan.
94 //
95 // Configuring this controls how we receive from a chan during the encoding process.
96 // - If ==0, we only consume the elements currently available in the chan.
97 // - if <0, we consume until the chan is closed.
98 // - If >0, we consume until this timeout.
99 ChanRecvTimeout time.Duration
100
101 // StructToArray specifies to encode a struct as an array, and not as a map
102 StructToArray bool
103
104 // Canonical representation means that encoding a value will always result in the same
105 // sequence of bytes.
106 //
107 // This only affects maps, as the iteration order for maps is random.
108 //
109 // The implementation MAY use the natural sort order for the map keys if possible:
110 //
111 // - If there is a natural sort order (ie for number, bool, string or []byte keys),
112 // then the map keys are first sorted in natural order and then written
113 // with corresponding map values to the strema.
114 // - If there is no natural sort order, then the map keys will first be
115 // encoded into []byte, and then sorted,
116 // before writing the sorted keys and the corresponding map values to the stream.
117 //
118 Canonical bool
119
120 // CheckCircularRef controls whether we check for circular references
121 // and error fast during an encode.
122 //
123 // If enabled, an error is received if a pointer to a struct
124 // references itself either directly or through one of its fields (iteratively).
125 //
126 // This is opt-in, as there may be a performance hit to checking circular references.
127 CheckCircularRef bool
128
129 // RecursiveEmptyCheck controls how we determine whether a value is empty.
130 //
131 // If true, we descend into interfaces and pointers to reursively check if value is empty.
132 //
133 // We *might* check struct fields one by one to see if empty
134 // (if we cannot directly check if a struct value is equal to its zero value).
135 // If so, we honor IsZero, Comparable, IsCodecEmpty(), etc.
136 // Note: This *may* make OmitEmpty more expensive due to the large number of reflect calls.
137 //
138 // If false, we check if the value is equal to its zero value (newly allocated state).
139 RecursiveEmptyCheck bool
140
141 // Raw controls whether we encode Raw values.
142 // This is a "dangerous" option and must be explicitly set.
143 // If set, we blindly encode Raw values as-is, without checking
144 // if they are a correct representation of a value in that format.
145 // If unset, we error out.
146 Raw bool
147
148 // StringToRaw controls how strings are encoded.
149 //
150 // As a go string is just an (immutable) sequence of bytes,
151 // it can be encoded either as raw bytes or as a UTF string.
152 //
153 // By default, strings are encoded as UTF-8.
154 // but can be treated as []byte during an encode.
155 //
156 // Note that things which we know (by definition) to be UTF-8
157 // are ALWAYS encoded as UTF-8 strings.
158 // These include encoding.TextMarshaler, time.Format calls, struct field names, etc.
159 StringToRaw bool
160
161 // OptimumSize controls whether we optimize for the smallest size.
162 //
163 // Some formats will use this flag to determine whether to encode
164 // in the smallest size possible, even if it takes slightly longer.
165 //
166 // For example, some formats that support half-floats might check if it is possible
167 // to store a float64 as a half float. Doing this check has a small performance cost,
168 // but the benefit is that the encoded message will be smaller.
169 OptimumSize bool
170
171 // NoAddressableReadonly controls whether we try to force a non-addressable value
172 // to be addressable so we can call a pointer method on it e.g. for types
173 // that support Selfer, json.Marshaler, etc.
174 //
175 // Use it in the very rare occurrence that your types modify a pointer value when calling
176 // an encode callback function e.g. JsonMarshal, TextMarshal, BinaryMarshal or CodecEncodeSelf.
177 NoAddressableReadonly bool
178 }
179
180 // ---------------------------------------------
181
182 func (e *Encoder) rawExt(f *codecFnInfo, rv reflect.Value) {
183 e.e.EncodeRawExt(rv2i(rv).(*RawExt))
184 }
185
186 func (e *Encoder) ext(f *codecFnInfo, rv reflect.Value) {
187 e.e.EncodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn)
188 }
189
190 func (e *Encoder) selferMarshal(f *codecFnInfo, rv reflect.Value) {
191 rv2i(rv).(Selfer).CodecEncodeSelf(e)
192 }
193
194 func (e *Encoder) binaryMarshal(f *codecFnInfo, rv reflect.Value) {
195 bs, fnerr := rv2i(rv).(encoding.BinaryMarshaler).MarshalBinary()
196 e.marshalRaw(bs, fnerr)
197 }
198
199 func (e *Encoder) textMarshal(f *codecFnInfo, rv reflect.Value) {
200 bs, fnerr := rv2i(rv).(encoding.TextMarshaler).MarshalText()
201 e.marshalUtf8(bs, fnerr)
202 }
203
204 func (e *Encoder) jsonMarshal(f *codecFnInfo, rv reflect.Value) {
205 bs, fnerr := rv2i(rv).(jsonMarshaler).MarshalJSON()
206 e.marshalAsis(bs, fnerr)
207 }
208
209 func (e *Encoder) raw(f *codecFnInfo, rv reflect.Value) {
210 e.rawBytes(rv2i(rv).(Raw))
211 }
212
213 func (e *Encoder) encodeComplex64(v complex64) {
214 if imag(v) != 0 {
215 e.errorf("cannot encode complex number: %v, with imaginary values: %v", v, imag(v))
216 }
217 e.e.EncodeFloat32(real(v))
218 }
219
220 func (e *Encoder) encodeComplex128(v complex128) {
221 if imag(v) != 0 {
222 e.errorf("cannot encode complex number: %v, with imaginary values: %v", v, imag(v))
223 }
224 e.e.EncodeFloat64(real(v))
225 }
226
227 func (e *Encoder) kBool(f *codecFnInfo, rv reflect.Value) {
228 e.e.EncodeBool(rvGetBool(rv))
229 }
230
231 func (e *Encoder) kTime(f *codecFnInfo, rv reflect.Value) {
232 e.e.EncodeTime(rvGetTime(rv))
233 }
234
235 func (e *Encoder) kString(f *codecFnInfo, rv reflect.Value) {
236 e.e.EncodeString(rvGetString(rv))
237 }
238
239 func (e *Encoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
240 e.e.EncodeFloat32(rvGetFloat32(rv))
241 }
242
243 func (e *Encoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
244 e.e.EncodeFloat64(rvGetFloat64(rv))
245 }
246
247 func (e *Encoder) kComplex64(f *codecFnInfo, rv reflect.Value) {
248 e.encodeComplex64(rvGetComplex64(rv))
249 }
250
251 func (e *Encoder) kComplex128(f *codecFnInfo, rv reflect.Value) {
252 e.encodeComplex128(rvGetComplex128(rv))
253 }
254
255 func (e *Encoder) kInt(f *codecFnInfo, rv reflect.Value) {
256 e.e.EncodeInt(int64(rvGetInt(rv)))
257 }
258
259 func (e *Encoder) kInt8(f *codecFnInfo, rv reflect.Value) {
260 e.e.EncodeInt(int64(rvGetInt8(rv)))
261 }
262
263 func (e *Encoder) kInt16(f *codecFnInfo, rv reflect.Value) {
264 e.e.EncodeInt(int64(rvGetInt16(rv)))
265 }
266
267 func (e *Encoder) kInt32(f *codecFnInfo, rv reflect.Value) {
268 e.e.EncodeInt(int64(rvGetInt32(rv)))
269 }
270
271 func (e *Encoder) kInt64(f *codecFnInfo, rv reflect.Value) {
272 e.e.EncodeInt(int64(rvGetInt64(rv)))
273 }
274
275 func (e *Encoder) kUint(f *codecFnInfo, rv reflect.Value) {
276 e.e.EncodeUint(uint64(rvGetUint(rv)))
277 }
278
279 func (e *Encoder) kUint8(f *codecFnInfo, rv reflect.Value) {
280 e.e.EncodeUint(uint64(rvGetUint8(rv)))
281 }
282
283 func (e *Encoder) kUint16(f *codecFnInfo, rv reflect.Value) {
284 e.e.EncodeUint(uint64(rvGetUint16(rv)))
285 }
286
287 func (e *Encoder) kUint32(f *codecFnInfo, rv reflect.Value) {
288 e.e.EncodeUint(uint64(rvGetUint32(rv)))
289 }
290
291 func (e *Encoder) kUint64(f *codecFnInfo, rv reflect.Value) {
292 e.e.EncodeUint(uint64(rvGetUint64(rv)))
293 }
294
295 func (e *Encoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
296 e.e.EncodeUint(uint64(rvGetUintptr(rv)))
297 }
298
299 func (e *Encoder) kErr(f *codecFnInfo, rv reflect.Value) {
300 e.errorf("unsupported kind %s, for %#v", rv.Kind(), rv)
301 }
302
303 func chanToSlice(rv reflect.Value, rtslice reflect.Type, timeout time.Duration) (rvcs reflect.Value) {
304 rvcs = rvZeroK(rtslice, reflect.Slice)
305 if timeout < 0 { // consume until close
306 for {
307 recv, recvOk := rv.Recv()
308 if !recvOk {
309 break
310 }
311 rvcs = reflect.Append(rvcs, recv)
312 }
313 } else {
314 cases := make([]reflect.SelectCase, 2)
315 cases[0] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: rv}
316 if timeout == 0 {
317 cases[1] = reflect.SelectCase{Dir: reflect.SelectDefault}
318 } else {
319 tt := time.NewTimer(timeout)
320 cases[1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(tt.C)}
321 }
322 for {
323 chosen, recv, recvOk := reflect.Select(cases)
324 if chosen == 1 || !recvOk {
325 break
326 }
327 rvcs = reflect.Append(rvcs, recv)
328 }
329 }
330 return
331 }
332
333 func (e *Encoder) kSeqFn(rtelem reflect.Type) (fn *codecFn) {
334 for rtelem.Kind() == reflect.Ptr {
335 rtelem = rtelem.Elem()
336 }
337 // if kind is reflect.Interface, do not pre-determine the encoding type,
338 // because preEncodeValue may break it down to a concrete type and kInterface will bomb.
339 if rtelem.Kind() != reflect.Interface {
340 fn = e.h.fn(rtelem)
341 }
342 return
343 }
344
345 func (e *Encoder) kSliceWMbs(rv reflect.Value, ti *typeInfo) {
346 var l = rvLenSlice(rv)
347 if l == 0 {
348 e.mapStart(0)
349 } else {
350 e.haltOnMbsOddLen(l)
351 e.mapStart(l >> 1) // e.mapStart(l / 2)
352 fn := e.kSeqFn(ti.elem)
353 for j := 0; j < l; j++ {
354 if j&1 == 0 { // j%2 == 0 {
355 e.mapElemKey()
356 } else {
357 e.mapElemValue()
358 }
359 e.encodeValue(rvSliceIndex(rv, j, ti), fn)
360 }
361 }
362 e.mapEnd()
363 }
364
365 func (e *Encoder) kSliceW(rv reflect.Value, ti *typeInfo) {
366 var l = rvLenSlice(rv)
367 e.arrayStart(l)
368 if l > 0 {
369 fn := e.kSeqFn(ti.elem)
370 for j := 0; j < l; j++ {
371 e.arrayElem()
372 e.encodeValue(rvSliceIndex(rv, j, ti), fn)
373 }
374 }
375 e.arrayEnd()
376 }
377
378 func (e *Encoder) kArrayWMbs(rv reflect.Value, ti *typeInfo) {
379 var l = rv.Len()
380 if l == 0 {
381 e.mapStart(0)
382 } else {
383 e.haltOnMbsOddLen(l)
384 e.mapStart(l >> 1) // e.mapStart(l / 2)
385 fn := e.kSeqFn(ti.elem)
386 for j := 0; j < l; j++ {
387 if j&1 == 0 { // j%2 == 0 {
388 e.mapElemKey()
389 } else {
390 e.mapElemValue()
391 }
392 e.encodeValue(rv.Index(j), fn)
393 }
394 }
395 e.mapEnd()
396 }
397
398 func (e *Encoder) kArrayW(rv reflect.Value, ti *typeInfo) {
399 var l = rv.Len()
400 e.arrayStart(l)
401 if l > 0 {
402 fn := e.kSeqFn(ti.elem)
403 for j := 0; j < l; j++ {
404 e.arrayElem()
405 e.encodeValue(rv.Index(j), fn)
406 }
407 }
408 e.arrayEnd()
409 }
410
411 func (e *Encoder) kChan(f *codecFnInfo, rv reflect.Value) {
412 if f.ti.chandir&uint8(reflect.RecvDir) == 0 {
413 e.errorf("send-only channel cannot be encoded")
414 }
415 if !f.ti.mbs && uint8TypId == rt2id(f.ti.elem) {
416 e.kSliceBytesChan(rv)
417 return
418 }
419 rtslice := reflect.SliceOf(f.ti.elem)
420 rv = chanToSlice(rv, rtslice, e.h.ChanRecvTimeout)
421 ti := e.h.getTypeInfo(rt2id(rtslice), rtslice)
422 if f.ti.mbs {
423 e.kSliceWMbs(rv, ti)
424 } else {
425 e.kSliceW(rv, ti)
426 }
427 }
428
429 func (e *Encoder) kSlice(f *codecFnInfo, rv reflect.Value) {
430 if f.ti.mbs {
431 e.kSliceWMbs(rv, f.ti)
432 } else if f.ti.rtid == uint8SliceTypId || uint8TypId == rt2id(f.ti.elem) {
433 e.e.EncodeStringBytesRaw(rvGetBytes(rv))
434 } else {
435 e.kSliceW(rv, f.ti)
436 }
437 }
438
439 func (e *Encoder) kArray(f *codecFnInfo, rv reflect.Value) {
440 if f.ti.mbs {
441 e.kArrayWMbs(rv, f.ti)
442 } else if handleBytesWithinKArray && uint8TypId == rt2id(f.ti.elem) {
443 e.e.EncodeStringBytesRaw(rvGetArrayBytes(rv, []byte{}))
444 } else {
445 e.kArrayW(rv, f.ti)
446 }
447 }
448
449 func (e *Encoder) kSliceBytesChan(rv reflect.Value) {
450 // do not use range, so that the number of elements encoded
451 // does not change, and encoding does not hang waiting on someone to close chan.
452
453 bs0 := e.blist.peek(32, true)
454 bs := bs0
455
456 irv := rv2i(rv)
457 ch, ok := irv.(<-chan byte)
458 if !ok {
459 ch = irv.(chan byte)
460 }
461
462 L1:
463 switch timeout := e.h.ChanRecvTimeout; {
464 case timeout == 0: // only consume available
465 for {
466 select {
467 case b := <-ch:
468 bs = append(bs, b)
469 default:
470 break L1
471 }
472 }
473 case timeout > 0: // consume until timeout
474 tt := time.NewTimer(timeout)
475 for {
476 select {
477 case b := <-ch:
478 bs = append(bs, b)
479 case <-tt.C:
480 // close(tt.C)
481 break L1
482 }
483 }
484 default: // consume until close
485 for b := range ch {
486 bs = append(bs, b)
487 }
488 }
489
490 e.e.EncodeStringBytesRaw(bs)
491 e.blist.put(bs)
492 if !byteSliceSameData(bs0, bs) {
493 e.blist.put(bs0)
494 }
495 }
496
497 func (e *Encoder) kStructSfi(f *codecFnInfo) []*structFieldInfo {
498 if e.h.Canonical {
499 return f.ti.sfi.sorted()
500 }
501 return f.ti.sfi.source()
502 }
503
504 func (e *Encoder) kStructNoOmitempty(f *codecFnInfo, rv reflect.Value) {
505 var tisfi []*structFieldInfo
506 if f.ti.toArray || e.h.StructToArray { // toArray
507 tisfi = f.ti.sfi.source()
508 e.arrayStart(len(tisfi))
509 for _, si := range tisfi {
510 e.arrayElem()
511 e.encodeValue(si.path.field(rv), nil)
512 }
513 e.arrayEnd()
514 } else {
515 tisfi = e.kStructSfi(f)
516 e.mapStart(len(tisfi))
517 keytyp := f.ti.keyType
518 for _, si := range tisfi {
519 e.mapElemKey()
520 e.kStructFieldKey(keytyp, si.path.encNameAsciiAlphaNum, si.encName)
521 e.mapElemValue()
522 e.encodeValue(si.path.field(rv), nil)
523 }
524 e.mapEnd()
525 }
526 }
527
528 func (e *Encoder) kStructFieldKey(keyType valueType, encNameAsciiAlphaNum bool, encName string) {
529 encStructFieldKey(encName, e.e, e.w(), keyType, encNameAsciiAlphaNum, e.js)
530 }
531
532 func (e *Encoder) kStruct(f *codecFnInfo, rv reflect.Value) {
533 var newlen int
534 ti := f.ti
535 toMap := !(ti.toArray || e.h.StructToArray)
536 var mf map[string]interface{}
537 if ti.flagMissingFielder {
538 mf = rv2i(rv).(MissingFielder).CodecMissingFields()
539 toMap = true
540 newlen += len(mf)
541 } else if ti.flagMissingFielderPtr {
542 rv2 := e.addrRV(rv, ti.rt, ti.ptr)
543 mf = rv2i(rv2).(MissingFielder).CodecMissingFields()
544 toMap = true
545 newlen += len(mf)
546 }
547 tisfi := ti.sfi.source()
548 newlen += len(tisfi)
549
550 var fkvs = e.slist.get(newlen)[:newlen]
551
552 recur := e.h.RecursiveEmptyCheck
553
554 var kv sfiRv
555 var j int
556 if toMap {
557 newlen = 0
558 for _, si := range e.kStructSfi(f) {
559 kv.r = si.path.field(rv)
560 if si.path.omitEmpty && isEmptyValue(kv.r, e.h.TypeInfos, recur) {
561 continue
562 }
563 kv.v = si
564 fkvs[newlen] = kv
565 newlen++
566 }
567
568 var mf2s []stringIntf
569 if len(mf) > 0 {
570 mf2s = make([]stringIntf, 0, len(mf))
571 for k, v := range mf {
572 if k == "" {
573 continue
574 }
575 if ti.infoFieldOmitempty && isEmptyValue(reflect.ValueOf(v), e.h.TypeInfos, recur) {
576 continue
577 }
578 mf2s = append(mf2s, stringIntf{k, v})
579 }
580 }
581
582 e.mapStart(newlen + len(mf2s))
583
584 // When there are missing fields, and Canonical flag is set,
585 // we cannot have the missing fields and struct fields sorted independently.
586 // We have to capture them together and sort as a unit.
587
588 if len(mf2s) > 0 && e.h.Canonical {
589 mf2w := make([]encStructFieldObj, newlen+len(mf2s))
590 for j = 0; j < newlen; j++ {
591 kv = fkvs[j]
592 mf2w[j] = encStructFieldObj{kv.v.encName, kv.r, nil, kv.v.path.encNameAsciiAlphaNum, true}
593 }
594 for _, v := range mf2s {
595 mf2w[j] = encStructFieldObj{v.v, reflect.Value{}, v.i, false, false}
596 j++
597 }
598 sort.Sort((encStructFieldObjSlice)(mf2w))
599 for _, v := range mf2w {
600 e.mapElemKey()
601 e.kStructFieldKey(ti.keyType, v.ascii, v.key)
602 e.mapElemValue()
603 if v.isRv {
604 e.encodeValue(v.rv, nil)
605 } else {
606 e.encode(v.intf)
607 }
608 }
609 } else {
610 keytyp := ti.keyType
611 for j = 0; j < newlen; j++ {
612 kv = fkvs[j]
613 e.mapElemKey()
614 e.kStructFieldKey(keytyp, kv.v.path.encNameAsciiAlphaNum, kv.v.encName)
615 e.mapElemValue()
616 e.encodeValue(kv.r, nil)
617 }
618 for _, v := range mf2s {
619 e.mapElemKey()
620 e.kStructFieldKey(keytyp, false, v.v)
621 e.mapElemValue()
622 e.encode(v.i)
623 }
624 }
625
626 e.mapEnd()
627 } else {
628 newlen = len(tisfi)
629 for i, si := range tisfi { // use unsorted array (to match sequence in struct)
630 kv.r = si.path.field(rv)
631 // use the zero value.
632 // if a reference or struct, set to nil (so you do not output too much)
633 if si.path.omitEmpty && isEmptyValue(kv.r, e.h.TypeInfos, recur) {
634 switch kv.r.Kind() {
635 case reflect.Struct, reflect.Interface, reflect.Ptr, reflect.Array, reflect.Map, reflect.Slice:
636 kv.r = reflect.Value{} //encode as nil
637 }
638 }
639 fkvs[i] = kv
640 }
641 // encode it all
642 e.arrayStart(newlen)
643 for j = 0; j < newlen; j++ {
644 e.arrayElem()
645 e.encodeValue(fkvs[j].r, nil)
646 }
647 e.arrayEnd()
648 }
649
650 // do not use defer. Instead, use explicit pool return at end of function.
651 // defer has a cost we are trying to avoid.
652 // If there is a panic and these slices are not returned, it is ok.
653 e.slist.put(fkvs)
654 }
655
656 func (e *Encoder) kMap(f *codecFnInfo, rv reflect.Value) {
657 l := rvLenMap(rv)
658 e.mapStart(l)
659 if l == 0 {
660 e.mapEnd()
661 return
662 }
663
664 // determine the underlying key and val encFn's for the map.
665 // This eliminates some work which is done for each loop iteration i.e.
666 // rv.Type(), ref.ValueOf(rt).Pointer(), then check map/list for fn.
667 //
668 // However, if kind is reflect.Interface, do not pre-determine the
669 // encoding type, because preEncodeValue may break it down to
670 // a concrete type and kInterface will bomb.
671
672 var keyFn, valFn *codecFn
673
674 ktypeKind := reflect.Kind(f.ti.keykind)
675 vtypeKind := reflect.Kind(f.ti.elemkind)
676
677 rtval := f.ti.elem
678 rtvalkind := vtypeKind
679 for rtvalkind == reflect.Ptr {
680 rtval = rtval.Elem()
681 rtvalkind = rtval.Kind()
682 }
683 if rtvalkind != reflect.Interface {
684 valFn = e.h.fn(rtval)
685 }
686
687 var rvv = mapAddrLoopvarRV(f.ti.elem, vtypeKind)
688
689 rtkey := f.ti.key
690 var keyTypeIsString = stringTypId == rt2id(rtkey) // rtkeyid
691 if keyTypeIsString {
692 keyFn = e.h.fn(rtkey)
693 } else {
694 for rtkey.Kind() == reflect.Ptr {
695 rtkey = rtkey.Elem()
696 }
697 if rtkey.Kind() != reflect.Interface {
698 keyFn = e.h.fn(rtkey)
699 }
700 }
701
702 if e.h.Canonical {
703 e.kMapCanonical(f.ti, rv, rvv, keyFn, valFn)
704 e.mapEnd()
705 return
706 }
707
708 var rvk = mapAddrLoopvarRV(f.ti.key, ktypeKind)
709
710 var it mapIter
711 mapRange(&it, rv, rvk, rvv, true)
712
713 for it.Next() {
714 e.mapElemKey()
715 if keyTypeIsString {
716 e.e.EncodeString(it.Key().String())
717 } else {
718 e.encodeValue(it.Key(), keyFn)
719 }
720 e.mapElemValue()
721 e.encodeValue(it.Value(), valFn)
722 }
723 it.Done()
724
725 e.mapEnd()
726 }
727
728 func (e *Encoder) kMapCanonical(ti *typeInfo, rv, rvv reflect.Value, keyFn, valFn *codecFn) {
729 // The base kind of the type of the map key is sufficient for ordering.
730 // We only do out of band if that kind is not ordered (number or string), bool or time.Time.
731 // If the key is a predeclared type, directly call methods on encDriver e.g. EncodeString
732 // but if not, call encodeValue, in case it has an extension registered or otherwise.
733 rtkey := ti.key
734 rtkeydecl := rtkey.PkgPath() == "" && rtkey.Name() != "" // key type is predeclared
735
736 mks := rv.MapKeys()
737 rtkeyKind := rtkey.Kind()
738 kfast := mapKeyFastKindFor(rtkeyKind)
739 visindirect := mapStoresElemIndirect(uintptr(ti.elemsize))
740 visref := refBitset.isset(ti.elemkind)
741
742 switch rtkeyKind {
743 case reflect.Bool:
744 // though bool keys make no sense in a map, it *could* happen.
745 // in that case, we MUST support it in reflection mode,
746 // as that is the fallback for even codecgen and others.
747
748 // sort the keys so that false comes before true
749 // ie if 2 keys in order (true, false), then swap them
750 if len(mks) == 2 && mks[0].Bool() {
751 mks[0], mks[1] = mks[1], mks[0]
752 }
753 for i := range mks {
754 e.mapElemKey()
755 if rtkeydecl {
756 e.e.EncodeBool(mks[i].Bool())
757 } else {
758 e.encodeValueNonNil(mks[i], keyFn)
759 }
760 e.mapElemValue()
761 e.encodeValue(mapGet(rv, mks[i], rvv, kfast, visindirect, visref), valFn)
762 }
763 case reflect.String:
764 mksv := make([]stringRv, len(mks))
765 for i, k := range mks {
766 v := &mksv[i]
767 v.r = k
768 v.v = k.String()
769 }
770 sort.Sort(stringRvSlice(mksv))
771 for i := range mksv {
772 e.mapElemKey()
773 if rtkeydecl {
774 e.e.EncodeString(mksv[i].v)
775 } else {
776 e.encodeValueNonNil(mksv[i].r, keyFn)
777 }
778 e.mapElemValue()
779 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
780 }
781 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr:
782 mksv := make([]uint64Rv, len(mks))
783 for i, k := range mks {
784 v := &mksv[i]
785 v.r = k
786 v.v = k.Uint()
787 }
788 sort.Sort(uint64RvSlice(mksv))
789 for i := range mksv {
790 e.mapElemKey()
791 if rtkeydecl {
792 e.e.EncodeUint(mksv[i].v)
793 } else {
794 e.encodeValueNonNil(mksv[i].r, keyFn)
795 }
796 e.mapElemValue()
797 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
798 }
799 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
800 mksv := make([]int64Rv, len(mks))
801 for i, k := range mks {
802 v := &mksv[i]
803 v.r = k
804 v.v = k.Int()
805 }
806 sort.Sort(int64RvSlice(mksv))
807 for i := range mksv {
808 e.mapElemKey()
809 if rtkeydecl {
810 e.e.EncodeInt(mksv[i].v)
811 } else {
812 e.encodeValueNonNil(mksv[i].r, keyFn)
813 }
814 e.mapElemValue()
815 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
816 }
817 case reflect.Float32:
818 mksv := make([]float64Rv, len(mks))
819 for i, k := range mks {
820 v := &mksv[i]
821 v.r = k
822 v.v = k.Float()
823 }
824 sort.Sort(float64RvSlice(mksv))
825 for i := range mksv {
826 e.mapElemKey()
827 if rtkeydecl {
828 e.e.EncodeFloat32(float32(mksv[i].v))
829 } else {
830 e.encodeValueNonNil(mksv[i].r, keyFn)
831 }
832 e.mapElemValue()
833 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
834 }
835 case reflect.Float64:
836 mksv := make([]float64Rv, len(mks))
837 for i, k := range mks {
838 v := &mksv[i]
839 v.r = k
840 v.v = k.Float()
841 }
842 sort.Sort(float64RvSlice(mksv))
843 for i := range mksv {
844 e.mapElemKey()
845 if rtkeydecl {
846 e.e.EncodeFloat64(mksv[i].v)
847 } else {
848 e.encodeValueNonNil(mksv[i].r, keyFn)
849 }
850 e.mapElemValue()
851 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
852 }
853 default:
854 if rtkey == timeTyp {
855 mksv := make([]timeRv, len(mks))
856 for i, k := range mks {
857 v := &mksv[i]
858 v.r = k
859 v.v = rv2i(k).(time.Time)
860 }
861 sort.Sort(timeRvSlice(mksv))
862 for i := range mksv {
863 e.mapElemKey()
864 e.e.EncodeTime(mksv[i].v)
865 e.mapElemValue()
866 e.encodeValue(mapGet(rv, mksv[i].r, rvv, kfast, visindirect, visref), valFn)
867 }
868 break
869 }
870
871 // out-of-band
872 // first encode each key to a []byte first, then sort them, then record
873 bs0 := e.blist.get(len(mks) * 16)
874 mksv := bs0
875 mksbv := make([]bytesRv, len(mks))
876
877 func() {
878 // replicate sideEncode logic
879 defer func(wb bytesEncAppender, bytes bool, c containerState, state interface{}) {
880 e.wb = wb
881 e.bytes = bytes
882 e.c = c
883 e.e.restoreState(state)
884 }(e.wb, e.bytes, e.c, e.e.captureState())
885
886 // e2 := NewEncoderBytes(&mksv, e.hh)
887 e.wb = bytesEncAppender{mksv[:0], &mksv}
888 e.bytes = true
889 e.c = 0
890 e.e.resetState()
891
892 for i, k := range mks {
893 v := &mksbv[i]
894 l := len(mksv)
895
896 e.encodeValue(k, nil)
897 e.atEndOfEncode()
898 e.w().end()
899
900 v.r = k
901 v.v = mksv[l:]
902 }
903 }()
904
905 sort.Sort(bytesRvSlice(mksbv))
906 for j := range mksbv {
907 e.mapElemKey()
908 e.encWr.writeb(mksbv[j].v)
909 e.mapElemValue()
910 e.encodeValue(mapGet(rv, mksbv[j].r, rvv, kfast, visindirect, visref), valFn)
911 }
912 e.blist.put(mksv)
913 if !byteSliceSameData(bs0, mksv) {
914 e.blist.put(bs0)
915 }
916 }
917 }
918
919 // Encoder writes an object to an output stream in a supported format.
920 //
921 // Encoder is NOT safe for concurrent use i.e. a Encoder cannot be used
922 // concurrently in multiple goroutines.
923 //
924 // However, as Encoder could be allocation heavy to initialize, a Reset method is provided
925 // so its state can be reused to decode new input streams repeatedly.
926 // This is the idiomatic way to use.
927 type Encoder struct {
928 panicHdl
929
930 e encDriver
931
932 h *BasicHandle
933
934 // hopefully, reduce derefencing cost by laying the encWriter inside the Encoder
935 encWr
936
937 // ---- cpu cache line boundary
938 hh Handle
939
940 blist bytesFreelist
941 err error
942
943 // ---- cpu cache line boundary
944
945 // ---- writable fields during execution --- *try* to keep in sep cache line
946
947 // ci holds interfaces during an encoding (if CheckCircularRef=true)
948 //
949 // We considered using a []uintptr (slice of pointer addresses) retrievable via rv.UnsafeAddr.
950 // However, it is possible for the same pointer to point to 2 different types e.g.
951 // type T struct { tHelper }
952 // Here, for var v T; &v and &v.tHelper are the same pointer.
953 // Consequently, we need a tuple of type and pointer, which interface{} natively provides.
954 ci []interface{} // []uintptr
955
956 perType encPerType
957
958 slist sfiRvFreelist
959 }
960
961 // NewEncoder returns an Encoder for encoding into an io.Writer.
962 //
963 // For efficiency, Users are encouraged to configure WriterBufferSize on the handle
964 // OR pass in a memory buffered writer (eg bufio.Writer, bytes.Buffer).
965 func NewEncoder(w io.Writer, h Handle) *Encoder {
966 e := h.newEncDriver().encoder()
967 if w != nil {
968 e.Reset(w)
969 }
970 return e
971 }
972
973 // NewEncoderBytes returns an encoder for encoding directly and efficiently
974 // into a byte slice, using zero-copying to temporary slices.
975 //
976 // It will potentially replace the output byte slice pointed to.
977 // After encoding, the out parameter contains the encoded contents.
978 func NewEncoderBytes(out *[]byte, h Handle) *Encoder {
979 e := h.newEncDriver().encoder()
980 if out != nil {
981 e.ResetBytes(out)
982 }
983 return e
984 }
985
986 func (e *Encoder) init(h Handle) {
987 initHandle(h)
988 e.err = errEncoderNotInitialized
989 e.bytes = true
990 e.hh = h
991 e.h = h.getBasicHandle()
992 e.be = e.hh.isBinary()
993 }
994
995 func (e *Encoder) w() *encWr {
996 return &e.encWr
997 }
998
999 func (e *Encoder) resetCommon() {
1000 e.e.reset()
1001 if e.ci != nil {
1002 e.ci = e.ci[:0]
1003 }
1004 e.c = 0
1005 e.calls = 0
1006 e.seq = 0
1007 e.err = nil
1008 }
1009
1010 // Reset resets the Encoder with a new output stream.
1011 //
1012 // This accommodates using the state of the Encoder,
1013 // where it has "cached" information about sub-engines.
1014 func (e *Encoder) Reset(w io.Writer) {
1015 e.bytes = false
1016 if e.wf == nil {
1017 e.wf = new(bufioEncWriter)
1018 }
1019 e.wf.reset(w, e.h.WriterBufferSize, &e.blist)
1020 e.resetCommon()
1021 }
1022
1023 // ResetBytes resets the Encoder with a new destination output []byte.
1024 func (e *Encoder) ResetBytes(out *[]byte) {
1025 e.bytes = true
1026 e.wb.reset(encInBytes(out), out)
1027 e.resetCommon()
1028 }
1029
1030 // Encode writes an object into a stream.
1031 //
1032 // Encoding can be configured via the struct tag for the fields.
1033 // The key (in the struct tags) that we look at is configurable.
1034 //
1035 // By default, we look up the "codec" key in the struct field's tags,
1036 // and fall bak to the "json" key if "codec" is absent.
1037 // That key in struct field's tag value is the key name,
1038 // followed by an optional comma and options.
1039 //
1040 // To set an option on all fields (e.g. omitempty on all fields), you
1041 // can create a field called _struct, and set flags on it. The options
1042 // which can be set on _struct are:
1043 // - omitempty: so all fields are omitted if empty
1044 // - toarray: so struct is encoded as an array
1045 // - int: so struct key names are encoded as signed integers (instead of strings)
1046 // - uint: so struct key names are encoded as unsigned integers (instead of strings)
1047 // - float: so struct key names are encoded as floats (instead of strings)
1048 //
1049 // More details on these below.
1050 //
1051 // Struct values "usually" encode as maps. Each exported struct field is encoded unless:
1052 // - the field's tag is "-", OR
1053 // - the field is empty (empty or the zero value) and its tag specifies the "omitempty" option.
1054 //
1055 // When encoding as a map, the first string in the tag (before the comma)
1056 // is the map key string to use when encoding.
1057 // ...
1058 // This key is typically encoded as a string.
1059 // However, there are instances where the encoded stream has mapping keys encoded as numbers.
1060 // For example, some cbor streams have keys as integer codes in the stream, but they should map
1061 // to fields in a structured object. Consequently, a struct is the natural representation in code.
1062 // For these, configure the struct to encode/decode the keys as numbers (instead of string).
1063 // This is done with the int,uint or float option on the _struct field (see above).
1064 //
1065 // However, struct values may encode as arrays. This happens when:
1066 // - StructToArray Encode option is set, OR
1067 // - the tag on the _struct field sets the "toarray" option
1068 //
1069 // Note that omitempty is ignored when encoding struct values as arrays,
1070 // as an entry must be encoded for each field, to maintain its position.
1071 //
1072 // Values with types that implement MapBySlice are encoded as stream maps.
1073 //
1074 // The empty values (for omitempty option) are false, 0, any nil pointer
1075 // or interface value, and any array, slice, map, or string of length zero.
1076 //
1077 // Anonymous fields are encoded inline except:
1078 // - the struct tag specifies a replacement name (first value)
1079 // - the field is of an interface type
1080 //
1081 // Examples:
1082 //
1083 // // NOTE: 'json:' can be used as struct tag key, in place 'codec:' below.
1084 // type MyStruct struct {
1085 // _struct bool `codec:",omitempty"` //set omitempty for every field
1086 // Field1 string `codec:"-"` //skip this field
1087 // Field2 int `codec:"myName"` //Use key "myName" in encode stream
1088 // Field3 int32 `codec:",omitempty"` //use key "Field3". Omit if empty.
1089 // Field4 bool `codec:"f4,omitempty"` //use key "f4". Omit if empty.
1090 // io.Reader //use key "Reader".
1091 // MyStruct `codec:"my1" //use key "my1".
1092 // MyStruct //inline it
1093 // ...
1094 // }
1095 //
1096 // type MyStruct struct {
1097 // _struct bool `codec:",toarray"` //encode struct as an array
1098 // }
1099 //
1100 // type MyStruct struct {
1101 // _struct bool `codec:",uint"` //encode struct with "unsigned integer" keys
1102 // Field1 string `codec:"1"` //encode Field1 key using: EncodeInt(1)
1103 // Field2 string `codec:"2"` //encode Field2 key using: EncodeInt(2)
1104 // }
1105 //
1106 // The mode of encoding is based on the type of the value. When a value is seen:
1107 // - If a Selfer, call its CodecEncodeSelf method
1108 // - If an extension is registered for it, call that extension function
1109 // - If implements encoding.(Binary|Text|JSON)Marshaler, call Marshal(Binary|Text|JSON) method
1110 // - Else encode it based on its reflect.Kind
1111 //
1112 // Note that struct field names and keys in map[string]XXX will be treated as symbols.
1113 // Some formats support symbols (e.g. binc) and will properly encode the string
1114 // only once in the stream, and use a tag to refer to it thereafter.
1115 func (e *Encoder) Encode(v interface{}) (err error) {
1116 // tried to use closure, as runtime optimizes defer with no params.
1117 // This seemed to be causing weird issues (like circular reference found, unexpected panic, etc).
1118 // Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139
1119 if !debugging {
1120 defer func() {
1121 // if error occurred during encoding, return that error;
1122 // else if error occurred on end'ing (i.e. during flush), return that error.
1123 if x := recover(); x != nil {
1124 panicValToErr(e, x, &e.err)
1125 err = e.err
1126 }
1127 }()
1128 }
1129
1130 e.MustEncode(v)
1131 return
1132 }
1133
1134 // MustEncode is like Encode, but panics if unable to Encode.
1135 //
1136 // Note: This provides insight to the code location that triggered the error.
1137 func (e *Encoder) MustEncode(v interface{}) {
1138 halt.onerror(e.err)
1139 if e.hh == nil {
1140 halt.onerror(errNoFormatHandle)
1141 }
1142
1143 e.calls++
1144 e.encode(v)
1145 e.calls--
1146 if e.calls == 0 {
1147 e.atEndOfEncode()
1148 e.w().end()
1149 }
1150 }
1151
1152 // Release releases shared (pooled) resources.
1153 //
1154 // It is important to call Release() when done with an Encoder, so those resources
1155 // are released instantly for use by subsequently created Encoders.
1156 //
1157 // Deprecated: Release is a no-op as pooled resources are not used with an Encoder.
1158 // This method is kept for compatibility reasons only.
1159 func (e *Encoder) Release() {
1160 }
1161
1162 func (e *Encoder) encode(iv interface{}) {
1163 // MARKER: a switch with only concrete types can be optimized.
1164 // consequently, we deal with nil and interfaces outside the switch.
1165
1166 if iv == nil {
1167 e.e.EncodeNil()
1168 return
1169 }
1170
1171 rv, ok := isNil(iv)
1172 if ok {
1173 e.e.EncodeNil()
1174 return
1175 }
1176
1177 switch v := iv.(type) {
1178 // case nil:
1179 // case Selfer:
1180 case Raw:
1181 e.rawBytes(v)
1182 case reflect.Value:
1183 e.encodeValue(v, nil)
1184
1185 case string:
1186 e.e.EncodeString(v)
1187 case bool:
1188 e.e.EncodeBool(v)
1189 case int:
1190 e.e.EncodeInt(int64(v))
1191 case int8:
1192 e.e.EncodeInt(int64(v))
1193 case int16:
1194 e.e.EncodeInt(int64(v))
1195 case int32:
1196 e.e.EncodeInt(int64(v))
1197 case int64:
1198 e.e.EncodeInt(v)
1199 case uint:
1200 e.e.EncodeUint(uint64(v))
1201 case uint8:
1202 e.e.EncodeUint(uint64(v))
1203 case uint16:
1204 e.e.EncodeUint(uint64(v))
1205 case uint32:
1206 e.e.EncodeUint(uint64(v))
1207 case uint64:
1208 e.e.EncodeUint(v)
1209 case uintptr:
1210 e.e.EncodeUint(uint64(v))
1211 case float32:
1212 e.e.EncodeFloat32(v)
1213 case float64:
1214 e.e.EncodeFloat64(v)
1215 case complex64:
1216 e.encodeComplex64(v)
1217 case complex128:
1218 e.encodeComplex128(v)
1219 case time.Time:
1220 e.e.EncodeTime(v)
1221 case []byte:
1222 e.e.EncodeStringBytesRaw(v)
1223 case *Raw:
1224 e.rawBytes(*v)
1225 case *string:
1226 e.e.EncodeString(*v)
1227 case *bool:
1228 e.e.EncodeBool(*v)
1229 case *int:
1230 e.e.EncodeInt(int64(*v))
1231 case *int8:
1232 e.e.EncodeInt(int64(*v))
1233 case *int16:
1234 e.e.EncodeInt(int64(*v))
1235 case *int32:
1236 e.e.EncodeInt(int64(*v))
1237 case *int64:
1238 e.e.EncodeInt(*v)
1239 case *uint:
1240 e.e.EncodeUint(uint64(*v))
1241 case *uint8:
1242 e.e.EncodeUint(uint64(*v))
1243 case *uint16:
1244 e.e.EncodeUint(uint64(*v))
1245 case *uint32:
1246 e.e.EncodeUint(uint64(*v))
1247 case *uint64:
1248 e.e.EncodeUint(*v)
1249 case *uintptr:
1250 e.e.EncodeUint(uint64(*v))
1251 case *float32:
1252 e.e.EncodeFloat32(*v)
1253 case *float64:
1254 e.e.EncodeFloat64(*v)
1255 case *complex64:
1256 e.encodeComplex64(*v)
1257 case *complex128:
1258 e.encodeComplex128(*v)
1259 case *time.Time:
1260 e.e.EncodeTime(*v)
1261 case *[]byte:
1262 if *v == nil {
1263 e.e.EncodeNil()
1264 } else {
1265 e.e.EncodeStringBytesRaw(*v)
1266 }
1267 default:
1268 // we can't check non-predefined types, as they might be a Selfer or extension.
1269 if skipFastpathTypeSwitchInDirectCall || !fastpathEncodeTypeSwitch(iv, e) {
1270 e.encodeValue(rv, nil)
1271 }
1272 }
1273 }
1274
1275 // encodeValue will encode a value.
1276 //
1277 // Note that encodeValue will handle nil in the stream early, so that the
1278 // subsequent calls i.e. kXXX methods, etc do not have to handle it themselves.
1279 func (e *Encoder) encodeValue(rv reflect.Value, fn *codecFn) {
1280 // if a valid fn is passed, it MUST BE for the dereferenced type of rv
1281
1282 // MARKER: We check if value is nil here, so that the kXXX method do not have to.
1283
1284 var sptr interface{}
1285 var rvp reflect.Value
1286 var rvpValid bool
1287 TOP:
1288 switch rv.Kind() {
1289 case reflect.Ptr:
1290 if rvIsNil(rv) {
1291 e.e.EncodeNil()
1292 return
1293 }
1294 rvpValid = true
1295 rvp = rv
1296 rv = rv.Elem()
1297 goto TOP
1298 case reflect.Interface:
1299 if rvIsNil(rv) {
1300 e.e.EncodeNil()
1301 return
1302 }
1303 rvpValid = false
1304 rvp = reflect.Value{}
1305 rv = rv.Elem()
1306 goto TOP
1307 case reflect.Struct:
1308 if rvpValid && e.h.CheckCircularRef {
1309 sptr = rv2i(rvp)
1310 for _, vv := range e.ci {
1311 if eq4i(sptr, vv) { // error if sptr already seen
1312 e.errorf("circular reference found: %p, %T", sptr, sptr)
1313 }
1314 }
1315 e.ci = append(e.ci, sptr)
1316 }
1317 case reflect.Slice, reflect.Map, reflect.Chan:
1318 if rvIsNil(rv) {
1319 e.e.EncodeNil()
1320 return
1321 }
1322 case reflect.Invalid, reflect.Func:
1323 e.e.EncodeNil()
1324 return
1325 }
1326
1327 if fn == nil {
1328 fn = e.h.fn(rvType(rv))
1329 }
1330
1331 if !fn.i.addrE { // typically, addrE = false, so check it first
1332 // keep rv same
1333 } else if rvpValid {
1334 rv = rvp
1335 } else {
1336 rv = e.addrRV(rv, fn.i.ti.rt, fn.i.ti.ptr)
1337 }
1338 fn.fe(e, &fn.i, rv)
1339
1340 if sptr != nil { // remove sptr
1341 e.ci = e.ci[:len(e.ci)-1]
1342 }
1343 }
1344
1345 // encodeValueNonNil can encode a number, bool, or string
1346 // OR non-nil values of kind map, slice and chan.
1347 func (e *Encoder) encodeValueNonNil(rv reflect.Value, fn *codecFn) {
1348 if fn == nil {
1349 fn = e.h.fn(rvType(rv))
1350 }
1351
1352 if fn.i.addrE { // typically, addrE = false, so check it first
1353 rv = e.addrRV(rv, fn.i.ti.rt, fn.i.ti.ptr)
1354 }
1355 fn.fe(e, &fn.i, rv)
1356 }
1357
1358 // addrRV returns a addressable value which may be readonly
1359 func (e *Encoder) addrRV(rv reflect.Value, typ, ptrType reflect.Type) (rva reflect.Value) {
1360 if rv.CanAddr() {
1361 return rvAddr(rv, ptrType)
1362 }
1363 if e.h.NoAddressableReadonly {
1364 rva = reflect.New(typ)
1365 rvSetDirect(rva.Elem(), rv)
1366 return
1367 }
1368 return rvAddr(e.perType.AddressableRO(rv), ptrType)
1369 }
1370
1371 func (e *Encoder) marshalUtf8(bs []byte, fnerr error) {
1372 e.onerror(fnerr)
1373 if bs == nil {
1374 e.e.EncodeNil()
1375 } else {
1376 e.e.EncodeString(stringView(bs))
1377 }
1378 }
1379
1380 func (e *Encoder) marshalAsis(bs []byte, fnerr error) {
1381 e.onerror(fnerr)
1382 if bs == nil {
1383 e.e.EncodeNil()
1384 } else {
1385 e.encWr.writeb(bs) // e.asis(bs)
1386 }
1387 }
1388
1389 func (e *Encoder) marshalRaw(bs []byte, fnerr error) {
1390 e.onerror(fnerr)
1391 if bs == nil {
1392 e.e.EncodeNil()
1393 } else {
1394 e.e.EncodeStringBytesRaw(bs)
1395 }
1396 }
1397
1398 func (e *Encoder) rawBytes(vv Raw) {
1399 v := []byte(vv)
1400 if !e.h.Raw {
1401 e.errorf("Raw values cannot be encoded: %v", v)
1402 }
1403 e.encWr.writeb(v)
1404 }
1405
1406 func (e *Encoder) wrapErr(v error, err *error) {
1407 *err = wrapCodecErr(v, e.hh.Name(), 0, true)
1408 }
1409
1410 // ---- container tracker methods
1411 // Note: We update the .c after calling the callback.
1412 // This way, the callback can know what the last status was.
1413
1414 func (e *Encoder) mapStart(length int) {
1415 e.e.WriteMapStart(length)
1416 e.c = containerMapStart
1417 }
1418
1419 func (e *Encoder) mapElemKey() {
1420 if e.js {
1421 e.jsondriver().WriteMapElemKey()
1422 }
1423 e.c = containerMapKey
1424 }
1425
1426 func (e *Encoder) mapElemValue() {
1427 if e.js {
1428 e.jsondriver().WriteMapElemValue()
1429 }
1430 e.c = containerMapValue
1431 }
1432
1433 func (e *Encoder) mapEnd() {
1434 e.e.WriteMapEnd()
1435 e.c = 0
1436 }
1437
1438 func (e *Encoder) arrayStart(length int) {
1439 e.e.WriteArrayStart(length)
1440 e.c = containerArrayStart
1441 }
1442
1443 func (e *Encoder) arrayElem() {
1444 if e.js {
1445 e.jsondriver().WriteArrayElem()
1446 }
1447 e.c = containerArrayElem
1448 }
1449
1450 func (e *Encoder) arrayEnd() {
1451 e.e.WriteArrayEnd()
1452 e.c = 0
1453 }
1454
1455 // ----------
1456
1457 func (e *Encoder) haltOnMbsOddLen(length int) {
1458 if length&1 != 0 { // similar to &1==1 or %2 == 1
1459 e.errorf("mapBySlice requires even slice length, but got %v", length)
1460 }
1461 }
1462
1463 func (e *Encoder) atEndOfEncode() {
1464 // e.e.atEndOfEncode()
1465 if e.js {
1466 e.jsondriver().atEndOfEncode()
1467 }
1468 }
1469
1470 func (e *Encoder) sideEncode(v interface{}, basetype reflect.Type, bs *[]byte) {
1471 // rv := baseRV(v)
1472 // e2 := NewEncoderBytes(bs, e.hh)
1473 // e2.encodeValue(rv, e2.h.fnNoExt(basetype))
1474 // e2.atEndOfEncode()
1475 // e2.w().end()
1476
1477 defer func(wb bytesEncAppender, bytes bool, c containerState, state interface{}) {
1478 e.wb = wb
1479 e.bytes = bytes
1480 e.c = c
1481 e.e.restoreState(state)
1482 }(e.wb, e.bytes, e.c, e.e.captureState())
1483
1484 e.wb = bytesEncAppender{encInBytes(bs)[:0], bs}
1485 e.bytes = true
1486 e.c = 0
1487 e.e.resetState()
1488
1489 // must call using fnNoExt
1490 rv := baseRV(v)
1491 e.encodeValue(rv, e.h.fnNoExt(basetype))
1492 e.atEndOfEncode()
1493 e.w().end()
1494 }
1495
1496 func encInBytes(out *[]byte) (in []byte) {
1497 in = *out
1498 if in == nil {
1499 in = make([]byte, defEncByteBufSize)
1500 }
1501 return
1502 }
1503
1504 func encStructFieldKey(encName string, ee encDriver, w *encWr,
1505 keyType valueType, encNameAsciiAlphaNum bool, js bool) {
1506 // use if-else-if, not switch (which compiles to binary-search)
1507 // since keyType is typically valueTypeString, branch prediction is pretty good.
1508
1509 if keyType == valueTypeString {
1510 if js && encNameAsciiAlphaNum { // keyType == valueTypeString
1511 w.writeqstr(encName)
1512 } else { // keyType == valueTypeString
1513 ee.EncodeString(encName)
1514 }
1515 } else if keyType == valueTypeInt {
1516 ee.EncodeInt(must.Int(strconv.ParseInt(encName, 10, 64)))
1517 } else if keyType == valueTypeUint {
1518 ee.EncodeUint(must.Uint(strconv.ParseUint(encName, 10, 64)))
1519 } else if keyType == valueTypeFloat {
1520 ee.EncodeFloat64(must.Float(strconv.ParseFloat(encName, 64)))
1521 } else {
1522 halt.errorf("invalid struct key type: %v", keyType)
1523 }
1524 }