helper.go 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  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. // Contains code shared by both encode and decode.
  5. // Some shared ideas around encoding/decoding
  6. // ------------------------------------------
  7. //
  8. // If an interface{} is passed, we first do a type assertion to see if it is
  9. // a primitive type or a map/slice of primitive types, and use a fastpath to handle it.
  10. //
  11. // If we start with a reflect.Value, we are already in reflect.Value land and
  12. // will try to grab the function for the underlying Type and directly call that function.
  13. // This is more performant than calling reflect.Value.Interface().
  14. //
  15. // This still helps us bypass many layers of reflection, and give best performance.
  16. //
  17. // Containers
  18. // ------------
  19. // Containers in the stream are either associative arrays (key-value pairs) or
  20. // regular arrays (indexed by incrementing integers).
  21. //
  22. // Some streams support indefinite-length containers, and use a breaking
  23. // byte-sequence to denote that the container has come to an end.
  24. //
  25. // Some streams also are text-based, and use explicit separators to denote the
  26. // end/beginning of different values.
  27. //
  28. // During encode, we use a high-level condition to determine how to iterate through
  29. // the container. That decision is based on whether the container is text-based (with
  30. // separators) or binary (without separators). If binary, we do not even call the
  31. // encoding of separators.
  32. //
  33. // During decode, we use a different high-level condition to determine how to iterate
  34. // through the containers. That decision is based on whether the stream contained
  35. // a length prefix, or if it used explicit breaks. If length-prefixed, we assume that
  36. // it has to be binary, and we do not even try to read separators.
  37. //
  38. // Philosophy
  39. // ------------
  40. // On decode, this codec will update containers appropriately:
  41. // - If struct, update fields from stream into fields of struct.
  42. // If field in stream not found in struct, handle appropriately (based on option).
  43. // If a struct field has no corresponding value in the stream, leave it AS IS.
  44. // If nil in stream, set value to nil/zero value.
  45. // - If map, update map from stream.
  46. // If the stream value is NIL, set the map to nil.
  47. // - if slice, try to update up to length of array in stream.
  48. // if container len is less than stream array length,
  49. // and container cannot be expanded, handled (based on option).
  50. // This means you can decode 4-element stream array into 1-element array.
  51. //
  52. // ------------------------------------
  53. // On encode, user can specify omitEmpty. This means that the value will be omitted
  54. // if the zero value. The problem may occur during decode, where omitted values do not affect
  55. // the value being decoded into. This means that if decoding into a struct with an
  56. // int field with current value=5, and the field is omitted in the stream, then after
  57. // decoding, the value will still be 5 (not 0).
  58. // omitEmpty only works if you guarantee that you always decode into zero-values.
  59. //
  60. // ------------------------------------
  61. // We could have truncated a map to remove keys not available in the stream,
  62. // or set values in the struct which are not in the stream to their zero values.
  63. // We decided against it because there is no efficient way to do it.
  64. // We may introduce it as an option later.
  65. // However, that will require enabling it for both runtime and code generation modes.
  66. //
  67. // To support truncate, we need to do 2 passes over the container:
  68. // map
  69. // - first collect all keys (e.g. in k1)
  70. // - for each key in stream, mark k1 that the key should not be removed
  71. // - after updating map, do second pass and call delete for all keys in k1 which are not marked
  72. // struct:
  73. // - for each field, track the *typeInfo s1
  74. // - iterate through all s1, and for each one not marked, set value to zero
  75. // - this involves checking the possible anonymous fields which are nil ptrs.
  76. // too much work.
  77. //
  78. // ------------------------------------------
  79. // Error Handling is done within the library using panic.
  80. //
  81. // This way, the code doesn't have to keep checking if an error has happened,
  82. // and we don't have to keep sending the error value along with each call
  83. // or storing it in the En|Decoder and checking it constantly along the way.
  84. //
  85. // The disadvantage is that small functions which use panics cannot be inlined.
  86. // The code accounts for that by only using panics behind an interface;
  87. // since interface calls cannot be inlined, this is irrelevant.
  88. //
  89. // We considered storing the error is En|Decoder.
  90. // - once it has its err field set, it cannot be used again.
  91. // - panicing will be optional, controlled by const flag.
  92. // - code should always check error first and return early.
  93. // We eventually decided against it as it makes the code clumsier to always
  94. // check for these error conditions.
  95. import (
  96. "bytes"
  97. "encoding"
  98. "encoding/binary"
  99. "errors"
  100. "fmt"
  101. "io"
  102. "math"
  103. "reflect"
  104. "sort"
  105. "strconv"
  106. "strings"
  107. "sync"
  108. "time"
  109. )
  110. const (
  111. scratchByteArrayLen = 32
  112. // initCollectionCap = 16 // 32 is defensive. 16 is preferred.
  113. // Support encoding.(Binary|Text)(Unm|M)arshaler.
  114. // This constant flag will enable or disable it.
  115. supportMarshalInterfaces = true
  116. // for debugging, set this to false, to catch panic traces.
  117. // Note that this will always cause rpc tests to fail, since they need io.EOF sent via panic.
  118. recoverPanicToErr = true
  119. // arrayCacheLen is the length of the cache used in encoder or decoder for
  120. // allowing zero-alloc initialization.
  121. arrayCacheLen = 8
  122. // size of the cacheline: defaulting to value for archs: amd64, arm64, 386
  123. // should use "runtime/internal/sys".CacheLineSize, but that is not exposed.
  124. cacheLineSize = 64
  125. wordSizeBits = strconv.IntSize
  126. wordSize = wordSizeBits / 8
  127. maxLevelsEmbedding = 15 // use this, so structFieldInfo fits into 8 bytes
  128. )
  129. var (
  130. oneByteArr = [1]byte{0}
  131. zeroByteSlice = oneByteArr[:0:0]
  132. )
  133. var refBitset bitset32
  134. var pool pooler
  135. var panicv panicHdl
  136. func init() {
  137. pool.init()
  138. refBitset.set(byte(reflect.Map))
  139. refBitset.set(byte(reflect.Ptr))
  140. refBitset.set(byte(reflect.Func))
  141. refBitset.set(byte(reflect.Chan))
  142. }
  143. type charEncoding uint8
  144. const (
  145. cRAW charEncoding = iota
  146. cUTF8
  147. cUTF16LE
  148. cUTF16BE
  149. cUTF32LE
  150. cUTF32BE
  151. )
  152. // valueType is the stream type
  153. type valueType uint8
  154. const (
  155. valueTypeUnset valueType = iota
  156. valueTypeNil
  157. valueTypeInt
  158. valueTypeUint
  159. valueTypeFloat
  160. valueTypeBool
  161. valueTypeString
  162. valueTypeSymbol
  163. valueTypeBytes
  164. valueTypeMap
  165. valueTypeArray
  166. valueTypeTime
  167. valueTypeExt
  168. // valueTypeInvalid = 0xff
  169. )
  170. var valueTypeStrings = [...]string{
  171. "Unset",
  172. "Nil",
  173. "Int",
  174. "Uint",
  175. "Float",
  176. "Bool",
  177. "String",
  178. "Symbol",
  179. "Bytes",
  180. "Map",
  181. "Array",
  182. "Timestamp",
  183. "Ext",
  184. }
  185. func (x valueType) String() string {
  186. if int(x) < len(valueTypeStrings) {
  187. return valueTypeStrings[x]
  188. }
  189. return strconv.FormatInt(int64(x), 10)
  190. }
  191. type seqType uint8
  192. const (
  193. _ seqType = iota
  194. seqTypeArray
  195. seqTypeSlice
  196. seqTypeChan
  197. )
  198. // note that containerMapStart and containerArraySend are not sent.
  199. // This is because the ReadXXXStart and EncodeXXXStart already does these.
  200. type containerState uint8
  201. const (
  202. _ containerState = iota
  203. containerMapStart // slot left open, since Driver method already covers it
  204. containerMapKey
  205. containerMapValue
  206. containerMapEnd
  207. containerArrayStart // slot left open, since Driver methods already cover it
  208. containerArrayElem
  209. containerArrayEnd
  210. )
  211. // // sfiIdx used for tracking where a (field/enc)Name is seen in a []*structFieldInfo
  212. // type sfiIdx struct {
  213. // name string
  214. // index int
  215. // }
  216. // do not recurse if a containing type refers to an embedded type
  217. // which refers back to its containing type (via a pointer).
  218. // The second time this back-reference happens, break out,
  219. // so as not to cause an infinite loop.
  220. const rgetMaxRecursion = 2
  221. // Anecdotally, we believe most types have <= 12 fields.
  222. // - even Java's PMD rules set TooManyFields threshold to 15.
  223. // However, go has embedded fields, which should be regarded as
  224. // top level, allowing structs to possibly double or triple.
  225. // In addition, we don't want to keep creating transient arrays,
  226. // especially for the sfi index tracking, and the evtypes tracking.
  227. //
  228. // So - try to keep typeInfoLoadArray within 2K bytes
  229. const (
  230. typeInfoLoadArraySfisLen = 16
  231. typeInfoLoadArraySfiidxLen = 8 * 112
  232. typeInfoLoadArrayEtypesLen = 12
  233. typeInfoLoadArrayBLen = 8 * 4
  234. )
  235. type typeInfoLoad struct {
  236. // fNames []string
  237. // encNames []string
  238. etypes []uintptr
  239. sfis []structFieldInfo
  240. }
  241. type typeInfoLoadArray struct {
  242. // fNames [typeInfoLoadArrayLen]string
  243. // encNames [typeInfoLoadArrayLen]string
  244. sfis [typeInfoLoadArraySfisLen]structFieldInfo
  245. sfiidx [typeInfoLoadArraySfiidxLen]byte
  246. etypes [typeInfoLoadArrayEtypesLen]uintptr
  247. b [typeInfoLoadArrayBLen]byte // scratch - used for struct field names
  248. }
  249. // mirror json.Marshaler and json.Unmarshaler here,
  250. // so we don't import the encoding/json package
  251. type jsonMarshaler interface {
  252. MarshalJSON() ([]byte, error)
  253. }
  254. type jsonUnmarshaler interface {
  255. UnmarshalJSON([]byte) error
  256. }
  257. type isZeroer interface {
  258. IsZero() bool
  259. }
  260. // type byteAccepter func(byte) bool
  261. var (
  262. bigen = binary.BigEndian
  263. structInfoFieldName = "_struct"
  264. mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
  265. mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil))
  266. intfSliceTyp = reflect.TypeOf([]interface{}(nil))
  267. intfTyp = intfSliceTyp.Elem()
  268. reflectValTyp = reflect.TypeOf((*reflect.Value)(nil)).Elem()
  269. stringTyp = reflect.TypeOf("")
  270. timeTyp = reflect.TypeOf(time.Time{})
  271. rawExtTyp = reflect.TypeOf(RawExt{})
  272. rawTyp = reflect.TypeOf(Raw{})
  273. uintptrTyp = reflect.TypeOf(uintptr(0))
  274. uint8Typ = reflect.TypeOf(uint8(0))
  275. uint8SliceTyp = reflect.TypeOf([]uint8(nil))
  276. uintTyp = reflect.TypeOf(uint(0))
  277. intTyp = reflect.TypeOf(int(0))
  278. mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem()
  279. binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
  280. binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
  281. textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
  282. textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
  283. jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem()
  284. jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem()
  285. selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem()
  286. iszeroTyp = reflect.TypeOf((*isZeroer)(nil)).Elem()
  287. uint8TypId = rt2id(uint8Typ)
  288. uint8SliceTypId = rt2id(uint8SliceTyp)
  289. rawExtTypId = rt2id(rawExtTyp)
  290. rawTypId = rt2id(rawTyp)
  291. intfTypId = rt2id(intfTyp)
  292. timeTypId = rt2id(timeTyp)
  293. stringTypId = rt2id(stringTyp)
  294. mapStrIntfTypId = rt2id(mapStrIntfTyp)
  295. mapIntfIntfTypId = rt2id(mapIntfIntfTyp)
  296. intfSliceTypId = rt2id(intfSliceTyp)
  297. // mapBySliceTypId = rt2id(mapBySliceTyp)
  298. intBitsize = uint8(intTyp.Bits())
  299. uintBitsize = uint8(uintTyp.Bits())
  300. bsAll0x00 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
  301. bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  302. chkOvf checkOverflow
  303. errNoFieldNameToStructFieldInfo = errors.New("no field name passed to parseStructFieldInfo")
  304. )
  305. var defTypeInfos = NewTypeInfos([]string{"codec", "json"})
  306. var immutableKindsSet = [32]bool{
  307. // reflect.Invalid: ,
  308. reflect.Bool: true,
  309. reflect.Int: true,
  310. reflect.Int8: true,
  311. reflect.Int16: true,
  312. reflect.Int32: true,
  313. reflect.Int64: true,
  314. reflect.Uint: true,
  315. reflect.Uint8: true,
  316. reflect.Uint16: true,
  317. reflect.Uint32: true,
  318. reflect.Uint64: true,
  319. reflect.Uintptr: true,
  320. reflect.Float32: true,
  321. reflect.Float64: true,
  322. reflect.Complex64: true,
  323. reflect.Complex128: true,
  324. // reflect.Array
  325. // reflect.Chan
  326. // reflect.Func: true,
  327. // reflect.Interface
  328. // reflect.Map
  329. // reflect.Ptr
  330. // reflect.Slice
  331. reflect.String: true,
  332. // reflect.Struct
  333. // reflect.UnsafePointer
  334. }
  335. // Selfer defines methods by which a value can encode or decode itself.
  336. //
  337. // Any type which implements Selfer will be able to encode or decode itself.
  338. // Consequently, during (en|de)code, this takes precedence over
  339. // (text|binary)(M|Unm)arshal or extension support.
  340. type Selfer interface {
  341. CodecEncodeSelf(*Encoder)
  342. CodecDecodeSelf(*Decoder)
  343. }
  344. // MapBySlice is a tag interface that denotes wrapped slice should encode as a map in the stream.
  345. // The slice contains a sequence of key-value pairs.
  346. // This affords storing a map in a specific sequence in the stream.
  347. //
  348. // Example usage:
  349. // type T1 []string // or []int or []Point or any other "slice" type
  350. // func (_ T1) MapBySlice{} // T1 now implements MapBySlice, and will be encoded as a map
  351. // type T2 struct { KeyValues T1 }
  352. //
  353. // var kvs = []string{"one", "1", "two", "2", "three", "3"}
  354. // var v2 = T2{ KeyValues: T1(kvs) }
  355. // // v2 will be encoded like the map: {"KeyValues": {"one": "1", "two": "2", "three": "3"} }
  356. //
  357. // The support of MapBySlice affords the following:
  358. // - A slice type which implements MapBySlice will be encoded as a map
  359. // - A slice can be decoded from a map in the stream
  360. // - It MUST be a slice type (not a pointer receiver) that implements MapBySlice
  361. type MapBySlice interface {
  362. MapBySlice()
  363. }
  364. // BasicHandle encapsulates the common options and extension functions.
  365. //
  366. // Deprecated: DO NOT USE DIRECTLY. EXPORTED FOR GODOC BENEFIT. WILL BE REMOVED.
  367. type BasicHandle struct {
  368. // BasicHandle is always a part of a different type.
  369. // It doesn't have to fit into it own cache lines.
  370. // TypeInfos is used to get the type info for any type.
  371. //
  372. // If not configured, the default TypeInfos is used, which uses struct tag keys: codec, json
  373. TypeInfos *TypeInfos
  374. // Note: BasicHandle is not comparable, due to these slices here (extHandle, intf2impls).
  375. // If *[]T is used instead, this becomes comparable, at the cost of extra indirection.
  376. // Thses slices are used all the time, so keep as slices (not pointers).
  377. extHandle
  378. intf2impls
  379. RPCOptions
  380. // ---- cache line
  381. DecodeOptions
  382. // ---- cache line
  383. EncodeOptions
  384. // noBuiltInTypeChecker
  385. }
  386. func (x *BasicHandle) getBasicHandle() *BasicHandle {
  387. return x
  388. }
  389. func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
  390. if x.TypeInfos == nil {
  391. return defTypeInfos.get(rtid, rt)
  392. }
  393. return x.TypeInfos.get(rtid, rt)
  394. }
  395. // Handle is the interface for a specific encoding format.
  396. //
  397. // Typically, a Handle is pre-configured before first time use,
  398. // and not modified while in use. Such a pre-configured Handle
  399. // is safe for concurrent access.
  400. type Handle interface {
  401. Name() string
  402. getBasicHandle() *BasicHandle
  403. recreateEncDriver(encDriver) bool
  404. newEncDriver(w *Encoder) encDriver
  405. newDecDriver(r *Decoder) decDriver
  406. isBinary() bool
  407. hasElemSeparators() bool
  408. // IsBuiltinType(rtid uintptr) bool
  409. }
  410. // Raw represents raw formatted bytes.
  411. // We "blindly" store it during encode and retrieve the raw bytes during decode.
  412. // Note: it is dangerous during encode, so we may gate the behaviour
  413. // behind an Encode flag which must be explicitly set.
  414. type Raw []byte
  415. // RawExt represents raw unprocessed extension data.
  416. // Some codecs will decode extension data as a *RawExt
  417. // if there is no registered extension for the tag.
  418. //
  419. // Only one of Data or Value is nil.
  420. // If Data is nil, then the content of the RawExt is in the Value.
  421. type RawExt struct {
  422. Tag uint64
  423. // Data is the []byte which represents the raw ext. If nil, ext is exposed in Value.
  424. // Data is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of types
  425. Data []byte
  426. // Value represents the extension, if Data is nil.
  427. // Value is used by codecs (e.g. cbor, json) which leverage the format to do
  428. // custom serialization of the types.
  429. Value interface{}
  430. }
  431. // BytesExt handles custom (de)serialization of types to/from []byte.
  432. // It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
  433. type BytesExt interface {
  434. // WriteExt converts a value to a []byte.
  435. //
  436. // Note: v is a pointer iff the registered extension type is a struct or array kind.
  437. WriteExt(v interface{}) []byte
  438. // ReadExt updates a value from a []byte.
  439. //
  440. // Note: dst is always a pointer kind to the registered extension type.
  441. ReadExt(dst interface{}, src []byte)
  442. }
  443. // InterfaceExt handles custom (de)serialization of types to/from another interface{} value.
  444. // The Encoder or Decoder will then handle the further (de)serialization of that known type.
  445. //
  446. // It is used by codecs (e.g. cbor, json) which use the format to do custom serialization of types.
  447. type InterfaceExt interface {
  448. // ConvertExt converts a value into a simpler interface for easy encoding
  449. // e.g. convert time.Time to int64.
  450. //
  451. // Note: v is a pointer iff the registered extension type is a struct or array kind.
  452. ConvertExt(v interface{}) interface{}
  453. // UpdateExt updates a value from a simpler interface for easy decoding
  454. // e.g. convert int64 to time.Time.
  455. //
  456. // Note: dst is always a pointer kind to the registered extension type.
  457. UpdateExt(dst interface{}, src interface{})
  458. }
  459. // Ext handles custom (de)serialization of custom types / extensions.
  460. type Ext interface {
  461. BytesExt
  462. InterfaceExt
  463. }
  464. // addExtWrapper is a wrapper implementation to support former AddExt exported method.
  465. type addExtWrapper struct {
  466. encFn func(reflect.Value) ([]byte, error)
  467. decFn func(reflect.Value, []byte) error
  468. }
  469. func (x addExtWrapper) WriteExt(v interface{}) []byte {
  470. bs, err := x.encFn(reflect.ValueOf(v))
  471. if err != nil {
  472. panic(err)
  473. }
  474. return bs
  475. }
  476. func (x addExtWrapper) ReadExt(v interface{}, bs []byte) {
  477. if err := x.decFn(reflect.ValueOf(v), bs); err != nil {
  478. panic(err)
  479. }
  480. }
  481. func (x addExtWrapper) ConvertExt(v interface{}) interface{} {
  482. return x.WriteExt(v)
  483. }
  484. func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) {
  485. x.ReadExt(dest, v.([]byte))
  486. }
  487. type extWrapper struct {
  488. BytesExt
  489. InterfaceExt
  490. }
  491. type bytesExtFailer struct{}
  492. func (bytesExtFailer) WriteExt(v interface{}) []byte {
  493. panicv.errorstr("BytesExt.WriteExt is not supported")
  494. return nil
  495. }
  496. func (bytesExtFailer) ReadExt(v interface{}, bs []byte) {
  497. panicv.errorstr("BytesExt.ReadExt is not supported")
  498. }
  499. type interfaceExtFailer struct{}
  500. func (interfaceExtFailer) ConvertExt(v interface{}) interface{} {
  501. panicv.errorstr("InterfaceExt.ConvertExt is not supported")
  502. return nil
  503. }
  504. func (interfaceExtFailer) UpdateExt(dest interface{}, v interface{}) {
  505. panicv.errorstr("InterfaceExt.UpdateExt is not supported")
  506. }
  507. type binaryEncodingType struct{}
  508. func (binaryEncodingType) isBinary() bool { return true }
  509. type textEncodingType struct{}
  510. func (textEncodingType) isBinary() bool { return false }
  511. // noBuiltInTypes is embedded into many types which do not support builtins
  512. // e.g. msgpack, simple, cbor.
  513. // type noBuiltInTypeChecker struct{}
  514. // func (noBuiltInTypeChecker) IsBuiltinType(rt uintptr) bool { return false }
  515. // type noBuiltInTypes struct{ noBuiltInTypeChecker }
  516. type noBuiltInTypes struct{}
  517. func (noBuiltInTypes) EncodeBuiltin(rt uintptr, v interface{}) {}
  518. func (noBuiltInTypes) DecodeBuiltin(rt uintptr, v interface{}) {}
  519. // type noStreamingCodec struct{}
  520. // func (noStreamingCodec) CheckBreak() bool { return false }
  521. // func (noStreamingCodec) hasElemSeparators() bool { return false }
  522. type noElemSeparators struct{}
  523. func (noElemSeparators) hasElemSeparators() (v bool) { return }
  524. func (noElemSeparators) recreateEncDriver(e encDriver) (v bool) { return }
  525. // bigenHelper.
  526. // Users must already slice the x completely, because we will not reslice.
  527. type bigenHelper struct {
  528. x []byte // must be correctly sliced to appropriate len. slicing is a cost.
  529. w encWriter
  530. }
  531. func (z bigenHelper) writeUint16(v uint16) {
  532. bigen.PutUint16(z.x, v)
  533. z.w.writeb(z.x)
  534. }
  535. func (z bigenHelper) writeUint32(v uint32) {
  536. bigen.PutUint32(z.x, v)
  537. z.w.writeb(z.x)
  538. }
  539. func (z bigenHelper) writeUint64(v uint64) {
  540. bigen.PutUint64(z.x, v)
  541. z.w.writeb(z.x)
  542. }
  543. type extTypeTagFn struct {
  544. rtid uintptr
  545. rtidptr uintptr
  546. rt reflect.Type
  547. tag uint64
  548. ext Ext
  549. _ [1]uint64 // padding
  550. }
  551. type extHandle []extTypeTagFn
  552. // AddExt registes an encode and decode function for a reflect.Type.
  553. // To deregister an Ext, call AddExt with nil encfn and/or nil decfn.
  554. //
  555. // Deprecated: Use SetBytesExt or SetInterfaceExt on the Handle instead.
  556. func (o *extHandle) AddExt(rt reflect.Type, tag byte,
  557. encfn func(reflect.Value) ([]byte, error),
  558. decfn func(reflect.Value, []byte) error) (err error) {
  559. if encfn == nil || decfn == nil {
  560. return o.SetExt(rt, uint64(tag), nil)
  561. }
  562. return o.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn})
  563. }
  564. // SetExt will set the extension for a tag and reflect.Type.
  565. // Note that the type must be a named type, and specifically not a pointer or Interface.
  566. // An error is returned if that is not honored.
  567. // To Deregister an ext, call SetExt with nil Ext.
  568. //
  569. // Deprecated: Use SetBytesExt or SetInterfaceExt on the Handle instead.
  570. func (o *extHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) {
  571. // o is a pointer, because we may need to initialize it
  572. rk := rt.Kind()
  573. for rk == reflect.Ptr {
  574. rt = rt.Elem()
  575. rk = rt.Kind()
  576. }
  577. if rt.PkgPath() == "" || rk == reflect.Interface { // || rk == reflect.Ptr {
  578. return fmt.Errorf("codec.Handle.SetExt: Takes named type, not a pointer or interface: %v", rt)
  579. }
  580. rtid := rt2id(rt)
  581. switch rtid {
  582. case timeTypId, rawTypId, rawExtTypId:
  583. // all natively supported type, so cannot have an extension
  584. return // TODO: should we silently ignore, or return an error???
  585. }
  586. // if o == nil {
  587. // return errors.New("codec.Handle.SetExt: extHandle not initialized")
  588. // }
  589. o2 := *o
  590. // if o2 == nil {
  591. // return errors.New("codec.Handle.SetExt: extHandle not initialized")
  592. // }
  593. for i := range o2 {
  594. v := &o2[i]
  595. if v.rtid == rtid {
  596. v.tag, v.ext = tag, ext
  597. return
  598. }
  599. }
  600. rtidptr := rt2id(reflect.PtrTo(rt))
  601. *o = append(o2, extTypeTagFn{rtid, rtidptr, rt, tag, ext, [1]uint64{}})
  602. return
  603. }
  604. func (o extHandle) getExt(rtid uintptr) (v *extTypeTagFn) {
  605. for i := range o {
  606. v = &o[i]
  607. if v.rtid == rtid || v.rtidptr == rtid {
  608. return
  609. }
  610. }
  611. return nil
  612. }
  613. func (o extHandle) getExtForTag(tag uint64) (v *extTypeTagFn) {
  614. for i := range o {
  615. v = &o[i]
  616. if v.tag == tag {
  617. return
  618. }
  619. }
  620. return nil
  621. }
  622. type intf2impl struct {
  623. rtid uintptr // for intf
  624. impl reflect.Type
  625. // _ [1]uint64 // padding // not-needed, as *intf2impl is never returned.
  626. }
  627. type intf2impls []intf2impl
  628. // Intf2Impl maps an interface to an implementing type.
  629. // This allows us support infering the concrete type
  630. // and populating it when passed an interface.
  631. // e.g. var v io.Reader can be decoded as a bytes.Buffer, etc.
  632. //
  633. // Passing a nil impl will clear the mapping.
  634. func (o *intf2impls) Intf2Impl(intf, impl reflect.Type) (err error) {
  635. if impl != nil && !impl.Implements(intf) {
  636. return fmt.Errorf("Intf2Impl: %v does not implement %v", impl, intf)
  637. }
  638. rtid := rt2id(intf)
  639. o2 := *o
  640. for i := range o2 {
  641. v := &o2[i]
  642. if v.rtid == rtid {
  643. v.impl = impl
  644. return
  645. }
  646. }
  647. *o = append(o2, intf2impl{rtid, impl})
  648. return
  649. }
  650. func (o intf2impls) intf2impl(rtid uintptr) (rv reflect.Value) {
  651. for i := range o {
  652. v := &o[i]
  653. if v.rtid == rtid {
  654. if v.impl == nil {
  655. return
  656. }
  657. if v.impl.Kind() == reflect.Ptr {
  658. return reflect.New(v.impl.Elem())
  659. }
  660. return reflect.New(v.impl).Elem()
  661. }
  662. }
  663. return
  664. }
  665. type structFieldInfoFlag uint8
  666. const (
  667. _ structFieldInfoFlag = 1 << iota
  668. structFieldInfoFlagReady
  669. structFieldInfoFlagOmitEmpty
  670. )
  671. func (x *structFieldInfoFlag) flagSet(f structFieldInfoFlag) {
  672. *x = *x | f
  673. }
  674. func (x *structFieldInfoFlag) flagClr(f structFieldInfoFlag) {
  675. *x = *x &^ f
  676. }
  677. func (x structFieldInfoFlag) flagGet(f structFieldInfoFlag) bool {
  678. return x&f != 0
  679. }
  680. func (x structFieldInfoFlag) omitEmpty() bool {
  681. return x.flagGet(structFieldInfoFlagOmitEmpty)
  682. }
  683. func (x structFieldInfoFlag) ready() bool {
  684. return x.flagGet(structFieldInfoFlagReady)
  685. }
  686. type structFieldInfo struct {
  687. encName string // encode name
  688. fieldName string // field name
  689. is [maxLevelsEmbedding]uint16 // (recursive/embedded) field index in struct
  690. nis uint8 // num levels of embedding. if 1, then it's not embedded.
  691. structFieldInfoFlag
  692. }
  693. func (si *structFieldInfo) setToZeroValue(v reflect.Value) {
  694. if v, valid := si.field(v, false); valid {
  695. v.Set(reflect.Zero(v.Type()))
  696. }
  697. }
  698. // rv returns the field of the struct.
  699. // If anonymous, it returns an Invalid
  700. func (si *structFieldInfo) field(v reflect.Value, update bool) (rv2 reflect.Value, valid bool) {
  701. // replicate FieldByIndex
  702. for i, x := range si.is {
  703. if uint8(i) == si.nis {
  704. break
  705. }
  706. if v, valid = baseStructRv(v, update); !valid {
  707. return
  708. }
  709. v = v.Field(int(x))
  710. }
  711. return v, true
  712. }
  713. // func (si *structFieldInfo) fieldval(v reflect.Value, update bool) reflect.Value {
  714. // v, _ = si.field(v, update)
  715. // return v
  716. // }
  717. func parseStructInfo(stag string) (toArray, omitEmpty bool, keytype valueType) {
  718. keytype = valueTypeString // default
  719. if stag == "" {
  720. return
  721. }
  722. for i, s := range strings.Split(stag, ",") {
  723. if i == 0 {
  724. } else {
  725. switch s {
  726. case "omitempty":
  727. omitEmpty = true
  728. case "toarray":
  729. toArray = true
  730. case "int":
  731. keytype = valueTypeInt
  732. case "uint":
  733. keytype = valueTypeUint
  734. case "float":
  735. keytype = valueTypeFloat
  736. // case "bool":
  737. // keytype = valueTypeBool
  738. case "string":
  739. keytype = valueTypeString
  740. }
  741. }
  742. }
  743. return
  744. }
  745. func (si *structFieldInfo) parseTag(stag string) {
  746. // if fname == "" {
  747. // panic(errNoFieldNameToStructFieldInfo)
  748. // }
  749. if stag == "" {
  750. return
  751. }
  752. for i, s := range strings.Split(stag, ",") {
  753. if i == 0 {
  754. if s != "" {
  755. si.encName = s
  756. }
  757. } else {
  758. switch s {
  759. case "omitempty":
  760. si.flagSet(structFieldInfoFlagOmitEmpty)
  761. // si.omitEmpty = true
  762. // case "toarray":
  763. // si.toArray = true
  764. }
  765. }
  766. }
  767. }
  768. type sfiSortedByEncName []*structFieldInfo
  769. func (p sfiSortedByEncName) Len() int {
  770. return len(p)
  771. }
  772. func (p sfiSortedByEncName) Less(i, j int) bool {
  773. return p[i].encName < p[j].encName
  774. }
  775. func (p sfiSortedByEncName) Swap(i, j int) {
  776. p[i], p[j] = p[j], p[i]
  777. }
  778. const structFieldNodeNumToCache = 4
  779. type structFieldNodeCache struct {
  780. rv [structFieldNodeNumToCache]reflect.Value
  781. idx [structFieldNodeNumToCache]uint32
  782. num uint8
  783. }
  784. func (x *structFieldNodeCache) get(key uint32) (fv reflect.Value, valid bool) {
  785. for i, k := range &x.idx {
  786. if uint8(i) == x.num {
  787. return // break
  788. }
  789. if key == k {
  790. return x.rv[i], true
  791. }
  792. }
  793. return
  794. }
  795. func (x *structFieldNodeCache) tryAdd(fv reflect.Value, key uint32) {
  796. if x.num < structFieldNodeNumToCache {
  797. x.rv[x.num] = fv
  798. x.idx[x.num] = key
  799. x.num++
  800. return
  801. }
  802. }
  803. type structFieldNode struct {
  804. v reflect.Value
  805. cache2 structFieldNodeCache
  806. cache3 structFieldNodeCache
  807. update bool
  808. }
  809. func (x *structFieldNode) field(si *structFieldInfo) (fv reflect.Value) {
  810. // return si.fieldval(x.v, x.update)
  811. // Note: we only cache if nis=2 or nis=3 i.e. up to 2 levels of embedding
  812. // This mostly saves us time on the repeated calls to v.Elem, v.Field, etc.
  813. var valid bool
  814. switch si.nis {
  815. case 1:
  816. fv = x.v.Field(int(si.is[0]))
  817. case 2:
  818. if fv, valid = x.cache2.get(uint32(si.is[0])); valid {
  819. fv = fv.Field(int(si.is[1]))
  820. return
  821. }
  822. fv = x.v.Field(int(si.is[0]))
  823. if fv, valid = baseStructRv(fv, x.update); !valid {
  824. return
  825. }
  826. x.cache2.tryAdd(fv, uint32(si.is[0]))
  827. fv = fv.Field(int(si.is[1]))
  828. case 3:
  829. var key uint32 = uint32(si.is[0])<<16 | uint32(si.is[1])
  830. if fv, valid = x.cache3.get(key); valid {
  831. fv = fv.Field(int(si.is[2]))
  832. return
  833. }
  834. fv = x.v.Field(int(si.is[0]))
  835. if fv, valid = baseStructRv(fv, x.update); !valid {
  836. return
  837. }
  838. fv = fv.Field(int(si.is[1]))
  839. if fv, valid = baseStructRv(fv, x.update); !valid {
  840. return
  841. }
  842. x.cache3.tryAdd(fv, key)
  843. fv = fv.Field(int(si.is[2]))
  844. default:
  845. fv, _ = si.field(x.v, x.update)
  846. }
  847. return
  848. }
  849. func baseStructRv(v reflect.Value, update bool) (v2 reflect.Value, valid bool) {
  850. for v.Kind() == reflect.Ptr {
  851. if v.IsNil() {
  852. if !update {
  853. return
  854. }
  855. v.Set(reflect.New(v.Type().Elem()))
  856. }
  857. v = v.Elem()
  858. }
  859. return v, true
  860. }
  861. // typeInfo keeps information about each (non-ptr) type referenced in the encode/decode sequence.
  862. //
  863. // During an encode/decode sequence, we work as below:
  864. // - If base is a built in type, en/decode base value
  865. // - If base is registered as an extension, en/decode base value
  866. // - If type is binary(M/Unm)arshaler, call Binary(M/Unm)arshal method
  867. // - If type is text(M/Unm)arshaler, call Text(M/Unm)arshal method
  868. // - Else decode appropriately based on the reflect.Kind
  869. type typeInfo struct {
  870. sfiSort []*structFieldInfo // sorted. Used when enc/dec struct to map.
  871. sfiSrc []*structFieldInfo // unsorted. Used when enc/dec struct to array.
  872. rt reflect.Type
  873. // ---- cpu cache line boundary?
  874. // sfis []structFieldInfo // all sfi, in src order, as created.
  875. sfiNamesSort []byte // all names, with indexes into the sfiSort
  876. rtid uintptr
  877. // rv0 reflect.Value // saved zero value, used if immutableKind
  878. numMeth uint16 // number of methods
  879. // comparable bool // true if a struct, and is comparable
  880. anyOmitEmpty bool // true if a struct, and any of the fields are tagged "omitempty"
  881. toArray bool // whether this (struct) type should be encoded as an array
  882. keyType valueType // if struct, how is the field name stored in a stream? default is string
  883. mbs bool // base type (T or *T) is a MapBySlice
  884. // format of marshal type fields below: [btj][mu]p? OR csp?
  885. bm bool // T is a binaryMarshaler
  886. bmp bool // *T is a binaryMarshaler
  887. bu bool // T is a binaryUnmarshaler
  888. bup bool // *T is a binaryUnmarshaler
  889. tm bool // T is a textMarshaler
  890. tmp bool // *T is a textMarshaler
  891. tu bool // T is a textUnmarshaler
  892. tup bool // *T is a textUnmarshaler
  893. jm bool // T is a jsonMarshaler
  894. jmp bool // *T is a jsonMarshaler
  895. ju bool // T is a jsonUnmarshaler
  896. jup bool // *T is a jsonUnmarshaler
  897. cs bool // T is a Selfer
  898. csp bool // *T is a Selfer
  899. _ [1]uint64 // padding
  900. }
  901. func (ti *typeInfo) indexForEncName(name []byte) (index int16) {
  902. var sn []byte
  903. if len(name)+2 <= 32 {
  904. var buf [32]byte // should not escape
  905. sn = buf[:len(name)+2]
  906. } else {
  907. sn = make([]byte, len(name)+2)
  908. }
  909. copy(sn[1:], name)
  910. sn[0], sn[len(sn)-1] = tiSep2(name), 0xff
  911. j := bytes.Index(ti.sfiNamesSort, sn)
  912. if j < 0 {
  913. return -1
  914. }
  915. index = int16(uint16(ti.sfiNamesSort[j+len(sn)+1]) | uint16(ti.sfiNamesSort[j+len(sn)])<<8)
  916. return
  917. }
  918. type rtid2ti struct {
  919. rtid uintptr
  920. ti *typeInfo
  921. }
  922. // TypeInfos caches typeInfo for each type on first inspection.
  923. //
  924. // It is configured with a set of tag keys, which are used to get
  925. // configuration for the type.
  926. type TypeInfos struct {
  927. // infos: formerly map[uintptr]*typeInfo, now *[]rtid2ti, 2 words expected
  928. infos atomicTypeInfoSlice
  929. mu sync.Mutex
  930. tags []string
  931. _ [2]uint64 // padding
  932. }
  933. // NewTypeInfos creates a TypeInfos given a set of struct tags keys.
  934. //
  935. // This allows users customize the struct tag keys which contain configuration
  936. // of their types.
  937. func NewTypeInfos(tags []string) *TypeInfos {
  938. return &TypeInfos{tags: tags}
  939. }
  940. func (x *TypeInfos) structTag(t reflect.StructTag) (s string) {
  941. // check for tags: codec, json, in that order.
  942. // this allows seamless support for many configured structs.
  943. for _, x := range x.tags {
  944. s = t.Get(x)
  945. if s != "" {
  946. return s
  947. }
  948. }
  949. return
  950. }
  951. func (x *TypeInfos) find(s []rtid2ti, rtid uintptr) (idx int, ti *typeInfo) {
  952. // binary search. adapted from sort/search.go.
  953. // if sp == nil {
  954. // return -1, nil
  955. // }
  956. // s := *sp
  957. h, i, j := 0, 0, len(s)
  958. for i < j {
  959. h = i + (j-i)/2
  960. if s[h].rtid < rtid {
  961. i = h + 1
  962. } else {
  963. j = h
  964. }
  965. }
  966. if i < len(s) && s[i].rtid == rtid {
  967. return i, s[i].ti
  968. }
  969. return i, nil
  970. }
  971. func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
  972. sp := x.infos.load()
  973. var idx int
  974. if sp != nil {
  975. idx, pti = x.find(*sp, rtid)
  976. if pti != nil {
  977. return
  978. }
  979. }
  980. rk := rt.Kind()
  981. if rk == reflect.Ptr { // || (rk == reflect.Interface && rtid != intfTypId) {
  982. panicv.errorf("invalid kind passed to TypeInfos.get: %v - %v", rk, rt)
  983. }
  984. // do not hold lock while computing this.
  985. // it may lead to duplication, but that's ok.
  986. ti := typeInfo{rt: rt, rtid: rtid}
  987. // ti.rv0 = reflect.Zero(rt)
  988. // ti.comparable = rt.Comparable()
  989. ti.numMeth = uint16(rt.NumMethod())
  990. ti.bm, ti.bmp = implIntf(rt, binaryMarshalerTyp)
  991. ti.bu, ti.bup = implIntf(rt, binaryUnmarshalerTyp)
  992. ti.tm, ti.tmp = implIntf(rt, textMarshalerTyp)
  993. ti.tu, ti.tup = implIntf(rt, textUnmarshalerTyp)
  994. ti.jm, ti.jmp = implIntf(rt, jsonMarshalerTyp)
  995. ti.ju, ti.jup = implIntf(rt, jsonUnmarshalerTyp)
  996. ti.cs, ti.csp = implIntf(rt, selferTyp)
  997. if rt.Kind() == reflect.Slice {
  998. ti.mbs, _ = implIntf(rt, mapBySliceTyp)
  999. }
  1000. if rk == reflect.Struct {
  1001. var omitEmpty bool
  1002. if f, ok := rt.FieldByName(structInfoFieldName); ok {
  1003. ti.toArray, omitEmpty, ti.keyType = parseStructInfo(x.structTag(f.Tag))
  1004. } else {
  1005. ti.keyType = valueTypeString
  1006. }
  1007. pp, pi := pool.tiLoad()
  1008. pv := pi.(*typeInfoLoadArray)
  1009. pv.etypes[0] = ti.rtid
  1010. // vv := typeInfoLoad{pv.fNames[:0], pv.encNames[:0], pv.etypes[:1], pv.sfis[:0]}
  1011. vv := typeInfoLoad{pv.etypes[:1], pv.sfis[:0]}
  1012. x.rget(rt, rtid, omitEmpty, nil, &vv)
  1013. // ti.sfis = vv.sfis
  1014. ti.sfiSrc, ti.sfiSort, ti.sfiNamesSort, ti.anyOmitEmpty = rgetResolveSFI(rt, vv.sfis, pv)
  1015. pp.Put(pi)
  1016. }
  1017. // sfi = sfiSrc
  1018. x.mu.Lock()
  1019. sp = x.infos.load()
  1020. if sp == nil {
  1021. pti = &ti
  1022. vs := append(make([]rtid2ti, 0, 16), rtid2ti{rtid, pti})
  1023. x.infos.store(&vs)
  1024. } else {
  1025. idx, pti = x.find(*sp, rtid)
  1026. if pti == nil {
  1027. pti = &ti
  1028. vs := append(*sp, rtid2ti{})
  1029. copy(vs[idx+1:], vs[idx:])
  1030. vs[idx] = rtid2ti{rtid, pti}
  1031. x.infos.store(&vs)
  1032. }
  1033. }
  1034. x.mu.Unlock()
  1035. return
  1036. }
  1037. func (x *TypeInfos) rget(rt reflect.Type, rtid uintptr, omitEmpty bool,
  1038. indexstack []uint16, pv *typeInfoLoad) {
  1039. // Read up fields and store how to access the value.
  1040. //
  1041. // It uses go's rules for message selectors,
  1042. // which say that the field with the shallowest depth is selected.
  1043. //
  1044. // Note: we consciously use slices, not a map, to simulate a set.
  1045. // Typically, types have < 16 fields,
  1046. // and iteration using equals is faster than maps there
  1047. flen := rt.NumField()
  1048. if flen > (1<<maxLevelsEmbedding - 1) {
  1049. panicv.errorf("codec: types with > %v fields are not supported - has %v fields",
  1050. (1<<maxLevelsEmbedding - 1), flen)
  1051. }
  1052. // pv.sfis = make([]structFieldInfo, flen)
  1053. LOOP:
  1054. for j, jlen := uint16(0), uint16(flen); j < jlen; j++ {
  1055. f := rt.Field(int(j))
  1056. fkind := f.Type.Kind()
  1057. // skip if a func type, or is unexported, or structTag value == "-"
  1058. switch fkind {
  1059. case reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:
  1060. continue LOOP
  1061. }
  1062. isUnexported := f.PkgPath != ""
  1063. if isUnexported && !f.Anonymous {
  1064. continue
  1065. }
  1066. stag := x.structTag(f.Tag)
  1067. if stag == "-" {
  1068. continue
  1069. }
  1070. var si structFieldInfo
  1071. var parsed bool
  1072. // if anonymous and no struct tag (or it's blank),
  1073. // and a struct (or pointer to struct), inline it.
  1074. if f.Anonymous && fkind != reflect.Interface {
  1075. // ^^ redundant but ok: per go spec, an embedded pointer type cannot be to an interface
  1076. ft := f.Type
  1077. isPtr := ft.Kind() == reflect.Ptr
  1078. for ft.Kind() == reflect.Ptr {
  1079. ft = ft.Elem()
  1080. }
  1081. isStruct := ft.Kind() == reflect.Struct
  1082. // Ignore embedded fields of unexported non-struct types.
  1083. // Also, from go1.10, ignore pointers to unexported struct types
  1084. // because unmarshal cannot assign a new struct to an unexported field.
  1085. // See https://golang.org/issue/21357
  1086. if (isUnexported && !isStruct) || (!allowSetUnexportedEmbeddedPtr && isUnexported && isPtr) {
  1087. continue
  1088. }
  1089. doInline := stag == ""
  1090. if !doInline {
  1091. si.parseTag(stag)
  1092. parsed = true
  1093. doInline = si.encName == ""
  1094. // doInline = si.isZero()
  1095. }
  1096. if doInline && isStruct {
  1097. // if etypes contains this, don't call rget again (as fields are already seen here)
  1098. ftid := rt2id(ft)
  1099. // We cannot recurse forever, but we need to track other field depths.
  1100. // So - we break if we see a type twice (not the first time).
  1101. // This should be sufficient to handle an embedded type that refers to its
  1102. // owning type, which then refers to its embedded type.
  1103. processIt := true
  1104. numk := 0
  1105. for _, k := range pv.etypes {
  1106. if k == ftid {
  1107. numk++
  1108. if numk == rgetMaxRecursion {
  1109. processIt = false
  1110. break
  1111. }
  1112. }
  1113. }
  1114. if processIt {
  1115. pv.etypes = append(pv.etypes, ftid)
  1116. indexstack2 := make([]uint16, len(indexstack)+1)
  1117. copy(indexstack2, indexstack)
  1118. indexstack2[len(indexstack)] = j
  1119. // indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
  1120. x.rget(ft, ftid, omitEmpty, indexstack2, pv)
  1121. }
  1122. continue
  1123. }
  1124. }
  1125. // after the anonymous dance: if an unexported field, skip
  1126. if isUnexported {
  1127. continue
  1128. }
  1129. if f.Name == "" {
  1130. panic(errNoFieldNameToStructFieldInfo)
  1131. }
  1132. // pv.fNames = append(pv.fNames, f.Name)
  1133. // if si.encName == "" {
  1134. if !parsed {
  1135. si.encName = f.Name
  1136. si.parseTag(stag)
  1137. parsed = true
  1138. } else if si.encName == "" {
  1139. si.encName = f.Name
  1140. }
  1141. si.fieldName = f.Name
  1142. si.flagSet(structFieldInfoFlagReady)
  1143. // pv.encNames = append(pv.encNames, si.encName)
  1144. // si.ikind = int(f.Type.Kind())
  1145. if len(indexstack) > maxLevelsEmbedding-1 {
  1146. panicv.errorf("codec: only supports up to %v depth of embedding - type has %v depth",
  1147. maxLevelsEmbedding-1, len(indexstack))
  1148. }
  1149. si.nis = uint8(len(indexstack)) + 1
  1150. copy(si.is[:], indexstack)
  1151. si.is[len(indexstack)] = j
  1152. if omitEmpty {
  1153. si.flagSet(structFieldInfoFlagOmitEmpty)
  1154. }
  1155. pv.sfis = append(pv.sfis, si)
  1156. }
  1157. }
  1158. func tiSep(name string) uint8 {
  1159. // (xn[0]%64) // (between 192-255 - outside ascii BMP)
  1160. // return 0xfe - (name[0] & 63)
  1161. // return 0xfe - (name[0] & 63) - uint8(len(name))
  1162. // return 0xfe - (name[0] & 63) - uint8(len(name)&63)
  1163. // return ((0xfe - (name[0] & 63)) & 0xf8) | (uint8(len(name) & 0x07))
  1164. return 0xfe - (name[0] & 63) - uint8(len(name)&63)
  1165. }
  1166. func tiSep2(name []byte) uint8 {
  1167. return 0xfe - (name[0] & 63) - uint8(len(name)&63)
  1168. }
  1169. // resolves the struct field info got from a call to rget.
  1170. // Returns a trimmed, unsorted and sorted []*structFieldInfo.
  1171. func rgetResolveSFI(rt reflect.Type, x []structFieldInfo, pv *typeInfoLoadArray) (
  1172. y, z []*structFieldInfo, ss []byte, anyOmitEmpty bool) {
  1173. sa := pv.sfiidx[:0]
  1174. sn := pv.b[:]
  1175. n := len(x)
  1176. var xn string
  1177. var ui uint16
  1178. var sep byte
  1179. for i := range x {
  1180. ui = uint16(i)
  1181. xn = x[i].encName // fieldName or encName? use encName for now.
  1182. if len(xn)+2 > cap(pv.b) {
  1183. sn = make([]byte, len(xn)+2)
  1184. } else {
  1185. sn = sn[:len(xn)+2]
  1186. }
  1187. // use a custom sep, so that misses are less frequent,
  1188. // since the sep (first char in search) is as unique as first char in field name.
  1189. sep = tiSep(xn)
  1190. sn[0], sn[len(sn)-1] = sep, 0xff
  1191. copy(sn[1:], xn)
  1192. j := bytes.Index(sa, sn)
  1193. if j == -1 {
  1194. sa = append(sa, sep)
  1195. sa = append(sa, xn...)
  1196. sa = append(sa, 0xff, byte(ui>>8), byte(ui))
  1197. } else {
  1198. index := uint16(sa[j+len(sn)+1]) | uint16(sa[j+len(sn)])<<8
  1199. // one of them must be reset to nil,
  1200. // and the index updated appropriately to the other one
  1201. if x[i].nis == x[index].nis {
  1202. } else if x[i].nis < x[index].nis {
  1203. sa[j+len(sn)], sa[j+len(sn)+1] = byte(ui>>8), byte(ui)
  1204. if x[index].ready() {
  1205. x[index].flagClr(structFieldInfoFlagReady)
  1206. n--
  1207. }
  1208. } else {
  1209. if x[i].ready() {
  1210. x[i].flagClr(structFieldInfoFlagReady)
  1211. n--
  1212. }
  1213. }
  1214. }
  1215. }
  1216. var w []structFieldInfo
  1217. sharingArray := len(x) <= typeInfoLoadArraySfisLen // sharing array with typeInfoLoadArray
  1218. if sharingArray {
  1219. w = make([]structFieldInfo, n)
  1220. }
  1221. // remove all the nils (non-ready)
  1222. y = make([]*structFieldInfo, n)
  1223. n = 0
  1224. var sslen int
  1225. for i := range x {
  1226. if !x[i].ready() {
  1227. continue
  1228. }
  1229. if !anyOmitEmpty && x[i].omitEmpty() {
  1230. anyOmitEmpty = true
  1231. }
  1232. if sharingArray {
  1233. w[n] = x[i]
  1234. y[n] = &w[n]
  1235. } else {
  1236. y[n] = &x[i]
  1237. }
  1238. sslen = sslen + len(x[i].encName) + 4
  1239. n++
  1240. }
  1241. if n != len(y) {
  1242. panicv.errorf("failure reading struct %v - expecting %d of %d valid fields, got %d",
  1243. rt, len(y), len(x), n)
  1244. }
  1245. z = make([]*structFieldInfo, len(y))
  1246. copy(z, y)
  1247. sort.Sort(sfiSortedByEncName(z))
  1248. sharingArray = len(sa) <= typeInfoLoadArraySfiidxLen
  1249. if sharingArray {
  1250. ss = make([]byte, 0, sslen)
  1251. } else {
  1252. ss = sa[:0] // reuse the newly made sa array if necessary
  1253. }
  1254. for i := range z {
  1255. xn = z[i].encName
  1256. sep = tiSep(xn)
  1257. ui = uint16(i)
  1258. ss = append(ss, sep)
  1259. ss = append(ss, xn...)
  1260. ss = append(ss, 0xff, byte(ui>>8), byte(ui))
  1261. }
  1262. return
  1263. }
  1264. func implIntf(rt, iTyp reflect.Type) (base bool, indir bool) {
  1265. return rt.Implements(iTyp), reflect.PtrTo(rt).Implements(iTyp)
  1266. }
  1267. // func roundFloat(x float64) float64 {
  1268. // t := math.Trunc(x)
  1269. // if math.Abs(x-t) >= 0.5 {
  1270. // return t + math.Copysign(1, x)
  1271. // }
  1272. // return t
  1273. // }
  1274. func panicToErr(h errstrDecorator, err *error) {
  1275. if recoverPanicToErr {
  1276. if x := recover(); x != nil {
  1277. // fmt.Printf("panic'ing with: %v\n", x)
  1278. // debug.PrintStack()
  1279. panicValToErr(h, x, err)
  1280. }
  1281. }
  1282. }
  1283. func panicToErrs2(h errstrDecorator, err1, err2 *error) {
  1284. if recoverPanicToErr {
  1285. if x := recover(); x != nil {
  1286. panicValToErr(h, x, err1)
  1287. panicValToErr(h, x, err2)
  1288. }
  1289. }
  1290. }
  1291. func panicValToErr(h errstrDecorator, v interface{}, err *error) {
  1292. switch xerr := v.(type) {
  1293. case nil:
  1294. case error:
  1295. switch xerr {
  1296. case nil:
  1297. case io.EOF, io.ErrUnexpectedEOF, errEncoderNotInitialized, errDecoderNotInitialized:
  1298. // treat as special (bubble up)
  1299. *err = xerr
  1300. default:
  1301. h.wrapErrstr(xerr.Error(), err)
  1302. }
  1303. case string:
  1304. if xerr != "" {
  1305. h.wrapErrstr(xerr, err)
  1306. }
  1307. case fmt.Stringer:
  1308. if xerr != nil {
  1309. h.wrapErrstr(xerr.String(), err)
  1310. }
  1311. default:
  1312. h.wrapErrstr(v, err)
  1313. }
  1314. }
  1315. func isImmutableKind(k reflect.Kind) (v bool) {
  1316. return immutableKindsSet[k]
  1317. }
  1318. // ----
  1319. type codecFnInfo struct {
  1320. ti *typeInfo
  1321. xfFn Ext
  1322. xfTag uint64
  1323. seq seqType
  1324. addrD bool
  1325. addrF bool // if addrD, this says whether decode function can take a value or a ptr
  1326. addrE bool
  1327. ready bool // ready to use
  1328. }
  1329. // codecFn encapsulates the captured variables and the encode function.
  1330. // This way, we only do some calculations one times, and pass to the
  1331. // code block that should be called (encapsulated in a function)
  1332. // instead of executing the checks every time.
  1333. type codecFn struct {
  1334. i codecFnInfo
  1335. fe func(*Encoder, *codecFnInfo, reflect.Value)
  1336. fd func(*Decoder, *codecFnInfo, reflect.Value)
  1337. _ [1]uint64 // padding
  1338. }
  1339. type codecRtidFn struct {
  1340. rtid uintptr
  1341. fn *codecFn
  1342. }
  1343. type codecFner struct {
  1344. // hh Handle
  1345. h *BasicHandle
  1346. s []codecRtidFn
  1347. be bool
  1348. js bool
  1349. _ [6]byte // padding
  1350. _ [3]uint64 // padding
  1351. }
  1352. func (c *codecFner) reset(hh Handle) {
  1353. bh := hh.getBasicHandle()
  1354. // only reset iff extensions changed or *TypeInfos changed
  1355. var hhSame = true &&
  1356. c.h == bh && c.h.TypeInfos == bh.TypeInfos &&
  1357. len(c.h.extHandle) == len(bh.extHandle) &&
  1358. (len(c.h.extHandle) == 0 || &c.h.extHandle[0] == &bh.extHandle[0])
  1359. if !hhSame {
  1360. // c.hh = hh
  1361. c.h, bh = bh, c.h // swap both
  1362. _, c.js = hh.(*JsonHandle)
  1363. c.be = hh.isBinary()
  1364. for i := range c.s {
  1365. c.s[i].fn.i.ready = false
  1366. }
  1367. }
  1368. }
  1369. func (c *codecFner) get(rt reflect.Type, checkFastpath, checkCodecSelfer bool) (fn *codecFn) {
  1370. rtid := rt2id(rt)
  1371. for _, x := range c.s {
  1372. if x.rtid == rtid {
  1373. // if rtid exists, then there's a *codenFn attached (non-nil)
  1374. fn = x.fn
  1375. if fn.i.ready {
  1376. return
  1377. }
  1378. break
  1379. }
  1380. }
  1381. var ti *typeInfo
  1382. if fn == nil {
  1383. fn = new(codecFn)
  1384. if c.s == nil {
  1385. c.s = make([]codecRtidFn, 0, 8)
  1386. }
  1387. c.s = append(c.s, codecRtidFn{rtid, fn})
  1388. } else {
  1389. ti = fn.i.ti
  1390. *fn = codecFn{}
  1391. fn.i.ti = ti
  1392. // fn.fe, fn.fd = nil, nil
  1393. }
  1394. fi := &(fn.i)
  1395. fi.ready = true
  1396. if ti == nil {
  1397. ti = c.h.getTypeInfo(rtid, rt)
  1398. fi.ti = ti
  1399. }
  1400. rk := rt.Kind()
  1401. if checkCodecSelfer && (ti.cs || ti.csp) {
  1402. fn.fe = (*Encoder).selferMarshal
  1403. fn.fd = (*Decoder).selferUnmarshal
  1404. fi.addrF = true
  1405. fi.addrD = ti.csp
  1406. fi.addrE = ti.csp
  1407. } else if rtid == timeTypId {
  1408. fn.fe = (*Encoder).kTime
  1409. fn.fd = (*Decoder).kTime
  1410. } else if rtid == rawTypId {
  1411. fn.fe = (*Encoder).raw
  1412. fn.fd = (*Decoder).raw
  1413. } else if rtid == rawExtTypId {
  1414. fn.fe = (*Encoder).rawExt
  1415. fn.fd = (*Decoder).rawExt
  1416. fi.addrF = true
  1417. fi.addrD = true
  1418. fi.addrE = true
  1419. } else if xfFn := c.h.getExt(rtid); xfFn != nil {
  1420. fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
  1421. fn.fe = (*Encoder).ext
  1422. fn.fd = (*Decoder).ext
  1423. fi.addrF = true
  1424. fi.addrD = true
  1425. if rk == reflect.Struct || rk == reflect.Array {
  1426. fi.addrE = true
  1427. }
  1428. } else if supportMarshalInterfaces && c.be && (ti.bm || ti.bmp) && (ti.bu || ti.bup) {
  1429. fn.fe = (*Encoder).binaryMarshal
  1430. fn.fd = (*Decoder).binaryUnmarshal
  1431. fi.addrF = true
  1432. fi.addrD = ti.bup
  1433. fi.addrE = ti.bmp
  1434. } else if supportMarshalInterfaces && !c.be && c.js && (ti.jm || ti.jmp) && (ti.ju || ti.jup) {
  1435. //If JSON, we should check JSONMarshal before textMarshal
  1436. fn.fe = (*Encoder).jsonMarshal
  1437. fn.fd = (*Decoder).jsonUnmarshal
  1438. fi.addrF = true
  1439. fi.addrD = ti.jup
  1440. fi.addrE = ti.jmp
  1441. } else if supportMarshalInterfaces && !c.be && (ti.tm || ti.tmp) && (ti.tu || ti.tup) {
  1442. fn.fe = (*Encoder).textMarshal
  1443. fn.fd = (*Decoder).textUnmarshal
  1444. fi.addrF = true
  1445. fi.addrD = ti.tup
  1446. fi.addrE = ti.tmp
  1447. } else {
  1448. if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) {
  1449. if rt.PkgPath() == "" { // un-named slice or map
  1450. if idx := fastpathAV.index(rtid); idx != -1 {
  1451. fn.fe = fastpathAV[idx].encfn
  1452. fn.fd = fastpathAV[idx].decfn
  1453. fi.addrD = true
  1454. fi.addrF = false
  1455. }
  1456. } else {
  1457. // use mapping for underlying type if there
  1458. var rtu reflect.Type
  1459. if rk == reflect.Map {
  1460. rtu = reflect.MapOf(rt.Key(), rt.Elem())
  1461. } else {
  1462. rtu = reflect.SliceOf(rt.Elem())
  1463. }
  1464. rtuid := rt2id(rtu)
  1465. if idx := fastpathAV.index(rtuid); idx != -1 {
  1466. xfnf := fastpathAV[idx].encfn
  1467. xrt := fastpathAV[idx].rt
  1468. fn.fe = func(e *Encoder, xf *codecFnInfo, xrv reflect.Value) {
  1469. xfnf(e, xf, xrv.Convert(xrt))
  1470. }
  1471. fi.addrD = true
  1472. fi.addrF = false // meaning it can be an address(ptr) or a value
  1473. xfnf2 := fastpathAV[idx].decfn
  1474. fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) {
  1475. if xrv.Kind() == reflect.Ptr {
  1476. xfnf2(d, xf, xrv.Convert(reflect.PtrTo(xrt)))
  1477. } else {
  1478. xfnf2(d, xf, xrv.Convert(xrt))
  1479. }
  1480. }
  1481. }
  1482. }
  1483. }
  1484. if fn.fe == nil && fn.fd == nil {
  1485. switch rk {
  1486. case reflect.Bool:
  1487. fn.fe = (*Encoder).kBool
  1488. fn.fd = (*Decoder).kBool
  1489. case reflect.String:
  1490. fn.fe = (*Encoder).kString
  1491. fn.fd = (*Decoder).kString
  1492. case reflect.Int:
  1493. fn.fd = (*Decoder).kInt
  1494. fn.fe = (*Encoder).kInt
  1495. case reflect.Int8:
  1496. fn.fe = (*Encoder).kInt8
  1497. fn.fd = (*Decoder).kInt8
  1498. case reflect.Int16:
  1499. fn.fe = (*Encoder).kInt16
  1500. fn.fd = (*Decoder).kInt16
  1501. case reflect.Int32:
  1502. fn.fe = (*Encoder).kInt32
  1503. fn.fd = (*Decoder).kInt32
  1504. case reflect.Int64:
  1505. fn.fe = (*Encoder).kInt64
  1506. fn.fd = (*Decoder).kInt64
  1507. case reflect.Uint:
  1508. fn.fd = (*Decoder).kUint
  1509. fn.fe = (*Encoder).kUint
  1510. case reflect.Uint8:
  1511. fn.fe = (*Encoder).kUint8
  1512. fn.fd = (*Decoder).kUint8
  1513. case reflect.Uint16:
  1514. fn.fe = (*Encoder).kUint16
  1515. fn.fd = (*Decoder).kUint16
  1516. case reflect.Uint32:
  1517. fn.fe = (*Encoder).kUint32
  1518. fn.fd = (*Decoder).kUint32
  1519. case reflect.Uint64:
  1520. fn.fe = (*Encoder).kUint64
  1521. fn.fd = (*Decoder).kUint64
  1522. case reflect.Uintptr:
  1523. fn.fe = (*Encoder).kUintptr
  1524. fn.fd = (*Decoder).kUintptr
  1525. case reflect.Float32:
  1526. fn.fe = (*Encoder).kFloat32
  1527. fn.fd = (*Decoder).kFloat32
  1528. case reflect.Float64:
  1529. fn.fe = (*Encoder).kFloat64
  1530. fn.fd = (*Decoder).kFloat64
  1531. case reflect.Invalid:
  1532. fn.fe = (*Encoder).kInvalid
  1533. fn.fd = (*Decoder).kErr
  1534. case reflect.Chan:
  1535. fi.seq = seqTypeChan
  1536. fn.fe = (*Encoder).kSlice
  1537. fn.fd = (*Decoder).kSlice
  1538. case reflect.Slice:
  1539. fi.seq = seqTypeSlice
  1540. fn.fe = (*Encoder).kSlice
  1541. fn.fd = (*Decoder).kSlice
  1542. case reflect.Array:
  1543. fi.seq = seqTypeArray
  1544. fn.fe = (*Encoder).kSlice
  1545. fi.addrF = false
  1546. fi.addrD = false
  1547. rt2 := reflect.SliceOf(rt.Elem())
  1548. fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) {
  1549. d.cfer().get(rt2, true, false).fd(d, xf, xrv.Slice(0, xrv.Len()))
  1550. }
  1551. // fn.fd = (*Decoder).kArray
  1552. case reflect.Struct:
  1553. if ti.anyOmitEmpty {
  1554. fn.fe = (*Encoder).kStruct
  1555. } else {
  1556. fn.fe = (*Encoder).kStructNoOmitempty
  1557. }
  1558. fn.fd = (*Decoder).kStruct
  1559. case reflect.Map:
  1560. fn.fe = (*Encoder).kMap
  1561. fn.fd = (*Decoder).kMap
  1562. case reflect.Interface:
  1563. // encode: reflect.Interface are handled already by preEncodeValue
  1564. fn.fd = (*Decoder).kInterface
  1565. fn.fe = (*Encoder).kErr
  1566. default:
  1567. // reflect.Ptr and reflect.Interface are handled already by preEncodeValue
  1568. fn.fe = (*Encoder).kErr
  1569. fn.fd = (*Decoder).kErr
  1570. }
  1571. }
  1572. }
  1573. return
  1574. }
  1575. type codecFnPooler struct {
  1576. cf *codecFner
  1577. cfp *sync.Pool
  1578. hh Handle
  1579. }
  1580. func (d *codecFnPooler) cfer() *codecFner {
  1581. if d.cf == nil {
  1582. var v interface{}
  1583. d.cfp, v = pool.codecFner()
  1584. d.cf = v.(*codecFner)
  1585. d.cf.reset(d.hh)
  1586. }
  1587. return d.cf
  1588. }
  1589. func (d *codecFnPooler) alwaysAtEnd() {
  1590. if d.cf != nil {
  1591. d.cfp.Put(d.cf)
  1592. d.cf, d.cfp = nil, nil
  1593. }
  1594. }
  1595. // ----
  1596. // these "checkOverflow" functions must be inlinable, and not call anybody.
  1597. // Overflow means that the value cannot be represented without wrapping/overflow.
  1598. // Overflow=false does not mean that the value can be represented without losing precision
  1599. // (especially for floating point).
  1600. type checkOverflow struct{}
  1601. // func (checkOverflow) Float16(f float64) (overflow bool) {
  1602. // panicv.errorf("unimplemented")
  1603. // if f < 0 {
  1604. // f = -f
  1605. // }
  1606. // return math.MaxFloat32 < f && f <= math.MaxFloat64
  1607. // }
  1608. func (checkOverflow) Float32(v float64) (overflow bool) {
  1609. if v < 0 {
  1610. v = -v
  1611. }
  1612. return math.MaxFloat32 < v && v <= math.MaxFloat64
  1613. }
  1614. func (checkOverflow) Uint(v uint64, bitsize uint8) (overflow bool) {
  1615. if bitsize == 0 || bitsize >= 64 || v == 0 {
  1616. return
  1617. }
  1618. if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc {
  1619. overflow = true
  1620. }
  1621. return
  1622. }
  1623. func (checkOverflow) Int(v int64, bitsize uint8) (overflow bool) {
  1624. if bitsize == 0 || bitsize >= 64 || v == 0 {
  1625. return
  1626. }
  1627. if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc {
  1628. overflow = true
  1629. }
  1630. return
  1631. }
  1632. func (checkOverflow) SignedInt(v uint64) (overflow bool) {
  1633. //e.g. -127 to 128 for int8
  1634. pos := (v >> 63) == 0
  1635. ui2 := v & 0x7fffffffffffffff
  1636. if pos {
  1637. if ui2 > math.MaxInt64 {
  1638. overflow = true
  1639. }
  1640. } else {
  1641. if ui2 > math.MaxInt64-1 {
  1642. overflow = true
  1643. }
  1644. }
  1645. return
  1646. }
  1647. func (x checkOverflow) Float32V(v float64) float64 {
  1648. if x.Float32(v) {
  1649. panicv.errorf("float32 overflow: %v", v)
  1650. }
  1651. return v
  1652. }
  1653. func (x checkOverflow) UintV(v uint64, bitsize uint8) uint64 {
  1654. if x.Uint(v, bitsize) {
  1655. panicv.errorf("uint64 overflow: %v", v)
  1656. }
  1657. return v
  1658. }
  1659. func (x checkOverflow) IntV(v int64, bitsize uint8) int64 {
  1660. if x.Int(v, bitsize) {
  1661. panicv.errorf("int64 overflow: %v", v)
  1662. }
  1663. return v
  1664. }
  1665. func (x checkOverflow) SignedIntV(v uint64) int64 {
  1666. if x.SignedInt(v) {
  1667. panicv.errorf("uint64 to int64 overflow: %v", v)
  1668. }
  1669. return int64(v)
  1670. }
  1671. // ------------------ SORT -----------------
  1672. func isNaN(f float64) bool { return f != f }
  1673. // -----------------------
  1674. type ioFlusher interface {
  1675. Flush() error
  1676. }
  1677. type ioPeeker interface {
  1678. Peek(int) ([]byte, error)
  1679. }
  1680. type ioBuffered interface {
  1681. Buffered() int
  1682. }
  1683. // -----------------------
  1684. type intSlice []int64
  1685. type uintSlice []uint64
  1686. // type uintptrSlice []uintptr
  1687. type floatSlice []float64
  1688. type boolSlice []bool
  1689. type stringSlice []string
  1690. // type bytesSlice [][]byte
  1691. func (p intSlice) Len() int { return len(p) }
  1692. func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
  1693. func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1694. func (p uintSlice) Len() int { return len(p) }
  1695. func (p uintSlice) Less(i, j int) bool { return p[i] < p[j] }
  1696. func (p uintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1697. // func (p uintptrSlice) Len() int { return len(p) }
  1698. // func (p uintptrSlice) Less(i, j int) bool { return p[i] < p[j] }
  1699. // func (p uintptrSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1700. func (p floatSlice) Len() int { return len(p) }
  1701. func (p floatSlice) Less(i, j int) bool {
  1702. return p[i] < p[j] || isNaN(p[i]) && !isNaN(p[j])
  1703. }
  1704. func (p floatSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1705. func (p stringSlice) Len() int { return len(p) }
  1706. func (p stringSlice) Less(i, j int) bool { return p[i] < p[j] }
  1707. func (p stringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1708. // func (p bytesSlice) Len() int { return len(p) }
  1709. // func (p bytesSlice) Less(i, j int) bool { return bytes.Compare(p[i], p[j]) == -1 }
  1710. // func (p bytesSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1711. func (p boolSlice) Len() int { return len(p) }
  1712. func (p boolSlice) Less(i, j int) bool { return !p[i] && p[j] }
  1713. func (p boolSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1714. // ---------------------
  1715. type intRv struct {
  1716. v int64
  1717. r reflect.Value
  1718. }
  1719. type intRvSlice []intRv
  1720. type uintRv struct {
  1721. v uint64
  1722. r reflect.Value
  1723. }
  1724. type uintRvSlice []uintRv
  1725. type floatRv struct {
  1726. v float64
  1727. r reflect.Value
  1728. }
  1729. type floatRvSlice []floatRv
  1730. type boolRv struct {
  1731. v bool
  1732. r reflect.Value
  1733. }
  1734. type boolRvSlice []boolRv
  1735. type stringRv struct {
  1736. v string
  1737. r reflect.Value
  1738. }
  1739. type stringRvSlice []stringRv
  1740. type bytesRv struct {
  1741. v []byte
  1742. r reflect.Value
  1743. }
  1744. type bytesRvSlice []bytesRv
  1745. type timeRv struct {
  1746. v time.Time
  1747. r reflect.Value
  1748. }
  1749. type timeRvSlice []timeRv
  1750. func (p intRvSlice) Len() int { return len(p) }
  1751. func (p intRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
  1752. func (p intRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1753. func (p uintRvSlice) Len() int { return len(p) }
  1754. func (p uintRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
  1755. func (p uintRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1756. func (p floatRvSlice) Len() int { return len(p) }
  1757. func (p floatRvSlice) Less(i, j int) bool {
  1758. return p[i].v < p[j].v || isNaN(p[i].v) && !isNaN(p[j].v)
  1759. }
  1760. func (p floatRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1761. func (p stringRvSlice) Len() int { return len(p) }
  1762. func (p stringRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
  1763. func (p stringRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1764. func (p bytesRvSlice) Len() int { return len(p) }
  1765. func (p bytesRvSlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
  1766. func (p bytesRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1767. func (p boolRvSlice) Len() int { return len(p) }
  1768. func (p boolRvSlice) Less(i, j int) bool { return !p[i].v && p[j].v }
  1769. func (p boolRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1770. func (p timeRvSlice) Len() int { return len(p) }
  1771. func (p timeRvSlice) Less(i, j int) bool { return p[i].v.Before(p[j].v) }
  1772. func (p timeRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1773. // -----------------
  1774. type bytesI struct {
  1775. v []byte
  1776. i interface{}
  1777. }
  1778. type bytesISlice []bytesI
  1779. func (p bytesISlice) Len() int { return len(p) }
  1780. func (p bytesISlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
  1781. func (p bytesISlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  1782. // -----------------
  1783. type set []uintptr
  1784. func (s *set) add(v uintptr) (exists bool) {
  1785. // e.ci is always nil, or len >= 1
  1786. x := *s
  1787. if x == nil {
  1788. x = make([]uintptr, 1, 8)
  1789. x[0] = v
  1790. *s = x
  1791. return
  1792. }
  1793. // typically, length will be 1. make this perform.
  1794. if len(x) == 1 {
  1795. if j := x[0]; j == 0 {
  1796. x[0] = v
  1797. } else if j == v {
  1798. exists = true
  1799. } else {
  1800. x = append(x, v)
  1801. *s = x
  1802. }
  1803. return
  1804. }
  1805. // check if it exists
  1806. for _, j := range x {
  1807. if j == v {
  1808. exists = true
  1809. return
  1810. }
  1811. }
  1812. // try to replace a "deleted" slot
  1813. for i, j := range x {
  1814. if j == 0 {
  1815. x[i] = v
  1816. return
  1817. }
  1818. }
  1819. // if unable to replace deleted slot, just append it.
  1820. x = append(x, v)
  1821. *s = x
  1822. return
  1823. }
  1824. func (s *set) remove(v uintptr) (exists bool) {
  1825. x := *s
  1826. if len(x) == 0 {
  1827. return
  1828. }
  1829. if len(x) == 1 {
  1830. if x[0] == v {
  1831. x[0] = 0
  1832. }
  1833. return
  1834. }
  1835. for i, j := range x {
  1836. if j == v {
  1837. exists = true
  1838. x[i] = 0 // set it to 0, as way to delete it.
  1839. // copy(x[i:], x[i+1:])
  1840. // x = x[:len(x)-1]
  1841. return
  1842. }
  1843. }
  1844. return
  1845. }
  1846. // ------
  1847. // bitset types are better than [256]bool, because they permit the whole
  1848. // bitset array being on a single cache line and use less memory.
  1849. // given x > 0 and n > 0 and x is exactly 2^n, then pos/x === pos>>n AND pos%x === pos&(x-1).
  1850. // consequently, pos/32 === pos>>5, pos/16 === pos>>4, pos/8 === pos>>3, pos%8 == pos&7
  1851. type bitset256 [32]byte
  1852. func (x *bitset256) isset(pos byte) bool {
  1853. return x[pos>>3]&(1<<(pos&7)) != 0
  1854. }
  1855. func (x *bitset256) issetv(pos byte) byte {
  1856. return x[pos>>3] & (1 << (pos & 7))
  1857. }
  1858. func (x *bitset256) set(pos byte) {
  1859. x[pos>>3] |= (1 << (pos & 7))
  1860. }
  1861. // func (x *bitset256) unset(pos byte) {
  1862. // x[pos>>3] &^= (1 << (pos & 7))
  1863. // }
  1864. type bitset128 [16]byte
  1865. func (x *bitset128) isset(pos byte) bool {
  1866. return x[pos>>3]&(1<<(pos&7)) != 0
  1867. }
  1868. func (x *bitset128) set(pos byte) {
  1869. x[pos>>3] |= (1 << (pos & 7))
  1870. }
  1871. // func (x *bitset128) unset(pos byte) {
  1872. // x[pos>>3] &^= (1 << (pos & 7))
  1873. // }
  1874. type bitset32 [4]byte
  1875. func (x *bitset32) isset(pos byte) bool {
  1876. return x[pos>>3]&(1<<(pos&7)) != 0
  1877. }
  1878. func (x *bitset32) set(pos byte) {
  1879. x[pos>>3] |= (1 << (pos & 7))
  1880. }
  1881. // func (x *bitset32) unset(pos byte) {
  1882. // x[pos>>3] &^= (1 << (pos & 7))
  1883. // }
  1884. // type bit2set256 [64]byte
  1885. // func (x *bit2set256) set(pos byte, v1, v2 bool) {
  1886. // var pos2 uint8 = (pos & 3) << 1 // returning 0, 2, 4 or 6
  1887. // if v1 {
  1888. // x[pos>>2] |= 1 << (pos2 + 1)
  1889. // }
  1890. // if v2 {
  1891. // x[pos>>2] |= 1 << pos2
  1892. // }
  1893. // }
  1894. // func (x *bit2set256) get(pos byte) uint8 {
  1895. // var pos2 uint8 = (pos & 3) << 1 // returning 0, 2, 4 or 6
  1896. // return x[pos>>2] << (6 - pos2) >> 6 // 11000000 -> 00000011
  1897. // }
  1898. // ------------
  1899. type pooler struct {
  1900. dn sync.Pool // for decNaked
  1901. cfn sync.Pool // for codecFner
  1902. tiload sync.Pool
  1903. strRv8, strRv16, strRv32, strRv64, strRv128 sync.Pool // for stringRV
  1904. }
  1905. func (p *pooler) init() {
  1906. p.strRv8.New = func() interface{} { return new([8]stringRv) }
  1907. p.strRv16.New = func() interface{} { return new([16]stringRv) }
  1908. p.strRv32.New = func() interface{} { return new([32]stringRv) }
  1909. p.strRv64.New = func() interface{} { return new([64]stringRv) }
  1910. p.strRv128.New = func() interface{} { return new([128]stringRv) }
  1911. p.dn.New = func() interface{} { x := new(decNaked); x.init(); return x }
  1912. p.tiload.New = func() interface{} { return new(typeInfoLoadArray) }
  1913. p.cfn.New = func() interface{} { return new(codecFner) }
  1914. }
  1915. func (p *pooler) stringRv8() (sp *sync.Pool, v interface{}) {
  1916. return &p.strRv8, p.strRv8.Get()
  1917. }
  1918. func (p *pooler) stringRv16() (sp *sync.Pool, v interface{}) {
  1919. return &p.strRv16, p.strRv16.Get()
  1920. }
  1921. func (p *pooler) stringRv32() (sp *sync.Pool, v interface{}) {
  1922. return &p.strRv32, p.strRv32.Get()
  1923. }
  1924. func (p *pooler) stringRv64() (sp *sync.Pool, v interface{}) {
  1925. return &p.strRv64, p.strRv64.Get()
  1926. }
  1927. func (p *pooler) stringRv128() (sp *sync.Pool, v interface{}) {
  1928. return &p.strRv128, p.strRv128.Get()
  1929. }
  1930. func (p *pooler) decNaked() (sp *sync.Pool, v interface{}) {
  1931. return &p.dn, p.dn.Get()
  1932. }
  1933. func (p *pooler) codecFner() (sp *sync.Pool, v interface{}) {
  1934. return &p.cfn, p.cfn.Get()
  1935. }
  1936. func (p *pooler) tiLoad() (sp *sync.Pool, v interface{}) {
  1937. return &p.tiload, p.tiload.Get()
  1938. }
  1939. // func (p *pooler) decNaked() (v *decNaked, f func(*decNaked) ) {
  1940. // sp := &(p.dn)
  1941. // vv := sp.Get()
  1942. // return vv.(*decNaked), func(x *decNaked) { sp.Put(vv) }
  1943. // }
  1944. // func (p *pooler) decNakedGet() (v interface{}) {
  1945. // return p.dn.Get()
  1946. // }
  1947. // func (p *pooler) codecFnerGet() (v interface{}) {
  1948. // return p.cfn.Get()
  1949. // }
  1950. // func (p *pooler) tiLoadGet() (v interface{}) {
  1951. // return p.tiload.Get()
  1952. // }
  1953. // func (p *pooler) decNakedPut(v interface{}) {
  1954. // p.dn.Put(v)
  1955. // }
  1956. // func (p *pooler) codecFnerPut(v interface{}) {
  1957. // p.cfn.Put(v)
  1958. // }
  1959. // func (p *pooler) tiLoadPut(v interface{}) {
  1960. // p.tiload.Put(v)
  1961. // }
  1962. type panicHdl struct{}
  1963. func (panicHdl) errorv(err error) {
  1964. if err != nil {
  1965. panic(err)
  1966. }
  1967. }
  1968. func (panicHdl) errorstr(message string) {
  1969. if message != "" {
  1970. panic(message)
  1971. }
  1972. }
  1973. func (panicHdl) errorf(format string, params ...interface{}) {
  1974. if format != "" {
  1975. if len(params) == 0 {
  1976. panic(format)
  1977. } else {
  1978. panic(fmt.Sprintf(format, params...))
  1979. }
  1980. }
  1981. }
  1982. type errstrDecorator interface {
  1983. wrapErrstr(interface{}, *error)
  1984. }
  1985. type errstrDecoratorDef struct{}
  1986. func (errstrDecoratorDef) wrapErrstr(v interface{}, e *error) { *e = fmt.Errorf("%v", v) }
  1987. type must struct{}
  1988. func (must) String(s string, err error) string {
  1989. if err != nil {
  1990. panicv.errorv(err)
  1991. }
  1992. return s
  1993. }
  1994. func (must) Int(s int64, err error) int64 {
  1995. if err != nil {
  1996. panicv.errorv(err)
  1997. }
  1998. return s
  1999. }
  2000. func (must) Uint(s uint64, err error) uint64 {
  2001. if err != nil {
  2002. panicv.errorv(err)
  2003. }
  2004. return s
  2005. }
  2006. func (must) Float(s float64, err error) float64 {
  2007. if err != nil {
  2008. panicv.errorv(err)
  2009. }
  2010. return s
  2011. }
  2012. // xdebugf prints the message in red on the terminal.
  2013. // Use it in place of fmt.Printf (which it calls internally)
  2014. func xdebugf(pattern string, args ...interface{}) {
  2015. var delim string
  2016. if len(pattern) > 0 && pattern[len(pattern)-1] != '\n' {
  2017. delim = "\n"
  2018. }
  2019. fmt.Printf("\033[1;31m"+pattern+delim+"\033[0m", args...)
  2020. }
  2021. // func isImmutableKind(k reflect.Kind) (v bool) {
  2022. // return false ||
  2023. // k == reflect.Int ||
  2024. // k == reflect.Int8 ||
  2025. // k == reflect.Int16 ||
  2026. // k == reflect.Int32 ||
  2027. // k == reflect.Int64 ||
  2028. // k == reflect.Uint ||
  2029. // k == reflect.Uint8 ||
  2030. // k == reflect.Uint16 ||
  2031. // k == reflect.Uint32 ||
  2032. // k == reflect.Uint64 ||
  2033. // k == reflect.Uintptr ||
  2034. // k == reflect.Float32 ||
  2035. // k == reflect.Float64 ||
  2036. // k == reflect.Bool ||
  2037. // k == reflect.String
  2038. // }
  2039. // func timeLocUTCName(tzint int16) string {
  2040. // if tzint == 0 {
  2041. // return "UTC"
  2042. // }
  2043. // var tzname = []byte("UTC+00:00")
  2044. // //tzname := fmt.Sprintf("UTC%s%02d:%02d", tzsign, tz/60, tz%60) //perf issue using Sprintf. inline below.
  2045. // //tzhr, tzmin := tz/60, tz%60 //faster if u convert to int first
  2046. // var tzhr, tzmin int16
  2047. // if tzint < 0 {
  2048. // tzname[3] = '-' // (TODO: verify. this works here)
  2049. // tzhr, tzmin = -tzint/60, (-tzint)%60
  2050. // } else {
  2051. // tzhr, tzmin = tzint/60, tzint%60
  2052. // }
  2053. // tzname[4] = timeDigits[tzhr/10]
  2054. // tzname[5] = timeDigits[tzhr%10]
  2055. // tzname[7] = timeDigits[tzmin/10]
  2056. // tzname[8] = timeDigits[tzmin%10]
  2057. // return string(tzname)
  2058. // //return time.FixedZone(string(tzname), int(tzint)*60)
  2059. // }