2018-01-26 23:51:00 +00:00
|
|
|
// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
|
2015-08-19 01:12:51 +00:00
|
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
2015-06-29 21:50:55 +00:00
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding"
|
2018-01-26 23:51:00 +00:00
|
|
|
"errors"
|
2015-06-29 21:50:55 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"reflect"
|
2019-04-12 15:51:37 +00:00
|
|
|
"runtime"
|
2015-06-29 21:50:55 +00:00
|
|
|
"sort"
|
2018-01-26 23:51:00 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2015-06-29 21:50:55 +00:00
|
|
|
)
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// defEncByteBufSize is the default size of []byte used
|
|
|
|
// for bufio buffer or []byte (when nil passed)
|
|
|
|
const defEncByteBufSize = 1 << 10 // 4:16, 6:64, 8:256, 10:1024
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
var errEncoderNotInitialized = errors.New("Encoder not initialized")
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
/*
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
// encWriter abstracts writing to a byte array or to an io.Writer.
|
2019-04-12 15:51:37 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Deprecated: Use encWriterSwitch instead.
|
2015-06-29 21:50:55 +00:00
|
|
|
type encWriter interface {
|
|
|
|
writeb([]byte)
|
|
|
|
writestr(string)
|
|
|
|
writen1(byte)
|
|
|
|
writen2(byte, byte)
|
2019-04-12 15:51:37 +00:00
|
|
|
end()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
*/
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
// encDriver abstracts the actual codec (binc vs msgpack, etc)
|
|
|
|
type encDriver interface {
|
|
|
|
EncodeNil()
|
|
|
|
EncodeInt(i int64)
|
|
|
|
EncodeUint(i uint64)
|
|
|
|
EncodeBool(b bool)
|
|
|
|
EncodeFloat32(f float32)
|
|
|
|
EncodeFloat64(f float64)
|
|
|
|
// encodeExtPreamble(xtag byte, length int)
|
|
|
|
EncodeRawExt(re *RawExt, e *Encoder)
|
|
|
|
EncodeExt(v interface{}, xtag uint64, ext Ext, e *Encoder)
|
2019-04-13 07:44:06 +00:00
|
|
|
// Deprecated: try to use EncodeStringEnc instead
|
2018-01-26 23:51:00 +00:00
|
|
|
EncodeString(c charEncoding, v string)
|
2019-04-13 07:44:06 +00:00
|
|
|
// c cannot be cRAW
|
|
|
|
EncodeStringEnc(c charEncoding, v string)
|
2019-04-12 15:51:37 +00:00
|
|
|
// EncodeSymbol(v string)
|
2019-04-13 07:44:06 +00:00
|
|
|
// Deprecated: try to use EncodeStringBytesRaw instead
|
|
|
|
EncodeStringBytes(c charEncoding, v []byte)
|
2019-04-12 15:51:37 +00:00
|
|
|
EncodeStringBytesRaw(v []byte)
|
2018-01-26 23:51:00 +00:00
|
|
|
EncodeTime(time.Time)
|
|
|
|
//encBignum(f *big.Int)
|
|
|
|
//encStringRunes(c charEncoding, v []rune)
|
2017-10-27 19:06:04 +00:00
|
|
|
WriteArrayStart(length int)
|
|
|
|
WriteArrayElem()
|
|
|
|
WriteArrayEnd()
|
|
|
|
WriteMapStart(length int)
|
|
|
|
WriteMapElemKey()
|
|
|
|
WriteMapElemValue()
|
|
|
|
WriteMapEnd()
|
2015-10-30 22:07:00 +00:00
|
|
|
|
|
|
|
reset()
|
2017-10-27 19:06:04 +00:00
|
|
|
atEndOfEncode()
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:44:21 +00:00
|
|
|
type encDriverAsis interface {
|
|
|
|
EncodeAsis(v []byte)
|
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
type encodeError struct {
|
|
|
|
codecError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e encodeError) Error() string {
|
|
|
|
return fmt.Sprintf("%s encode error: %v", e.name, e.err)
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
type encDriverNoopContainerWriter struct{}
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
func (encDriverNoopContainerWriter) WriteArrayStart(length int) {}
|
|
|
|
func (encDriverNoopContainerWriter) WriteArrayElem() {}
|
|
|
|
func (encDriverNoopContainerWriter) WriteArrayEnd() {}
|
|
|
|
func (encDriverNoopContainerWriter) WriteMapStart(length int) {}
|
|
|
|
func (encDriverNoopContainerWriter) WriteMapElemKey() {}
|
|
|
|
func (encDriverNoopContainerWriter) WriteMapElemValue() {}
|
|
|
|
func (encDriverNoopContainerWriter) WriteMapEnd() {}
|
|
|
|
func (encDriverNoopContainerWriter) atEndOfEncode() {}
|
|
|
|
|
|
|
|
type encDriverTrackContainerWriter struct {
|
|
|
|
c containerState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteArrayStart(length int) { e.c = containerArrayStart }
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteArrayElem() { e.c = containerArrayElem }
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteArrayEnd() { e.c = containerArrayEnd }
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteMapStart(length int) { e.c = containerMapStart }
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteMapElemKey() { e.c = containerMapKey }
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteMapElemValue() { e.c = containerMapValue }
|
|
|
|
func (e *encDriverTrackContainerWriter) WriteMapEnd() { e.c = containerMapEnd }
|
|
|
|
func (e *encDriverTrackContainerWriter) atEndOfEncode() {}
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
// type ioEncWriterWriter interface {
|
|
|
|
// WriteByte(c byte) error
|
|
|
|
// WriteString(s string) (n int, err error)
|
|
|
|
// Write(p []byte) (n int, err error)
|
|
|
|
// }
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// EncodeOptions captures configuration options during encode.
|
2015-06-29 21:50:55 +00:00
|
|
|
type EncodeOptions struct {
|
2018-01-26 23:51:00 +00:00
|
|
|
// WriterBufferSize is the size of the buffer used when writing.
|
|
|
|
//
|
|
|
|
// if > 0, we use a smart buffer internally for performance purposes.
|
|
|
|
WriterBufferSize int
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// ChanRecvTimeout is the timeout used when selecting from a chan.
|
|
|
|
//
|
|
|
|
// Configuring this controls how we receive from a chan during the encoding process.
|
|
|
|
// - If ==0, we only consume the elements currently available in the chan.
|
|
|
|
// - if <0, we consume until the chan is closed.
|
|
|
|
// - If >0, we consume until this timeout.
|
|
|
|
ChanRecvTimeout time.Duration
|
|
|
|
|
|
|
|
// StructToArray specifies to encode a struct as an array, and not as a map
|
2015-06-29 21:50:55 +00:00
|
|
|
StructToArray bool
|
|
|
|
|
|
|
|
// Canonical representation means that encoding a value will always result in the same
|
|
|
|
// sequence of bytes.
|
|
|
|
//
|
2015-09-22 15:44:21 +00:00
|
|
|
// This only affects maps, as the iteration order for maps is random.
|
2015-10-15 21:52:18 +00:00
|
|
|
//
|
|
|
|
// The implementation MAY use the natural sort order for the map keys if possible:
|
|
|
|
//
|
|
|
|
// - If there is a natural sort order (ie for number, bool, string or []byte keys),
|
|
|
|
// then the map keys are first sorted in natural order and then written
|
|
|
|
// with corresponding map values to the strema.
|
|
|
|
// - If there is no natural sort order, then the map keys will first be
|
|
|
|
// encoded into []byte, and then sorted,
|
|
|
|
// before writing the sorted keys and the corresponding map values to the stream.
|
|
|
|
//
|
2015-06-29 21:50:55 +00:00
|
|
|
Canonical bool
|
|
|
|
|
2016-04-26 00:18:04 +00:00
|
|
|
// CheckCircularRef controls whether we check for circular references
|
|
|
|
// and error fast during an encode.
|
|
|
|
//
|
|
|
|
// If enabled, an error is received if a pointer to a struct
|
|
|
|
// references itself either directly or through one of its fields (iteratively).
|
|
|
|
//
|
|
|
|
// This is opt-in, as there may be a performance hit to checking circular references.
|
|
|
|
CheckCircularRef bool
|
|
|
|
|
2016-09-02 22:05:09 +00:00
|
|
|
// RecursiveEmptyCheck controls whether we descend into interfaces, structs and pointers
|
|
|
|
// when checking if a value is empty.
|
|
|
|
//
|
|
|
|
// Note that this may make OmitEmpty more expensive, as it incurs a lot more reflect calls.
|
|
|
|
RecursiveEmptyCheck bool
|
|
|
|
|
2016-09-12 19:16:55 +00:00
|
|
|
// Raw controls whether we encode Raw values.
|
|
|
|
// This is a "dangerous" option and must be explicitly set.
|
|
|
|
// If set, we blindly encode Raw values as-is, without checking
|
|
|
|
// if they are a correct representation of a value in that format.
|
|
|
|
// If unset, we error out.
|
|
|
|
Raw bool
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// // AsSymbols defines what should be encoded as symbols.
|
|
|
|
// //
|
|
|
|
// // Encoding as symbols can reduce the encoded size significantly.
|
|
|
|
// //
|
|
|
|
// // However, during decoding, each string to be encoded as a symbol must
|
|
|
|
// // be checked to see if it has been seen before. Consequently, encoding time
|
|
|
|
// // will increase if using symbols, because string comparisons has a clear cost.
|
|
|
|
// //
|
|
|
|
// // Sample values:
|
|
|
|
// // AsSymbolNone
|
|
|
|
// // AsSymbolAll
|
|
|
|
// // AsSymbolMapStringKeys
|
|
|
|
// // AsSymbolMapStringKeysFlag | AsSymbolStructFieldNameFlag
|
|
|
|
// AsSymbols AsSymbolFlag
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
type ioEncStringWriter interface {
|
|
|
|
WriteString(s string) (n int, err error)
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
// ioEncWriter implements encWriter and can write to an io.Writer implementation
|
|
|
|
type ioEncWriter struct {
|
2015-06-29 21:50:55 +00:00
|
|
|
w io.Writer
|
2017-10-27 19:06:04 +00:00
|
|
|
ww io.Writer
|
2015-06-29 21:50:55 +00:00
|
|
|
bw io.ByteWriter
|
|
|
|
sw ioEncStringWriter
|
2018-01-26 23:51:00 +00:00
|
|
|
fw ioFlusher
|
2017-10-27 19:06:04 +00:00
|
|
|
b [8]byte
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
func (z *ioEncWriter) reset(w io.Writer) {
|
|
|
|
z.w = w
|
|
|
|
var ok bool
|
|
|
|
if z.bw, ok = w.(io.ByteWriter); !ok {
|
|
|
|
z.bw = z
|
|
|
|
}
|
|
|
|
if z.sw, ok = w.(ioEncStringWriter); !ok {
|
|
|
|
z.sw = z
|
|
|
|
}
|
|
|
|
z.fw, _ = w.(ioFlusher)
|
|
|
|
z.ww = w
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (z *ioEncWriter) WriteByte(b byte) (err error) {
|
|
|
|
z.b[0] = b
|
|
|
|
_, err = z.w.Write(z.b[:1])
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (z *ioEncWriter) WriteString(s string) (n int, err error) {
|
|
|
|
return z.w.Write(bytesView(s))
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ioEncWriter) writeb(bs []byte) {
|
2017-10-27 19:06:04 +00:00
|
|
|
if _, err := z.ww.Write(bs); err != nil {
|
2015-06-29 21:50:55 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ioEncWriter) writestr(s string) {
|
2017-10-27 19:06:04 +00:00
|
|
|
if _, err := z.sw.WriteString(s); err != nil {
|
2015-06-29 21:50:55 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ioEncWriter) writen1(b byte) {
|
2017-10-27 19:06:04 +00:00
|
|
|
if err := z.bw.WriteByte(b); err != nil {
|
2015-06-29 21:50:55 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (z *ioEncWriter) writen2(b1, b2 byte) {
|
|
|
|
var err error
|
|
|
|
if err = z.bw.WriteByte(b1); err == nil {
|
|
|
|
if err = z.bw.WriteByte(b2); err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(err)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
// func (z *ioEncWriter) writen5(b1, b2, b3, b4, b5 byte) {
|
|
|
|
// z.b[0], z.b[1], z.b[2], z.b[3], z.b[4] = b1, b2, b3, b4, b5
|
|
|
|
// if _, err := z.ww.Write(z.b[:5]); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
//go:noinline - so *encWriterSwitch.XXX has the bytesEncAppender.XXX inlined
|
|
|
|
func (z *ioEncWriter) end() {
|
2017-10-27 19:06:04 +00:00
|
|
|
if z.fw != nil {
|
2018-02-11 00:29:52 +00:00
|
|
|
if err := z.fw.Flush(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// ---------------------------------------------
|
|
|
|
|
|
|
|
// bufioEncWriter
|
|
|
|
type bufioEncWriter struct {
|
|
|
|
buf []byte
|
|
|
|
w io.Writer
|
|
|
|
n int
|
|
|
|
sz int // buf size
|
|
|
|
|
|
|
|
// Extensions can call Encode() within a current Encode() call.
|
|
|
|
// We need to know when the top level Encode() call returns,
|
|
|
|
// so we can decide whether to Release() or not.
|
|
|
|
calls uint16 // what depth in mustDecode are we in now.
|
|
|
|
|
|
|
|
_ [6]uint8 // padding
|
|
|
|
|
|
|
|
bytesBufPooler
|
|
|
|
|
|
|
|
_ [1]uint64 // padding
|
|
|
|
// a int
|
|
|
|
// b [4]byte
|
|
|
|
// err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *bufioEncWriter) reset(w io.Writer, bufsize int) {
|
|
|
|
z.w = w
|
|
|
|
z.n = 0
|
|
|
|
z.calls = 0
|
|
|
|
if bufsize <= 0 {
|
|
|
|
bufsize = defEncByteBufSize
|
|
|
|
}
|
|
|
|
z.sz = bufsize
|
|
|
|
if cap(z.buf) >= bufsize {
|
|
|
|
z.buf = z.buf[:cap(z.buf)]
|
|
|
|
} else {
|
|
|
|
z.buf = z.bytesBufPooler.get(bufsize)
|
|
|
|
// z.buf = make([]byte, bufsize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *bufioEncWriter) release() {
|
|
|
|
z.buf = nil
|
|
|
|
z.bytesBufPooler.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:noinline - flush only called intermittently
|
2019-04-13 07:44:06 +00:00
|
|
|
func (z *bufioEncWriter) flush() {
|
2019-04-12 15:51:37 +00:00
|
|
|
n, err := z.w.Write(z.buf[:z.n])
|
|
|
|
z.n -= n
|
|
|
|
if z.n > 0 && err == nil {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
2019-04-13 07:44:06 +00:00
|
|
|
if err != nil {
|
|
|
|
if n > 0 && z.n > 0 {
|
|
|
|
copy(z.buf, z.buf[n:z.n+n])
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *bufioEncWriter) writeb(s []byte) {
|
|
|
|
LOOP:
|
|
|
|
a := len(z.buf) - z.n
|
|
|
|
if len(s) > a {
|
|
|
|
z.n += copy(z.buf[z.n:], s[:a])
|
|
|
|
s = s[a:]
|
|
|
|
z.flush()
|
|
|
|
goto LOOP
|
|
|
|
}
|
|
|
|
z.n += copy(z.buf[z.n:], s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *bufioEncWriter) writestr(s string) {
|
|
|
|
// z.writeb(bytesView(s)) // inlined below
|
|
|
|
LOOP:
|
|
|
|
a := len(z.buf) - z.n
|
|
|
|
if len(s) > a {
|
|
|
|
z.n += copy(z.buf[z.n:], s[:a])
|
|
|
|
s = s[a:]
|
|
|
|
z.flush()
|
|
|
|
goto LOOP
|
|
|
|
}
|
|
|
|
z.n += copy(z.buf[z.n:], s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *bufioEncWriter) writen1(b1 byte) {
|
|
|
|
if 1 > len(z.buf)-z.n {
|
|
|
|
z.flush()
|
|
|
|
}
|
|
|
|
z.buf[z.n] = b1
|
|
|
|
z.n++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *bufioEncWriter) writen2(b1, b2 byte) {
|
|
|
|
if 2 > len(z.buf)-z.n {
|
|
|
|
z.flush()
|
|
|
|
}
|
|
|
|
z.buf[z.n+1] = b2
|
|
|
|
z.buf[z.n] = b1
|
|
|
|
z.n += 2
|
|
|
|
}
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
func (z *bufioEncWriter) end() {
|
2019-04-12 15:51:37 +00:00
|
|
|
if z.n > 0 {
|
2019-04-13 07:44:06 +00:00
|
|
|
z.flush()
|
2019-04-12 15:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// ---------------------------------------------
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// bytesEncAppender implements encWriter and can write to an byte slice.
|
|
|
|
type bytesEncAppender struct {
|
2015-06-29 21:50:55 +00:00
|
|
|
b []byte
|
2018-01-26 23:51:00 +00:00
|
|
|
out *[]byte
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
func (z *bytesEncAppender) writeb(s []byte) {
|
|
|
|
z.b = append(z.b, s...)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
func (z *bytesEncAppender) writestr(s string) {
|
|
|
|
z.b = append(z.b, s...)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
func (z *bytesEncAppender) writen1(b1 byte) {
|
|
|
|
z.b = append(z.b, b1)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
func (z *bytesEncAppender) writen2(b1, b2 byte) {
|
|
|
|
z.b = append(z.b, b1, b2)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2019-04-13 07:44:06 +00:00
|
|
|
func (z *bytesEncAppender) end() {
|
2018-01-26 23:51:00 +00:00
|
|
|
*(z.out) = z.b
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
func (z *bytesEncAppender) reset(in []byte, out *[]byte) {
|
|
|
|
z.b = in[:0]
|
|
|
|
z.out = out
|
2016-12-01 19:46:57 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
// ---------------------------------------------
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) rawExt(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
e.e.EncodeRawExt(rv2i(rv).(*RawExt), e)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) ext(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
e.e.EncodeExt(rv2i(rv), f.xfTag, f.xfFn, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) selferMarshal(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
rv2i(rv).(Selfer).CodecEncodeSelf(e)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) binaryMarshal(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
bs, fnerr := rv2i(rv).(encoding.BinaryMarshaler).MarshalBinary()
|
2019-04-12 15:51:37 +00:00
|
|
|
e.marshalRaw(bs, fnerr)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) textMarshal(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
bs, fnerr := rv2i(rv).(encoding.TextMarshaler).MarshalText()
|
2019-04-12 15:51:37 +00:00
|
|
|
e.marshalUtf8(bs, fnerr)
|
2015-09-22 15:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) jsonMarshal(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
bs, fnerr := rv2i(rv).(jsonMarshaler).MarshalJSON()
|
2019-04-12 15:51:37 +00:00
|
|
|
e.marshalAsis(bs, fnerr)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:57:05 +00:00
|
|
|
func (e *Encoder) raw(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
e.rawBytes(rv2i(rv).(Raw))
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) kInvalid(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
e.e.EncodeNil()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) kErr(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
e.errorf("unsupported kind %s, for %#v", rv.Kind(), rv)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) kSlice(f *codecFnInfo, rv reflect.Value) {
|
2015-06-29 21:50:55 +00:00
|
|
|
ti := f.ti
|
2017-10-27 19:06:04 +00:00
|
|
|
ee := e.e
|
2015-06-29 21:50:55 +00:00
|
|
|
// array may be non-addressable, so we have to manage with care
|
|
|
|
// (don't call rv.Bytes, rv.Slice, etc).
|
|
|
|
// E.g. type struct S{B [2]byte};
|
|
|
|
// Encode(S{}) will bomb on "panic: slice of unaddressable array".
|
|
|
|
if f.seq != seqTypeArray {
|
|
|
|
if rv.IsNil() {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.EncodeNil()
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// If in this method, then there was no extension function defined.
|
|
|
|
// So it's okay to treat as []byte.
|
|
|
|
if ti.rtid == uint8SliceTypId {
|
2019-04-12 15:51:37 +00:00
|
|
|
ee.EncodeStringBytesRaw(rv.Bytes())
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
if f.seq == seqTypeChan && ti.chandir&uint8(reflect.RecvDir) == 0 {
|
2018-07-06 16:09:34 +00:00
|
|
|
e.errorf("send-only channel cannot be encoded")
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
|
|
|
elemsep := e.esep
|
|
|
|
rtelem := ti.elem
|
|
|
|
rtelemIsByte := uint8TypId == rt2id(rtelem) // NOT rtelem.Kind() == reflect.Uint8
|
2018-07-06 16:09:34 +00:00
|
|
|
var l int
|
2018-01-26 23:51:00 +00:00
|
|
|
// if a slice, array or chan of bytes, treat specially
|
|
|
|
if rtelemIsByte {
|
2015-06-29 21:50:55 +00:00
|
|
|
switch f.seq {
|
2018-01-26 23:51:00 +00:00
|
|
|
case seqTypeSlice:
|
2019-04-12 15:51:37 +00:00
|
|
|
ee.EncodeStringBytesRaw(rv.Bytes())
|
2015-06-29 21:50:55 +00:00
|
|
|
case seqTypeArray:
|
2018-07-06 16:09:34 +00:00
|
|
|
l = rv.Len()
|
2015-06-29 21:50:55 +00:00
|
|
|
if rv.CanAddr() {
|
2019-04-12 15:51:37 +00:00
|
|
|
ee.EncodeStringBytesRaw(rv.Slice(0, l).Bytes())
|
2015-06-29 21:50:55 +00:00
|
|
|
} else {
|
|
|
|
var bs []byte
|
2015-10-30 22:07:00 +00:00
|
|
|
if l <= cap(e.b) {
|
|
|
|
bs = e.b[:l]
|
2015-06-29 21:50:55 +00:00
|
|
|
} else {
|
|
|
|
bs = make([]byte, l)
|
|
|
|
}
|
|
|
|
reflect.Copy(reflect.ValueOf(bs), rv)
|
2019-04-12 15:51:37 +00:00
|
|
|
ee.EncodeStringBytesRaw(bs)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
case seqTypeChan:
|
|
|
|
// do not use range, so that the number of elements encoded
|
|
|
|
// does not change, and encoding does not hang waiting on someone to close chan.
|
|
|
|
// for b := range rv2i(rv).(<-chan byte) { bs = append(bs, b) }
|
|
|
|
// ch := rv2i(rv).(<-chan byte) // fix error - that this is a chan byte, not a <-chan byte.
|
2018-07-06 16:09:34 +00:00
|
|
|
|
|
|
|
if rv.IsNil() {
|
|
|
|
ee.EncodeNil()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
bs := e.b[:0]
|
2018-01-26 23:51:00 +00:00
|
|
|
irv := rv2i(rv)
|
|
|
|
ch, ok := irv.(<-chan byte)
|
|
|
|
if !ok {
|
|
|
|
ch = irv.(chan byte)
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
|
|
|
|
L1:
|
|
|
|
switch timeout := e.h.ChanRecvTimeout; {
|
|
|
|
case timeout == 0: // only consume available
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b := <-ch:
|
|
|
|
bs = append(bs, b)
|
|
|
|
default:
|
|
|
|
break L1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case timeout > 0: // consume until timeout
|
|
|
|
tt := time.NewTimer(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b := <-ch:
|
|
|
|
bs = append(bs, b)
|
|
|
|
case <-tt.C:
|
|
|
|
// close(tt.C)
|
|
|
|
break L1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: // consume until close
|
|
|
|
for b := range ch {
|
|
|
|
bs = append(bs, b)
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
ee.EncodeStringBytesRaw(bs)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
// if chan, consume chan into a slice, and work off that slice.
|
|
|
|
if f.seq == seqTypeChan {
|
2019-04-12 15:51:37 +00:00
|
|
|
rvcs := reflect.Zero(reflect.SliceOf(rtelem))
|
2018-07-06 16:09:34 +00:00
|
|
|
timeout := e.h.ChanRecvTimeout
|
|
|
|
if timeout < 0 { // consume until close
|
|
|
|
for {
|
|
|
|
recv, recvOk := rv.Recv()
|
|
|
|
if !recvOk {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rvcs = reflect.Append(rvcs, recv)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cases := make([]reflect.SelectCase, 2)
|
|
|
|
cases[0] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: rv}
|
|
|
|
if timeout == 0 {
|
|
|
|
cases[1] = reflect.SelectCase{Dir: reflect.SelectDefault}
|
|
|
|
} else {
|
|
|
|
tt := time.NewTimer(timeout)
|
|
|
|
cases[1] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(tt.C)}
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
chosen, recv, recvOk := reflect.Select(cases)
|
|
|
|
if chosen == 1 || !recvOk {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rvcs = reflect.Append(rvcs, recv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = rvcs // TODO: ensure this doesn't mess up anywhere that rv of kind chan is expected
|
|
|
|
}
|
|
|
|
|
|
|
|
l = rv.Len()
|
2015-06-29 21:50:55 +00:00
|
|
|
if ti.mbs {
|
|
|
|
if l%2 == 1 {
|
2015-10-30 22:07:00 +00:00
|
|
|
e.errorf("mapBySlice requires even slice length, but got %v", l)
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapStart(l / 2)
|
2015-06-29 21:50:55 +00:00
|
|
|
} else {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteArrayStart(l)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if l > 0 {
|
2017-10-27 19:06:04 +00:00
|
|
|
var fn *codecFn
|
2017-11-07 16:57:05 +00:00
|
|
|
for rtelem.Kind() == reflect.Ptr {
|
|
|
|
rtelem = rtelem.Elem()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2017-11-07 16:57:05 +00:00
|
|
|
// if kind is reflect.Interface, do not pre-determine the
|
|
|
|
// encoding type, because preEncodeValue may break it down to
|
|
|
|
// a concrete type and kInterface will bomb.
|
|
|
|
if rtelem.Kind() != reflect.Interface {
|
2019-04-12 15:51:37 +00:00
|
|
|
fn = e.h.fn(rtelem, true, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2015-09-22 15:44:21 +00:00
|
|
|
for j := 0; j < l; j++ {
|
2017-10-27 19:06:04 +00:00
|
|
|
if elemsep {
|
2015-10-30 22:07:00 +00:00
|
|
|
if ti.mbs {
|
2016-04-26 00:18:04 +00:00
|
|
|
if j%2 == 0 {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapElemKey()
|
2015-10-30 22:07:00 +00:00
|
|
|
} else {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapElemValue()
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteArrayElem()
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
e.encodeValue(rv.Index(j), fn, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 15:44:21 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
if ti.mbs {
|
|
|
|
ee.WriteMapEnd()
|
|
|
|
} else {
|
|
|
|
ee.WriteArrayEnd()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) kStructNoOmitempty(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
fti := f.ti
|
2018-01-26 23:51:00 +00:00
|
|
|
tisfi := fti.sfiSrc
|
2017-10-27 19:06:04 +00:00
|
|
|
toMap := !(fti.toArray || e.h.StructToArray)
|
|
|
|
if toMap {
|
2018-01-26 23:51:00 +00:00
|
|
|
tisfi = fti.sfiSort
|
2017-10-27 19:06:04 +00:00
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
ee := e.e
|
|
|
|
|
|
|
|
sfn := structFieldNode{v: rv, update: false}
|
|
|
|
if toMap {
|
|
|
|
ee.WriteMapStart(len(tisfi))
|
2019-04-12 15:51:37 +00:00
|
|
|
if e.esep {
|
2017-10-27 19:06:04 +00:00
|
|
|
for _, si := range tisfi {
|
2018-01-26 23:51:00 +00:00
|
|
|
ee.WriteMapElemKey()
|
2019-04-13 07:44:06 +00:00
|
|
|
// ee.EncodeStringEnc(cUTF8, si.encName)
|
|
|
|
e.kStructFieldKey(fti.keyType, si)
|
2018-01-26 23:51:00 +00:00
|
|
|
ee.WriteMapElemValue()
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(sfn.field(si), nil, true)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, si := range tisfi {
|
2019-04-13 07:44:06 +00:00
|
|
|
// ee.EncodeStringEnc(cUTF8, si.encName)
|
|
|
|
e.kStructFieldKey(fti.keyType, si)
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(sfn.field(si), nil, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ee.WriteMapEnd()
|
|
|
|
} else {
|
|
|
|
ee.WriteArrayStart(len(tisfi))
|
2019-04-12 15:51:37 +00:00
|
|
|
if e.esep {
|
2017-10-27 19:06:04 +00:00
|
|
|
for _, si := range tisfi {
|
2018-01-26 23:51:00 +00:00
|
|
|
ee.WriteArrayElem()
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(sfn.field(si), nil, true)
|
|
|
|
}
|
2015-10-30 22:07:00 +00:00
|
|
|
} else {
|
2017-10-27 19:06:04 +00:00
|
|
|
for _, si := range tisfi {
|
|
|
|
e.encodeValue(sfn.field(si), nil, true)
|
|
|
|
}
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteArrayEnd()
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 07:44:06 +00:00
|
|
|
func (e *Encoder) kStructFieldKey(keyType valueType, s *structFieldInfo) {
|
|
|
|
var m must
|
|
|
|
// use if-else-if, not switch (which compiles to binary-search)
|
|
|
|
// since keyType is typically valueTypeString, branch prediction is pretty good.
|
|
|
|
if keyType == valueTypeString {
|
|
|
|
if e.js && s.encNameAsciiAlphaNum { // keyType == valueTypeString
|
|
|
|
e.w.writen1('"')
|
|
|
|
e.w.writestr(s.encName)
|
|
|
|
e.w.writen1('"')
|
|
|
|
} else { // keyType == valueTypeString
|
|
|
|
e.e.EncodeStringEnc(cUTF8, s.encName)
|
|
|
|
}
|
|
|
|
} else if keyType == valueTypeInt {
|
|
|
|
e.e.EncodeInt(m.Int(strconv.ParseInt(s.encName, 10, 64)))
|
|
|
|
} else if keyType == valueTypeUint {
|
|
|
|
e.e.EncodeUint(m.Uint(strconv.ParseUint(s.encName, 10, 64)))
|
|
|
|
} else if keyType == valueTypeFloat {
|
|
|
|
e.e.EncodeFloat64(m.Float(strconv.ParseFloat(s.encName, 64)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) kStructFieldKeyName(keyType valueType, encName string) {
|
|
|
|
var m must
|
|
|
|
// use if-else-if, not switch (which compiles to binary-search)
|
|
|
|
// since keyType is typically valueTypeString, branch prediction is pretty good.
|
|
|
|
if keyType == valueTypeString {
|
|
|
|
e.e.EncodeStringEnc(cUTF8, encName)
|
|
|
|
} else if keyType == valueTypeInt {
|
|
|
|
e.e.EncodeInt(m.Int(strconv.ParseInt(encName, 10, 64)))
|
|
|
|
} else if keyType == valueTypeUint {
|
|
|
|
e.e.EncodeUint(m.Uint(strconv.ParseUint(encName, 10, 64)))
|
|
|
|
} else if keyType == valueTypeFloat {
|
|
|
|
e.e.EncodeFloat64(m.Float(strconv.ParseFloat(encName, 64)))
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) kStruct(f *codecFnInfo, rv reflect.Value) {
|
2015-06-29 21:50:55 +00:00
|
|
|
fti := f.ti
|
2018-01-26 23:51:00 +00:00
|
|
|
elemsep := e.esep
|
|
|
|
tisfi := fti.sfiSrc
|
2018-10-03 16:55:26 +00:00
|
|
|
var newlen int
|
2015-06-29 21:50:55 +00:00
|
|
|
toMap := !(fti.toArray || e.h.StructToArray)
|
2018-10-03 16:55:26 +00:00
|
|
|
var mf map[string]interface{}
|
|
|
|
if f.ti.mf {
|
|
|
|
mf = rv2i(rv).(MissingFielder).CodecMissingFields()
|
|
|
|
toMap = true
|
|
|
|
newlen += len(mf)
|
|
|
|
} else if f.ti.mfp {
|
|
|
|
if rv.CanAddr() {
|
|
|
|
mf = rv2i(rv.Addr()).(MissingFielder).CodecMissingFields()
|
|
|
|
} else {
|
|
|
|
// make a new addressable value of same one, and use it
|
|
|
|
rv2 := reflect.New(rv.Type())
|
|
|
|
rv2.Elem().Set(rv)
|
|
|
|
mf = rv2i(rv2).(MissingFielder).CodecMissingFields()
|
|
|
|
}
|
|
|
|
toMap = true
|
|
|
|
newlen += len(mf)
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
// if toMap, use the sorted array. If toArray, use unsorted array (to match sequence in struct)
|
|
|
|
if toMap {
|
2018-01-26 23:51:00 +00:00
|
|
|
tisfi = fti.sfiSort
|
2017-10-27 19:06:04 +00:00
|
|
|
}
|
2018-10-03 16:55:26 +00:00
|
|
|
newlen += len(tisfi)
|
2017-10-27 19:06:04 +00:00
|
|
|
ee := e.e
|
2015-10-07 20:10:00 +00:00
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
// Use sync.Pool to reduce allocating slices unnecessarily.
|
2016-04-26 00:18:04 +00:00
|
|
|
// The cost of sync.Pool is less than the cost of new allocation.
|
2017-10-27 19:06:04 +00:00
|
|
|
//
|
|
|
|
// Each element of the array pools one of encStructPool(8|16|32|64).
|
|
|
|
// It allows the re-use of slices up to 64 in length.
|
|
|
|
// A performance cost of encoding structs was collecting
|
|
|
|
// which values were empty and should be omitted.
|
|
|
|
// We needed slices of reflect.Value and string to collect them.
|
|
|
|
// This shared pool reduces the amount of unnecessary creation we do.
|
|
|
|
// The cost is that of locking sometimes, but sync.Pool is efficient
|
|
|
|
// enough to reduce thread contention.
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// fmt.Printf(">>>>>>>>>>>>>> encode.kStruct: newlen: %d\n", newlen)
|
2019-04-12 15:51:37 +00:00
|
|
|
var spool sfiRvPooler
|
|
|
|
var fkvs = spool.get(newlen)
|
2017-10-27 19:06:04 +00:00
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
var kv sfiRv
|
2016-09-02 22:05:09 +00:00
|
|
|
recur := e.h.RecursiveEmptyCheck
|
2017-10-27 19:06:04 +00:00
|
|
|
sfn := structFieldNode{v: rv, update: false}
|
2019-04-12 15:51:37 +00:00
|
|
|
newlen = 0
|
2015-06-29 21:50:55 +00:00
|
|
|
for _, si := range tisfi {
|
2017-10-27 19:06:04 +00:00
|
|
|
// kv.r = si.field(rv, false)
|
|
|
|
kv.r = sfn.field(si)
|
2015-06-29 21:50:55 +00:00
|
|
|
if toMap {
|
2018-01-26 23:51:00 +00:00
|
|
|
if si.omitEmpty() && isEmptyValue(kv.r, e.h.TypeInfos, recur, recur) {
|
2015-06-29 21:50:55 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-10-03 16:55:26 +00:00
|
|
|
kv.v = si // si.encName
|
2015-06-29 21:50:55 +00:00
|
|
|
} else {
|
|
|
|
// use the zero value.
|
|
|
|
// if a reference or struct, set to nil (so you do not output too much)
|
2018-01-26 23:51:00 +00:00
|
|
|
if si.omitEmpty() && isEmptyValue(kv.r, e.h.TypeInfos, recur, recur) {
|
2015-10-15 21:52:18 +00:00
|
|
|
switch kv.r.Kind() {
|
2019-04-12 15:51:37 +00:00
|
|
|
case reflect.Struct, reflect.Interface, reflect.Ptr,
|
|
|
|
reflect.Array, reflect.Map, reflect.Slice:
|
2015-10-15 21:52:18 +00:00
|
|
|
kv.r = reflect.Value{} //encode as nil
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fkvs[newlen] = kv
|
|
|
|
newlen++
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
fkvs = fkvs[:newlen]
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
var mflen int
|
|
|
|
for k, v := range mf {
|
|
|
|
if k == "" {
|
|
|
|
delete(mf, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fti.infoFieldOmitempty && isEmptyValue(reflect.ValueOf(v), e.h.TypeInfos, recur, recur) {
|
|
|
|
delete(mf, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mflen++
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
var j int
|
2015-09-22 15:44:21 +00:00
|
|
|
if toMap {
|
2018-10-03 16:55:26 +00:00
|
|
|
ee.WriteMapStart(newlen + mflen)
|
2018-01-26 23:51:00 +00:00
|
|
|
if elemsep {
|
2019-04-12 15:51:37 +00:00
|
|
|
for j = 0; j < len(fkvs); j++ {
|
2017-10-27 19:06:04 +00:00
|
|
|
kv = fkvs[j]
|
2018-01-26 23:51:00 +00:00
|
|
|
ee.WriteMapElemKey()
|
2019-04-13 07:44:06 +00:00
|
|
|
// ee.EncodeStringEnc(cUTF8, kv.v)
|
|
|
|
e.kStructFieldKey(fti.keyType, kv.v)
|
2018-01-26 23:51:00 +00:00
|
|
|
ee.WriteMapElemValue()
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(kv.r, nil, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
} else {
|
2019-04-12 15:51:37 +00:00
|
|
|
for j = 0; j < len(fkvs); j++ {
|
2017-10-27 19:06:04 +00:00
|
|
|
kv = fkvs[j]
|
2019-04-13 07:44:06 +00:00
|
|
|
// ee.EncodeStringEnc(cUTF8, kv.v)
|
|
|
|
e.kStructFieldKey(fti.keyType, kv.v)
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(kv.r, nil, true)
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-03 16:55:26 +00:00
|
|
|
// now, add the others
|
|
|
|
for k, v := range mf {
|
|
|
|
ee.WriteMapElemKey()
|
2019-04-13 07:44:06 +00:00
|
|
|
e.kStructFieldKeyName(fti.keyType, k)
|
2018-10-03 16:55:26 +00:00
|
|
|
ee.WriteMapElemValue()
|
|
|
|
e.encode(v)
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapEnd()
|
2015-06-29 21:50:55 +00:00
|
|
|
} else {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteArrayStart(newlen)
|
2018-01-26 23:51:00 +00:00
|
|
|
if elemsep {
|
2019-04-12 15:51:37 +00:00
|
|
|
for j = 0; j < len(fkvs); j++ {
|
2018-01-26 23:51:00 +00:00
|
|
|
ee.WriteArrayElem()
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(fkvs[j].r, nil, true)
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-12 15:51:37 +00:00
|
|
|
for j = 0; j < len(fkvs); j++ {
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(fkvs[j].r, nil, true)
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteArrayEnd()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// do not use defer. Instead, use explicit pool return at end of function.
|
|
|
|
// defer has a cost we are trying to avoid.
|
|
|
|
// If there is a panic and these slices are not returned, it is ok.
|
2019-04-12 15:51:37 +00:00
|
|
|
spool.end()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) kMap(f *codecFnInfo, rv reflect.Value) {
|
|
|
|
ee := e.e
|
2015-06-29 21:50:55 +00:00
|
|
|
if rv.IsNil() {
|
2015-10-07 20:10:00 +00:00
|
|
|
ee.EncodeNil()
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l := rv.Len()
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapStart(l)
|
2015-06-29 21:50:55 +00:00
|
|
|
if l == 0 {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapEnd()
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
// var asSymbols bool
|
2015-06-29 21:50:55 +00:00
|
|
|
// determine the underlying key and val encFn's for the map.
|
|
|
|
// This eliminates some work which is done for each loop iteration i.e.
|
|
|
|
// rv.Type(), ref.ValueOf(rt).Pointer(), then check map/list for fn.
|
|
|
|
//
|
|
|
|
// However, if kind is reflect.Interface, do not pre-determine the
|
|
|
|
// encoding type, because preEncodeValue may break it down to
|
|
|
|
// a concrete type and kInterface will bomb.
|
2017-10-27 19:06:04 +00:00
|
|
|
var keyFn, valFn *codecFn
|
2015-06-29 21:50:55 +00:00
|
|
|
ti := f.ti
|
2018-01-26 23:51:00 +00:00
|
|
|
rtkey0 := ti.key
|
2017-10-27 19:06:04 +00:00
|
|
|
rtkey := rtkey0
|
2018-01-26 23:51:00 +00:00
|
|
|
rtval0 := ti.elem
|
2017-10-27 19:06:04 +00:00
|
|
|
rtval := rtval0
|
2018-01-26 23:51:00 +00:00
|
|
|
// rtkeyid := rt2id(rtkey0)
|
2017-10-27 19:06:04 +00:00
|
|
|
for rtval.Kind() == reflect.Ptr {
|
|
|
|
rtval = rtval.Elem()
|
|
|
|
}
|
|
|
|
if rtval.Kind() != reflect.Interface {
|
2019-04-12 15:51:37 +00:00
|
|
|
valFn = e.h.fn(rtval, true, true)
|
2017-10-27 19:06:04 +00:00
|
|
|
}
|
|
|
|
mks := rv.MapKeys()
|
|
|
|
|
|
|
|
if e.h.Canonical {
|
2018-01-26 23:51:00 +00:00
|
|
|
e.kMapCanonical(rtkey, rv, mks, valFn)
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapEnd()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
var keyTypeIsString = stringTypId == rt2id(rtkey0) // rtkeyid
|
|
|
|
if !keyTypeIsString {
|
2015-06-29 21:50:55 +00:00
|
|
|
for rtkey.Kind() == reflect.Ptr {
|
|
|
|
rtkey = rtkey.Elem()
|
|
|
|
}
|
|
|
|
if rtkey.Kind() != reflect.Interface {
|
2018-01-26 23:51:00 +00:00
|
|
|
// rtkeyid = rt2id(rtkey)
|
2019-04-12 15:51:37 +00:00
|
|
|
keyFn = e.h.fn(rtkey, true, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-15 21:52:18 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
// for j, lmks := 0, len(mks); j < lmks; j++ {
|
|
|
|
for j := range mks {
|
2019-04-12 15:51:37 +00:00
|
|
|
if e.esep {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapElemKey()
|
|
|
|
}
|
|
|
|
if keyTypeIsString {
|
2019-04-13 07:44:06 +00:00
|
|
|
ee.EncodeStringEnc(cUTF8, mks[j].String())
|
2017-10-27 19:06:04 +00:00
|
|
|
} else {
|
|
|
|
e.encodeValue(mks[j], keyFn, true)
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
if e.esep {
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapElemValue()
|
|
|
|
}
|
2017-11-07 16:57:05 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mks[j]), valFn, true)
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.WriteMapEnd()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
func (e *Encoder) kMapCanonical(rtkey reflect.Type, rv reflect.Value, mks []reflect.Value, valFn *codecFn) {
|
2015-10-15 21:52:18 +00:00
|
|
|
ee := e.e
|
2018-01-26 23:51:00 +00:00
|
|
|
elemsep := e.esep
|
2015-10-15 21:52:18 +00:00
|
|
|
// we previously did out-of-band if an extension was registered.
|
|
|
|
// This is not necessary, as the natural kind is sufficient for ordering.
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
switch rtkey.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
mksv := make([]boolRv, len(mks))
|
2015-10-15 21:52:18 +00:00
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
2017-10-27 19:06:04 +00:00
|
|
|
v.v = k.Bool()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
sort.Sort(boolRvSlice(mksv))
|
2015-10-15 21:52:18 +00:00
|
|
|
for i := range mksv {
|
2017-10-27 19:06:04 +00:00
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.EncodeBool(mksv[i].v)
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
case reflect.String:
|
|
|
|
mksv := make([]stringRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
|
|
|
v.v = k.String()
|
|
|
|
}
|
|
|
|
sort.Sort(stringRvSlice(mksv))
|
|
|
|
for i := range mksv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2019-04-13 07:44:06 +00:00
|
|
|
ee.EncodeStringEnc(cUTF8, mksv[i].v)
|
2017-10-27 19:06:04 +00:00
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
|
|
|
}
|
|
|
|
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr:
|
|
|
|
mksv := make([]uintRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
|
|
|
v.v = k.Uint()
|
|
|
|
}
|
|
|
|
sort.Sort(uintRvSlice(mksv))
|
|
|
|
for i := range mksv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.EncodeUint(mksv[i].v)
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
|
|
|
}
|
|
|
|
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
|
|
|
mksv := make([]intRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
|
|
|
v.v = k.Int()
|
|
|
|
}
|
|
|
|
sort.Sort(intRvSlice(mksv))
|
|
|
|
for i := range mksv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.EncodeInt(mksv[i].v)
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
|
|
|
}
|
|
|
|
case reflect.Float32:
|
|
|
|
mksv := make([]floatRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
|
|
|
v.v = k.Float()
|
|
|
|
}
|
|
|
|
sort.Sort(floatRvSlice(mksv))
|
|
|
|
for i := range mksv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.EncodeFloat32(float32(mksv[i].v))
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
|
|
|
}
|
|
|
|
case reflect.Float64:
|
|
|
|
mksv := make([]floatRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
|
|
|
v.v = k.Float()
|
|
|
|
}
|
|
|
|
sort.Sort(floatRvSlice(mksv))
|
|
|
|
for i := range mksv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
ee.EncodeFloat64(mksv[i].v)
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
case reflect.Struct:
|
|
|
|
if rv.Type() == timeTyp {
|
|
|
|
mksv := make([]timeRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksv[i]
|
|
|
|
v.r = k
|
|
|
|
v.v = rv2i(k).(time.Time)
|
|
|
|
}
|
|
|
|
sort.Sort(timeRvSlice(mksv))
|
|
|
|
for i := range mksv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
|
|
|
}
|
|
|
|
ee.EncodeTime(mksv[i].v)
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
|
|
|
}
|
|
|
|
e.encodeValue(rv.MapIndex(mksv[i].r), valFn, true)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
2017-10-27 19:06:04 +00:00
|
|
|
default:
|
|
|
|
// out-of-band
|
|
|
|
// first encode each key to a []byte first, then sort them, then record
|
|
|
|
var mksv []byte = make([]byte, 0, len(mks)*16) // temporary byte slice for the encoding
|
|
|
|
e2 := NewEncoderBytes(&mksv, e.hh)
|
|
|
|
mksbv := make([]bytesRv, len(mks))
|
|
|
|
for i, k := range mks {
|
|
|
|
v := &mksbv[i]
|
|
|
|
l := len(mksv)
|
|
|
|
e2.MustEncode(k)
|
|
|
|
v.r = k
|
|
|
|
v.v = mksv[l:]
|
|
|
|
}
|
|
|
|
sort.Sort(bytesRvSlice(mksbv))
|
|
|
|
for j := range mksbv {
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemKey()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.asis(mksbv[j].v)
|
|
|
|
if elemsep {
|
|
|
|
ee.WriteMapElemValue()
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(rv.MapIndex(mksbv[j].r), valFn, true)
|
2015-10-15 21:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
// // --------------------------------------------------
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
type encWriterSwitch struct {
|
2019-04-12 15:51:37 +00:00
|
|
|
// wi *ioEncWriter
|
|
|
|
wb bytesEncAppender
|
|
|
|
wf *bufioEncWriter
|
|
|
|
// typ entryType
|
|
|
|
bytes bool // encoding to []byte
|
|
|
|
esep bool // whether it has elem separators
|
|
|
|
isas bool // whether e.as != nil
|
|
|
|
js bool // is json encoder?
|
|
|
|
be bool // is binary encoder?
|
|
|
|
_ [2]byte // padding
|
|
|
|
// _ [2]uint64 // padding
|
|
|
|
// _ uint64 // padding
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *encWriterSwitch) writeb(s []byte) {
|
|
|
|
if z.bytes {
|
|
|
|
z.wb.writeb(s)
|
|
|
|
} else {
|
|
|
|
z.wf.writeb(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writestr(s string) {
|
|
|
|
if z.bytes {
|
|
|
|
z.wb.writestr(s)
|
|
|
|
} else {
|
|
|
|
z.wf.writestr(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writen1(b1 byte) {
|
|
|
|
if z.bytes {
|
|
|
|
z.wb.writen1(b1)
|
|
|
|
} else {
|
|
|
|
z.wf.writen1(b1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writen2(b1, b2 byte) {
|
|
|
|
if z.bytes {
|
|
|
|
z.wb.writen2(b1, b2)
|
|
|
|
} else {
|
|
|
|
z.wf.writen2(b1, b2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) end() {
|
2019-04-13 07:44:06 +00:00
|
|
|
if z.bytes {
|
|
|
|
z.wb.end()
|
|
|
|
} else {
|
|
|
|
z.wf.end()
|
2019-04-12 15:51:37 +00:00
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
/*
|
2018-01-26 23:51:00 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// ------------------------------------------
|
|
|
|
func (z *encWriterSwitch) writeb(s []byte) {
|
|
|
|
switch z.typ {
|
|
|
|
case entryTypeBytes:
|
|
|
|
z.wb.writeb(s)
|
|
|
|
case entryTypeIo:
|
|
|
|
z.wi.writeb(s)
|
|
|
|
default:
|
|
|
|
z.wf.writeb(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writestr(s string) {
|
|
|
|
switch z.typ {
|
|
|
|
case entryTypeBytes:
|
|
|
|
z.wb.writestr(s)
|
|
|
|
case entryTypeIo:
|
|
|
|
z.wi.writestr(s)
|
|
|
|
default:
|
|
|
|
z.wf.writestr(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writen1(b1 byte) {
|
|
|
|
switch z.typ {
|
|
|
|
case entryTypeBytes:
|
|
|
|
z.wb.writen1(b1)
|
|
|
|
case entryTypeIo:
|
|
|
|
z.wi.writen1(b1)
|
|
|
|
default:
|
|
|
|
z.wf.writen1(b1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writen2(b1, b2 byte) {
|
|
|
|
switch z.typ {
|
|
|
|
case entryTypeBytes:
|
|
|
|
z.wb.writen2(b1, b2)
|
|
|
|
case entryTypeIo:
|
|
|
|
z.wi.writen2(b1, b2)
|
|
|
|
default:
|
|
|
|
z.wf.writen2(b1, b2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) end() {
|
|
|
|
switch z.typ {
|
|
|
|
case entryTypeBytes:
|
|
|
|
z.wb.end()
|
|
|
|
case entryTypeIo:
|
|
|
|
z.wi.end()
|
|
|
|
default:
|
|
|
|
z.wf.end()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------
|
2018-10-03 16:55:26 +00:00
|
|
|
func (z *encWriterSwitch) writeb(s []byte) {
|
2019-04-12 15:51:37 +00:00
|
|
|
if z.bytes {
|
2018-10-03 16:55:26 +00:00
|
|
|
z.wb.writeb(s)
|
|
|
|
} else {
|
|
|
|
z.wi.writeb(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writestr(s string) {
|
2019-04-12 15:51:37 +00:00
|
|
|
if z.bytes {
|
2018-10-03 16:55:26 +00:00
|
|
|
z.wb.writestr(s)
|
|
|
|
} else {
|
|
|
|
z.wi.writestr(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writen1(b1 byte) {
|
2019-04-12 15:51:37 +00:00
|
|
|
if z.bytes {
|
2018-10-03 16:55:26 +00:00
|
|
|
z.wb.writen1(b1)
|
|
|
|
} else {
|
|
|
|
z.wi.writen1(b1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (z *encWriterSwitch) writen2(b1, b2 byte) {
|
2019-04-12 15:51:37 +00:00
|
|
|
if z.bytes {
|
2018-10-03 16:55:26 +00:00
|
|
|
z.wb.writen2(b1, b2)
|
|
|
|
} else {
|
|
|
|
z.wi.writen2(b1, b2)
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
func (z *encWriterSwitch) end() {
|
|
|
|
if z.bytes {
|
|
|
|
z.wb.end()
|
2018-10-03 16:55:26 +00:00
|
|
|
} else {
|
2019-04-12 15:51:37 +00:00
|
|
|
z.wi.end()
|
2018-10-03 16:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
2018-01-26 23:51:00 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// Encoder writes an object to an output stream in a supported format.
|
|
|
|
//
|
|
|
|
// Encoder is NOT safe for concurrent use i.e. a Encoder cannot be used
|
|
|
|
// concurrently in multiple goroutines.
|
|
|
|
//
|
|
|
|
// However, as Encoder could be allocation heavy to initialize, a Reset method is provided
|
|
|
|
// so its state can be reused to decode new input streams repeatedly.
|
|
|
|
// This is the idiomatic way to use.
|
2015-06-29 21:50:55 +00:00
|
|
|
type Encoder struct {
|
2018-01-26 23:51:00 +00:00
|
|
|
panicHdl
|
2015-06-29 21:50:55 +00:00
|
|
|
// hopefully, reduce derefencing cost by laying the encWriter inside the Encoder
|
2015-09-22 15:44:21 +00:00
|
|
|
e encDriver
|
2019-04-12 15:51:37 +00:00
|
|
|
|
2015-09-22 15:44:21 +00:00
|
|
|
// NOTE: Encoder shouldn't call it's write methods,
|
|
|
|
// as the handler MAY need to do some coordination.
|
2019-04-12 15:51:37 +00:00
|
|
|
w *encWriterSwitch
|
2017-10-27 19:06:04 +00:00
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
// bw *bufio.Writer
|
2018-01-26 23:51:00 +00:00
|
|
|
as encDriverAsis
|
2017-10-27 19:06:04 +00:00
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
err error
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
h *BasicHandle
|
|
|
|
hh Handle
|
|
|
|
// ---- cpu cache line boundary? + 3
|
2018-01-26 23:51:00 +00:00
|
|
|
encWriterSwitch
|
2015-10-30 22:07:00 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
ci set
|
2017-10-27 19:06:04 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
b [(5 * 8)]byte // for encoding chan or (non-addressable) [N]byte
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// ---- writable fields during execution --- *try* to keep in sep cache line
|
2015-10-30 22:07:00 +00:00
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// ---- cpu cache line boundary?
|
|
|
|
// b [scratchByteArrayLen]byte
|
|
|
|
// _ [cacheLineSize - scratchByteArrayLen]byte // padding
|
2019-04-12 15:51:37 +00:00
|
|
|
// b [cacheLineSize - (8 * 0)]byte // used for encoding a chan or (non-addressable) array of bytes
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncoder returns an Encoder for encoding into an io.Writer.
|
|
|
|
//
|
2019-04-12 15:51:37 +00:00
|
|
|
// For efficiency, Users are encouraged to configure WriterBufferSize on the handle
|
|
|
|
// OR pass in a memory buffered writer (eg bufio.Writer, bytes.Buffer).
|
2015-06-29 21:50:55 +00:00
|
|
|
func NewEncoder(w io.Writer, h Handle) *Encoder {
|
2015-10-30 22:07:00 +00:00
|
|
|
e := newEncoder(h)
|
|
|
|
e.Reset(w)
|
2015-06-29 21:50:55 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncoderBytes returns an encoder for encoding directly and efficiently
|
|
|
|
// into a byte slice, using zero-copying to temporary slices.
|
|
|
|
//
|
|
|
|
// It will potentially replace the output byte slice pointed to.
|
|
|
|
// After encoding, the out parameter contains the encoded contents.
|
|
|
|
func NewEncoderBytes(out *[]byte, h Handle) *Encoder {
|
2015-10-30 22:07:00 +00:00
|
|
|
e := newEncoder(h)
|
|
|
|
e.ResetBytes(out)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEncoder(h Handle) *Encoder {
|
2019-04-12 15:51:37 +00:00
|
|
|
e := &Encoder{h: basicHandle(h), err: errEncoderNotInitialized}
|
|
|
|
e.bytes = true
|
|
|
|
if useFinalizers {
|
|
|
|
runtime.SetFinalizer(e, (*Encoder).finalize)
|
|
|
|
// xdebugf(">>>> new(Encoder) with finalizer")
|
|
|
|
}
|
|
|
|
e.w = &e.encWriterSwitch
|
2018-01-26 23:51:00 +00:00
|
|
|
e.hh = h
|
|
|
|
e.esep = h.hasElemSeparators()
|
2019-04-12 15:51:37 +00:00
|
|
|
|
2015-10-30 22:07:00 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
func (e *Encoder) resetCommon() {
|
2018-10-03 16:55:26 +00:00
|
|
|
// e.w = &e.encWriterSwitch
|
2018-01-26 23:51:00 +00:00
|
|
|
if e.e == nil || e.hh.recreateEncDriver(e.e) {
|
|
|
|
e.e = e.hh.newEncDriver(e)
|
|
|
|
e.as, e.isas = e.e.(encDriverAsis)
|
|
|
|
// e.cr, _ = e.e.(containerStateRecv)
|
|
|
|
}
|
|
|
|
e.be = e.hh.isBinary()
|
|
|
|
_, e.js = e.hh.(*JsonHandle)
|
|
|
|
e.e.reset()
|
|
|
|
e.err = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets the Encoder with a new output stream.
|
2015-10-30 22:07:00 +00:00
|
|
|
//
|
2016-07-23 00:11:47 +00:00
|
|
|
// This accommodates using the state of the Encoder,
|
2015-10-30 22:07:00 +00:00
|
|
|
// where it has "cached" information about sub-engines.
|
|
|
|
func (e *Encoder) Reset(w io.Writer) {
|
2018-01-26 23:51:00 +00:00
|
|
|
if w == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
// var ok bool
|
|
|
|
e.bytes = false
|
|
|
|
if e.wf == nil {
|
|
|
|
e.wf = new(bufioEncWriter)
|
2018-01-26 23:51:00 +00:00
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
// e.typ = entryTypeUnset
|
|
|
|
// if e.h.WriterBufferSize > 0 {
|
|
|
|
// // bw := bufio.NewWriterSize(w, e.h.WriterBufferSize)
|
|
|
|
// // e.wi.bw = bw
|
|
|
|
// // e.wi.sw = bw
|
|
|
|
// // e.wi.fw = bw
|
|
|
|
// // e.wi.ww = bw
|
|
|
|
// if e.wf == nil {
|
|
|
|
// e.wf = new(bufioEncWriter)
|
|
|
|
// }
|
|
|
|
// e.wf.reset(w, e.h.WriterBufferSize)
|
|
|
|
// e.typ = entryTypeBufio
|
|
|
|
// } else {
|
|
|
|
// if e.wi == nil {
|
|
|
|
// e.wi = new(ioEncWriter)
|
|
|
|
// }
|
|
|
|
// e.wi.reset(w)
|
|
|
|
// e.typ = entryTypeIo
|
|
|
|
// }
|
|
|
|
e.wf.reset(w, e.h.WriterBufferSize)
|
|
|
|
// e.typ = entryTypeBufio
|
|
|
|
|
|
|
|
// e.w = e.wi
|
2018-01-26 23:51:00 +00:00
|
|
|
e.resetCommon()
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 23:51:00 +00:00
|
|
|
// ResetBytes resets the Encoder with a new destination output []byte.
|
2015-10-30 22:07:00 +00:00
|
|
|
func (e *Encoder) ResetBytes(out *[]byte) {
|
2018-01-26 23:51:00 +00:00
|
|
|
if out == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
var in []byte = *out
|
2015-06-29 21:50:55 +00:00
|
|
|
if in == nil {
|
|
|
|
in = make([]byte, defEncByteBufSize)
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
e.bytes = true
|
|
|
|
// e.typ = entryTypeBytes
|
2018-01-26 23:51:00 +00:00
|
|
|
e.wb.reset(in, out)
|
2019-04-12 15:51:37 +00:00
|
|
|
// e.w = &e.wb
|
2018-01-26 23:51:00 +00:00
|
|
|
e.resetCommon()
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode writes an object into a stream.
|
|
|
|
//
|
|
|
|
// Encoding can be configured via the struct tag for the fields.
|
2018-07-06 16:09:34 +00:00
|
|
|
// The key (in the struct tags) that we look at is configurable.
|
|
|
|
//
|
|
|
|
// By default, we look up the "codec" key in the struct field's tags,
|
|
|
|
// and fall bak to the "json" key if "codec" is absent.
|
|
|
|
// That key in struct field's tag value is the key name,
|
2015-06-29 21:50:55 +00:00
|
|
|
// followed by an optional comma and options.
|
|
|
|
//
|
|
|
|
// To set an option on all fields (e.g. omitempty on all fields), you
|
2018-01-26 23:51:00 +00:00
|
|
|
// can create a field called _struct, and set flags on it. The options
|
|
|
|
// which can be set on _struct are:
|
|
|
|
// - omitempty: so all fields are omitted if empty
|
|
|
|
// - toarray: so struct is encoded as an array
|
|
|
|
// - int: so struct key names are encoded as signed integers (instead of strings)
|
|
|
|
// - uint: so struct key names are encoded as unsigned integers (instead of strings)
|
|
|
|
// - float: so struct key names are encoded as floats (instead of strings)
|
|
|
|
// More details on these below.
|
2015-06-29 21:50:55 +00:00
|
|
|
//
|
|
|
|
// Struct values "usually" encode as maps. Each exported struct field is encoded unless:
|
|
|
|
// - the field's tag is "-", OR
|
|
|
|
// - the field is empty (empty or the zero value) and its tag specifies the "omitempty" option.
|
|
|
|
//
|
|
|
|
// When encoding as a map, the first string in the tag (before the comma)
|
|
|
|
// is the map key string to use when encoding.
|
2018-01-26 23:51:00 +00:00
|
|
|
// ...
|
|
|
|
// This key is typically encoded as a string.
|
|
|
|
// However, there are instances where the encoded stream has mapping keys encoded as numbers.
|
|
|
|
// For example, some cbor streams have keys as integer codes in the stream, but they should map
|
|
|
|
// to fields in a structured object. Consequently, a struct is the natural representation in code.
|
|
|
|
// For these, configure the struct to encode/decode the keys as numbers (instead of string).
|
|
|
|
// This is done with the int,uint or float option on the _struct field (see above).
|
2015-06-29 21:50:55 +00:00
|
|
|
//
|
|
|
|
// However, struct values may encode as arrays. This happens when:
|
|
|
|
// - StructToArray Encode option is set, OR
|
|
|
|
// - the tag on the _struct field sets the "toarray" option
|
2017-07-18 14:15:54 +00:00
|
|
|
// Note that omitempty is ignored when encoding struct values as arrays,
|
|
|
|
// as an entry must be encoded for each field, to maintain its position.
|
2015-06-29 21:50:55 +00:00
|
|
|
//
|
|
|
|
// Values with types that implement MapBySlice are encoded as stream maps.
|
|
|
|
//
|
|
|
|
// The empty values (for omitempty option) are false, 0, any nil pointer
|
|
|
|
// or interface value, and any array, slice, map, or string of length zero.
|
|
|
|
//
|
2015-09-22 15:44:21 +00:00
|
|
|
// Anonymous fields are encoded inline except:
|
|
|
|
// - the struct tag specifies a replacement name (first value)
|
|
|
|
// - the field is of an interface type
|
2015-06-29 21:50:55 +00:00
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// // NOTE: 'json:' can be used as struct tag key, in place 'codec:' below.
|
|
|
|
// type MyStruct struct {
|
|
|
|
// _struct bool `codec:",omitempty"` //set omitempty for every field
|
|
|
|
// Field1 string `codec:"-"` //skip this field
|
|
|
|
// Field2 int `codec:"myName"` //Use key "myName" in encode stream
|
|
|
|
// Field3 int32 `codec:",omitempty"` //use key "Field3". Omit if empty.
|
|
|
|
// Field4 bool `codec:"f4,omitempty"` //use key "f4". Omit if empty.
|
2015-09-22 15:44:21 +00:00
|
|
|
// io.Reader //use key "Reader".
|
|
|
|
// MyStruct `codec:"my1" //use key "my1".
|
|
|
|
// MyStruct //inline it
|
2015-06-29 21:50:55 +00:00
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// type MyStruct struct {
|
2018-01-26 23:51:00 +00:00
|
|
|
// _struct bool `codec:",toarray"` //encode struct as an array
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// type MyStruct struct {
|
|
|
|
// _struct bool `codec:",uint"` //encode struct with "unsigned integer" keys
|
|
|
|
// Field1 string `codec:"1"` //encode Field1 key using: EncodeInt(1)
|
|
|
|
// Field2 string `codec:"2"` //encode Field2 key using: EncodeInt(2)
|
2015-06-29 21:50:55 +00:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The mode of encoding is based on the type of the value. When a value is seen:
|
2015-09-22 15:44:21 +00:00
|
|
|
// - If a Selfer, call its CodecEncodeSelf method
|
2015-06-29 21:50:55 +00:00
|
|
|
// - If an extension is registered for it, call that extension function
|
2018-01-26 23:51:00 +00:00
|
|
|
// - If implements encoding.(Binary|Text|JSON)Marshaler, call Marshal(Binary|Text|JSON) method
|
2015-06-29 21:50:55 +00:00
|
|
|
// - Else encode it based on its reflect.Kind
|
|
|
|
//
|
|
|
|
// Note that struct field names and keys in map[string]XXX will be treated as symbols.
|
|
|
|
// Some formats support symbols (e.g. binc) and will properly encode the string
|
|
|
|
// only once in the stream, and use a tag to refer to it thereafter.
|
|
|
|
func (e *Encoder) Encode(v interface{}) (err error) {
|
2019-04-12 15:51:37 +00:00
|
|
|
// tried to use closure, as runtime optimizes defer with no params.
|
|
|
|
// This seemed to be causing weird issues (like circular reference found, unexpected panic, etc).
|
|
|
|
// Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139
|
|
|
|
// defer func() { e.deferred(&err) }() }
|
|
|
|
// { x, y := e, &err; defer func() { x.deferred(y) }() }
|
|
|
|
if e.err != nil {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
if recoverPanicToErr {
|
|
|
|
defer func() {
|
2019-04-13 07:44:06 +00:00
|
|
|
e.w.end()
|
|
|
|
if x := recover(); x != nil {
|
2019-04-12 15:51:37 +00:00
|
|
|
panicValToErr(e, x, &e.err)
|
|
|
|
err = e.err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// defer e.deferred(&err)
|
|
|
|
e.mustEncode(v)
|
2015-06-29 21:50:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustEncode is like Encode, but panics if unable to Encode.
|
|
|
|
// This provides insight to the code location that triggered the error.
|
|
|
|
func (e *Encoder) MustEncode(v interface{}) {
|
2017-10-27 19:06:04 +00:00
|
|
|
if e.err != nil {
|
|
|
|
panic(e.err)
|
|
|
|
}
|
2019-04-12 15:51:37 +00:00
|
|
|
e.mustEncode(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) mustEncode(v interface{}) {
|
|
|
|
if e.wf == nil {
|
|
|
|
e.encode(v)
|
|
|
|
e.e.atEndOfEncode()
|
|
|
|
e.w.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.wf.buf == nil {
|
|
|
|
e.wf.buf = e.wf.bytesBufPooler.get(e.wf.sz)
|
|
|
|
}
|
|
|
|
e.wf.calls++
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
e.encode(v)
|
2017-10-27 19:06:04 +00:00
|
|
|
e.e.atEndOfEncode()
|
2019-04-12 15:51:37 +00:00
|
|
|
e.w.end()
|
2015-06-29 21:50:55 +00:00
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
e.wf.calls--
|
|
|
|
|
|
|
|
if !e.h.ExplicitRelease && e.wf.calls == 0 {
|
|
|
|
e.wf.release()
|
2018-07-06 16:09:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// func (e *Encoder) deferred(err1 *error) {
|
|
|
|
// e.w.end()
|
|
|
|
// if recoverPanicToErr {
|
|
|
|
// if x := recover(); x != nil {
|
|
|
|
// panicValToErr(e, x, err1)
|
|
|
|
// panicValToErr(e, x, &e.err)
|
|
|
|
// }
|
|
|
|
// }
|
2018-01-26 23:51:00 +00:00
|
|
|
// }
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
//go:noinline -- as it is run by finalizer
|
|
|
|
func (e *Encoder) finalize() {
|
|
|
|
// xdebugf("finalizing Encoder")
|
|
|
|
e.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release releases shared (pooled) resources.
|
|
|
|
//
|
|
|
|
// It is important to call Release() when done with an Encoder, so those resources
|
|
|
|
// are released instantly for use by subsequently created Encoders.
|
|
|
|
func (e *Encoder) Release() {
|
|
|
|
if e.wf != nil {
|
|
|
|
e.wf.release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
func (e *Encoder) encode(iv interface{}) {
|
2019-04-12 15:51:37 +00:00
|
|
|
// a switch with only concrete types can be optimized.
|
|
|
|
// consequently, we deal with nil and interfaces outside the switch.
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
if iv == nil || definitelyNil(iv) {
|
2015-06-29 21:50:55 +00:00
|
|
|
e.e.EncodeNil()
|
2017-10-27 19:06:04 +00:00
|
|
|
return
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
switch v := iv.(type) {
|
2019-04-12 15:51:37 +00:00
|
|
|
// case nil:
|
|
|
|
// case Selfer:
|
2016-09-12 19:16:55 +00:00
|
|
|
case Raw:
|
2017-10-27 19:06:04 +00:00
|
|
|
e.rawBytes(v)
|
2015-06-29 21:50:55 +00:00
|
|
|
case reflect.Value:
|
2017-10-27 19:06:04 +00:00
|
|
|
e.encodeValue(v, nil, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
|
|
|
|
case string:
|
2019-04-13 07:44:06 +00:00
|
|
|
e.e.EncodeStringEnc(cUTF8, v)
|
2015-06-29 21:50:55 +00:00
|
|
|
case bool:
|
|
|
|
e.e.EncodeBool(v)
|
|
|
|
case int:
|
|
|
|
e.e.EncodeInt(int64(v))
|
|
|
|
case int8:
|
|
|
|
e.e.EncodeInt(int64(v))
|
|
|
|
case int16:
|
|
|
|
e.e.EncodeInt(int64(v))
|
|
|
|
case int32:
|
|
|
|
e.e.EncodeInt(int64(v))
|
|
|
|
case int64:
|
|
|
|
e.e.EncodeInt(v)
|
|
|
|
case uint:
|
|
|
|
e.e.EncodeUint(uint64(v))
|
|
|
|
case uint8:
|
|
|
|
e.e.EncodeUint(uint64(v))
|
|
|
|
case uint16:
|
|
|
|
e.e.EncodeUint(uint64(v))
|
|
|
|
case uint32:
|
|
|
|
e.e.EncodeUint(uint64(v))
|
|
|
|
case uint64:
|
|
|
|
e.e.EncodeUint(v)
|
2017-10-27 19:06:04 +00:00
|
|
|
case uintptr:
|
|
|
|
e.e.EncodeUint(uint64(v))
|
2015-06-29 21:50:55 +00:00
|
|
|
case float32:
|
|
|
|
e.e.EncodeFloat32(v)
|
|
|
|
case float64:
|
|
|
|
e.e.EncodeFloat64(v)
|
2018-01-26 23:51:00 +00:00
|
|
|
case time.Time:
|
|
|
|
e.e.EncodeTime(v)
|
2015-06-29 21:50:55 +00:00
|
|
|
case []uint8:
|
2019-04-12 15:51:37 +00:00
|
|
|
e.e.EncodeStringBytesRaw(v)
|
2018-01-26 23:51:00 +00:00
|
|
|
|
|
|
|
case *Raw:
|
|
|
|
e.rawBytes(*v)
|
2015-06-29 21:50:55 +00:00
|
|
|
|
|
|
|
case *string:
|
2019-04-13 07:44:06 +00:00
|
|
|
e.e.EncodeStringEnc(cUTF8, *v)
|
2015-06-29 21:50:55 +00:00
|
|
|
case *bool:
|
|
|
|
e.e.EncodeBool(*v)
|
|
|
|
case *int:
|
|
|
|
e.e.EncodeInt(int64(*v))
|
|
|
|
case *int8:
|
|
|
|
e.e.EncodeInt(int64(*v))
|
|
|
|
case *int16:
|
|
|
|
e.e.EncodeInt(int64(*v))
|
|
|
|
case *int32:
|
|
|
|
e.e.EncodeInt(int64(*v))
|
|
|
|
case *int64:
|
|
|
|
e.e.EncodeInt(*v)
|
|
|
|
case *uint:
|
|
|
|
e.e.EncodeUint(uint64(*v))
|
|
|
|
case *uint8:
|
|
|
|
e.e.EncodeUint(uint64(*v))
|
|
|
|
case *uint16:
|
|
|
|
e.e.EncodeUint(uint64(*v))
|
|
|
|
case *uint32:
|
|
|
|
e.e.EncodeUint(uint64(*v))
|
|
|
|
case *uint64:
|
|
|
|
e.e.EncodeUint(*v)
|
2017-10-27 19:06:04 +00:00
|
|
|
case *uintptr:
|
|
|
|
e.e.EncodeUint(uint64(*v))
|
2015-06-29 21:50:55 +00:00
|
|
|
case *float32:
|
|
|
|
e.e.EncodeFloat32(*v)
|
|
|
|
case *float64:
|
|
|
|
e.e.EncodeFloat64(*v)
|
2018-01-26 23:51:00 +00:00
|
|
|
case *time.Time:
|
|
|
|
e.e.EncodeTime(*v)
|
2015-06-29 21:50:55 +00:00
|
|
|
|
|
|
|
case *[]uint8:
|
2019-04-12 15:51:37 +00:00
|
|
|
e.e.EncodeStringBytesRaw(*v)
|
2015-06-29 21:50:55 +00:00
|
|
|
|
|
|
|
default:
|
2019-04-12 15:51:37 +00:00
|
|
|
if v, ok := iv.(Selfer); ok {
|
|
|
|
v.CodecEncodeSelf(e)
|
|
|
|
} else if !fastpathEncodeTypeSwitch(iv, e) {
|
2017-10-27 19:06:04 +00:00
|
|
|
// checkfastpath=true (not false), as underlying slice/map type may be fast-path
|
|
|
|
e.encodeValue(reflect.ValueOf(iv), nil, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) encodeValue(rv reflect.Value, fn *codecFn, checkFastpath bool) {
|
|
|
|
// if a valid fn is passed, it MUST BE for the dereferenced type of rv
|
|
|
|
var sptr uintptr
|
|
|
|
var rvp reflect.Value
|
|
|
|
var rvpValid bool
|
2015-10-30 22:07:00 +00:00
|
|
|
TOP:
|
|
|
|
switch rv.Kind() {
|
2016-04-26 00:18:04 +00:00
|
|
|
case reflect.Ptr:
|
|
|
|
if rv.IsNil() {
|
|
|
|
e.e.EncodeNil()
|
|
|
|
return
|
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
rvpValid = true
|
|
|
|
rvp = rv
|
2016-04-26 00:18:04 +00:00
|
|
|
rv = rv.Elem()
|
|
|
|
if e.h.CheckCircularRef && rv.Kind() == reflect.Struct {
|
|
|
|
// TODO: Movable pointers will be an issue here. Future problem.
|
|
|
|
sptr = rv.UnsafeAddr()
|
|
|
|
break TOP
|
|
|
|
}
|
|
|
|
goto TOP
|
|
|
|
case reflect.Interface:
|
2015-10-30 22:07:00 +00:00
|
|
|
if rv.IsNil() {
|
2015-06-29 21:50:55 +00:00
|
|
|
e.e.EncodeNil()
|
|
|
|
return
|
|
|
|
}
|
2015-10-30 22:07:00 +00:00
|
|
|
rv = rv.Elem()
|
|
|
|
goto TOP
|
|
|
|
case reflect.Slice, reflect.Map:
|
|
|
|
if rv.IsNil() {
|
|
|
|
e.e.EncodeNil()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case reflect.Invalid, reflect.Func:
|
|
|
|
e.e.EncodeNil()
|
|
|
|
return
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
if sptr != 0 && (&e.ci).add(sptr) {
|
|
|
|
e.errorf("circular reference found: # %d", sptr)
|
2016-04-26 00:18:04 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
|
2016-04-26 00:18:04 +00:00
|
|
|
if fn == nil {
|
|
|
|
rt := rv.Type()
|
2017-10-27 19:06:04 +00:00
|
|
|
// always pass checkCodecSelfer=true, in case T or ****T is passed, where *T is a Selfer
|
2019-04-12 15:51:37 +00:00
|
|
|
fn = e.h.fn(rt, checkFastpath, true)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
if fn.i.addrE {
|
|
|
|
if rvpValid {
|
|
|
|
fn.fe(e, &fn.i, rvp)
|
|
|
|
} else if rv.CanAddr() {
|
|
|
|
fn.fe(e, &fn.i, rv.Addr())
|
|
|
|
} else {
|
|
|
|
rv2 := reflect.New(rv.Type())
|
|
|
|
rv2.Elem().Set(rv)
|
|
|
|
fn.fe(e, &fn.i, rv2)
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-27 19:06:04 +00:00
|
|
|
fn.fe(e, &fn.i, rv)
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
if sptr != 0 {
|
|
|
|
(&e.ci).remove(sptr)
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 15:51:37 +00:00
|
|
|
// func (e *Encoder) marshal(bs []byte, fnerr error, asis bool, c charEncoding) {
|
|
|
|
// if fnerr != nil {
|
|
|
|
// panic(fnerr)
|
|
|
|
// }
|
|
|
|
// if bs == nil {
|
|
|
|
// e.e.EncodeNil()
|
|
|
|
// } else if asis {
|
|
|
|
// e.asis(bs)
|
|
|
|
// } else {
|
2019-04-13 07:44:06 +00:00
|
|
|
// e.e.EncodeStringBytes(c, bs)
|
2019-04-12 15:51:37 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (e *Encoder) marshalUtf8(bs []byte, fnerr error) {
|
2015-09-22 15:44:21 +00:00
|
|
|
if fnerr != nil {
|
|
|
|
panic(fnerr)
|
|
|
|
}
|
|
|
|
if bs == nil {
|
|
|
|
e.e.EncodeNil()
|
2019-04-12 15:51:37 +00:00
|
|
|
} else {
|
|
|
|
e.e.EncodeStringEnc(cUTF8, stringView(bs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) marshalAsis(bs []byte, fnerr error) {
|
|
|
|
if fnerr != nil {
|
|
|
|
panic(fnerr)
|
|
|
|
}
|
|
|
|
if bs == nil {
|
|
|
|
e.e.EncodeNil()
|
|
|
|
} else {
|
2015-09-22 15:44:21 +00:00
|
|
|
e.asis(bs)
|
2019-04-12 15:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) marshalRaw(bs []byte, fnerr error) {
|
|
|
|
if fnerr != nil {
|
|
|
|
panic(fnerr)
|
|
|
|
}
|
|
|
|
if bs == nil {
|
|
|
|
e.e.EncodeNil()
|
2015-09-22 15:44:21 +00:00
|
|
|
} else {
|
2019-04-12 15:51:37 +00:00
|
|
|
e.e.EncodeStringBytesRaw(bs)
|
2015-09-22 15:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Encoder) asis(v []byte) {
|
2018-01-26 23:51:00 +00:00
|
|
|
if e.isas {
|
2015-09-22 15:44:21 +00:00
|
|
|
e.as.EncodeAsis(v)
|
2018-01-26 23:51:00 +00:00
|
|
|
} else {
|
|
|
|
e.w.writeb(v)
|
2015-09-22 15:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:06:04 +00:00
|
|
|
func (e *Encoder) rawBytes(vv Raw) {
|
2016-09-12 19:16:55 +00:00
|
|
|
v := []byte(vv)
|
|
|
|
if !e.h.Raw {
|
|
|
|
e.errorf("Raw values cannot be encoded: %v", v)
|
|
|
|
}
|
2018-01-26 23:51:00 +00:00
|
|
|
e.asis(v)
|
2016-09-12 19:16:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 16:55:26 +00:00
|
|
|
func (e *Encoder) wrapErr(v interface{}, err *error) {
|
|
|
|
*err = encodeError{codecError{name: e.hh.Name(), err: v}}
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|