| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008 |
- // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
- // Use of this source code is governed by a MIT license found in the LICENSE file.
- package codec
- import (
- "encoding"
- "errors"
- "fmt"
- "io"
- "reflect"
- "runtime"
- "strconv"
- "time"
- )
- // Some tagging information for error messages.
- const (
- msgBadDesc = "unrecognized descriptor byte"
- // msgDecCannotExpandArr = "cannot expand go array from %v to stream length: %v"
- )
- const (
- decDefMaxDepth = 1024 // maximum depth
- decDefSliceCap = 8
- decDefChanCap = 64 // should be large, as cap cannot be expanded
- decScratchByteArrayLen = cacheLineSize // - 5 // + (8 * 2) // - (8 * 1)
- )
- var (
- errstrOnlyMapOrArrayCanDecodeIntoStruct = "only encoded map or array can be decoded into a struct"
- errstrCannotDecodeIntoNil = "cannot decode into nil"
- errmsgExpandSliceOverflow = "expand slice: slice overflow"
- errmsgExpandSliceCannotChange = "expand slice: cannot change"
- errDecoderNotInitialized = errors.New("Decoder not initialized")
- errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read")
- errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read")
- errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown")
- errMaxDepthExceeded = errors.New("maximum decoding depth exceeded")
- errBytesDecReaderCannotUnread = errors.New("cannot unread last byte read")
- )
- type decDriver interface {
- // this will check if the next token is a break.
- CheckBreak() bool
- // TryDecodeAsNil tries to decode as nil.
- // Note: TryDecodeAsNil should be careful not to share any temporary []byte with
- // the rest of the decDriver. This is because sometimes, we optimize by holding onto
- // a transient []byte, and ensuring the only other call we make to the decDriver
- // during that time is maybe a TryDecodeAsNil() call.
- TryDecodeAsNil() bool
- // ContainerType returns one of: Bytes, String, Nil, Slice or Map. Return unSet if not known.
- ContainerType() (vt valueType)
- // IsBuiltinType(rt uintptr) bool
- // DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt.
- // For maps and arrays, it will not do the decoding in-band, but will signal
- // the decoder, so that is done later, by setting the decNaked.valueType field.
- //
- // Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types).
- // for extensions, DecodeNaked must read the tag and the []byte if it exists.
- // if the []byte is not read, then kInterfaceNaked will treat it as a Handle
- // that stores the subsequent value in-band, and complete reading the RawExt.
- //
- // extensions should also use readx to decode them, for efficiency.
- // kInterface will extract the detached byte slice if it has to pass it outside its realm.
- DecodeNaked()
- DecodeInt64() (i int64)
- DecodeUint64() (ui uint64)
- DecodeFloat64() (f float64)
- DecodeBool() (b bool)
- // DecodeStringAsBytes returns the bytes representing a string.
- // By definition, it will return a view into a scratch buffer.
- //
- // Note: This can also decode symbols, if supported.
- //
- // Users should consume it right away and not store it for later use.
- DecodeStringAsBytes() (v []byte)
- // DecodeBytes may be called directly, without going through reflection.
- // Consequently, it must be designed to handle possible nil.
- DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte)
- // DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte)
- // DecodeExt will decode into a *RawExt or into an extension.
- DecodeExt(v interface{}, xtag uint64, ext Ext) (realxtag uint64)
- // decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte)
- DecodeTime() (t time.Time)
- ReadArrayStart() int
- ReadArrayEnd()
- ReadMapStart() int
- ReadMapEnd()
- reset()
- atEndOfDecode()
- uncacheRead()
- }
- type decDriverContainerTracker interface {
- ReadArrayElem()
- ReadMapElemKey()
- ReadMapElemValue()
- }
- type decodeError struct {
- codecError
- pos int
- }
- func (d decodeError) Error() string {
- return fmt.Sprintf("%s decode error [pos %d]: %v", d.name, d.pos, d.err)
- }
- type decDriverNoopContainerReader struct{}
- func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { return }
- func (x decDriverNoopContainerReader) ReadArrayEnd() {}
- func (x decDriverNoopContainerReader) ReadMapStart() (v int) { return }
- func (x decDriverNoopContainerReader) ReadMapEnd() {}
- func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return }
- func (x decDriverNoopContainerReader) atEndOfDecode() {}
- // func (x decDriverNoopContainerReader) ReadArrayElem() {}
- // func (x decDriverNoopContainerReader) ReadMapElemKey() {}
- // func (x decDriverNoopContainerReader) ReadMapElemValue() {}
- // func (x decNoSeparator) uncacheRead() {}
- // DecodeOptions captures configuration options during decode.
- type DecodeOptions struct {
- // MapType specifies type to use during schema-less decoding of a map in the stream.
- // If nil (unset), we default to map[string]interface{} iff json handle and MapStringAsKey=true,
- // else map[interface{}]interface{}.
- MapType reflect.Type
- // SliceType specifies type to use during schema-less decoding of an array in the stream.
- // If nil (unset), we default to []interface{} for all formats.
- SliceType reflect.Type
- // MaxInitLen defines the maxinum initial length that we "make" a collection
- // (string, slice, map, chan). If 0 or negative, we default to a sensible value
- // based on the size of an element in the collection.
- //
- // For example, when decoding, a stream may say that it has 2^64 elements.
- // We should not auto-matically provision a slice of that size, to prevent Out-Of-Memory crash.
- // Instead, we provision up to MaxInitLen, fill that up, and start appending after that.
- MaxInitLen int
- // ReaderBufferSize is the size of the buffer used when reading.
- //
- // if > 0, we use a smart buffer internally for performance purposes.
- ReaderBufferSize int
- // MaxDepth defines the maximum depth when decoding nested
- // maps and slices. If 0 or negative, we default to a suitably large number (currently 1024).
- MaxDepth int16
- // If ErrorIfNoField, return an error when decoding a map
- // from a codec stream into a struct, and no matching struct field is found.
- ErrorIfNoField bool
- // If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded.
- // For example, the stream contains an array of 8 items, but you are decoding into a [4]T array,
- // or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set).
- ErrorIfNoArrayExpand bool
- // If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64).
- SignedInteger bool
- // MapValueReset controls how we decode into a map value.
- //
- // By default, we MAY retrieve the mapping for a key, and then decode into that.
- // However, especially with big maps, that retrieval may be expensive and unnecessary
- // if the stream already contains all that is necessary to recreate the value.
- //
- // If true, we will never retrieve the previous mapping,
- // but rather decode into a new value and set that in the map.
- //
- // If false, we will retrieve the previous mapping if necessary e.g.
- // the previous mapping is a pointer, or is a struct or array with pre-set state,
- // or is an interface.
- MapValueReset bool
- // SliceElementReset: on decoding a slice, reset the element to a zero value first.
- //
- // concern: if the slice already contained some garbage, we will decode into that garbage.
- SliceElementReset bool
- // InterfaceReset controls how we decode into an interface.
- //
- // By default, when we see a field that is an interface{...},
- // or a map with interface{...} value, we will attempt decoding into the
- // "contained" value.
- //
- // However, this prevents us from reading a string into an interface{}
- // that formerly contained a number.
- //
- // If true, we will decode into a new "blank" value, and set that in the interface.
- // If false, we will decode into whatever is contained in the interface.
- InterfaceReset bool
- // InternString controls interning of strings during decoding.
- //
- // Some handles, e.g. json, typically will read map keys as strings.
- // If the set of keys are finite, it may help reduce allocation to
- // look them up from a map (than to allocate them afresh).
- //
- // Note: Handles will be smart when using the intern functionality.
- // Every string should not be interned.
- // An excellent use-case for interning is struct field names,
- // or map keys where key type is string.
- InternString bool
- // PreferArrayOverSlice controls whether to decode to an array or a slice.
- //
- // This only impacts decoding into a nil interface{}.
- // Consequently, it has no effect on codecgen.
- //
- // *Note*: This only applies if using go1.5 and above,
- // as it requires reflect.ArrayOf support which was absent before go1.5.
- PreferArrayOverSlice bool
- // DeleteOnNilMapValue controls how to decode a nil value in the stream.
- //
- // If true, we will delete the mapping of the key.
- // Else, just set the mapping to the zero value of the type.
- DeleteOnNilMapValue bool
- // RawToString controls how raw bytes in a stream are decoded into a nil interface{}.
- // By default, they are decoded as []byte, but can be decoded as string (if configured).
- RawToString bool
- }
- // ----------------------------------------
- // func (d *Decoder) builtin(f *codecFnInfo, rv reflect.Value) {
- // d.d.DecodeBuiltin(f.ti.rtid, rv2i(rv))
- // }
- func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) {
- d.d.DecodeExt(rv2i(rv), 0, nil)
- }
- func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) {
- d.d.DecodeExt(rv2i(rv), f.xfTag, f.xfFn)
- }
- func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) {
- rv2i(rv).(Selfer).CodecDecodeSelf(d)
- }
- func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) {
- bm := rv2i(rv).(encoding.BinaryUnmarshaler)
- xbs := d.d.DecodeBytes(nil, true)
- if fnerr := bm.UnmarshalBinary(xbs); fnerr != nil {
- panic(fnerr)
- }
- }
- func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) {
- tm := rv2i(rv).(encoding.TextUnmarshaler)
- fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes())
- if fnerr != nil {
- panic(fnerr)
- }
- }
- func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) {
- tm := rv2i(rv).(jsonUnmarshaler)
- // bs := d.d.DecodeBytes(d.b[:], true, true)
- // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
- fnerr := tm.UnmarshalJSON(d.nextValueBytes())
- if fnerr != nil {
- panic(fnerr)
- }
- }
- func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) {
- d.errorf("no decoding function defined for kind %v", rv.Kind())
- }
- // var kIntfCtr uint64
- func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) {
- // nil interface:
- // use some hieristics to decode it appropriately
- // based on the detected next value in the stream.
- n := d.naked()
- d.d.DecodeNaked()
- if n.v == valueTypeNil {
- return
- }
- // We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader).
- if f.ti.numMeth > 0 {
- d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth)
- return
- }
- // var useRvn bool
- switch n.v {
- case valueTypeMap:
- // if json, default to a map type with string keys
- mtid := d.mtid
- if mtid == 0 {
- if d.jsms {
- mtid = mapStrIntfTypId
- } else {
- mtid = mapIntfIntfTypId
- }
- }
- if mtid == mapIntfIntfTypId {
- var v2 map[interface{}]interface{}
- d.decode(&v2)
- rvn = reflect.ValueOf(&v2).Elem()
- } else if mtid == mapStrIntfTypId { // for json performance
- var v2 map[string]interface{}
- d.decode(&v2)
- rvn = reflect.ValueOf(&v2).Elem()
- } else {
- if d.mtr {
- rvn = reflect.New(d.h.MapType)
- d.decode(rv2i(rvn))
- rvn = rvn.Elem()
- } else {
- rvn = reflect.New(d.h.MapType).Elem()
- d.decodeValue(rvn, nil)
- }
- }
- case valueTypeArray:
- if d.stid == 0 || d.stid == intfSliceTypId {
- var v2 []interface{}
- d.decode(&v2)
- rvn = reflect.ValueOf(&v2).Elem()
- if reflectArrayOfSupported && d.stid == 0 && d.h.PreferArrayOverSlice {
- rvn2 := reflect.New(reflectArrayOf(rvn.Len(), intfTyp)).Elem()
- reflect.Copy(rvn2, rvn)
- rvn = rvn2
- }
- } else {
- if d.str {
- rvn = reflect.New(d.h.SliceType)
- d.decode(rv2i(rvn))
- rvn = rvn.Elem()
- } else {
- rvn = reflect.New(d.h.SliceType).Elem()
- d.decodeValue(rvn, nil)
- }
- }
- case valueTypeExt:
- tag, bytes := n.u, n.l // calling decode below might taint the values
- bfn := d.h.getExtForTag(tag)
- var re = RawExt{Tag: tag}
- if bytes == nil {
- // it is one of the InterfaceExt ones: json and cbor.
- // most likely cbor, as json decoding never reveals valueTypeExt (no tagging support)
- if bfn == nil {
- d.decode(&re.Value)
- rvn = reflect.ValueOf(&re).Elem()
- } else {
- rvn = reflect.New(bfn.rt)
- if bfn.ext == SelfExt {
- d.decodeValue(rvn, d.h.fnNoExt(rvn.Type().Elem()))
- } else {
- d.interfaceExtConvertAndDecode(rv2i(rvn), bfn.ext)
- }
- rvn = rvn.Elem()
- }
- } else {
- // one of the BytesExt ones: binc, msgpack, simple
- if bfn == nil {
- re.Data = detachZeroCopyBytes(d.bytes, nil, bytes)
- rvn = reflect.ValueOf(&re).Elem()
- } else {
- rvn = reflect.New(bfn.rt)
- if bfn.ext == SelfExt {
- d.sideDecode(rv2i(rvn), bytes)
- } else {
- bfn.ext.ReadExt(rv2i(rvn), bytes)
- }
- rvn = rvn.Elem()
- }
- }
- case valueTypeNil:
- // no-op
- case valueTypeInt:
- rvn = n.ri()
- case valueTypeUint:
- rvn = n.ru()
- case valueTypeFloat:
- rvn = n.rf()
- case valueTypeBool:
- rvn = n.rb()
- case valueTypeString, valueTypeSymbol:
- rvn = n.rs()
- case valueTypeBytes:
- rvn = n.rl()
- case valueTypeTime:
- rvn = n.rt()
- default:
- panicv.errorf("kInterfaceNaked: unexpected valueType: %d", n.v)
- }
- return
- }
- func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) {
- // Note:
- // A consequence of how kInterface works, is that
- // if an interface already contains something, we try
- // to decode into what was there before.
- // We do not replace with a generic value (as got from decodeNaked).
- // every interface passed here MUST be settable.
- var rvn reflect.Value
- if rvisnil(rv) || d.h.InterfaceReset {
- // check if mapping to a type: if so, initialize it and move on
- rvn = d.h.intf2impl(f.ti.rtid)
- if rvn.IsValid() {
- rv.Set(rvn)
- } else {
- rvn = d.kInterfaceNaked(f)
- if rvn.IsValid() {
- rv.Set(rvn)
- } else if d.h.InterfaceReset {
- // reset to zero value based on current type in there.
- rv.Set(reflect.Zero(rv.Elem().Type()))
- }
- return
- }
- } else {
- // now we have a non-nil interface value, meaning it contains a type
- rvn = rv.Elem()
- }
- if d.d.TryDecodeAsNil() {
- rv.Set(reflect.Zero(rvn.Type()))
- return
- }
- // Note: interface{} is settable, but underlying type may not be.
- // Consequently, we MAY have to create a decodable value out of the underlying value,
- // decode into it, and reset the interface itself.
- // fmt.Printf(">>>> kInterface: rvn type: %v, rv type: %v\n", rvn.Type(), rv.Type())
- rvn2, canDecode := isDecodeable(rvn)
- if canDecode {
- d.decodeValue(rvn2, nil)
- return
- }
- rvn2 = reflect.New(rvn.Type()).Elem()
- rvn2.Set(rvn)
- d.decodeValue(rvn2, nil)
- rv.Set(rvn2)
- }
- func decStructFieldKey(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) {
- // use if-else-if, not switch (which compiles to binary-search)
- // since keyType is typically valueTypeString, branch prediction is pretty good.
- if keyType == valueTypeString {
- rvkencname = dd.DecodeStringAsBytes()
- } else if keyType == valueTypeInt {
- rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10)
- } else if keyType == valueTypeUint {
- rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10)
- } else if keyType == valueTypeFloat {
- rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64)
- } else {
- rvkencname = dd.DecodeStringAsBytes()
- }
- return rvkencname
- }
- func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) {
- fti := f.ti
- dd := d.d
- sfn := structFieldNode{v: rv, update: true}
- ctyp := dd.ContainerType()
- var mf MissingFielder
- if fti.mf {
- mf = rv2i(rv).(MissingFielder)
- } else if fti.mfp {
- mf = rv2i(rv.Addr()).(MissingFielder)
- }
- if ctyp == valueTypeMap {
- containerLen := d.mapStart()
- if containerLen == 0 {
- d.mapEnd()
- return
- }
- tisfi := fti.sfiSort
- hasLen := containerLen >= 0
- var rvkencname []byte
- for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
- d.mapElemKey()
- rvkencname = decStructFieldKey(dd, fti.keyType, &d.b)
- d.mapElemValue()
- if k := fti.indexForEncName(rvkencname); k > -1 {
- si := tisfi[k]
- if dd.TryDecodeAsNil() {
- si.setToZeroValue(rv)
- } else {
- d.decodeValue(sfn.field(si), nil)
- }
- } else if mf != nil {
- // store rvkencname in new []byte, as it previously shares Decoder.b, which is used in decode
- name2 := rvkencname
- rvkencname = make([]byte, len(rvkencname))
- copy(rvkencname, name2)
- var f interface{}
- d.decode(&f)
- if !mf.CodecMissingField(rvkencname, f) && d.h.ErrorIfNoField {
- d.errorf("no matching struct field found when decoding stream map with key: %s ",
- stringView(rvkencname))
- }
- } else {
- d.structFieldNotFound(-1, stringView(rvkencname))
- }
- // keepAlive4StringView(rvkencnameB) // not needed, as reference is outside loop
- }
- d.mapEnd()
- } else if ctyp == valueTypeArray {
- containerLen := d.arrayStart()
- if containerLen == 0 {
- d.arrayEnd()
- return
- }
- // Not much gain from doing it two ways for array.
- // Arrays are not used as much for structs.
- hasLen := containerLen >= 0
- var checkbreak bool
- for j, si := range fti.sfiSrc {
- if hasLen && j == containerLen {
- break
- }
- if !hasLen && dd.CheckBreak() {
- checkbreak = true
- break
- }
- d.arrayElem()
- if dd.TryDecodeAsNil() {
- si.setToZeroValue(rv)
- } else {
- d.decodeValue(sfn.field(si), nil)
- }
- }
- if (hasLen && containerLen > len(fti.sfiSrc)) || (!hasLen && !checkbreak) {
- // read remaining values and throw away
- for j := len(fti.sfiSrc); ; j++ {
- if (hasLen && j == containerLen) || (!hasLen && dd.CheckBreak()) {
- break
- }
- d.arrayElem()
- d.structFieldNotFound(j, "")
- }
- }
- d.arrayEnd()
- } else {
- d.errorstr(errstrOnlyMapOrArrayCanDecodeIntoStruct)
- return
- }
- }
- func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) {
- // A slice can be set from a map or array in stream.
- // This way, the order can be kept (as order is lost with map).
- frt := f.ti.rt
- if f.seq == seqTypeChan && f.ti.chandir&uint8(reflect.SendDir) == 0 {
- d.errorf("receive-only channel cannot be decoded")
- }
- dd := d.d
- rtelem0 := f.ti.elem
- ctyp := dd.ContainerType()
- if ctyp == valueTypeBytes || ctyp == valueTypeString {
- // you can only decode bytes or string in the stream into a slice or array of bytes
- if !(f.ti.rtid == uint8SliceTypId || rtelem0.Kind() == reflect.Uint8) {
- d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", frt)
- }
- if f.seq == seqTypeChan {
- bs2 := dd.DecodeBytes(nil, true)
- irv := rv2i(rv)
- ch, ok := irv.(chan<- byte)
- if !ok {
- ch = irv.(chan byte)
- }
- for _, b := range bs2 {
- ch <- b
- }
- } else {
- rvbs := rv.Bytes()
- bs2 := dd.DecodeBytes(rvbs, false)
- // if rvbs == nil && bs2 != nil || rvbs != nil && bs2 == nil || len(bs2) != len(rvbs) {
- if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) {
- if rv.CanSet() {
- rv.SetBytes(bs2)
- } else if len(rvbs) > 0 && len(bs2) > 0 {
- copy(rvbs, bs2)
- }
- }
- }
- return
- }
- // array := f.seq == seqTypeChan
- slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map)
- // an array can never return a nil slice. so no need to check f.array here.
- if containerLenS == 0 {
- if rv.CanSet() {
- if f.seq == seqTypeSlice {
- if rvisnil(rv) {
- rv.Set(reflect.MakeSlice(frt, 0, 0))
- } else {
- rvssetlen(rv, 0)
- }
- } else if f.seq == seqTypeChan {
- if rvisnil(rv) {
- rv.Set(reflect.MakeChan(frt, 0))
- }
- }
- }
- slh.End()
- return
- }
- rtelem0Size := int(rtelem0.Size())
- rtElem0Kind := rtelem0.Kind()
- rtelem0Mut := !isImmutableKind(rtElem0Kind)
- rtelem := rtelem0
- rtelemkind := rtelem.Kind()
- for rtelemkind == reflect.Ptr {
- rtelem = rtelem.Elem()
- rtelemkind = rtelem.Kind()
- }
- var fn *codecFn
- var rvCanset = rv.CanSet()
- var rvChanged bool
- var rv0 = rv
- var rv9 reflect.Value
- rvlen := rv.Len()
- rvcap := rv.Cap()
- hasLen := containerLenS > 0
- if hasLen && f.seq == seqTypeSlice {
- if containerLenS > rvcap {
- oldRvlenGtZero := rvlen > 0
- rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(rtelem0.Size()))
- if rvlen <= rvcap {
- if rvCanset {
- rvssetlen(rv, rvlen)
- }
- } else if rvCanset {
- rv = reflect.MakeSlice(frt, rvlen, rvlen)
- rvcap = rvlen
- rvChanged = true
- } else {
- d.errorf("cannot decode into non-settable slice")
- }
- if rvChanged && oldRvlenGtZero && rtelem0Mut { // !isImmutableKind(rtelem0.Kind()) {
- reflect.Copy(rv, rv0) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap)
- }
- } else if containerLenS != rvlen {
- rvlen = containerLenS
- if rvCanset {
- rvssetlen(rv, rvlen)
- }
- // else {
- // rv = rv.Slice(0, rvlen)
- // rvChanged = true
- // d.errorf("cannot decode into non-settable slice")
- // }
- }
- }
- // consider creating new element once, and just decoding into it.
- var rtelem0Zero reflect.Value
- var rtelem0ZeroValid bool
- var decodeAsNil bool
- var j int
- for ; (hasLen && j < containerLenS) || !(hasLen || dd.CheckBreak()); j++ {
- if j == 0 && (f.seq == seqTypeSlice || f.seq == seqTypeChan) && rvisnil(rv) {
- if hasLen {
- rvlen = decInferLen(containerLenS, d.h.MaxInitLen, rtelem0Size)
- } else if f.seq == seqTypeSlice {
- rvlen = decDefSliceCap
- } else {
- rvlen = decDefChanCap
- }
- if rvCanset {
- if f.seq == seqTypeSlice {
- rv = reflect.MakeSlice(frt, rvlen, rvlen)
- rvChanged = true
- } else { // chan
- rv = reflect.MakeChan(frt, rvlen)
- rvChanged = true
- }
- } else {
- d.errorf("cannot decode into non-settable slice")
- }
- }
- slh.ElemContainerState(j)
- decodeAsNil = dd.TryDecodeAsNil()
- if f.seq == seqTypeChan {
- if decodeAsNil {
- rv.Send(reflect.Zero(rtelem0))
- continue
- }
- if rtelem0Mut || !rv9.IsValid() { // || (rtElem0Kind == reflect.Ptr && rvisnil(rv9)) {
- rv9 = reflect.New(rtelem0).Elem()
- }
- if fn == nil {
- fn = d.h.fn(rtelem)
- }
- d.decodeValue(rv9, fn)
- rv.Send(rv9)
- } else {
- // if indefinite, etc, then expand the slice if necessary
- var decodeIntoBlank bool
- if j >= rvlen {
- if f.seq == seqTypeArray {
- d.arrayCannotExpand(rvlen, j+1)
- decodeIntoBlank = true
- } else { // if f.seq == seqTypeSlice
- // rv = reflect.Append(rv, reflect.Zero(rtelem0)) // append logic + varargs
- var rvcap2 int
- var rvErrmsg2 string
- rv9, rvcap2, rvChanged, rvErrmsg2 =
- expandSliceRV(rv, frt, rvCanset, rtelem0Size, 1, rvlen, rvcap)
- if rvErrmsg2 != "" {
- d.errorf(rvErrmsg2)
- }
- rvlen++
- if rvChanged {
- rv = rv9
- rvcap = rvcap2
- }
- }
- }
- if decodeIntoBlank {
- if !decodeAsNil {
- d.swallow()
- }
- } else {
- rv9 = rv.Index(j)
- if d.h.SliceElementReset || decodeAsNil {
- if !rtelem0ZeroValid {
- rtelem0ZeroValid = true
- rtelem0Zero = reflect.Zero(rtelem0)
- }
- rv9.Set(rtelem0Zero)
- if decodeAsNil {
- continue
- }
- }
- if fn == nil {
- fn = d.h.fn(rtelem)
- }
- d.decodeValue(rv9, fn)
- }
- }
- }
- if f.seq == seqTypeSlice {
- if j < rvlen {
- if rv.CanSet() {
- rvssetlen(rv, j)
- } else if rvCanset {
- rv = rv.Slice(0, j)
- rvChanged = true
- } // else { d.errorf("kSlice: cannot change non-settable slice") }
- rvlen = j
- } else if j == 0 && rvisnil(rv) {
- if rvCanset {
- rv = reflect.MakeSlice(frt, 0, 0)
- rvChanged = true
- } // else { d.errorf("kSlice: cannot change non-settable slice") }
- }
- }
- slh.End()
- if rvChanged { // infers rvCanset=true, so it can be reset
- rv0.Set(rv)
- }
- }
- // func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) {
- // // d.decodeValueFn(rv.Slice(0, rv.Len()))
- // f.kSlice(rv.Slice(0, rv.Len()))
- // }
- func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) {
- dd := d.d
- containerLen := d.mapStart()
- ti := f.ti
- if rvisnil(rv) {
- rvlen := decInferLen(containerLen, d.h.MaxInitLen, int(ti.key.Size()+ti.elem.Size()))
- rv.Set(makeMapReflect(ti.rt, rvlen))
- }
- if containerLen == 0 {
- d.mapEnd()
- return
- }
- ktype, vtype := ti.key, ti.elem
- ktypeId := rt2id(ktype)
- vtypeKind := vtype.Kind()
- // ktypeKind := ktype.Kind()
- var keyFn, valFn *codecFn
- var ktypeLo, vtypeLo reflect.Type
- for ktypeLo = ktype; ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() {
- }
- for vtypeLo = vtype; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() {
- }
- rvvMut := !isImmutableKind(vtypeKind)
- // we do a doMapGet if kind is mutable, and InterfaceReset=true if interface
- var doMapGet, doMapSet bool
- if !d.h.MapValueReset {
- if rvvMut {
- if vtypeKind == reflect.Interface {
- if !d.h.InterfaceReset {
- doMapGet = true
- }
- } else {
- doMapGet = true
- }
- }
- }
- var rvk, rvkn, rvv, rvvn, rvva, rvvz reflect.Value
- var rvvaSet bool
- rvkMut := !isImmutableKind(ktype.Kind()) // if ktype is immutable, then re-use the same rvk.
- ktypeIsString := ktypeId == stringTypId
- ktypeIsIntf := ktypeId == intfTypId
- hasLen := containerLen > 0
- var kstrbs []byte
- for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
- if j == 0 {
- // rvvz = reflect.Zero(vtype)
- // rvkz = reflect.Zero(ktype)
- if !rvkMut {
- rvkn = reflect.New(ktype).Elem() //, ktypeKind)
- }
- if !rvvMut {
- rvvn = reflect.New(vtype).Elem() //, vtypeKind)
- }
- }
- if rvkMut {
- rvk = reflect.New(ktype).Elem() //, ktypeKind)
- } else {
- rvk = rvkn
- }
- d.mapElemKey()
- if ktypeIsString {
- kstrbs = dd.DecodeStringAsBytes()
- rvk.SetString(stringView(kstrbs)) // NOTE: if doing an insert, use real string (not stringview)
- } else {
- if keyFn == nil {
- keyFn = d.h.fn(ktypeLo)
- }
- d.decodeValue(rvk, keyFn)
- }
- // special case if interface wrapping a byte array.
- if ktypeIsIntf {
- if rvk2 := rvk.Elem(); rvk2.IsValid() && rvk2.Type() == uint8SliceTyp {
- rvk.Set(reflect.ValueOf(d.string(rvk2.Bytes())))
- }
- // NOTE: consider failing early if map/slice/func
- }
- d.mapElemValue()
- // Brittle, but OK per TryDecodeAsNil() contract.
- // i.e. TryDecodeAsNil never shares slices with other decDriver procedures
- if dd.TryDecodeAsNil() {
- if d.h.DeleteOnNilMapValue {
- mapDelete(rv, rvk)
- } else {
- if ktypeIsString { // set to a real string (not string view)
- rvk.SetString(d.string(kstrbs))
- }
- if !rvvz.IsValid() {
- rvvz = reflect.Zero(vtype)
- }
- mapSet(rv, rvk, rvvz)
- }
- continue
- }
- doMapSet = true // set to false if u do a get, and its a non-nil pointer
- if doMapGet {
- if !rvvaSet {
- rvva = mapAddressableRV(vtype)
- rvvaSet = true
- }
- rvv = mapGet(rv, rvk, rvva) // reflect.Value{})
- if vtypeKind == reflect.Ptr {
- if rvv.IsValid() && !rvisnil(rvv) {
- doMapSet = false
- } else {
- rvv = reflect.New(vtype.Elem())
- }
- } else if rvv.IsValid() && vtypeKind == reflect.Interface && !rvisnil(rvv) {
- rvvn = reflect.New(vtype).Elem()
- rvvn.Set(rvv)
- rvv = rvvn
- } else if rvvMut {
- rvv = reflect.New(vtype).Elem()
- } else {
- rvv = rvvn
- }
- } else if rvvMut {
- rvv = reflect.New(vtype).Elem() //, vtypeKind)
- } else {
- rvv = rvvn
- }
- if valFn == nil {
- valFn = d.h.fn(vtypeLo)
- }
- // We MUST be done with the stringview of the key, BEFORE decoding the value (rvv)
- // so that we don't unknowingly reuse the rvk backing buffer during rvv decode.
- if doMapSet && ktypeIsString { // set to a real string (not string view)
- rvk.SetString(d.string(kstrbs))
- }
- d.decodeValue(rvv, valFn)
- if doMapSet {
- mapSet(rv, rvk, rvv)
- }
- // if ktypeIsString {
- // // keepAlive4StringView(kstrbs) // not needed, as reference is outside loop
- // }
- }
- d.mapEnd()
- }
- // decNaked is used to keep track of the primitives decoded.
- // Without it, we would have to decode each primitive and wrap it
- // in an interface{}, causing an allocation.
- // In this model, the primitives are decoded in a "pseudo-atomic" fashion,
- // so we can rest assured that no other decoding happens while these
- // primitives are being decoded.
- //
- // maps and arrays are not handled by this mechanism.
- // However, RawExt is, and we accommodate for extensions that decode
- // RawExt from DecodeNaked, but need to decode the value subsequently.
- // kInterfaceNaked and swallow, which call DecodeNaked, handle this caveat.
- //
- // However, decNaked also keeps some arrays of default maps and slices
- // used in DecodeNaked. This way, we can get a pointer to it
- // without causing a new heap allocation.
- //
- // kInterfaceNaked will ensure that there is no allocation for the common
- // uses.
- type decNaked struct {
- // r RawExt // used for RawExt, uint, []byte.
- // primitives below
- u uint64
- i int64
- f float64
- l []byte
- s string
- // ---- cpu cache line boundary?
- t time.Time
- b bool
- // state
- v valueType
- // _ [6]bool // padding
- // ru, ri, rf, rl, rs, rb, rt reflect.Value // mapping to the primitives above
- //
- // _ [3]uint64 // padding
- }
- // func (n *decNaked) init() {
- // n.ru = reflect.ValueOf(&n.u).Elem()
- // n.ri = reflect.ValueOf(&n.i).Elem()
- // n.rf = reflect.ValueOf(&n.f).Elem()
- // n.rl = reflect.ValueOf(&n.l).Elem()
- // n.rs = reflect.ValueOf(&n.s).Elem()
- // n.rt = reflect.ValueOf(&n.t).Elem()
- // n.rb = reflect.ValueOf(&n.b).Elem()
- // // n.rr[] = reflect.ValueOf(&n.)
- // }
- // type decNakedPooler struct {
- // n *decNaked
- // nsp *sync.Pool
- // }
- // // naked must be called before each call to .DecodeNaked, as they will use it.
- // func (d *decNakedPooler) naked() *decNaked {
- // if d.n == nil {
- // // consider one of:
- // // - get from sync.Pool (if GC is frequent, there's no value here)
- // // - new alloc (safest. only init'ed if it a naked decode will be done)
- // // - field in Decoder (makes the Decoder struct very big)
- // // To support using a decoder where a DecodeNaked is not needed,
- // // we prefer #1 or #2.
- // // d.n = new(decNaked) // &d.nv // new(decNaked) // grab from a sync.Pool
- // // d.n.init()
- // var v interface{}
- // d.nsp, v = pool.decNaked()
- // d.n = v.(*decNaked)
- // }
- // return d.n
- // }
- // func (d *decNakedPooler) end() {
- // if d.n != nil {
- // // if n != nil, then nsp != nil (they are always set together)
- // d.nsp.Put(d.n)
- // d.n, d.nsp = nil, nil
- // }
- // }
- // type rtid2rv struct {
- // rtid uintptr
- // rv reflect.Value
- // }
- // Decoder reads and decodes an object from an input stream in a supported format.
- //
- // Decoder is NOT safe for concurrent use i.e. a Decoder cannot be used
- // concurrently in multiple goroutines.
- //
- // However, as Decoder 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.
- type Decoder struct {
- panicHdl
- // hopefully, reduce derefencing cost by laying the decReader inside the Decoder.
- // Try to put things that go together to fit within a cache line (8 words).
- d decDriver
- // NOTE: Decoder shouldn't call its read methods,
- // as the handler MAY need to do some coordination.
- // r *decReaderSwitch
- // bi *bufioDecReader
- // cache the mapTypeId and sliceTypeId for faster comparisons
- mtid uintptr
- stid uintptr
- jdec *jsonDecDriver
- h *BasicHandle
- hh Handle
- // ---- cpu cache line boundary?
- decReaderSwitch
- // ---- cpu cache line boundary?
- n decNaked
- // cr containerStateRecv
- // _ [4]uint8 // padding
- is map[string]string // used for interning strings
- err error
- // ---- cpu cache line boundary?
- // ---- writable fields during execution --- *try* to keep in sep cache line
- maxdepth int16
- depth int16
- c containerState
- _ [3]byte // padding
- b [decScratchByteArrayLen]byte // scratch buffer, used by Decoder and xxxDecDrivers
- // padding - false sharing help // modify 232 if Decoder struct changes.
- // _ [cacheLineSize - 232%cacheLineSize]byte
- }
- // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
- //
- // For efficiency, Users are encouraged to configure ReaderBufferSize on the handle
- // OR pass in a memory buffered reader (eg bufio.Reader, bytes.Buffer).
- func NewDecoder(r io.Reader, h Handle) *Decoder {
- d := newDecoder(h)
- d.Reset(r)
- return d
- }
- // NewDecoderBytes returns a Decoder which efficiently decodes directly
- // from a byte slice with zero copying.
- func NewDecoderBytes(in []byte, h Handle) *Decoder {
- d := newDecoder(h)
- d.ResetBytes(in)
- return d
- }
- // var defaultDecNaked decNaked
- func newDecoder(h Handle) *Decoder {
- d := &Decoder{h: basicHandle(h), err: errDecoderNotInitialized}
- d.bytes = true
- if useFinalizers {
- runtime.SetFinalizer(d, (*Decoder).finalize)
- }
- // d.r = &d.decReaderSwitch
- d.hh = h
- d.be = h.isBinary()
- // NOTE: do not initialize d.n here. It is lazily initialized in d.naked()
- var jh *JsonHandle
- jh, d.js = h.(*JsonHandle)
- d.jdec = nil
- if d.js {
- d.jsms = jh.MapKeyAsString
- }
- d.esep = d.hh.hasElemSeparators()
- if d.h.InternString {
- d.is = make(map[string]string, 32)
- }
- d.d = h.newDecDriver(d)
- if d.js {
- d.jdec = d.d.(*jsonDecDriver)
- }
- // d.cr, _ = d.d.(containerStateRecv)
- return d
- }
- func (d *Decoder) r() *decReaderSwitch {
- return &d.decReaderSwitch
- }
- func (d *Decoder) resetCommon() {
- // d.r = &d.decReaderSwitch
- d.d.reset()
- d.err = nil
- d.depth = 0
- d.maxdepth = d.h.MaxDepth
- if d.maxdepth <= 0 {
- d.maxdepth = decDefMaxDepth
- }
- // reset all things which were cached from the Handle, but could change
- d.mtid, d.stid = 0, 0
- d.mtr, d.str = false, false
- if d.h.MapType != nil {
- d.mtid = rt2id(d.h.MapType)
- d.mtr = fastpathAV.index(d.mtid) != -1
- }
- if d.h.SliceType != nil {
- d.stid = rt2id(d.h.SliceType)
- d.str = fastpathAV.index(d.stid) != -1
- }
- }
- // Reset the Decoder with a new Reader to decode from,
- // clearing all state from last run(s).
- func (d *Decoder) Reset(r io.Reader) {
- if r == nil {
- return
- }
- d.bytes = false
- // d.typ = entryTypeUnset
- if d.h.ReaderBufferSize > 0 {
- if d.bi == nil {
- d.bi = new(bufioDecReader)
- }
- d.bi.reset(r, d.h.ReaderBufferSize)
- // d.r = d.bi
- // d.typ = entryTypeBufio
- d.bufio = true
- } else {
- // d.ri.x = &d.b
- // d.s = d.sa[:0]
- if d.ri == nil {
- d.ri = new(ioDecReader)
- }
- d.ri.reset(r)
- // d.r = d.ri
- // d.typ = entryTypeIo
- d.bufio = false
- }
- d.resetCommon()
- }
- // ResetBytes resets the Decoder with a new []byte to decode from,
- // clearing all state from last run(s).
- func (d *Decoder) ResetBytes(in []byte) {
- if in == nil {
- return
- }
- d.bytes = true
- d.bufio = false
- // d.typ = entryTypeBytes
- d.rb.reset(in)
- // d.r = &d.rb
- d.resetCommon()
- }
- func (d *Decoder) naked() *decNaked {
- return &d.n
- }
- // Decode decodes the stream from reader and stores the result in the
- // value pointed to by v. v cannot be a nil pointer. v can also be
- // a reflect.Value of a pointer.
- //
- // Note that a pointer to a nil interface is not a nil pointer.
- // If you do not know what type of stream it is, pass in a pointer to a nil interface.
- // We will decode and store a value in that nil interface.
- //
- // Sample usages:
- // // Decoding into a non-nil typed value
- // var f float32
- // err = codec.NewDecoder(r, handle).Decode(&f)
- //
- // // Decoding into nil interface
- // var v interface{}
- // dec := codec.NewDecoder(r, handle)
- // err = dec.Decode(&v)
- //
- // When decoding into a nil interface{}, we will decode into an appropriate value based
- // on the contents of the stream:
- // - Numbers are decoded as float64, int64 or uint64.
- // - Other values are decoded appropriately depending on the type:
- // bool, string, []byte, time.Time, etc
- // - Extensions are decoded as RawExt (if no ext function registered for the tag)
- // Configurations exist on the Handle to override defaults
- // (e.g. for MapType, SliceType and how to decode raw bytes).
- //
- // When decoding into a non-nil interface{} value, 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 BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error
- // - Else decode it based on its reflect.Kind
- //
- // There are some special rules when decoding into containers (slice/array/map/struct).
- // Decode will typically use the stream contents to UPDATE the container i.e. the values
- // in these containers will not be zero'ed before decoding.
- // - A map can be decoded from a stream map, by updating matching keys.
- // - A slice can be decoded from a stream array,
- // by updating the first n elements, where n is length of the stream.
- // - A slice can be decoded from a stream map, by decoding as if
- // it contains a sequence of key-value pairs.
- // - A struct can be decoded from a stream map, by updating matching fields.
- // - A struct can be decoded from a stream array,
- // by updating fields as they occur in the struct (by index).
- //
- // This in-place update maintains consistency in the decoding philosophy (i.e. we ALWAYS update
- // in place by default). However, the consequence of this is that values in slices or maps
- // which are not zero'ed before hand, will have part of the prior values in place after decode
- // if the stream doesn't contain an update for those parts.
- //
- // This in-place update can be disabled by configuring the MapValueReset and SliceElementReset
- // decode options available on every handle.
- //
- // Furthermore, when decoding a stream map or array with length of 0 into a nil map or slice,
- // we reset the destination map or slice to a zero-length value.
- //
- // However, when decoding a stream nil, we reset the destination container
- // to its "zero" value (e.g. nil for slice/map, etc).
- //
- // Note: we allow nil values in the stream anywhere except for map keys.
- // A nil value in the encoded stream where a map key is expected is treated as an error.
- func (d *Decoder) Decode(v interface{}) (err error) {
- // 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() { d.deferred(&err) }()
- // { x, y := d, &err; defer func() { x.deferred(y) }() }
- if d.err != nil {
- return d.err
- }
- if recoverPanicToErr {
- defer func() {
- if x := recover(); x != nil {
- panicValToErr(d, x, &d.err)
- if d.err != err {
- err = d.err
- }
- }
- }()
- }
- // defer d.deferred(&err)
- d.mustDecode(v)
- return
- }
- // MustDecode is like Decode, but panics if unable to Decode.
- // This provides insight to the code location that triggered the error.
- func (d *Decoder) MustDecode(v interface{}) {
- if d.err != nil {
- panic(d.err)
- }
- d.mustDecode(v)
- }
- // MustDecode is like Decode, but panics if unable to Decode.
- // This provides insight to the code location that triggered the error.
- func (d *Decoder) mustDecode(v interface{}) {
- // TODO: Top-level: ensure that v is a pointer and not nil.
- if d.bi == nil {
- if d.d.TryDecodeAsNil() {
- setZero(v)
- } else {
- d.decode(v)
- }
- d.d.atEndOfDecode()
- return
- }
- if d.d.TryDecodeAsNil() {
- setZero(v)
- } else {
- d.bi.calls++
- d.decode(v)
- d.bi.calls--
- }
- if d.bi.calls == 0 {
- d.d.atEndOfDecode()
- if !d.h.ExplicitRelease {
- d.bi.release()
- }
- }
- }
- // func (d *Decoder) deferred(err1 *error) {
- // if recoverPanicToErr {
- // if x := recover(); x != nil {
- // panicValToErr(d, x, err1)
- // panicValToErr(d, x, &d.err)
- // }
- // }
- // }
- //go:noinline -- as it is run by finalizer
- func (d *Decoder) finalize() {
- d.Release()
- }
- // Release releases shared (pooled) resources.
- //
- // It is important to call Release() when done with a Decoder, so those resources
- // are released instantly for use by subsequently created Decoders.
- //
- // By default, Release() is automatically called unless the option ExplicitRelease is set.
- func (d *Decoder) Release() {
- if d.bi != nil {
- d.bi.release()
- }
- // d.decNakedPooler.end()
- }
- // // this is not a smart swallow, as it allocates objects and does unnecessary work.
- // func (d *Decoder) swallowViaHammer() {
- // var blank interface{}
- // d.decodeValueNoFn(reflect.ValueOf(&blank).Elem())
- // }
- func (d *Decoder) swallow() {
- // smarter decode that just swallows the content
- dd := d.d
- if dd.TryDecodeAsNil() {
- return
- }
- switch dd.ContainerType() {
- case valueTypeMap:
- containerLen := d.mapStart()
- hasLen := containerLen >= 0
- for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
- // if clenGtEqualZero {if j >= containerLen {break} } else if dd.CheckBreak() {break}
- d.mapElemKey()
- d.swallow()
- d.mapElemValue()
- d.swallow()
- }
- d.mapEnd()
- case valueTypeArray:
- containerLen := d.arrayStart()
- hasLen := containerLen >= 0
- for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ {
- d.arrayElem()
- d.swallow()
- }
- d.arrayEnd()
- case valueTypeBytes:
- dd.DecodeBytes(d.b[:], true)
- case valueTypeString:
- dd.DecodeStringAsBytes()
- default:
- // these are all primitives, which we can get from decodeNaked
- // if RawExt using Value, complete the processing.
- n := d.naked()
- dd.DecodeNaked()
- if n.v == valueTypeExt && n.l == nil {
- var v2 interface{}
- d.decode(&v2)
- }
- }
- }
- func setZero(iv interface{}) {
- if iv == nil || definitelyNil(iv) {
- return
- }
- var canDecode bool
- switch v := iv.(type) {
- case *string:
- *v = ""
- case *bool:
- *v = false
- case *int:
- *v = 0
- case *int8:
- *v = 0
- case *int16:
- *v = 0
- case *int32:
- *v = 0
- case *int64:
- *v = 0
- case *uint:
- *v = 0
- case *uint8:
- *v = 0
- case *uint16:
- *v = 0
- case *uint32:
- *v = 0
- case *uint64:
- *v = 0
- case *float32:
- *v = 0
- case *float64:
- *v = 0
- case *[]uint8:
- *v = nil
- case *Raw:
- *v = nil
- case *time.Time:
- *v = time.Time{}
- case reflect.Value:
- if v, canDecode = isDecodeable(v); canDecode && v.CanSet() {
- v.Set(reflect.Zero(v.Type()))
- } // TODO: else drain if chan, clear if map, set all to nil if slice???
- default:
- if !fastpathDecodeSetZeroTypeSwitch(iv) {
- v := reflect.ValueOf(iv)
- if v, canDecode = isDecodeable(v); canDecode && v.CanSet() {
- v.Set(reflect.Zero(v.Type()))
- } // TODO: else drain if chan, clear if map, set all to nil if slice???
- }
- }
- }
- func (d *Decoder) decode(iv interface{}) {
- // a switch with only concrete types can be optimized.
- // consequently, we deal with nil and interfaces outside the switch.
- if iv == nil {
- d.errorstr(errstrCannotDecodeIntoNil)
- return
- }
- switch v := iv.(type) {
- // case nil:
- // case Selfer:
- case reflect.Value:
- v = d.ensureDecodeable(v)
- d.decodeValue(v, nil)
- case *string:
- *v = string(d.d.DecodeStringAsBytes())
- case *bool:
- *v = d.d.DecodeBool()
- case *int:
- *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
- case *int8:
- *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
- case *int16:
- *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
- case *int32:
- *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
- case *int64:
- *v = d.d.DecodeInt64()
- case *uint:
- *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
- case *uint8:
- *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
- case *uint16:
- *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
- case *uint32:
- *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
- case *uint64:
- *v = d.d.DecodeUint64()
- case *float32:
- *v = float32(d.decodeFloat32())
- case *float64:
- *v = d.d.DecodeFloat64()
- case *[]uint8:
- *v = d.d.DecodeBytes(*v, false)
- case []uint8:
- b := d.d.DecodeBytes(v, false)
- if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) {
- copy(v, b)
- }
- case *time.Time:
- *v = d.d.DecodeTime()
- case *Raw:
- *v = d.rawBytes()
- case *interface{}:
- d.decodeValue(reflect.ValueOf(iv).Elem(), nil)
- // d.decodeValueNotNil(reflect.ValueOf(iv).Elem())
- default:
- if v, ok := iv.(Selfer); ok {
- v.CodecDecodeSelf(d)
- } else if !fastpathDecodeTypeSwitch(iv, d) {
- v := reflect.ValueOf(iv)
- v = d.ensureDecodeable(v)
- d.decodeValue(v, nil) // TODO: find a way to say: no fast path??? Not necessary???
- // d.decodeValueFallback(v)
- }
- }
- }
- func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) {
- // If stream is not containing a nil value, then we can deref to the base
- // non-pointer value, and decode into that.
- var rvp reflect.Value
- var rvpValid bool
- if rv.Kind() == reflect.Ptr {
- rvpValid = true
- for {
- if rvisnil(rv) {
- rv.Set(reflect.New(rv.Type().Elem()))
- }
- rvp = rv
- rv = rv.Elem()
- if rv.Kind() != reflect.Ptr {
- break
- }
- }
- }
- if fn == nil {
- fn = d.h.fn(rv.Type())
- }
- if fn.i.addrD {
- if rvpValid {
- fn.fd(d, &fn.i, rvp)
- } else if rv.CanAddr() {
- fn.fd(d, &fn.i, rv.Addr())
- } else if !fn.i.addrF {
- fn.fd(d, &fn.i, rv)
- } else {
- d.errorf("cannot decode into a non-pointer value")
- }
- } else {
- fn.fd(d, &fn.i, rv)
- }
- // return rv
- }
- func (d *Decoder) structFieldNotFound(index int, rvkencname string) {
- // NOTE: rvkencname may be a stringView, so don't pass it to another function.
- if d.h.ErrorIfNoField {
- if index >= 0 {
- d.errorf("no matching struct field found when decoding stream array at index %v", index)
- return
- } else if rvkencname != "" {
- d.errorf("no matching struct field found when decoding stream map with key " + rvkencname)
- return
- }
- }
- d.swallow()
- }
- func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) {
- if d.h.ErrorIfNoArrayExpand {
- d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen)
- }
- }
- func isDecodeable(rv reflect.Value) (rv2 reflect.Value, canDecode bool) {
- switch rv.Kind() {
- case reflect.Array:
- return rv, rv.CanAddr()
- case reflect.Ptr:
- if !rvisnil(rv) {
- return rv.Elem(), true
- }
- case reflect.Slice, reflect.Chan, reflect.Map:
- if !rvisnil(rv) {
- return rv, true
- }
- }
- return
- }
- func (d *Decoder) ensureDecodeable(rv reflect.Value) (rv2 reflect.Value) {
- // decode can take any reflect.Value that is a inherently addressable i.e.
- // - array
- // - non-nil chan (we will SEND to it)
- // - non-nil slice (we will set its elements)
- // - non-nil map (we will put into it)
- // - non-nil pointer (we can "update" it)
- rv2, canDecode := isDecodeable(rv)
- if canDecode {
- return
- }
- if !rv.IsValid() {
- d.errorstr(errstrCannotDecodeIntoNil)
- return
- }
- if !rv.CanInterface() {
- d.errorf("cannot decode into a value without an interface: %v", rv)
- return
- }
- rvi := rv2i(rv)
- rvk := rv.Kind()
- d.errorf("cannot decode into value of kind: %v, type: %T, %v", rvk, rvi, rvi)
- return
- }
- func (d *Decoder) depthIncr() {
- d.depth++
- if d.depth >= d.maxdepth {
- panic(errMaxDepthExceeded)
- }
- }
- func (d *Decoder) depthDecr() {
- d.depth--
- }
- // Possibly get an interned version of a string
- //
- // This should mostly be used for map keys, where the key type is string.
- // This is because keys of a map/struct are typically reused across many objects.
- func (d *Decoder) string(v []byte) (s string) {
- if d.is == nil {
- return string(v) // don't return stringView, as we need a real string here.
- }
- s, ok := d.is[string(v)] // no allocation here, per go implementation
- if !ok {
- s = string(v) // new allocation here
- d.is[s] = s
- }
- return s
- }
- // nextValueBytes returns the next value in the stream as a set of bytes.
- func (d *Decoder) nextValueBytes() (bs []byte) {
- d.d.uncacheRead()
- d.r().track()
- d.swallow()
- bs = d.r().stopTrack()
- return
- }
- func (d *Decoder) rawBytes() []byte {
- // ensure that this is not a view into the bytes
- // i.e. make new copy always.
- bs := d.nextValueBytes()
- bs2 := make([]byte, len(bs))
- copy(bs2, bs)
- return bs2
- }
- func (d *Decoder) wrapErr(v interface{}, err *error) {
- *err = decodeError{codecError: codecError{name: d.hh.Name(), err: v}, pos: int(d.r().numread())}
- }
- // NumBytesRead returns the number of bytes read
- func (d *Decoder) NumBytesRead() int {
- return int(d.r().numread())
- }
- // decodeFloat32 will delegate to an appropriate DecodeFloat32 implementation (if exists),
- // else if will call DecodeFloat64 and ensure the value doesn't overflow.
- //
- // Note that we return float64 to reduce unnecessary conversions
- func (d *Decoder) decodeFloat32() float32 {
- if d.js {
- return d.jdec.DecodeFloat32() // custom implementation for 32-bit
- }
- return float32(chkOvf.Float32V(d.d.DecodeFloat64()))
- }
- // ---- container tracking
- // Note: We update the .c after calling the callback.
- // This way, the callback can know what the last status was.
- func (d *Decoder) mapStart() (v int) {
- v = d.d.ReadMapStart()
- d.depthIncr()
- d.c = containerMapStart
- return
- }
- func (d *Decoder) mapElemKey() {
- if d.js {
- d.jdec.ReadMapElemKey()
- }
- d.c = containerMapKey
- }
- func (d *Decoder) mapElemValue() {
- if d.js {
- d.jdec.ReadMapElemValue()
- }
- d.c = containerMapValue
- }
- func (d *Decoder) mapEnd() {
- d.d.ReadMapEnd()
- d.depthDecr()
- d.c = containerMapEnd
- d.c = 0
- }
- func (d *Decoder) arrayStart() (v int) {
- v = d.d.ReadArrayStart()
- d.depthIncr()
- d.c = containerArrayStart
- return
- }
- func (d *Decoder) arrayElem() {
- if d.js {
- d.jdec.ReadArrayElem()
- }
- d.c = containerArrayElem
- }
- func (d *Decoder) arrayEnd() {
- d.d.ReadArrayEnd()
- d.depthDecr()
- d.c = containerArrayEnd
- d.c = 0
- }
- func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext Ext) {
- // var v interface{} = ext.ConvertExt(rv)
- // d.d.decode(&v)
- // ext.UpdateExt(rv, v)
- // assume v is a pointer:
- // - if struct|array, pass as is to ConvertExt
- // - else make it non-addressable and pass to ConvertExt
- // - make return value from ConvertExt addressable
- // - decode into it
- // - return the interface for passing into UpdateExt.
- // - interface should be a pointer if struct|array, else a value
- var s interface{}
- rv := reflect.ValueOf(v)
- rv2 := rv.Elem()
- rvk := rv2.Kind()
- if rvk == reflect.Struct || rvk == reflect.Array {
- s = ext.ConvertExt(v)
- } else {
- s = ext.ConvertExt(rv2i(rv2))
- }
- rv = reflect.ValueOf(s)
- if !rv.CanAddr() {
- if rv.Kind() == reflect.Ptr {
- rv2 = reflect.New(rv.Type().Elem())
- rv2.Set(rv)
- } else {
- rv2 = reflect.New(rv.Type()).Elem()
- rv2.Set(rv)
- }
- rv = rv2
- }
- d.decodeValue(rv, nil)
- ext.UpdateExt(v, rv2i(rv))
- }
- func (d *Decoder) sideDecode(v interface{}, bs []byte) {
- rv := baseRV(v)
- NewDecoderBytes(bs, d.hh).decodeValue(rv, d.h.fnNoExt(rv.Type()))
- }
- // --------------------------------------------------
- // decSliceHelper assists when decoding into a slice, from a map or an array in the stream.
- // A slice can be set from a map or array in stream. This supports the MapBySlice interface.
- type decSliceHelper struct {
- d *Decoder
- ct valueType
- array bool
- }
- func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) {
- x.ct = d.d.ContainerType()
- x.d = d
- switch x.ct {
- case valueTypeArray:
- x.array = true
- clen = d.arrayStart()
- case valueTypeMap:
- clen = d.mapStart() * 2
- default:
- d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct)
- }
- return
- }
- func (x decSliceHelper) End() {
- if x.array {
- x.d.arrayEnd()
- } else {
- x.d.mapEnd()
- }
- }
- func (x decSliceHelper) ElemContainerState(index int) {
- if x.array {
- x.d.arrayElem()
- } else if index%2 == 0 {
- x.d.mapElemKey()
- } else {
- x.d.mapElemValue()
- }
- }
- func decByteSlice(r *decReaderSwitch, clen, maxInitLen int, bs []byte) (bsOut []byte) {
- if clen == 0 {
- return zeroByteSlice
- }
- if len(bs) == clen {
- bsOut = bs
- r.readb(bsOut)
- } else if cap(bs) >= clen {
- bsOut = bs[:clen]
- r.readb(bsOut)
- } else {
- // bsOut = make([]byte, clen)
- len2 := decInferLen(clen, maxInitLen, 1)
- bsOut = make([]byte, len2)
- r.readb(bsOut)
- for len2 < clen {
- len3 := decInferLen(clen-len2, maxInitLen, 1)
- bs3 := bsOut
- bsOut = make([]byte, len2+len3)
- copy(bsOut, bs3)
- r.readb(bsOut[len2:])
- len2 += len3
- }
- }
- return
- }
- // func decByteSliceZeroCopy(r decReader, clen, maxInitLen int, bs []byte) (bsOut []byte) {
- // if _, ok := r.(*bytesDecReader); ok && clen <= maxInitLen {
- // return r.readx(clen)
- // }
- // return decByteSlice(r, clen, maxInitLen, bs)
- // }
- func detachZeroCopyBytes(isBytesReader bool, dest []byte, in []byte) (out []byte) {
- if xlen := len(in); xlen > 0 {
- if isBytesReader || xlen <= scratchByteArrayLen {
- if cap(dest) >= xlen {
- out = dest[:xlen]
- } else {
- out = make([]byte, xlen)
- }
- copy(out, in)
- return
- }
- }
- return in
- }
- // decInferLen will infer a sensible length, given the following:
- // - clen: length wanted.
- // - maxlen: max length to be returned.
- // if <= 0, it is unset, and we infer it based on the unit size
- // - unit: number of bytes for each element of the collection
- func decInferLen(clen, maxlen, unit int) (rvlen int) {
- const maxLenIfUnset = 8 // 64
- // handle when maxlen is not set i.e. <= 0
- // clen==0: use 0
- // maxlen<=0, clen<0: use default
- // maxlen> 0, clen<0: use default
- // maxlen<=0, clen>0: infer maxlen, and cap on it
- // maxlen> 0, clen>0: cap at maxlen
- if clen == 0 {
- return
- }
- if clen < 0 {
- return maxLenIfUnset
- }
- if unit == 0 {
- return clen
- }
- if maxlen <= 0 {
- // no maxlen defined. Use maximum of 256K memory, with a floor of 4K items.
- // maxlen = 256 * 1024 / unit
- // if maxlen < (4 * 1024) {
- // maxlen = 4 * 1024
- // }
- if unit < (256 / 4) {
- maxlen = 256 * 1024 / unit
- } else {
- maxlen = 4 * 1024
- }
- // if maxlen > maxLenIfUnset {
- // maxlen = maxLenIfUnset
- // }
- }
- if clen > maxlen {
- rvlen = maxlen
- } else {
- rvlen = clen
- }
- return
- }
- func expandSliceRV(s reflect.Value, st reflect.Type, canChange bool, stElemSize, num, slen, scap int) (
- s2 reflect.Value, scap2 int, changed bool, err string) {
- l1 := slen + num // new slice length
- if l1 < slen {
- err = errmsgExpandSliceOverflow
- return
- }
- if l1 <= scap {
- if s.CanSet() {
- rvssetlen(s, l1)
- } else if canChange {
- s2 = s.Slice(0, l1)
- scap2 = scap
- changed = true
- } else {
- err = errmsgExpandSliceCannotChange
- return
- }
- return
- }
- if !canChange {
- err = errmsgExpandSliceCannotChange
- return
- }
- scap2 = growCap(scap, stElemSize, num)
- s2 = reflect.MakeSlice(st, l1, scap2)
- changed = true
- reflect.Copy(s2, s)
- return
- }
- func decReadFull(r io.Reader, bs []byte) (n uint, err error) {
- var nn int
- for n < uint(len(bs)) && err == nil {
- nn, err = r.Read(bs[n:])
- if nn > 0 {
- if err == io.EOF {
- // leave EOF for next time
- err = nil
- }
- n += uint(nn)
- }
- }
- // do not do this - it serves no purpose
- // if n != len(bs) && err == io.EOF { err = io.ErrUnexpectedEOF }
- return
- }
- func decNakedReadRawBytes(dr decDriver, d *Decoder, n *decNaked, rawToString bool) {
- if rawToString {
- n.v = valueTypeString
- n.s = string(dr.DecodeBytes(d.b[:], true))
- } else {
- n.v = valueTypeBytes
- n.l = dr.DecodeBytes(nil, false)
- }
- }
|