decode.go 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "encoding"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "math"
  10. "reflect"
  11. "runtime"
  12. "strconv"
  13. "time"
  14. )
  15. // Some tagging information for error messages.
  16. const (
  17. msgBadDesc = "unrecognized descriptor byte"
  18. // msgDecCannotExpandArr = "cannot expand go array from %v to stream length: %v"
  19. )
  20. const (
  21. decDefMaxDepth = 1024 // maximum depth
  22. decDefSliceCap = 8
  23. decDefChanCap = 64 // should be large, as cap cannot be expanded
  24. decScratchByteArrayLen = cacheLineSize // - 5 // + (8 * 2) // - (8 * 1)
  25. // decContainerLenUnknown is length returned from Read(Map|Array)Len
  26. // when a format doesn't know apiori.
  27. // For example, json doesn't pre-determine the length of a container (sequence/map).
  28. decContainerLenUnknown = -1
  29. // decContainerLenNil is length returned from Read(Map|Array)Len
  30. // when a 'nil' was encountered in the stream.
  31. decContainerLenNil = math.MinInt32
  32. // decFailNonEmptyIntf configures whether we error when decoding naked into a non-empty interface.
  33. //
  34. // Typically, we cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader).
  35. // However, in some scenarios, this should be allowed:
  36. // - MapType
  37. // - SliceType
  38. // - Extensions
  39. //
  40. // Consequently, we should relax this. Put it behind a const flag for now.
  41. decFailNonEmptyIntf = false
  42. )
  43. var (
  44. errstrOnlyMapOrArrayCanDecodeIntoStruct = "only encoded map or array can be decoded into a struct"
  45. errstrCannotDecodeIntoNil = "cannot decode into nil"
  46. errmsgExpandSliceOverflow = "expand slice: slice overflow"
  47. errmsgExpandSliceCannotChange = "expand slice: cannot change"
  48. errDecoderNotInitialized = errors.New("Decoder not initialized")
  49. errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read")
  50. errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read")
  51. errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown")
  52. errMaxDepthExceeded = errors.New("maximum decoding depth exceeded")
  53. errBytesDecReaderCannotUnread = errors.New("cannot unread last byte read")
  54. )
  55. type decDriver interface {
  56. // this will check if the next token is a break.
  57. CheckBreak() bool
  58. // // TryDecodeAsNil tries to decode as nil.
  59. // //
  60. // // Note: TryDecodeAsNil should be careful not to share any temporary []byte with
  61. // // the rest of the decDriver. This is because sometimes, we optimize by holding onto
  62. // // a transient []byte, and ensuring the only other call we make to the decDriver
  63. // // during that time is maybe a TryDecodeAsNil() call.
  64. // TryDecodeAsNil() bool
  65. // // Nil says whether the last scalar value read from the stream was a nil value.
  66. // //
  67. // // This is sometimes inspected by the decoder if they need to determine whether
  68. // // a pointer should be set to nil or the returned zero value.
  69. // Nil() bool
  70. // TryNil tries to decode as nil.
  71. TryNil() bool
  72. // ContainerType returns one of: Bytes, String, Nil, Slice or Map.
  73. //
  74. // Return unSet if not known.
  75. //
  76. // Note: Implementations MUST fully consume sentinel container types, specifically Nil.
  77. ContainerType() (vt valueType)
  78. // IsBuiltinType(rt uintptr) bool
  79. // DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt.
  80. // For maps and arrays, it will not do the decoding in-band, but will signal
  81. // the decoder, so that is done later, by setting the decNaked.valueType field.
  82. //
  83. // Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types).
  84. // for extensions, DecodeNaked must read the tag and the []byte if it exists.
  85. // if the []byte is not read, then kInterfaceNaked will treat it as a Handle
  86. // that stores the subsequent value in-band, and complete reading the RawExt.
  87. //
  88. // extensions should also use readx to decode them, for efficiency.
  89. // kInterface will extract the detached byte slice if it has to pass it outside its realm.
  90. DecodeNaked()
  91. DecodeInt64() (i int64)
  92. DecodeUint64() (ui uint64)
  93. DecodeFloat64() (f float64)
  94. DecodeBool() (b bool)
  95. // DecodeStringAsBytes returns the bytes representing a string.
  96. // By definition, it will return a view into a scratch buffer.
  97. //
  98. // Note: This can also decode symbols, if supported.
  99. //
  100. // Users should consume it right away and not store it for later use.
  101. DecodeStringAsBytes() (v []byte)
  102. // DecodeBytes may be called directly, without going through reflection.
  103. // Consequently, it must be designed to handle possible nil.
  104. DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte)
  105. // DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte)
  106. // DecodeExt will decode into a *RawExt or into an extension.
  107. DecodeExt(v interface{}, xtag uint64, ext Ext)
  108. // decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte)
  109. DecodeTime() (t time.Time)
  110. // ReadArrayStart will return the length of the array.
  111. // If the format doesn't prefix the length, it returns decContainerLenUnknown.
  112. // If the expected array was a nil in the stream, it returns decContainerLenNil.
  113. ReadArrayStart() int
  114. ReadArrayEnd()
  115. // ReadMapStart will return the length of the array.
  116. // If the format doesn't prefix the length, it returns decContainerLenUnknown.
  117. // If the expected array was a nil in the stream, it returns decContainerLenNil.
  118. ReadMapStart() int
  119. ReadMapEnd()
  120. reset()
  121. atEndOfDecode()
  122. uncacheRead()
  123. }
  124. type decDriverContainerTracker interface {
  125. ReadArrayElem()
  126. ReadMapElemKey()
  127. ReadMapElemValue()
  128. }
  129. type decodeError struct {
  130. codecError
  131. pos int
  132. }
  133. func (d decodeError) Error() string {
  134. return fmt.Sprintf("%s decode error [pos %d]: %v", d.name, d.pos, d.err)
  135. }
  136. type decDriverNoopContainerReader struct{}
  137. func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { return }
  138. func (x decDriverNoopContainerReader) ReadArrayEnd() {}
  139. func (x decDriverNoopContainerReader) ReadMapStart() (v int) { return }
  140. func (x decDriverNoopContainerReader) ReadMapEnd() {}
  141. func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return }
  142. func (x decDriverNoopContainerReader) atEndOfDecode() {}
  143. // func (x decDriverNoopContainerReader) ReadArrayElem() {}
  144. // func (x decDriverNoopContainerReader) ReadMapElemKey() {}
  145. // func (x decDriverNoopContainerReader) ReadMapElemValue() {}
  146. // func (x decNoSeparator) uncacheRead() {}
  147. // DecodeOptions captures configuration options during decode.
  148. type DecodeOptions struct {
  149. // MapType specifies type to use during schema-less decoding of a map in the stream.
  150. // If nil (unset), we default to map[string]interface{} iff json handle and MapStringAsKey=true,
  151. // else map[interface{}]interface{}.
  152. MapType reflect.Type
  153. // SliceType specifies type to use during schema-less decoding of an array in the stream.
  154. // If nil (unset), we default to []interface{} for all formats.
  155. SliceType reflect.Type
  156. // MaxInitLen defines the maxinum initial length that we "make" a collection
  157. // (string, slice, map, chan). If 0 or negative, we default to a sensible value
  158. // based on the size of an element in the collection.
  159. //
  160. // For example, when decoding, a stream may say that it has 2^64 elements.
  161. // We should not auto-matically provision a slice of that size, to prevent Out-Of-Memory crash.
  162. // Instead, we provision up to MaxInitLen, fill that up, and start appending after that.
  163. MaxInitLen int
  164. // ReaderBufferSize is the size of the buffer used when reading.
  165. //
  166. // if > 0, we use a smart buffer internally for performance purposes.
  167. ReaderBufferSize int
  168. // MaxDepth defines the maximum depth when decoding nested
  169. // maps and slices. If 0 or negative, we default to a suitably large number (currently 1024).
  170. MaxDepth int16
  171. // If ErrorIfNoField, return an error when decoding a map
  172. // from a codec stream into a struct, and no matching struct field is found.
  173. ErrorIfNoField bool
  174. // If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded.
  175. // For example, the stream contains an array of 8 items, but you are decoding into a [4]T array,
  176. // or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set).
  177. ErrorIfNoArrayExpand bool
  178. // If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64).
  179. SignedInteger bool
  180. // MapValueReset controls how we decode into a map value.
  181. //
  182. // By default, we MAY retrieve the mapping for a key, and then decode into that.
  183. // However, especially with big maps, that retrieval may be expensive and unnecessary
  184. // if the stream already contains all that is necessary to recreate the value.
  185. //
  186. // If true, we will never retrieve the previous mapping,
  187. // but rather decode into a new value and set that in the map.
  188. //
  189. // If false, we will retrieve the previous mapping if necessary e.g.
  190. // the previous mapping is a pointer, or is a struct or array with pre-set state,
  191. // or is an interface.
  192. MapValueReset bool
  193. // SliceElementReset: on decoding a slice, reset the element to a zero value first.
  194. //
  195. // concern: if the slice already contained some garbage, we will decode into that garbage.
  196. SliceElementReset bool
  197. // InterfaceReset controls how we decode into an interface.
  198. //
  199. // By default, when we see a field that is an interface{...},
  200. // or a map with interface{...} value, we will attempt decoding into the
  201. // "contained" value.
  202. //
  203. // However, this prevents us from reading a string into an interface{}
  204. // that formerly contained a number.
  205. //
  206. // If true, we will decode into a new "blank" value, and set that in the interface.
  207. // If false, we will decode into whatever is contained in the interface.
  208. InterfaceReset bool
  209. // InternString controls interning of strings during decoding.
  210. //
  211. // Some handles, e.g. json, typically will read map keys as strings.
  212. // If the set of keys are finite, it may help reduce allocation to
  213. // look them up from a map (than to allocate them afresh).
  214. //
  215. // Note: Handles will be smart when using the intern functionality.
  216. // Every string should not be interned.
  217. // An excellent use-case for interning is struct field names,
  218. // or map keys where key type is string.
  219. InternString bool
  220. // PreferArrayOverSlice controls whether to decode to an array or a slice.
  221. //
  222. // This only impacts decoding into a nil interface{}.
  223. // Consequently, it has no effect on codecgen.
  224. //
  225. // *Note*: This only applies if using go1.5 and above,
  226. // as it requires reflect.ArrayOf support which was absent before go1.5.
  227. PreferArrayOverSlice bool
  228. // DeleteOnNilMapValue controls how to decode a nil value in the stream.
  229. //
  230. // If true, we will delete the mapping of the key.
  231. // Else, just set the mapping to the zero value of the type.
  232. //
  233. // Deprecated: This does NOTHING and is left behind for compiling compatibility.
  234. // This change is necessitated because 'nil' in a stream now consistently
  235. // means the zero value (ie reset the value to its zero state).
  236. DeleteOnNilMapValue bool
  237. // RawToString controls how raw bytes in a stream are decoded into a nil interface{}.
  238. // By default, they are decoded as []byte, but can be decoded as string (if configured).
  239. RawToString bool
  240. }
  241. // ----------------------------------------
  242. // func (d *Decoder) builtin(f *codecFnInfo, rv reflect.Value) {
  243. // d.d.DecodeBuiltin(f.ti.rtid, rv2i(rv))
  244. // }
  245. func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) {
  246. d.d.DecodeExt(rv2i(rv), 0, nil)
  247. }
  248. func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) {
  249. d.d.DecodeExt(rv2i(rv), f.xfTag, f.xfFn)
  250. }
  251. func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) {
  252. rv2i(rv).(Selfer).CodecDecodeSelf(d)
  253. }
  254. func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) {
  255. bm := rv2i(rv).(encoding.BinaryUnmarshaler)
  256. xbs := d.d.DecodeBytes(nil, true)
  257. if fnerr := bm.UnmarshalBinary(xbs); fnerr != nil {
  258. panic(fnerr)
  259. }
  260. }
  261. func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) {
  262. tm := rv2i(rv).(encoding.TextUnmarshaler)
  263. fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes())
  264. if fnerr != nil {
  265. panic(fnerr)
  266. }
  267. }
  268. func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) {
  269. tm := rv2i(rv).(jsonUnmarshaler)
  270. // bs := d.d.DecodeBytes(d.b[:], true, true)
  271. // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
  272. fnerr := tm.UnmarshalJSON(d.nextValueBytes())
  273. if fnerr != nil {
  274. panic(fnerr)
  275. }
  276. }
  277. func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) {
  278. d.errorf("no decoding function defined for kind %v", rv.Kind())
  279. }
  280. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  281. rvSetBytes(rv, d.rawBytes())
  282. }
  283. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  284. rvSetString(rv, string(d.d.DecodeStringAsBytes()))
  285. }
  286. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  287. rvSetBool(rv, d.d.DecodeBool())
  288. }
  289. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  290. rvSetTime(rv, d.d.DecodeTime())
  291. }
  292. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  293. rvSetFloat32(rv, d.decodeFloat32())
  294. }
  295. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  296. rvSetFloat64(rv, d.d.DecodeFloat64())
  297. }
  298. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  299. rvSetInt(rv, int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)))
  300. }
  301. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  302. rvSetInt8(rv, int8(chkOvf.IntV(d.d.DecodeInt64(), 8)))
  303. }
  304. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  305. rvSetInt16(rv, int16(chkOvf.IntV(d.d.DecodeInt64(), 16)))
  306. }
  307. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  308. rvSetInt32(rv, int32(chkOvf.IntV(d.d.DecodeInt64(), 32)))
  309. }
  310. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  311. rvSetInt64(rv, d.d.DecodeInt64())
  312. }
  313. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  314. rvSetUint(rv, uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
  315. }
  316. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  317. rvSetUintptr(rv, uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
  318. }
  319. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  320. rvSetUint8(rv, uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)))
  321. }
  322. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  323. rvSetUint16(rv, uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)))
  324. }
  325. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  326. rvSetUint32(rv, uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)))
  327. }
  328. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  329. rvSetUint64(rv, d.d.DecodeUint64())
  330. }
  331. // var kIntfCtr uint64
  332. func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) {
  333. // nil interface:
  334. // use some hieristics to decode it appropriately
  335. // based on the detected next value in the stream.
  336. n := d.naked()
  337. d.d.DecodeNaked()
  338. // if n.v == valueTypeNil {
  339. // return
  340. // }
  341. // We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader).
  342. // Howver, it is possible that the user has ways to pass in a type for a given interface
  343. // - MapType
  344. // - SliceType
  345. // - Extensions
  346. //
  347. // Consequently, we should relax this. Put it behind a const flag for now.
  348. if decFailNonEmptyIntf && f.ti.numMeth > 0 {
  349. d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth)
  350. return
  351. }
  352. // var useRvn bool
  353. switch n.v {
  354. case valueTypeMap:
  355. // if json, default to a map type with string keys
  356. mtid := d.mtid
  357. if mtid == 0 {
  358. if d.jsms {
  359. mtid = mapStrIntfTypId
  360. } else {
  361. mtid = mapIntfIntfTypId
  362. }
  363. }
  364. if mtid == mapIntfIntfTypId {
  365. var v2 map[interface{}]interface{}
  366. d.decode(&v2)
  367. rvn = rv4i(&v2).Elem()
  368. } else if mtid == mapStrIntfTypId { // for json performance
  369. var v2 map[string]interface{}
  370. d.decode(&v2)
  371. rvn = rv4i(&v2).Elem()
  372. } else {
  373. if d.mtr {
  374. rvn = reflect.New(d.h.MapType)
  375. d.decode(rv2i(rvn))
  376. rvn = rvn.Elem()
  377. } else {
  378. rvn = rvzeroaddrk(d.h.MapType, reflect.Map)
  379. d.decodeValue(rvn, nil)
  380. }
  381. }
  382. case valueTypeArray:
  383. if d.stid == 0 || d.stid == intfSliceTypId {
  384. var v2 []interface{}
  385. d.decode(&v2)
  386. rvn = rv4i(&v2).Elem()
  387. if reflectArrayOfSupported && d.stid == 0 && d.h.PreferArrayOverSlice {
  388. rvn2 := rvzeroaddrk(reflectArrayOf(rvn.Len(), intfTyp), reflect.Array)
  389. reflect.Copy(rvn2, rvn)
  390. rvn = rvn2
  391. }
  392. } else {
  393. if d.str {
  394. rvn = reflect.New(d.h.SliceType)
  395. d.decode(rv2i(rvn))
  396. rvn = rvn.Elem()
  397. } else {
  398. rvn = rvzeroaddrk(d.h.SliceType, reflect.Slice)
  399. d.decodeValue(rvn, nil)
  400. }
  401. }
  402. case valueTypeExt:
  403. tag, bytes := n.u, n.l // calling decode below might taint the values
  404. bfn := d.h.getExtForTag(tag)
  405. var re = RawExt{Tag: tag}
  406. if bytes == nil {
  407. // it is one of the InterfaceExt ones: json and cbor.
  408. // most likely cbor, as json decoding never reveals valueTypeExt (no tagging support)
  409. if bfn == nil {
  410. d.decode(&re.Value)
  411. rvn = rv4i(&re).Elem()
  412. } else {
  413. if bfn.ext == SelfExt {
  414. rvn = rvzeroaddrk(bfn.rt, bfn.rt.Kind())
  415. d.decodeValue(rvn, d.h.fnNoExt(bfn.rt))
  416. } else {
  417. rvn = reflect.New(bfn.rt)
  418. d.interfaceExtConvertAndDecode(rv2i(rvn), bfn.ext)
  419. rvn = rvn.Elem()
  420. }
  421. }
  422. } else {
  423. // one of the BytesExt ones: binc, msgpack, simple
  424. if bfn == nil {
  425. re.Data = detachZeroCopyBytes(d.bytes, nil, bytes)
  426. rvn = rv4i(&re).Elem()
  427. } else {
  428. rvn = reflect.New(bfn.rt)
  429. if bfn.ext == SelfExt {
  430. d.sideDecode(rv2i(rvn), bytes)
  431. } else {
  432. bfn.ext.ReadExt(rv2i(rvn), bytes)
  433. }
  434. rvn = rvn.Elem()
  435. }
  436. }
  437. case valueTypeNil:
  438. // rvn = reflect.Zero(f.ti.rt) // TODO
  439. // no-op
  440. case valueTypeInt:
  441. rvn = n.ri()
  442. case valueTypeUint:
  443. rvn = n.ru()
  444. case valueTypeFloat:
  445. rvn = n.rf()
  446. case valueTypeBool:
  447. rvn = n.rb()
  448. case valueTypeString, valueTypeSymbol:
  449. rvn = n.rs()
  450. case valueTypeBytes:
  451. rvn = n.rl()
  452. case valueTypeTime:
  453. rvn = n.rt()
  454. default:
  455. panicv.errorf("kInterfaceNaked: unexpected valueType: %d", n.v)
  456. }
  457. return
  458. }
  459. func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) {
  460. // Note:
  461. // A consequence of how kInterface works, is that
  462. // if an interface already contains something, we try
  463. // to decode into what was there before.
  464. // We do not replace with a generic value (as got from decodeNaked).
  465. // every interface passed here MUST be settable.
  466. // xdebugf("kInterface: 0")
  467. var rvn reflect.Value
  468. if rvisnil(rv) || d.h.InterfaceReset {
  469. // check if mapping to a type: if so, initialize it and move on
  470. rvn = d.h.intf2impl(f.ti.rtid)
  471. if rvn.IsValid() {
  472. rv.Set(rvn)
  473. } else {
  474. rvn = d.kInterfaceNaked(f)
  475. // xdebugf("kInterface: %v", rvn)
  476. if rvn.IsValid() {
  477. rv.Set(rvn)
  478. } else if d.h.InterfaceReset {
  479. // reset to zero value based on current type in there.
  480. if rvelem := rv.Elem(); rvelem.IsValid() {
  481. rv.Set(reflect.Zero(rvelem.Type()))
  482. }
  483. // } else {
  484. // rv.Set(reflect.Zero(rv.Type()))
  485. // }
  486. }
  487. return
  488. }
  489. } else {
  490. // now we have a non-nil interface value, meaning it contains a type
  491. rvn = rv.Elem()
  492. }
  493. // if d.d.TryDecodeAsNil() {
  494. // rv.Set(reflect.Zero(rvn.Type()))
  495. // return
  496. // }
  497. // Note: interface{} is settable, but underlying type may not be.
  498. // Consequently, we MAY have to create a decodable value out of the underlying value,
  499. // decode into it, and reset the interface itself.
  500. // fmt.Printf(">>>> kInterface: rvn type: %v, rv type: %v\n", rvn.Type(), rv.Type())
  501. if isDecodeable(rvn) {
  502. d.decodeValue(rvn, nil)
  503. return
  504. }
  505. rvn2 := rvzeroaddrk(rvn.Type(), rvn.Kind())
  506. rvn2.Set(rvn)
  507. d.decodeValue(rvn2, nil)
  508. rv.Set(rvn2)
  509. }
  510. func decStructFieldKey(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) {
  511. // use if-else-if, not switch (which compiles to binary-search)
  512. // since keyType is typically valueTypeString, branch prediction is pretty good.
  513. if keyType == valueTypeString {
  514. rvkencname = dd.DecodeStringAsBytes()
  515. } else if keyType == valueTypeInt {
  516. rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10)
  517. } else if keyType == valueTypeUint {
  518. rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10)
  519. } else if keyType == valueTypeFloat {
  520. rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64)
  521. } else {
  522. rvkencname = dd.DecodeStringAsBytes()
  523. }
  524. return
  525. }
  526. func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) {
  527. sfn := structFieldNode{v: rv, update: true}
  528. ctyp := d.d.ContainerType()
  529. // xdebugf("kStruct: rv: %#v", rv)
  530. if ctyp == valueTypeNil {
  531. rv.Set(f.ti.rv0)
  532. return
  533. }
  534. var mf MissingFielder
  535. if f.ti.isFlag(tiflagMissingFielder) {
  536. mf = rv2i(rv).(MissingFielder)
  537. } else if f.ti.isFlag(tiflagMissingFielderPtr) {
  538. mf = rv2i(rv.Addr()).(MissingFielder)
  539. }
  540. if ctyp == valueTypeMap {
  541. containerLen := d.mapStart()
  542. if containerLen == 0 {
  543. d.mapEnd()
  544. return
  545. }
  546. tisfi := f.ti.sfiSort
  547. hasLen := containerLen >= 0
  548. var rvkencname []byte
  549. for j := 0; (hasLen && j < containerLen) || !(hasLen || d.d.CheckBreak()); j++ {
  550. d.mapElemKey()
  551. rvkencname = decStructFieldKey(d.d, f.ti.keyType, &d.b)
  552. // xdebugf("key: '%s'", rvkencname)
  553. d.mapElemValue()
  554. if k := f.ti.indexForEncName(rvkencname); k > -1 {
  555. si := tisfi[k]
  556. // if d.d.TryDecodeAsNil() {
  557. // si.setToZeroValue(rv)
  558. // } else {
  559. // d.decodeValue(sfn.field(si), nil)
  560. // }
  561. d.decodeValue(sfn.field(si), nil)
  562. // xdebugf("value: '%#v'", sfn.field(si))
  563. } else if mf != nil {
  564. // store rvkencname in new []byte, as it previously shares Decoder.b, which is used in decode
  565. name2 := rvkencname
  566. rvkencname = make([]byte, len(rvkencname))
  567. copy(rvkencname, name2)
  568. var f interface{}
  569. d.decode(&f)
  570. if !mf.CodecMissingField(rvkencname, f) && d.h.ErrorIfNoField {
  571. d.errorf("no matching struct field found when decoding stream map with key: %s ",
  572. stringView(rvkencname))
  573. }
  574. } else {
  575. d.structFieldNotFound(-1, stringView(rvkencname))
  576. }
  577. // keepAlive4StringView(rvkencnameB) // not needed, as reference is outside loop
  578. }
  579. d.mapEnd()
  580. } else if ctyp == valueTypeArray {
  581. containerLen := d.arrayStart()
  582. if containerLen == 0 {
  583. d.arrayEnd()
  584. return
  585. }
  586. // Not much gain from doing it two ways for array.
  587. // Arrays are not used as much for structs.
  588. hasLen := containerLen >= 0
  589. var checkbreak bool
  590. for j, si := range f.ti.sfiSrc {
  591. if hasLen && j == containerLen {
  592. break
  593. }
  594. if !hasLen && d.d.CheckBreak() {
  595. checkbreak = true
  596. break
  597. }
  598. d.arrayElem()
  599. // if d.d.TryDecodeAsNil() {
  600. // si.setToZeroValue(rv)
  601. // } else {
  602. // d.decodeValue(sfn.field(si), nil)
  603. // }
  604. d.decodeValue(sfn.field(si), nil)
  605. }
  606. if (hasLen && containerLen > len(f.ti.sfiSrc)) || (!hasLen && !checkbreak) {
  607. // read remaining values and throw away
  608. for j := len(f.ti.sfiSrc); ; j++ {
  609. if (hasLen && j == containerLen) || (!hasLen && d.d.CheckBreak()) {
  610. break
  611. }
  612. d.arrayElem()
  613. d.structFieldNotFound(j, "")
  614. }
  615. }
  616. d.arrayEnd()
  617. } else {
  618. d.errorstr(errstrOnlyMapOrArrayCanDecodeIntoStruct)
  619. return
  620. }
  621. }
  622. func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) {
  623. // A slice can be set from a map or array in stream.
  624. // This way, the order can be kept (as order is lost with map).
  625. rtelem0 := f.ti.elem
  626. ctyp := d.d.ContainerType()
  627. if ctyp == valueTypeNil {
  628. // xdebug2f("rv: %v, type: %v, canset: %v", rv, rv.Type(), rv.CanSet())
  629. // rv.Set(reflect.New(f.ti.rt).Elem())
  630. if rv.CanSet() {
  631. rv.Set(f.ti.rv0)
  632. }
  633. return
  634. }
  635. if ctyp == valueTypeBytes || ctyp == valueTypeString {
  636. // you can only decode bytes or string in the stream into a slice or array of bytes
  637. if !(f.ti.rtid == uint8SliceTypId || rtelem0.Kind() == reflect.Uint8) {
  638. d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", f.ti.rt)
  639. }
  640. rvbs := rv.Bytes()
  641. bs2 := d.d.DecodeBytes(rvbs, false)
  642. // if rvbs == nil && bs2 != nil || rvbs != nil && bs2 == nil || len(bs2) != len(rvbs) {
  643. if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) {
  644. if rv.CanSet() {
  645. rv.SetBytes(bs2)
  646. } else if len(rvbs) > 0 && len(bs2) > 0 {
  647. copy(rvbs, bs2)
  648. }
  649. }
  650. return
  651. }
  652. // array := f.seq == seqTypeChan
  653. slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map)
  654. // // handle if nil
  655. // if slh.IsNil {
  656. // if rv.CanSet() && !rvisnil(rv) {
  657. // // rv.Set(reflect.New(f.ti.rt).Elem())
  658. // rv.Set(reflect.Zero(f.ti.rt))
  659. // }
  660. // return
  661. // }
  662. // an array can never return a nil slice. so no need to check f.array here.
  663. if containerLenS == 0 {
  664. if rv.CanSet() {
  665. if rvisnil(rv) {
  666. rv.Set(reflect.MakeSlice(f.ti.rt, 0, 0))
  667. } else {
  668. rvssetlen(rv, 0)
  669. }
  670. }
  671. slh.End()
  672. return
  673. }
  674. rtelem0Size := int(rtelem0.Size())
  675. rtElem0Kind := rtelem0.Kind()
  676. rtelem0Mut := !isImmutableKind(rtElem0Kind)
  677. rtelem := rtelem0
  678. rtelemkind := rtelem.Kind()
  679. for rtelemkind == reflect.Ptr {
  680. rtelem = rtelem.Elem()
  681. rtelemkind = rtelem.Kind()
  682. }
  683. var fn *codecFn
  684. var rvCanset = rv.CanSet()
  685. var rvChanged bool
  686. var rv0 = rv
  687. var rv9 reflect.Value
  688. rvlen := rv.Len()
  689. rvcap := rv.Cap()
  690. hasLen := containerLenS > 0
  691. if hasLen {
  692. if containerLenS > rvcap {
  693. oldRvlenGtZero := rvlen > 0
  694. rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(rtelem0.Size()))
  695. if rvlen <= rvcap {
  696. if rvCanset {
  697. rvssetlen(rv, rvlen)
  698. }
  699. } else if rvCanset {
  700. rv = reflect.MakeSlice(f.ti.rt, rvlen, rvlen)
  701. rvcap = rvlen
  702. rvChanged = true
  703. } else {
  704. d.errorf("cannot decode into non-settable slice")
  705. }
  706. if rvChanged && oldRvlenGtZero && rtelem0Mut { // !isImmutableKind(rtelem0.Kind()) {
  707. reflect.Copy(rv, rv0) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap)
  708. }
  709. } else if containerLenS != rvlen {
  710. rvlen = containerLenS
  711. if rvCanset {
  712. rvssetlen(rv, rvlen)
  713. }
  714. // else {
  715. // rv = rv.Slice(0, rvlen)
  716. // rvChanged = true
  717. // d.errorf("cannot decode into non-settable slice")
  718. // }
  719. }
  720. }
  721. // consider creating new element once, and just decoding into it.
  722. var rtelem0Zero reflect.Value
  723. var rtelem0ZeroValid bool
  724. var j int
  725. for ; (hasLen && j < containerLenS) || !(hasLen || d.d.CheckBreak()); j++ {
  726. if j == 0 && f.seq == seqTypeSlice && rvisnil(rv) {
  727. if hasLen {
  728. rvlen = decInferLen(containerLenS, d.h.MaxInitLen, rtelem0Size)
  729. } else {
  730. rvlen = decDefSliceCap
  731. }
  732. if rvCanset {
  733. rv = reflect.MakeSlice(f.ti.rt, rvlen, rvlen)
  734. rvChanged = true
  735. } else {
  736. d.errorf("cannot decode into non-settable slice")
  737. }
  738. }
  739. slh.ElemContainerState(j)
  740. // if indefinite, etc, then expand the slice if necessary
  741. if j >= rvlen {
  742. if f.seq == seqTypeArray {
  743. d.arrayCannotExpand(rvlen, j+1)
  744. // drain completely and return
  745. d.swallow()
  746. j++
  747. for ; (hasLen && j < containerLenS) || !(hasLen || d.d.CheckBreak()); j++ {
  748. slh.ElemContainerState(j)
  749. d.swallow()
  750. }
  751. slh.End()
  752. return
  753. }
  754. // rv = reflect.Append(rv, reflect.Zero(rtelem0)) // append logic + varargs
  755. var rvcap2 int
  756. var rvErrmsg2 string
  757. rv9, rvcap2, rvChanged, rvErrmsg2 =
  758. expandSliceRV(rv, f.ti.rt, rvCanset, rtelem0Size, 1, rvlen, rvcap)
  759. if rvErrmsg2 != "" {
  760. d.errorf(rvErrmsg2)
  761. }
  762. rvlen++
  763. if rvChanged {
  764. rv = rv9
  765. rvcap = rvcap2
  766. }
  767. }
  768. rv9 = rv.Index(j)
  769. if d.h.SliceElementReset {
  770. if !rtelem0ZeroValid {
  771. rtelem0ZeroValid = true
  772. rtelem0Zero = reflect.Zero(rtelem0)
  773. }
  774. rv9.Set(rtelem0Zero)
  775. }
  776. if fn == nil {
  777. fn = d.h.fn(rtelem)
  778. }
  779. d.decodeValue(rv9, fn)
  780. }
  781. if j < rvlen {
  782. if rv.CanSet() {
  783. rvssetlen(rv, j)
  784. } else if rvCanset {
  785. rv = rv.Slice(0, j)
  786. rvChanged = true
  787. } // else { d.errorf("kSlice: cannot change non-settable slice") }
  788. rvlen = j
  789. } else if j == 0 && rvisnil(rv) {
  790. if rvCanset {
  791. rv = reflect.MakeSlice(f.ti.rt, 0, 0)
  792. rvChanged = true
  793. } // else { d.errorf("kSlice: cannot change non-settable slice") }
  794. }
  795. slh.End()
  796. if rvChanged { // infers rvCanset=true, so it can be reset
  797. rv0.Set(rv)
  798. }
  799. }
  800. func (d *Decoder) kSliceForChan(f *codecFnInfo, rv reflect.Value) {
  801. // A slice can be set from a map or array in stream.
  802. // This way, the order can be kept (as order is lost with map).
  803. if f.ti.chandir&uint8(reflect.SendDir) == 0 {
  804. d.errorf("receive-only channel cannot be decoded")
  805. }
  806. rtelem0 := f.ti.elem
  807. ctyp := d.d.ContainerType()
  808. if ctyp == valueTypeNil {
  809. rv.Set(f.ti.rv0)
  810. return
  811. }
  812. if ctyp == valueTypeBytes || ctyp == valueTypeString {
  813. // you can only decode bytes or string in the stream into a slice or array of bytes
  814. if !(f.ti.rtid == uint8SliceTypId || rtelem0.Kind() == reflect.Uint8) {
  815. d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", f.ti.rt)
  816. }
  817. bs2 := d.d.DecodeBytes(nil, true)
  818. irv := rv2i(rv)
  819. ch, ok := irv.(chan<- byte)
  820. if !ok {
  821. ch = irv.(chan byte)
  822. }
  823. for _, b := range bs2 {
  824. ch <- b
  825. }
  826. return
  827. }
  828. // array := f.seq == seqTypeChan
  829. // only expects valueType(Array|Map - nil handled above)
  830. slh, containerLenS := d.decSliceHelperStart()
  831. // // handle if nil
  832. // if slh.IsNil {
  833. // if rv.CanSet() && !rvisnil(rv) {
  834. // rv.Set(reflect.Zero(f.ti.rt))
  835. // }
  836. // return
  837. // }
  838. // an array can never return a nil slice. so no need to check f.array here.
  839. if containerLenS == 0 {
  840. if rv.CanSet() && rvisnil(rv) {
  841. rv.Set(reflect.MakeChan(f.ti.rt, 0))
  842. }
  843. slh.End()
  844. return
  845. }
  846. rtelem0Size := int(rtelem0.Size())
  847. rtElem0Kind := rtelem0.Kind()
  848. rtelem0Mut := !isImmutableKind(rtElem0Kind)
  849. rtelem := rtelem0
  850. rtelemkind := rtelem.Kind()
  851. for rtelemkind == reflect.Ptr {
  852. rtelem = rtelem.Elem()
  853. rtelemkind = rtelem.Kind()
  854. }
  855. var fn *codecFn
  856. var rvCanset = rv.CanSet()
  857. var rvChanged bool
  858. var rv0 = rv
  859. var rv9 reflect.Value
  860. var rvlen int // := rv.Len()
  861. // rvcap := rv.Cap()
  862. hasLen := containerLenS > 0
  863. // consider creating new element once, and just decoding into it.
  864. // var rtelem0Zero reflect.Value
  865. // var rtelem0ZeroValid bool
  866. var j int
  867. for ; (hasLen && j < containerLenS) || !(hasLen || d.d.CheckBreak()); j++ {
  868. if j == 0 && rvisnil(rv) {
  869. if hasLen {
  870. rvlen = decInferLen(containerLenS, d.h.MaxInitLen, rtelem0Size)
  871. } else {
  872. rvlen = decDefChanCap
  873. }
  874. if rvCanset {
  875. rv = reflect.MakeChan(f.ti.rt, rvlen)
  876. rvChanged = true
  877. } else {
  878. d.errorf("cannot decode into non-settable chan")
  879. }
  880. }
  881. slh.ElemContainerState(j)
  882. // if d.d.TryDecodeAsNil() {
  883. // // rv.Send(reflect.Zero(rtelem0))
  884. // continue
  885. // }
  886. if rtelem0Mut || !rv9.IsValid() { // || (rtElem0Kind == reflect.Ptr && rvisnil(rv9)) {
  887. rv9 = rvzeroaddrk(rtelem0, rtElem0Kind)
  888. }
  889. if fn == nil {
  890. fn = d.h.fn(rtelem)
  891. }
  892. d.decodeValue(rv9, fn)
  893. rv.Send(rv9)
  894. }
  895. slh.End()
  896. if rvChanged { // infers rvCanset=true, so it can be reset
  897. rv0.Set(rv)
  898. }
  899. }
  900. // func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) {
  901. // // d.decodeValueFn(rv.Slice(0, rv.Len()))
  902. // f.kSlice(rv.Slice(0, rv.Len()))
  903. // }
  904. func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) {
  905. containerLen := d.mapStart()
  906. if containerLen == decContainerLenNil {
  907. rv.Set(f.ti.rv0)
  908. return
  909. }
  910. ti := f.ti
  911. if rvisnil(rv) {
  912. rvlen := decInferLen(containerLen, d.h.MaxInitLen, int(ti.key.Size()+ti.elem.Size()))
  913. rv.Set(makeMapReflect(ti.rt, rvlen))
  914. }
  915. if containerLen == 0 {
  916. d.mapEnd()
  917. return
  918. }
  919. ktype, vtype := ti.key, ti.elem
  920. ktypeId := rt2id(ktype)
  921. vtypeKind := vtype.Kind()
  922. ktypeKind := ktype.Kind()
  923. var vtypeElem reflect.Type
  924. var keyFn, valFn *codecFn
  925. var ktypeLo, vtypeLo reflect.Type
  926. for ktypeLo = ktype; ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() {
  927. }
  928. for vtypeLo = vtype; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() {
  929. }
  930. rvvMut := !isImmutableKind(vtypeKind)
  931. // we do a doMapGet if kind is mutable, and InterfaceReset=true if interface
  932. var doMapGet, doMapSet bool
  933. if !d.h.MapValueReset {
  934. if rvvMut {
  935. if vtypeKind == reflect.Interface {
  936. if !d.h.InterfaceReset {
  937. doMapGet = true
  938. }
  939. } else {
  940. doMapGet = true
  941. }
  942. }
  943. }
  944. var rvk, rvkn, rvv, rvvn, rvva reflect.Value
  945. var rvvaSet bool
  946. rvkMut := !isImmutableKind(ktype.Kind()) // if ktype is immutable, then re-use the same rvk.
  947. ktypeIsString := ktypeId == stringTypId
  948. ktypeIsIntf := ktypeId == intfTypId
  949. hasLen := containerLen > 0
  950. var kstrbs []byte
  951. for j := 0; (hasLen && j < containerLen) || !(hasLen || d.d.CheckBreak()); j++ {
  952. if j == 0 {
  953. // rvvz = reflect.Zero(vtype)
  954. // rvkz = reflect.Zero(ktype)
  955. if !rvkMut {
  956. rvkn = rvzeroaddrk(ktype, ktypeKind)
  957. }
  958. if !rvvMut {
  959. rvvn = rvzeroaddrk(vtype, vtypeKind)
  960. }
  961. }
  962. if rvkMut {
  963. rvk = rvzeroaddrk(ktype, ktypeKind)
  964. } else {
  965. rvk = rvkn
  966. }
  967. d.mapElemKey()
  968. if ktypeIsString {
  969. kstrbs = d.d.DecodeStringAsBytes()
  970. rvk.SetString(stringView(kstrbs)) // NOTE: if doing an insert, use real string (not stringview)
  971. } else {
  972. if keyFn == nil {
  973. keyFn = d.h.fn(ktypeLo)
  974. }
  975. d.decodeValue(rvk, keyFn)
  976. }
  977. // special case if interface wrapping a byte array.
  978. if ktypeIsIntf {
  979. if rvk2 := rvk.Elem(); rvk2.IsValid() && rvk2.Type() == uint8SliceTyp {
  980. rvk.Set(rv4i(d.string(rvk2.Bytes())))
  981. }
  982. // NOTE: consider failing early if map/slice/func
  983. }
  984. d.mapElemValue()
  985. // // Brittle, but OK per TryDecodeAsNil() contract.
  986. // // i.e. TryDecodeAsNil never shares slices with other decDriver procedures
  987. // if d.d.TryDecodeAsNil() {
  988. // if d.h.DeleteOnNilMapValue {
  989. // mapDelete(rv, rvk)
  990. // } else {
  991. // if ktypeIsString { // set to a real string (not string view)
  992. // rvk.SetString(d.string(kstrbs))
  993. // }
  994. // if !rvvz.IsValid() {
  995. // rvvz = reflect.Zero(vtype)
  996. // }
  997. // mapSet(rv, rvk, rvvz)
  998. // }
  999. // continue
  1000. // }
  1001. doMapSet = true // set to false if u do a get, and its a non-nil pointer
  1002. if doMapGet {
  1003. if !rvvaSet {
  1004. rvva = mapAddressableRV(vtype, vtypeKind)
  1005. rvvaSet = true
  1006. }
  1007. rvv = mapGet(rv, rvk, rvva) // reflect.Value{})
  1008. if vtypeKind == reflect.Ptr {
  1009. if rvv.IsValid() && !rvisnil(rvv) {
  1010. doMapSet = false
  1011. } else {
  1012. if vtypeElem == nil {
  1013. vtypeElem = vtype.Elem()
  1014. }
  1015. rvv = reflect.New(vtypeElem) // TODO: use rvzeroaddr?
  1016. }
  1017. } else if rvv.IsValid() && vtypeKind == reflect.Interface && !rvisnil(rvv) {
  1018. rvvn = rvzeroaddrk(vtype, vtypeKind)
  1019. rvvn.Set(rvv)
  1020. rvv = rvvn
  1021. } else if rvvMut {
  1022. rvv = rvzeroaddrk(vtype, vtypeKind)
  1023. } else {
  1024. rvv = rvvn
  1025. }
  1026. } else if rvvMut {
  1027. rvv = rvzeroaddrk(vtype, vtypeKind)
  1028. } else {
  1029. rvv = rvvn
  1030. }
  1031. if valFn == nil {
  1032. valFn = d.h.fn(vtypeLo)
  1033. }
  1034. // We MUST be done with the stringview of the key, BEFORE decoding the value (rvv)
  1035. // so that we don't unknowingly reuse the rvk backing buffer during rvv decode.
  1036. if doMapSet && ktypeIsString { // set to a real string (not string view)
  1037. rvk.SetString(d.string(kstrbs))
  1038. }
  1039. d.decodeValue(rvv, valFn)
  1040. if doMapSet {
  1041. mapSet(rv, rvk, rvv)
  1042. }
  1043. // if ktypeIsString {
  1044. // // keepAlive4StringView(kstrbs) // not needed, as reference is outside loop
  1045. // }
  1046. }
  1047. d.mapEnd()
  1048. }
  1049. // decNaked is used to keep track of the primitives decoded.
  1050. // Without it, we would have to decode each primitive and wrap it
  1051. // in an interface{}, causing an allocation.
  1052. // In this model, the primitives are decoded in a "pseudo-atomic" fashion,
  1053. // so we can rest assured that no other decoding happens while these
  1054. // primitives are being decoded.
  1055. //
  1056. // maps and arrays are not handled by this mechanism.
  1057. // However, RawExt is, and we accommodate for extensions that decode
  1058. // RawExt from DecodeNaked, but need to decode the value subsequently.
  1059. // kInterfaceNaked and swallow, which call DecodeNaked, handle this caveat.
  1060. //
  1061. // However, decNaked also keeps some arrays of default maps and slices
  1062. // used in DecodeNaked. This way, we can get a pointer to it
  1063. // without causing a new heap allocation.
  1064. //
  1065. // kInterfaceNaked will ensure that there is no allocation for the common
  1066. // uses.
  1067. type decNaked struct {
  1068. // r RawExt // used for RawExt, uint, []byte.
  1069. // primitives below
  1070. u uint64
  1071. i int64
  1072. f float64
  1073. l []byte
  1074. s string
  1075. // ---- cpu cache line boundary?
  1076. t time.Time
  1077. b bool
  1078. // state
  1079. v valueType
  1080. // _ [6]bool // padding
  1081. // ru, ri, rf, rl, rs, rb, rt reflect.Value // mapping to the primitives above
  1082. //
  1083. // _ [3]uint64 // padding
  1084. }
  1085. // func (n *decNaked) init() {
  1086. // n.ru = rv4i(&n.u).Elem()
  1087. // n.ri = rv4i(&n.i).Elem()
  1088. // n.rf = rv4i(&n.f).Elem()
  1089. // n.rl = rv4i(&n.l).Elem()
  1090. // n.rs = rv4i(&n.s).Elem()
  1091. // n.rt = rv4i(&n.t).Elem()
  1092. // n.rb = rv4i(&n.b).Elem()
  1093. // // n.rr[] = rv4i(&n.)
  1094. // }
  1095. // type decNakedPooler struct {
  1096. // n *decNaked
  1097. // nsp *sync.Pool
  1098. // }
  1099. // // naked must be called before each call to .DecodeNaked, as they will use it.
  1100. // func (d *decNakedPooler) naked() *decNaked {
  1101. // if d.n == nil {
  1102. // // consider one of:
  1103. // // - get from sync.Pool (if GC is frequent, there's no value here)
  1104. // // - new alloc (safest. only init'ed if it a naked decode will be done)
  1105. // // - field in Decoder (makes the Decoder struct very big)
  1106. // // To support using a decoder where a DecodeNaked is not needed,
  1107. // // we prefer #1 or #2.
  1108. // // d.n = new(decNaked) // &d.nv // new(decNaked) // grab from a sync.Pool
  1109. // // d.n.init()
  1110. // var v interface{}
  1111. // d.nsp, v = pool.decNaked()
  1112. // d.n = v.(*decNaked)
  1113. // }
  1114. // return d.n
  1115. // }
  1116. // func (d *decNakedPooler) end() {
  1117. // if d.n != nil {
  1118. // // if n != nil, then nsp != nil (they are always set together)
  1119. // d.nsp.Put(d.n)
  1120. // d.n, d.nsp = nil, nil
  1121. // }
  1122. // }
  1123. // type rtid2rv struct {
  1124. // rtid uintptr
  1125. // rv reflect.Value
  1126. // }
  1127. // Decoder reads and decodes an object from an input stream in a supported format.
  1128. //
  1129. // Decoder is NOT safe for concurrent use i.e. a Decoder cannot be used
  1130. // concurrently in multiple goroutines.
  1131. //
  1132. // However, as Decoder could be allocation heavy to initialize, a Reset method is provided
  1133. // so its state can be reused to decode new input streams repeatedly.
  1134. // This is the idiomatic way to use.
  1135. type Decoder struct {
  1136. panicHdl
  1137. // hopefully, reduce derefencing cost by laying the decReader inside the Decoder.
  1138. // Try to put things that go together to fit within a cache line (8 words).
  1139. d decDriver
  1140. // NOTE: Decoder shouldn't call its read methods,
  1141. // as the handler MAY need to do some coordination.
  1142. // r *decReaderSwitch
  1143. // bi *bufioDecReader
  1144. // cache the mapTypeId and sliceTypeId for faster comparisons
  1145. mtid uintptr
  1146. stid uintptr
  1147. jdec *jsonDecDriver
  1148. h *BasicHandle
  1149. hh Handle
  1150. // ---- cpu cache line boundary?
  1151. decReaderSwitch
  1152. // ---- cpu cache line boundary?
  1153. n decNaked
  1154. // cr containerStateRecv
  1155. // _ [4]uint8 // padding
  1156. is map[string]string // used for interning strings
  1157. err error
  1158. // ---- cpu cache line boundary?
  1159. // ---- writable fields during execution --- *try* to keep in sep cache line
  1160. maxdepth int16
  1161. depth int16
  1162. c containerState
  1163. _ [3]byte // padding
  1164. b [decScratchByteArrayLen]byte // scratch buffer, used by Decoder and xxxDecDrivers
  1165. // padding - false sharing help // modify 232 if Decoder struct changes.
  1166. // _ [cacheLineSize - 232%cacheLineSize]byte
  1167. }
  1168. // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
  1169. //
  1170. // For efficiency, Users are encouraged to configure ReaderBufferSize on the handle
  1171. // OR pass in a memory buffered reader (eg bufio.Reader, bytes.Buffer).
  1172. func NewDecoder(r io.Reader, h Handle) *Decoder {
  1173. d := newDecoder(h)
  1174. d.Reset(r)
  1175. return d
  1176. }
  1177. // NewDecoderBytes returns a Decoder which efficiently decodes directly
  1178. // from a byte slice with zero copying.
  1179. func NewDecoderBytes(in []byte, h Handle) *Decoder {
  1180. d := newDecoder(h)
  1181. d.ResetBytes(in)
  1182. return d
  1183. }
  1184. // var defaultDecNaked decNaked
  1185. func newDecoder(h Handle) *Decoder {
  1186. d := &Decoder{h: basicHandle(h), err: errDecoderNotInitialized}
  1187. d.bytes = true
  1188. if useFinalizers {
  1189. runtime.SetFinalizer(d, (*Decoder).finalize)
  1190. }
  1191. // d.r = &d.decReaderSwitch
  1192. d.hh = h
  1193. d.be = h.isBinary()
  1194. // NOTE: do not initialize d.n here. It is lazily initialized in d.naked()
  1195. var jh *JsonHandle
  1196. jh, d.js = h.(*JsonHandle)
  1197. d.jdec = nil
  1198. if d.js {
  1199. d.jsms = jh.MapKeyAsString
  1200. }
  1201. d.esep = d.hh.hasElemSeparators()
  1202. if d.h.InternString {
  1203. d.is = make(map[string]string, 32)
  1204. }
  1205. d.d = h.newDecDriver(d)
  1206. if d.js {
  1207. d.jdec = d.d.(*jsonDecDriver)
  1208. }
  1209. // d.cr, _ = d.d.(containerStateRecv)
  1210. return d
  1211. }
  1212. func (d *Decoder) r() *decReaderSwitch {
  1213. return &d.decReaderSwitch
  1214. }
  1215. func (d *Decoder) resetCommon() {
  1216. // d.r = &d.decReaderSwitch
  1217. d.d.reset()
  1218. d.err = nil
  1219. d.depth = 0
  1220. d.maxdepth = d.h.MaxDepth
  1221. if d.maxdepth <= 0 {
  1222. d.maxdepth = decDefMaxDepth
  1223. }
  1224. // reset all things which were cached from the Handle, but could change
  1225. d.mtid, d.stid = 0, 0
  1226. d.mtr, d.str = false, false
  1227. if d.h.MapType != nil {
  1228. d.mtid = rt2id(d.h.MapType)
  1229. d.mtr = fastpathAV.index(d.mtid) != -1
  1230. }
  1231. if d.h.SliceType != nil {
  1232. d.stid = rt2id(d.h.SliceType)
  1233. d.str = fastpathAV.index(d.stid) != -1
  1234. }
  1235. }
  1236. // Reset the Decoder with a new Reader to decode from,
  1237. // clearing all state from last run(s).
  1238. func (d *Decoder) Reset(r io.Reader) {
  1239. if r == nil {
  1240. return
  1241. }
  1242. d.bytes = false
  1243. // d.typ = entryTypeUnset
  1244. if d.h.ReaderBufferSize > 0 {
  1245. if d.bi == nil {
  1246. d.bi = new(bufioDecReader)
  1247. }
  1248. d.bi.reset(r, d.h.ReaderBufferSize)
  1249. // d.r = d.bi
  1250. // d.typ = entryTypeBufio
  1251. d.bufio = true
  1252. } else {
  1253. // d.ri.x = &d.b
  1254. // d.s = d.sa[:0]
  1255. if d.ri == nil {
  1256. d.ri = new(ioDecReader)
  1257. }
  1258. d.ri.reset(r)
  1259. // d.r = d.ri
  1260. // d.typ = entryTypeIo
  1261. d.bufio = false
  1262. }
  1263. d.resetCommon()
  1264. }
  1265. // ResetBytes resets the Decoder with a new []byte to decode from,
  1266. // clearing all state from last run(s).
  1267. func (d *Decoder) ResetBytes(in []byte) {
  1268. if in == nil {
  1269. return
  1270. }
  1271. d.bytes = true
  1272. d.bufio = false
  1273. // d.typ = entryTypeBytes
  1274. d.rb.reset(in)
  1275. // d.r = &d.rb
  1276. d.resetCommon()
  1277. }
  1278. func (d *Decoder) naked() *decNaked {
  1279. return &d.n
  1280. }
  1281. // Decode decodes the stream from reader and stores the result in the
  1282. // value pointed to by v. v cannot be a nil pointer. v can also be
  1283. // a reflect.Value of a pointer.
  1284. //
  1285. // Note that a pointer to a nil interface is not a nil pointer.
  1286. // If you do not know what type of stream it is, pass in a pointer to a nil interface.
  1287. // We will decode and store a value in that nil interface.
  1288. //
  1289. // Sample usages:
  1290. // // Decoding into a non-nil typed value
  1291. // var f float32
  1292. // err = codec.NewDecoder(r, handle).Decode(&f)
  1293. //
  1294. // // Decoding into nil interface
  1295. // var v interface{}
  1296. // dec := codec.NewDecoder(r, handle)
  1297. // err = dec.Decode(&v)
  1298. //
  1299. // When decoding into a nil interface{}, we will decode into an appropriate value based
  1300. // on the contents of the stream:
  1301. // - Numbers are decoded as float64, int64 or uint64.
  1302. // - Other values are decoded appropriately depending on the type:
  1303. // bool, string, []byte, time.Time, etc
  1304. // - Extensions are decoded as RawExt (if no ext function registered for the tag)
  1305. // Configurations exist on the Handle to override defaults
  1306. // (e.g. for MapType, SliceType and how to decode raw bytes).
  1307. //
  1308. // When decoding into a non-nil interface{} value, the mode of encoding is based on the
  1309. // type of the value. When a value is seen:
  1310. // - If an extension is registered for it, call that extension function
  1311. // - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error
  1312. // - Else decode it based on its reflect.Kind
  1313. //
  1314. // There are some special rules when decoding into containers (slice/array/map/struct).
  1315. // Decode will typically use the stream contents to UPDATE the container i.e. the values
  1316. // in these containers will not be zero'ed before decoding.
  1317. // - A map can be decoded from a stream map, by updating matching keys.
  1318. // - A slice can be decoded from a stream array,
  1319. // by updating the first n elements, where n is length of the stream.
  1320. // - A slice can be decoded from a stream map, by decoding as if
  1321. // it contains a sequence of key-value pairs.
  1322. // - A struct can be decoded from a stream map, by updating matching fields.
  1323. // - A struct can be decoded from a stream array,
  1324. // by updating fields as they occur in the struct (by index).
  1325. //
  1326. // This in-place update maintains consistency in the decoding philosophy (i.e. we ALWAYS update
  1327. // in place by default). However, the consequence of this is that values in slices or maps
  1328. // which are not zero'ed before hand, will have part of the prior values in place after decode
  1329. // if the stream doesn't contain an update for those parts.
  1330. //
  1331. // This in-place update can be disabled by configuring the MapValueReset and SliceElementReset
  1332. // decode options available on every handle.
  1333. //
  1334. // Furthermore, when decoding a stream map or array with length of 0 into a nil map or slice,
  1335. // we reset the destination map or slice to a zero-length value.
  1336. //
  1337. // However, when decoding a stream nil, we reset the destination container
  1338. // to its "zero" value (e.g. nil for slice/map, etc).
  1339. //
  1340. // Note: we allow nil values in the stream anywhere except for map keys.
  1341. // A nil value in the encoded stream where a map key is expected is treated as an error.
  1342. func (d *Decoder) Decode(v interface{}) (err error) {
  1343. // tried to use closure, as runtime optimizes defer with no params.
  1344. // This seemed to be causing weird issues (like circular reference found, unexpected panic, etc).
  1345. // Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139
  1346. // defer func() { d.deferred(&err) }()
  1347. // { x, y := d, &err; defer func() { x.deferred(y) }() }
  1348. if d.err != nil {
  1349. return d.err
  1350. }
  1351. if recoverPanicToErr {
  1352. defer func() {
  1353. if x := recover(); x != nil {
  1354. panicValToErr(d, x, &d.err)
  1355. if d.err != err {
  1356. err = d.err
  1357. }
  1358. }
  1359. }()
  1360. }
  1361. // defer d.deferred(&err)
  1362. d.mustDecode(v)
  1363. return
  1364. }
  1365. // MustDecode is like Decode, but panics if unable to Decode.
  1366. // This provides insight to the code location that triggered the error.
  1367. func (d *Decoder) MustDecode(v interface{}) {
  1368. // xdebugf("MustDecode: v: %#v", v)
  1369. // debug.PrintStack()
  1370. if d.err != nil {
  1371. panic(d.err)
  1372. }
  1373. d.mustDecode(v)
  1374. }
  1375. // MustDecode is like Decode, but panics if unable to Decode.
  1376. // This provides insight to the code location that triggered the error.
  1377. func (d *Decoder) mustDecode(v interface{}) {
  1378. // xdebug2f(".... mustDecode: v: %#v", v)
  1379. // TODO: Top-level: ensure that v is a pointer and not nil.
  1380. if d.bi == nil {
  1381. // if d.d.TryDecodeAsNil() {
  1382. // setZero(v)
  1383. // } else {
  1384. // d.decode(v)
  1385. // }
  1386. d.decode(v)
  1387. d.d.atEndOfDecode()
  1388. // release
  1389. if !d.h.ExplicitRelease {
  1390. if d.jdec != nil {
  1391. d.jdec.release()
  1392. }
  1393. }
  1394. return
  1395. }
  1396. // if d.d.TryDecodeAsNil() {
  1397. // setZero(v)
  1398. // } else {
  1399. // d.bi.calls++
  1400. // d.decode(v)
  1401. // d.bi.calls--
  1402. // }
  1403. d.bi.calls++
  1404. d.decode(v)
  1405. d.bi.calls--
  1406. if d.bi.calls == 0 {
  1407. d.d.atEndOfDecode()
  1408. // release
  1409. if !d.h.ExplicitRelease {
  1410. d.bi.release()
  1411. if d.jdec != nil {
  1412. d.jdec.release()
  1413. }
  1414. }
  1415. }
  1416. }
  1417. // func (d *Decoder) deferred(err1 *error) {
  1418. // if recoverPanicToErr {
  1419. // if x := recover(); x != nil {
  1420. // panicValToErr(d, x, err1)
  1421. // panicValToErr(d, x, &d.err)
  1422. // }
  1423. // }
  1424. // }
  1425. //go:noinline -- as it is run by finalizer
  1426. func (d *Decoder) finalize() {
  1427. d.Release()
  1428. }
  1429. // Release releases shared (pooled) resources.
  1430. //
  1431. // It is important to call Release() when done with a Decoder, so those resources
  1432. // are released instantly for use by subsequently created Decoders.
  1433. //
  1434. // By default, Release() is automatically called unless the option ExplicitRelease is set.
  1435. func (d *Decoder) Release() {
  1436. if d.bi != nil {
  1437. d.bi.release()
  1438. }
  1439. if d.jdec != nil {
  1440. d.jdec.release()
  1441. }
  1442. // d.decNakedPooler.end()
  1443. }
  1444. // // this is not a smart swallow, as it allocates objects and does unnecessary work.
  1445. // func (d *Decoder) swallowViaHammer() {
  1446. // var blank interface{}
  1447. // d.decodeValueNoFn(rv4i(&blank).Elem())
  1448. // }
  1449. func (d *Decoder) swallow() {
  1450. // smarter decode that just swallows the content
  1451. // if d.d.TryDecodeAsNil() {
  1452. // return
  1453. // }
  1454. switch d.d.ContainerType() {
  1455. case valueTypeNil:
  1456. case valueTypeMap:
  1457. containerLen := d.mapStart()
  1458. hasLen := containerLen >= 0
  1459. for j := 0; (hasLen && j < containerLen) || !(hasLen || d.d.CheckBreak()); j++ {
  1460. // if clenGtEqualZero {if j >= containerLen {break} } else if d.d.CheckBreak() {break}
  1461. d.mapElemKey()
  1462. d.swallow()
  1463. d.mapElemValue()
  1464. d.swallow()
  1465. }
  1466. d.mapEnd()
  1467. case valueTypeArray:
  1468. containerLen := d.arrayStart()
  1469. hasLen := containerLen >= 0
  1470. for j := 0; (hasLen && j < containerLen) || !(hasLen || d.d.CheckBreak()); j++ {
  1471. d.arrayElem()
  1472. d.swallow()
  1473. }
  1474. d.arrayEnd()
  1475. case valueTypeBytes:
  1476. d.d.DecodeBytes(d.b[:], true)
  1477. case valueTypeString:
  1478. d.d.DecodeStringAsBytes()
  1479. default:
  1480. // these are all primitives, which we can get from decodeNaked
  1481. // if RawExt using Value, complete the processing.
  1482. n := d.naked()
  1483. d.d.DecodeNaked()
  1484. if n.v == valueTypeExt && n.l == nil {
  1485. var v2 interface{}
  1486. d.decode(&v2)
  1487. }
  1488. }
  1489. }
  1490. func setZero(iv interface{}) {
  1491. if iv == nil {
  1492. return
  1493. }
  1494. if _, ok := isNil(iv); ok {
  1495. return
  1496. }
  1497. // var canDecode bool
  1498. switch v := iv.(type) {
  1499. case *string:
  1500. *v = ""
  1501. case *bool:
  1502. *v = false
  1503. case *int:
  1504. *v = 0
  1505. case *int8:
  1506. *v = 0
  1507. case *int16:
  1508. *v = 0
  1509. case *int32:
  1510. *v = 0
  1511. case *int64:
  1512. *v = 0
  1513. case *uint:
  1514. *v = 0
  1515. case *uint8:
  1516. *v = 0
  1517. case *uint16:
  1518. *v = 0
  1519. case *uint32:
  1520. *v = 0
  1521. case *uint64:
  1522. *v = 0
  1523. case *float32:
  1524. *v = 0
  1525. case *float64:
  1526. *v = 0
  1527. case *[]uint8:
  1528. *v = nil
  1529. case *Raw:
  1530. *v = nil
  1531. case *time.Time:
  1532. *v = time.Time{}
  1533. case reflect.Value:
  1534. setZeroRV(v)
  1535. default:
  1536. if !fastpathDecodeSetZeroTypeSwitch(iv) {
  1537. setZeroRV(rv4i(iv))
  1538. }
  1539. }
  1540. }
  1541. func setZeroRV(v reflect.Value) {
  1542. if isDecodeable(v) {
  1543. if v.Kind() == reflect.Ptr {
  1544. v = v.Elem()
  1545. }
  1546. if v.CanSet() {
  1547. v.Set(reflect.Zero(v.Type()))
  1548. }
  1549. } // TODO: else drain if chan, clear if map, set all to nil if slice???
  1550. }
  1551. func (d *Decoder) decode(iv interface{}) {
  1552. // a switch with only concrete types can be optimized.
  1553. // consequently, we deal with nil and interfaces outside the switch.
  1554. if iv == nil {
  1555. d.errorstr(errstrCannotDecodeIntoNil)
  1556. return
  1557. }
  1558. switch v := iv.(type) {
  1559. // case nil:
  1560. // case Selfer:
  1561. case reflect.Value:
  1562. d.ensureDecodeable(v)
  1563. d.decodeValue(v, nil)
  1564. case *string:
  1565. *v = string(d.d.DecodeStringAsBytes())
  1566. case *bool:
  1567. *v = d.d.DecodeBool()
  1568. case *int:
  1569. *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  1570. case *int8:
  1571. *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
  1572. case *int16:
  1573. *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
  1574. case *int32:
  1575. *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
  1576. case *int64:
  1577. *v = d.d.DecodeInt64()
  1578. case *uint:
  1579. *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  1580. case *uint8:
  1581. *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
  1582. case *uint16:
  1583. *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
  1584. case *uint32:
  1585. *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
  1586. case *uint64:
  1587. *v = d.d.DecodeUint64()
  1588. case *float32:
  1589. *v = float32(d.decodeFloat32())
  1590. case *float64:
  1591. *v = d.d.DecodeFloat64()
  1592. case *[]uint8:
  1593. *v = d.d.DecodeBytes(*v, false)
  1594. case []uint8:
  1595. b := d.d.DecodeBytes(v, false)
  1596. if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) {
  1597. copy(v, b)
  1598. }
  1599. case *time.Time:
  1600. *v = d.d.DecodeTime()
  1601. case *Raw:
  1602. *v = d.rawBytes()
  1603. case *interface{}:
  1604. d.decodeValue(rv4i(iv), nil)
  1605. // d.decodeValue(rv4i(iv).Elem(), nil)
  1606. // d.decodeValueNotNil(rv4i(iv).Elem())
  1607. default:
  1608. if v, ok := iv.(Selfer); ok {
  1609. v.CodecDecodeSelf(d)
  1610. } else if !fastpathDecodeTypeSwitch(iv, d) {
  1611. v := rv4i(iv)
  1612. d.ensureDecodeable(v)
  1613. d.decodeValue(v, nil) // TODO: find a way to say: no fast path??? Not necessary???
  1614. // d.decodeValueFallback(v)
  1615. }
  1616. }
  1617. }
  1618. // decodeValue MUST be called by the actual value we want to decode into,
  1619. // not its addr or a reference to it.
  1620. //
  1621. // This way, we know if it is itself a pointer, and can handle nil in
  1622. // the stream effectively.
  1623. func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) {
  1624. // If stream is not containing a nil value, then we can deref to the base
  1625. // non-pointer value, and decode into that.
  1626. var rvp reflect.Value
  1627. var rvpValid bool
  1628. if rv.Kind() == reflect.Ptr {
  1629. if d.d.TryNil() {
  1630. if rvelem := rv.Elem(); rvelem.CanSet() {
  1631. rvelem.Set(reflect.Zero(rvelem.Type()))
  1632. }
  1633. return
  1634. }
  1635. rvpValid = true
  1636. for rv.Kind() == reflect.Ptr {
  1637. if rvisnil(rv) {
  1638. rv.Set(reflect.New(rv.Type().Elem()))
  1639. }
  1640. rvp = rv
  1641. rv = rv.Elem()
  1642. }
  1643. }
  1644. if fn == nil {
  1645. fn = d.h.fn(rv.Type())
  1646. }
  1647. if fn.i.addrD {
  1648. if rvpValid {
  1649. fn.fd(d, &fn.i, rvp)
  1650. } else if rv.CanAddr() {
  1651. fn.fd(d, &fn.i, rv.Addr())
  1652. } else if !fn.i.addrF {
  1653. fn.fd(d, &fn.i, rv)
  1654. } else {
  1655. d.errorf("cannot decode into a non-pointer value")
  1656. }
  1657. } else {
  1658. fn.fd(d, &fn.i, rv)
  1659. }
  1660. // const check = true
  1661. // if check && rvpValid && scalarBitset.isset(byte(rv.Kind())) && d.d.Nil() {
  1662. // xdebug2f("setting %v (canset: %v) to %v, after updating %v to %v",
  1663. // rv0.Type(), rv0.CanSet(), reflect.Zero(rv0.Type()), rv.Type(), rv)
  1664. // rv0.Set(reflect.Zero(rv0.Type()))
  1665. // }
  1666. // return rv
  1667. }
  1668. func (d *Decoder) structFieldNotFound(index int, rvkencname string) {
  1669. // NOTE: rvkencname may be a stringView, so don't pass it to another function.
  1670. if d.h.ErrorIfNoField {
  1671. if index >= 0 {
  1672. d.errorf("no matching struct field found when decoding stream array at index %v", index)
  1673. return
  1674. } else if rvkencname != "" {
  1675. d.errorf("no matching struct field found when decoding stream map with key " + rvkencname)
  1676. return
  1677. }
  1678. }
  1679. d.swallow()
  1680. }
  1681. func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) {
  1682. if d.h.ErrorIfNoArrayExpand {
  1683. d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen)
  1684. }
  1685. }
  1686. func isDecodeable(rv reflect.Value) (canDecode bool) {
  1687. switch rv.Kind() {
  1688. case reflect.Array:
  1689. return rv.CanAddr()
  1690. case reflect.Ptr:
  1691. if !rvisnil(rv) {
  1692. return true
  1693. }
  1694. case reflect.Slice, reflect.Chan, reflect.Map:
  1695. if !rvisnil(rv) {
  1696. return true
  1697. }
  1698. }
  1699. return
  1700. }
  1701. func (d *Decoder) ensureDecodeable(rv reflect.Value) {
  1702. // decode can take any reflect.Value that is a inherently addressable i.e.
  1703. // - array
  1704. // - non-nil chan (we will SEND to it)
  1705. // - non-nil slice (we will set its elements)
  1706. // - non-nil map (we will put into it)
  1707. // - non-nil pointer (we can "update" it)
  1708. if isDecodeable(rv) {
  1709. return
  1710. }
  1711. if !rv.IsValid() {
  1712. d.errorstr(errstrCannotDecodeIntoNil)
  1713. return
  1714. }
  1715. if !rv.CanInterface() {
  1716. d.errorf("cannot decode into a value without an interface: %v", rv)
  1717. return
  1718. }
  1719. rvi := rv2i(rv)
  1720. rvk := rv.Kind()
  1721. d.errorf("cannot decode into value of kind: %v, type: %T, %#v", rvk, rvi, rvi)
  1722. }
  1723. func (d *Decoder) depthIncr() {
  1724. d.depth++
  1725. if d.depth >= d.maxdepth {
  1726. panic(errMaxDepthExceeded)
  1727. }
  1728. }
  1729. func (d *Decoder) depthDecr() {
  1730. d.depth--
  1731. }
  1732. // Possibly get an interned version of a string
  1733. //
  1734. // This should mostly be used for map keys, where the key type is string.
  1735. // This is because keys of a map/struct are typically reused across many objects.
  1736. func (d *Decoder) string(v []byte) (s string) {
  1737. if v == nil {
  1738. return
  1739. }
  1740. if d.is == nil {
  1741. return string(v) // don't return stringView, as we need a real string here.
  1742. }
  1743. s, ok := d.is[string(v)] // no allocation here, per go implementation
  1744. if !ok {
  1745. s = string(v) // new allocation here
  1746. d.is[s] = s
  1747. }
  1748. return
  1749. }
  1750. // nextValueBytes returns the next value in the stream as a set of bytes.
  1751. func (d *Decoder) nextValueBytes() (bs []byte) {
  1752. d.d.uncacheRead()
  1753. d.r().track()
  1754. d.swallow()
  1755. bs = d.r().stopTrack()
  1756. return
  1757. }
  1758. func (d *Decoder) rawBytes() []byte {
  1759. // ensure that this is not a view into the bytes
  1760. // i.e. make new copy always.
  1761. bs := d.nextValueBytes()
  1762. bs2 := make([]byte, len(bs))
  1763. copy(bs2, bs)
  1764. return bs2
  1765. }
  1766. func (d *Decoder) wrapErr(v interface{}, err *error) {
  1767. *err = decodeError{codecError: codecError{name: d.hh.Name(), err: v}, pos: int(d.r().numread())}
  1768. }
  1769. // NumBytesRead returns the number of bytes read
  1770. func (d *Decoder) NumBytesRead() int {
  1771. return int(d.r().numread())
  1772. }
  1773. // decodeFloat32 will delegate to an appropriate DecodeFloat32 implementation (if exists),
  1774. // else if will call DecodeFloat64 and ensure the value doesn't overflow.
  1775. //
  1776. // Note that we return float64 to reduce unnecessary conversions
  1777. func (d *Decoder) decodeFloat32() float32 {
  1778. if d.js {
  1779. return d.jdec.DecodeFloat32() // custom implementation for 32-bit
  1780. }
  1781. return float32(chkOvf.Float32V(d.d.DecodeFloat64()))
  1782. }
  1783. // ---- container tracking
  1784. // Note: We update the .c after calling the callback.
  1785. // This way, the callback can know what the last status was.
  1786. // Note: if you call mapStart and it returns decContainerLenNil,
  1787. // then do NOT call mapEnd.
  1788. func (d *Decoder) mapStart() (v int) {
  1789. v = d.d.ReadMapStart()
  1790. if v != decContainerLenNil {
  1791. d.depthIncr()
  1792. d.c = containerMapStart
  1793. }
  1794. return
  1795. }
  1796. func (d *Decoder) mapElemKey() {
  1797. if d.js {
  1798. d.jdec.ReadMapElemKey()
  1799. }
  1800. d.c = containerMapKey
  1801. }
  1802. func (d *Decoder) mapElemValue() {
  1803. if d.js {
  1804. d.jdec.ReadMapElemValue()
  1805. }
  1806. d.c = containerMapValue
  1807. }
  1808. func (d *Decoder) mapEnd() {
  1809. d.d.ReadMapEnd()
  1810. d.depthDecr()
  1811. d.c = containerMapEnd
  1812. d.c = 0
  1813. }
  1814. func (d *Decoder) arrayStart() (v int) {
  1815. v = d.d.ReadArrayStart()
  1816. d.depthIncr()
  1817. d.c = containerArrayStart
  1818. return
  1819. }
  1820. func (d *Decoder) arrayElem() {
  1821. if d.js {
  1822. d.jdec.ReadArrayElem()
  1823. }
  1824. d.c = containerArrayElem
  1825. }
  1826. func (d *Decoder) arrayEnd() {
  1827. d.d.ReadArrayEnd()
  1828. d.depthDecr()
  1829. d.c = containerArrayEnd
  1830. d.c = 0
  1831. }
  1832. func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext Ext) {
  1833. // var v interface{} = ext.ConvertExt(rv)
  1834. // d.d.decode(&v)
  1835. // ext.UpdateExt(rv, v)
  1836. // assume v is a pointer:
  1837. // - if struct|array, pass as is to ConvertExt
  1838. // - else make it non-addressable and pass to ConvertExt
  1839. // - make return value from ConvertExt addressable
  1840. // - decode into it
  1841. // - return the interface for passing into UpdateExt.
  1842. // - interface should be a pointer if struct|array, else a value
  1843. var s interface{}
  1844. rv := rv4i(v)
  1845. rv2 := rv.Elem()
  1846. rvk := rv2.Kind()
  1847. if rvk == reflect.Struct || rvk == reflect.Array {
  1848. s = ext.ConvertExt(v)
  1849. } else {
  1850. s = ext.ConvertExt(rv2i(rv2))
  1851. }
  1852. rv = rv4i(s)
  1853. if !rv.CanAddr() {
  1854. if rv.Kind() == reflect.Ptr {
  1855. rv2 = reflect.New(rv.Type().Elem())
  1856. rv2.Set(rv)
  1857. } else {
  1858. rv2 = rvzeroaddrk(rv.Type(), rv.Kind())
  1859. rv2.Set(rv)
  1860. }
  1861. rv = rv2
  1862. }
  1863. d.decodeValue(rv, nil)
  1864. ext.UpdateExt(v, rv2i(rv))
  1865. }
  1866. func (d *Decoder) sideDecode(v interface{}, bs []byte) {
  1867. rv := baseRV(v)
  1868. NewDecoderBytes(bs, d.hh).decodeValue(rv, d.h.fnNoExt(rv.Type()))
  1869. }
  1870. // --------------------------------------------------
  1871. // decSliceHelper assists when decoding into a slice, from a map or an array in the stream.
  1872. // A slice can be set from a map or array in stream. This supports the MapBySlice interface.
  1873. type decSliceHelper struct {
  1874. d *Decoder
  1875. ct valueType
  1876. Array bool
  1877. IsNil bool
  1878. }
  1879. func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) {
  1880. x.ct = d.d.ContainerType()
  1881. x.d = d
  1882. switch x.ct {
  1883. case valueTypeNil:
  1884. x.IsNil = true
  1885. case valueTypeArray:
  1886. x.Array = true
  1887. clen = d.arrayStart()
  1888. case valueTypeMap:
  1889. clen = d.mapStart() * 2
  1890. default:
  1891. d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct)
  1892. }
  1893. return
  1894. }
  1895. func (x decSliceHelper) End() {
  1896. if x.IsNil {
  1897. } else if x.Array {
  1898. x.d.arrayEnd()
  1899. } else {
  1900. x.d.mapEnd()
  1901. }
  1902. }
  1903. func (x decSliceHelper) ElemContainerState(index int) {
  1904. // Note: if isnil, clen=0, so we never call into ElemContainerState
  1905. if x.IsNil { //TODO: take out this check
  1906. } else if x.Array {
  1907. x.d.arrayElem()
  1908. } else if index%2 == 0 {
  1909. x.d.mapElemKey()
  1910. } else {
  1911. x.d.mapElemValue()
  1912. }
  1913. }
  1914. func decByteSlice(r *decReaderSwitch, clen, maxInitLen int, bs []byte) (bsOut []byte) {
  1915. if clen == 0 {
  1916. return zeroByteSlice
  1917. }
  1918. if len(bs) == clen {
  1919. bsOut = bs
  1920. r.readb(bsOut)
  1921. } else if cap(bs) >= clen {
  1922. bsOut = bs[:clen]
  1923. r.readb(bsOut)
  1924. } else {
  1925. // bsOut = make([]byte, clen)
  1926. len2 := decInferLen(clen, maxInitLen, 1)
  1927. bsOut = make([]byte, len2)
  1928. r.readb(bsOut)
  1929. for len2 < clen {
  1930. len3 := decInferLen(clen-len2, maxInitLen, 1)
  1931. bs3 := bsOut
  1932. bsOut = make([]byte, len2+len3)
  1933. copy(bsOut, bs3)
  1934. r.readb(bsOut[len2:])
  1935. len2 += len3
  1936. }
  1937. }
  1938. return
  1939. }
  1940. // func decByteSliceZeroCopy(r decReader, clen, maxInitLen int, bs []byte) (bsOut []byte) {
  1941. // if _, ok := r.(*bytesDecReader); ok && clen <= maxInitLen {
  1942. // return r.readx(clen)
  1943. // }
  1944. // return decByteSlice(r, clen, maxInitLen, bs)
  1945. // }
  1946. func detachZeroCopyBytes(isBytesReader bool, dest []byte, in []byte) (out []byte) {
  1947. if xlen := len(in); xlen > 0 {
  1948. if isBytesReader || xlen <= scratchByteArrayLen {
  1949. if cap(dest) >= xlen {
  1950. out = dest[:xlen]
  1951. } else {
  1952. out = make([]byte, xlen)
  1953. }
  1954. copy(out, in)
  1955. return
  1956. }
  1957. }
  1958. return in
  1959. }
  1960. // decInferLen will infer a sensible length, given the following:
  1961. // - clen: length wanted.
  1962. // - maxlen: max length to be returned.
  1963. // if <= 0, it is unset, and we infer it based on the unit size
  1964. // - unit: number of bytes for each element of the collection
  1965. func decInferLen(clen, maxlen, unit int) (rvlen int) {
  1966. const maxLenIfUnset = 8 // 64
  1967. // handle when maxlen is not set i.e. <= 0
  1968. // clen==0: use 0
  1969. // maxlen<=0, clen<0: use default
  1970. // maxlen> 0, clen<0: use default
  1971. // maxlen<=0, clen>0: infer maxlen, and cap on it
  1972. // maxlen> 0, clen>0: cap at maxlen
  1973. if clen == 0 {
  1974. return
  1975. }
  1976. if clen < 0 {
  1977. if clen == decContainerLenNil {
  1978. return 0
  1979. }
  1980. return maxLenIfUnset
  1981. }
  1982. if unit == 0 {
  1983. return clen
  1984. }
  1985. if maxlen <= 0 {
  1986. // no maxlen defined. Use maximum of 256K memory, with a floor of 4K items.
  1987. // maxlen = 256 * 1024 / unit
  1988. // if maxlen < (4 * 1024) {
  1989. // maxlen = 4 * 1024
  1990. // }
  1991. if unit < (256 / 4) {
  1992. maxlen = 256 * 1024 / unit
  1993. } else {
  1994. maxlen = 4 * 1024
  1995. }
  1996. // if maxlen > maxLenIfUnset {
  1997. // maxlen = maxLenIfUnset
  1998. // }
  1999. }
  2000. if clen > maxlen {
  2001. rvlen = maxlen
  2002. } else {
  2003. rvlen = clen
  2004. }
  2005. return
  2006. }
  2007. func expandSliceRV(s reflect.Value, st reflect.Type, canChange bool, stElemSize, num, slen, scap int) (
  2008. s2 reflect.Value, scap2 int, changed bool, err string) {
  2009. l1 := slen + num // new slice length
  2010. if l1 < slen {
  2011. err = errmsgExpandSliceOverflow
  2012. return
  2013. }
  2014. if l1 <= scap {
  2015. if s.CanSet() {
  2016. rvssetlen(s, l1)
  2017. } else if canChange {
  2018. s2 = s.Slice(0, l1)
  2019. scap2 = scap
  2020. changed = true
  2021. } else {
  2022. err = errmsgExpandSliceCannotChange
  2023. return
  2024. }
  2025. return
  2026. }
  2027. if !canChange {
  2028. err = errmsgExpandSliceCannotChange
  2029. return
  2030. }
  2031. scap2 = growCap(scap, stElemSize, num)
  2032. s2 = reflect.MakeSlice(st, l1, scap2)
  2033. changed = true
  2034. reflect.Copy(s2, s)
  2035. return
  2036. }
  2037. func decReadFull(r io.Reader, bs []byte) (n uint, err error) {
  2038. var nn int
  2039. for n < uint(len(bs)) && err == nil {
  2040. nn, err = r.Read(bs[n:])
  2041. if nn > 0 {
  2042. if err == io.EOF {
  2043. // leave EOF for next time
  2044. err = nil
  2045. }
  2046. n += uint(nn)
  2047. }
  2048. }
  2049. // do not do this - it serves no purpose
  2050. // if n != len(bs) && err == io.EOF { err = io.ErrUnexpectedEOF }
  2051. return
  2052. }
  2053. func decNakedReadRawBytes(dr decDriver, d *Decoder, n *decNaked, rawToString bool) {
  2054. if rawToString {
  2055. n.v = valueTypeString
  2056. n.s = string(dr.DecodeBytes(d.b[:], true))
  2057. } else {
  2058. n.v = valueTypeBytes
  2059. n.l = dr.DecodeBytes(nil, false)
  2060. }
  2061. }