| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894 |
- // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
- // Use of this source code is governed by a BSD-style license found in the LICENSE file.
- package codec
- import (
- "io"
- "reflect"
- )
- const (
- // Some tagging information for error messages.
- msgTagEnc = "codec.encoder"
- defEncByteBufSize = 1 << 6 // 4:16, 6:64, 8:256, 10:1024
- // maxTimeSecs32 = math.MaxInt32 / 60 / 24 / 366
- )
- // AsSymbolFlag defines what should be encoded as symbols.
- type AsSymbolFlag uint8
- const (
- // AsSymbolDefault is default.
- // Currently, this means only encode struct field names as symbols.
- // The default is subject to change.
- AsSymbolDefault AsSymbolFlag = iota
- // AsSymbolAll means encode anything which could be a symbol as a symbol.
- AsSymbolAll = 0xfe
- // AsSymbolNone means do not encode anything as a symbol.
- AsSymbolNone = 1 << iota
- // AsSymbolMapStringKeys means encode keys in map[string]XXX as symbols.
- AsSymbolMapStringKeysFlag
- // AsSymbolStructFieldName means encode struct field names as symbols.
- AsSymbolStructFieldNameFlag
- )
- // fastpathsEnc holds the rtid (reflect.Type Pointer) to fast encode function for a selected slice/map type.
- var fastpathsEnc = make(map[uintptr]func(*encFnInfo, reflect.Value))
- // encWriter abstracts writing to a byte array or to an io.Writer.
- type encWriter interface {
- writeUint16(uint16)
- writeUint32(uint32)
- writeUint64(uint64)
- writeb([]byte)
- writestr(string)
- writen1(byte)
- writen2(byte, byte)
- atEndOfEncode()
- }
- // encDriver abstracts the actual codec (binc vs msgpack, etc)
- type encDriver interface {
- isBuiltinType(rt uintptr) bool
- encodeBuiltin(rt uintptr, v 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(rv reflect.Value, xtag uint64, ext Ext, e *Encoder)
- encodeArrayPreamble(length int)
- encodeMapPreamble(length int)
- encodeString(c charEncoding, v string)
- encodeSymbol(v string)
- encodeStringBytes(c charEncoding, v []byte)
- //TODO
- //encBignum(f *big.Int)
- //encStringRunes(c charEncoding, v []rune)
- }
- type ioEncWriterWriter interface {
- WriteByte(c byte) error
- WriteString(s string) (n int, err error)
- Write(p []byte) (n int, err error)
- }
- type ioEncStringWriter interface {
- WriteString(s string) (n int, err error)
- }
- type EncodeOptions struct {
- // Encode a struct as an array, and not as a map.
- StructToArray bool
- // 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
- }
- // ---------------------------------------------
- type simpleIoEncWriterWriter struct {
- w io.Writer
- bw io.ByteWriter
- sw ioEncStringWriter
- }
- func (o *simpleIoEncWriterWriter) WriteByte(c byte) (err error) {
- if o.bw != nil {
- return o.bw.WriteByte(c)
- }
- _, err = o.w.Write([]byte{c})
- return
- }
- func (o *simpleIoEncWriterWriter) WriteString(s string) (n int, err error) {
- if o.sw != nil {
- return o.sw.WriteString(s)
- }
- return o.w.Write([]byte(s))
- }
- func (o *simpleIoEncWriterWriter) Write(p []byte) (n int, err error) {
- return o.w.Write(p)
- }
- // ----------------------------------------
- // ioEncWriter implements encWriter and can write to an io.Writer implementation
- type ioEncWriter struct {
- w ioEncWriterWriter
- x [8]byte // temp byte array re-used internally for efficiency
- }
- func (z *ioEncWriter) writeUint16(v uint16) {
- bigen.PutUint16(z.x[:2], v)
- z.writeb(z.x[:2])
- }
- func (z *ioEncWriter) writeUint32(v uint32) {
- bigen.PutUint32(z.x[:4], v)
- z.writeb(z.x[:4])
- }
- func (z *ioEncWriter) writeUint64(v uint64) {
- bigen.PutUint64(z.x[:8], v)
- z.writeb(z.x[:8])
- }
- func (z *ioEncWriter) writeb(bs []byte) {
- if len(bs) == 0 {
- return
- }
- n, err := z.w.Write(bs)
- if err != nil {
- panic(err)
- }
- if n != len(bs) {
- encErr("write: Incorrect num bytes written. Expecting: %v, Wrote: %v", len(bs), n)
- }
- }
- func (z *ioEncWriter) writestr(s string) {
- n, err := z.w.WriteString(s)
- if err != nil {
- panic(err)
- }
- if n != len(s) {
- encErr("write: Incorrect num bytes written. Expecting: %v, Wrote: %v", len(s), n)
- }
- }
- func (z *ioEncWriter) writen1(b byte) {
- if err := z.w.WriteByte(b); err != nil {
- panic(err)
- }
- }
- func (z *ioEncWriter) writen2(b1 byte, b2 byte) {
- z.writen1(b1)
- z.writen1(b2)
- }
- func (z *ioEncWriter) atEndOfEncode() {}
- // ----------------------------------------
- // bytesEncWriter implements encWriter and can write to an byte slice.
- // It is used by Marshal function.
- type bytesEncWriter struct {
- b []byte
- c int // cursor
- out *[]byte // write out on atEndOfEncode
- }
- func (z *bytesEncWriter) writeUint16(v uint16) {
- c := z.grow(2)
- z.b[c] = byte(v >> 8)
- z.b[c+1] = byte(v)
- }
- func (z *bytesEncWriter) writeUint32(v uint32) {
- c := z.grow(4)
- z.b[c] = byte(v >> 24)
- z.b[c+1] = byte(v >> 16)
- z.b[c+2] = byte(v >> 8)
- z.b[c+3] = byte(v)
- }
- func (z *bytesEncWriter) writeUint64(v uint64) {
- c := z.grow(8)
- z.b[c] = byte(v >> 56)
- z.b[c+1] = byte(v >> 48)
- z.b[c+2] = byte(v >> 40)
- z.b[c+3] = byte(v >> 32)
- z.b[c+4] = byte(v >> 24)
- z.b[c+5] = byte(v >> 16)
- z.b[c+6] = byte(v >> 8)
- z.b[c+7] = byte(v)
- }
- func (z *bytesEncWriter) writeb(s []byte) {
- if len(s) == 0 {
- return
- }
- c := z.grow(len(s))
- copy(z.b[c:], s)
- }
- func (z *bytesEncWriter) writestr(s string) {
- c := z.grow(len(s))
- copy(z.b[c:], s)
- }
- func (z *bytesEncWriter) writen1(b1 byte) {
- c := z.grow(1)
- z.b[c] = b1
- }
- func (z *bytesEncWriter) writen2(b1 byte, b2 byte) {
- c := z.grow(2)
- z.b[c] = b1
- z.b[c+1] = b2
- }
- func (z *bytesEncWriter) atEndOfEncode() {
- *(z.out) = z.b[:z.c]
- }
- func (z *bytesEncWriter) grow(n int) (oldcursor int) {
- oldcursor = z.c
- z.c = oldcursor + n
- if z.c > cap(z.b) {
- // Tried using appendslice logic: (if cap < 1024, *2, else *1.25).
- // However, it was too expensive, causing too many iterations of copy.
- // Using bytes.Buffer model was much better (2*cap + n)
- bs := make([]byte, 2*cap(z.b)+n)
- copy(bs, z.b[:oldcursor])
- z.b = bs
- } else if z.c > len(z.b) {
- z.b = z.b[:cap(z.b)]
- }
- return
- }
- // ---------------------------------------------
- type encFnInfo struct {
- ti *typeInfo
- e *Encoder
- ee encDriver
- xfFn Ext
- xfTag uint64
- }
- func (f *encFnInfo) builtin(rv reflect.Value) {
- f.ee.encodeBuiltin(f.ti.rtid, rv.Interface())
- }
- func (f *encFnInfo) rawExt(rv reflect.Value) {
- f.ee.encodeRawExt(rv.Interface().(*RawExt), f.e)
- }
- func (f *encFnInfo) ext(rv reflect.Value) {
- f.ee.encodeExt(rv, f.xfTag, f.xfFn, f.e)
- }
- func (f *encFnInfo) binaryMarshal(rv reflect.Value) {
- var bm binaryMarshaler
- if f.ti.mIndir == 0 {
- bm = rv.Interface().(binaryMarshaler)
- } else if f.ti.mIndir == -1 {
- bm = rv.Addr().Interface().(binaryMarshaler)
- } else {
- for j, k := int8(0), f.ti.mIndir; j < k; j++ {
- if rv.IsNil() {
- f.ee.encodeNil()
- return
- }
- rv = rv.Elem()
- }
- bm = rv.Interface().(binaryMarshaler)
- }
- // debugf(">>>> binaryMarshaler: %T", rv.Interface())
- bs, fnerr := bm.MarshalBinary()
- if fnerr != nil {
- panic(fnerr)
- }
- if bs == nil {
- f.ee.encodeNil()
- } else {
- f.ee.encodeStringBytes(c_RAW, bs)
- }
- }
- func (f *encFnInfo) kBool(rv reflect.Value) {
- f.ee.encodeBool(rv.Bool())
- }
- func (f *encFnInfo) kString(rv reflect.Value) {
- f.ee.encodeString(c_UTF8, rv.String())
- }
- func (f *encFnInfo) kFloat64(rv reflect.Value) {
- f.ee.encodeFloat64(rv.Float())
- }
- func (f *encFnInfo) kFloat32(rv reflect.Value) {
- f.ee.encodeFloat32(float32(rv.Float()))
- }
- func (f *encFnInfo) kInt(rv reflect.Value) {
- f.ee.encodeInt(rv.Int())
- }
- func (f *encFnInfo) kUint(rv reflect.Value) {
- f.ee.encodeUint(rv.Uint())
- }
- func (f *encFnInfo) kInvalid(rv reflect.Value) {
- f.ee.encodeNil()
- }
- func (f *encFnInfo) kErr(rv reflect.Value) {
- encErr("Unsupported kind: %s, for: %#v", rv.Kind(), rv)
- }
- func (f *encFnInfo) kSlice(rv reflect.Value) {
- if rv.IsNil() {
- f.ee.encodeNil()
- return
- }
- // If in this method, then there was no extension function defined.
- // So it's okay to treat as []byte.
- if f.ti.rtid == uint8SliceTypId || f.ti.rt.Elem().Kind() == reflect.Uint8 {
- f.ee.encodeStringBytes(c_RAW, rv.Bytes())
- return
- }
- l := rv.Len()
- if f.ti.mbs {
- if l%2 == 1 {
- encErr("mapBySlice: invalid length (must be divisible by 2): %v", l)
- }
- f.ee.encodeMapPreamble(l / 2)
- } else {
- f.ee.encodeArrayPreamble(l)
- }
- if l == 0 {
- return
- }
- rtelem := f.ti.rt.Elem()
- for rtelem.Kind() == reflect.Ptr {
- rtelem = rtelem.Elem()
- }
- fn := f.e.getEncFn(rtelem)
- for j := 0; j < l; j++ {
- // TODO: Consider perf implication of encoding odd index values as symbols if type is string
- f.e.encodeValue(rv.Index(j), fn)
- }
- }
- func (f *encFnInfo) kArray(rv reflect.Value) {
- // We cannot share kSlice method, because the array may be non-addressable.
- // E.g. type struct S{B [2]byte}; Encode(S{}) will bomb on "panic: slice of unaddressable array".
- // So we have to duplicate the functionality here.
- // f.e.encodeValue(rv.Slice(0, rv.Len()))
- // f.kSlice(rv.Slice(0, rv.Len()))
- l := rv.Len()
- // Handle an array of bytes specially (in line with what is done for slices)
- rtelem := f.ti.rt.Elem()
- if rtelem.Kind() == reflect.Uint8 {
- if l == 0 {
- f.ee.encodeStringBytes(c_RAW, nil)
- return
- }
- var bs []byte
- if rv.CanAddr() {
- bs = rv.Slice(0, l).Bytes()
- } else {
- bs = make([]byte, l)
- for i := 0; i < l; i++ {
- bs[i] = byte(rv.Index(i).Uint())
- }
- }
- f.ee.encodeStringBytes(c_RAW, bs)
- return
- }
- if f.ti.mbs {
- if l%2 == 1 {
- encErr("mapBySlice: invalid length (must be divisible by 2): %v", l)
- }
- f.ee.encodeMapPreamble(l / 2)
- } else {
- f.ee.encodeArrayPreamble(l)
- }
- if l == 0 {
- return
- }
- for rtelem.Kind() == reflect.Ptr {
- rtelem = rtelem.Elem()
- }
- fn := f.e.getEncFn(rtelem)
- for j := 0; j < l; j++ {
- // TODO: Consider perf implication of encoding odd index values as symbols if type is string
- f.e.encodeValue(rv.Index(j), fn)
- }
- }
- func (f *encFnInfo) kStruct(rv reflect.Value) {
- fti := f.ti
- newlen := len(fti.sfi)
- rvals := make([]reflect.Value, newlen)
- var encnames []string
- e := f.e
- tisfi := fti.sfip
- toMap := !(fti.toArray || e.h.StructToArray)
- // if toMap, use the sorted array. If toArray, use unsorted array (to match sequence in struct)
- if toMap {
- tisfi = fti.sfi
- encnames = make([]string, newlen)
- }
- newlen = 0
- for _, si := range tisfi {
- if si.i != -1 {
- rvals[newlen] = rv.Field(int(si.i))
- } else {
- rvals[newlen] = rv.FieldByIndex(si.is)
- }
- if toMap {
- if si.omitEmpty && isEmptyValue(rvals[newlen]) {
- continue
- }
- encnames[newlen] = si.encName
- } else {
- if si.omitEmpty && isEmptyValue(rvals[newlen]) {
- rvals[newlen] = reflect.Value{} //encode as nil
- }
- }
- newlen++
- }
- // debugf(">>>> kStruct: newlen: %v", newlen)
- if toMap {
- ee := f.ee //don't dereference everytime
- ee.encodeMapPreamble(newlen)
- // asSymbols := e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
- asSymbols := e.h.AsSymbols == AsSymbolDefault || e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
- for j := 0; j < newlen; j++ {
- if asSymbols {
- ee.encodeSymbol(encnames[j])
- } else {
- ee.encodeString(c_UTF8, encnames[j])
- }
- e.encodeValue(rvals[j], encFn{})
- }
- } else {
- f.ee.encodeArrayPreamble(newlen)
- for j := 0; j < newlen; j++ {
- e.encodeValue(rvals[j], encFn{})
- }
- }
- }
- // func (f *encFnInfo) kPtr(rv reflect.Value) {
- // debugf(">>>>>>> ??? encode kPtr called - shouldn't get called")
- // if rv.IsNil() {
- // f.ee.encodeNil()
- // return
- // }
- // f.e.encodeValue(rv.Elem())
- // }
- func (f *encFnInfo) kInterface(rv reflect.Value) {
- if rv.IsNil() {
- f.ee.encodeNil()
- return
- }
- f.e.encodeValue(rv.Elem(), encFn{})
- }
- func (f *encFnInfo) kMap(rv reflect.Value) {
- if rv.IsNil() {
- f.ee.encodeNil()
- return
- }
- l := rv.Len()
- f.ee.encodeMapPreamble(l)
- if l == 0 {
- return
- }
- var asSymbols bool
- // 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.
- var keyFn, valFn encFn
- rtkey := f.ti.rt.Key()
- rtval := f.ti.rt.Elem()
- // keyTypeIsString := f.ti.rt.Key().Kind() == reflect.String
- var keyTypeIsString = rtkey == stringTyp
- if keyTypeIsString {
- asSymbols = f.e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0
- } else {
- for rtkey.Kind() == reflect.Ptr {
- rtkey = rtkey.Elem()
- }
- keyFn = f.e.getEncFn(rtkey)
- }
- for rtval.Kind() == reflect.Ptr {
- rtval = rtval.Elem()
- }
- valFn = f.e.getEncFn(rtval)
- mks := rv.MapKeys()
- // for j, lmks := 0, len(mks); j < lmks; j++ {
- for j := range mks {
- if keyTypeIsString {
- if asSymbols {
- f.ee.encodeSymbol(mks[j].String())
- } else {
- f.ee.encodeString(c_UTF8, mks[j].String())
- }
- } else {
- f.e.encodeValue(mks[j], keyFn)
- }
- f.e.encodeValue(rv.MapIndex(mks[j]), valFn)
- }
- }
- // --------------------------------------------------
- // encFn encapsulates the captured variables and the encode function.
- // This way, we only do some calculations one times, and pass to the
- // code block that should be called (encapsulated in a function)
- // instead of executing the checks every time.
- type encFn struct {
- i *encFnInfo
- f func(*encFnInfo, reflect.Value)
- }
- // --------------------------------------------------
- // An Encoder writes an object to an output stream in the codec format.
- type Encoder struct {
- w encWriter
- e encDriver
- h *BasicHandle
- hh Handle
- f map[uintptr]encFn
- x []uintptr
- s []encFn
- }
- // NewEncoder returns an Encoder for encoding into an io.Writer.
- //
- // For efficiency, Users are encouraged to pass in a memory buffered writer
- // (eg bufio.Writer, bytes.Buffer).
- func NewEncoder(w io.Writer, h Handle) *Encoder {
- ww, ok := w.(ioEncWriterWriter)
- if !ok {
- sww := simpleIoEncWriterWriter{w: w}
- sww.bw, _ = w.(io.ByteWriter)
- sww.sw, _ = w.(ioEncStringWriter)
- ww = &sww
- //ww = bufio.NewWriterSize(w, defEncByteBufSize)
- }
- z := ioEncWriter{
- w: ww,
- }
- return &Encoder{w: &z, hh: h, h: h.getBasicHandle(), e: h.newEncDriver(&z)}
- }
- // 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 {
- in := *out
- if in == nil {
- in = make([]byte, defEncByteBufSize)
- }
- z := bytesEncWriter{
- b: in,
- out: out,
- }
- return &Encoder{w: &z, hh: h, h: h.getBasicHandle(), e: h.newEncDriver(&z)}
- }
- // Encode writes an object into a stream in the codec format.
- //
- // Encoding can be configured via the "codec" struct tag for the fields.
- //
- // The "codec" key in struct field's tag value is the key name,
- // followed by an optional comma and options.
- //
- // To set an option on all fields (e.g. omitempty on all fields), you
- // can create a field called _struct, and set flags on it.
- //
- // Struct values "usually" encode as maps. Each exported struct field is encoded unless:
- // - the field's codec tag is "-", OR
- // - the field is empty and its codec 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.
- //
- // However, struct values may encode as arrays. This happens when:
- // - StructToArray Encode option is set, OR
- // - the codec tag on the _struct field sets the "toarray" option
- //
- // 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.
- //
- // Anonymous fields are encoded inline if no struct tag is present.
- // Else they are encoded as regular fields.
- //
- // Examples:
- //
- // 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.
- // ...
- // }
- //
- // type MyStruct struct {
- // _struct bool `codec:",omitempty,toarray"` //set omitempty for every field
- // //and encode struct as an array
- // }
- //
- // The mode of encoding is based on the type of the value. When a value is seen:
- // - If an extension is registered for it, call that extension function
- // - If it implements BinaryMarshaler, call its MarshalBinary() (data []byte, err error)
- // - 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) {
- defer panicToErr(&err)
- e.encode(v)
- e.w.atEndOfEncode()
- 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{}) {
- e.encode(v)
- e.w.atEndOfEncode()
- }
- func (e *Encoder) Write(bs []byte) (err error) {
- defer panicToErr(&err)
- e.w.writeb(bs)
- return
- }
- func (e *Encoder) MustWrite(bs []byte) {
- e.w.writeb(bs)
- }
- func (e *Encoder) encode(iv interface{}) {
- switch v := iv.(type) {
- case nil:
- e.e.encodeNil()
- case reflect.Value:
- e.encodeValue(v, encFn{})
- case string:
- e.e.encodeString(c_UTF8, v)
- 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)
- case float32:
- e.e.encodeFloat32(v)
- case float64:
- e.e.encodeFloat64(v)
- case []uint8:
- e.e.encodeStringBytes(c_RAW, v)
- case *string:
- e.e.encodeString(c_UTF8, *v)
- 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)
- case *float32:
- e.e.encodeFloat32(*v)
- case *float64:
- e.e.encodeFloat64(*v)
- case *[]uint8:
- e.e.encodeStringBytes(c_RAW, *v)
- default:
- e.encodeValue(reflect.ValueOf(iv), encFn{})
- }
- }
- func (e *Encoder) encodeValue(rv reflect.Value, fn encFn) {
- // if a valid fn is passed, it MUST BE for the dereferenced type of rv
- for rv.Kind() == reflect.Ptr {
- if rv.IsNil() {
- e.e.encodeNil()
- return
- }
- rv = rv.Elem()
- }
- if fn.i == nil {
- fn = e.getEncFn(rv.Type())
- }
- fn.f(fn.i, rv)
- }
- func (e *Encoder) getEncFn(rt reflect.Type) (fn encFn) {
- // if e.f == nil && e.s == nil { debugf("---->Creating new enc f map for type: %v\n", rt) }
- rtid := reflect.ValueOf(rt).Pointer()
- var ok bool
- if useMapForCodecCache {
- fn, ok = e.f[rtid]
- } else {
- // if len(e.x) > 0 && len(e.x)%10 == 0 {
- // println("len(e.x) ", len(e.x))
- // }
- for i, v := range e.x {
- if v == rtid {
- fn, ok = e.s[i], true
- break
- }
- }
- }
- if !ok {
- // debugf("\tCreating new enc fn for type: %v\n", rt)
- fi := encFnInfo{ti: getTypeInfo(rtid, rt), e: e, ee: e.e}
- fn.i = &fi
- if rtid == rawExtTypId {
- fn.f = (*encFnInfo).rawExt
- } else if e.e.isBuiltinType(rtid) {
- fn.f = (*encFnInfo).builtin
- } else if xfFn := e.h.getExt(rtid); xfFn != nil {
- fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
- fn.f = (*encFnInfo).ext
- } else if supportBinaryMarshal && fi.ti.m {
- fn.f = (*encFnInfo).binaryMarshal
- } else {
- rk := rt.Kind()
- if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice) {
- if fn.f, ok = fastpathsEnc[rtid]; !ok && rt.PkgPath() != "" {
- // use mapping for underlying type if there
- var rtu reflect.Type
- if rk == reflect.Map {
- rtu = reflect.MapOf(rt.Key(), rt.Elem())
- } else {
- rtu = reflect.SliceOf(rt.Elem())
- }
- rtuid := reflect.ValueOf(rtu).Pointer()
- if fn.f, ok = fastpathsEnc[rtuid]; ok {
- xfnf := fn.f
- xrt := fastpathsTyp[rtuid]
- fn.f = func(xf *encFnInfo, xrv reflect.Value) {
- xfnf(xf, xrv.Convert(xrt))
- }
- }
- }
- }
- if fn.f == nil {
- switch rk {
- case reflect.Bool:
- fn.f = (*encFnInfo).kBool
- case reflect.String:
- fn.f = (*encFnInfo).kString
- case reflect.Float64:
- fn.f = (*encFnInfo).kFloat64
- case reflect.Float32:
- fn.f = (*encFnInfo).kFloat32
- case reflect.Int, reflect.Int8, reflect.Int64, reflect.Int32, reflect.Int16:
- fn.f = (*encFnInfo).kInt
- case reflect.Uint8, reflect.Uint64, reflect.Uint, reflect.Uint32, reflect.Uint16:
- fn.f = (*encFnInfo).kUint
- case reflect.Invalid:
- fn.f = (*encFnInfo).kInvalid
- case reflect.Slice:
- fn.f = (*encFnInfo).kSlice
- case reflect.Array:
- fn.f = (*encFnInfo).kArray
- case reflect.Struct:
- fn.f = (*encFnInfo).kStruct
- // case reflect.Ptr:
- // fn.f = (*encFnInfo).kPtr
- case reflect.Interface:
- fn.f = (*encFnInfo).kInterface
- case reflect.Map:
- fn.f = (*encFnInfo).kMap
- default:
- fn.f = (*encFnInfo).kErr
- }
- }
- }
- if useMapForCodecCache {
- if e.f == nil {
- e.f = make(map[uintptr]encFn, 64)
- }
- e.f[rtid] = fn
- } else {
- e.s = append(e.s, fn)
- e.x = append(e.x, rtid)
- }
- }
- return
- }
- // ----------------------------------------
- func encErr(format string, params ...interface{}) {
- doPanic(msgTagEnc, format, params...)
- }
|