helper.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. // Copyright (c) 2012-2015 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. // The only codec that may suffer (slightly) is cbor, and only when decoding indefinite-length.
  39. // It may suffer because we treat it like a text-based codec, and read separators.
  40. // However, this read is a no-op and the cost is insignificant.
  41. //
  42. // Philosophy
  43. // ------------
  44. // On decode, this codec will update containers appropriately:
  45. // - If struct, update fields from stream into fields of struct.
  46. // If field in stream not found in struct, handle appropriately (based on option).
  47. // If a struct field has no corresponding value in the stream, leave it AS IS.
  48. // If nil in stream, set value to nil/zero value.
  49. // - If map, update map from stream.
  50. // If the stream value is NIL, set the map to nil.
  51. // - if slice, try to update up to length of array in stream.
  52. // if container len is less than stream array length,
  53. // and container cannot be expanded, handled (based on option).
  54. // This means you can decode 4-element stream array into 1-element array.
  55. //
  56. // ------------------------------------
  57. // On encode, user can specify omitEmpty. This means that the value will be omitted
  58. // if the zero value. The problem may occur during decode, where omitted values do not affect
  59. // the value being decoded into. This means that if decoding into a struct with an
  60. // int field with current value=5, and the field is omitted in the stream, then after
  61. // decoding, the value will still be 5 (not 0).
  62. // omitEmpty only works if you guarantee that you always decode into zero-values.
  63. //
  64. // ------------------------------------
  65. // We could have truncated a map to remove keys not available in the stream,
  66. // or set values in the struct which are not in the stream to their zero values.
  67. // We decided against it because there is no efficient way to do it.
  68. // We may introduce it as an option later.
  69. // However, that will require enabling it for both runtime and code generation modes.
  70. //
  71. // To support truncate, we need to do 2 passes over the container:
  72. // map
  73. // - first collect all keys (e.g. in k1)
  74. // - for each key in stream, mark k1 that the key should not be removed
  75. // - after updating map, do second pass and call delete for all keys in k1 which are not marked
  76. // struct:
  77. // - for each field, track the *typeInfo s1
  78. // - iterate through all s1, and for each one not marked, set value to zero
  79. // - this involves checking the possible anonymous fields which are nil ptrs.
  80. // too much work.
  81. //
  82. // ------------------------------------------
  83. // Error Handling is done within the library using panic.
  84. //
  85. // This way, the code doesn't have to keep checking if an error has happened,
  86. // and we don't have to keep sending the error value along with each call
  87. // or storing it in the En|Decoder and checking it constantly along the way.
  88. //
  89. // The disadvantage is that small functions which use panics cannot be inlined.
  90. // The code accounts for that by only using panics behind an interface;
  91. // since interface calls cannot be inlined, this is irrelevant.
  92. //
  93. // We considered storing the error is En|Decoder.
  94. // - once it has its err field set, it cannot be used again.
  95. // - panicing will be optional, controlled by const flag.
  96. // - code should always check error first and return early.
  97. // We eventually decided against it as it makes the code clumsier to always
  98. // check for these error conditions.
  99. import (
  100. "bytes"
  101. "encoding"
  102. "encoding/binary"
  103. "errors"
  104. "fmt"
  105. "math"
  106. "reflect"
  107. "sort"
  108. "strings"
  109. "sync"
  110. "time"
  111. )
  112. const (
  113. scratchByteArrayLen = 32
  114. initCollectionCap = 32 // 32 is defensive. 16 is preferred.
  115. // Support encoding.(Binary|Text)(Unm|M)arshaler.
  116. // This constant flag will enable or disable it.
  117. supportMarshalInterfaces = true
  118. // Each Encoder or Decoder uses a cache of functions based on conditionals,
  119. // so that the conditionals are not run every time.
  120. //
  121. // Either a map or a slice is used to keep track of the functions.
  122. // The map is more natural, but has a higher cost than a slice/array.
  123. // This flag (useMapForCodecCache) controls which is used.
  124. //
  125. // From benchmarks, slices with linear search perform better with < 32 entries.
  126. // We have typically seen a high threshold of about 24 entries.
  127. useMapForCodecCache = false
  128. // for debugging, set this to false, to catch panic traces.
  129. // Note that this will always cause rpc tests to fail, since they need io.EOF sent via panic.
  130. recoverPanicToErr = true
  131. // Fast path functions try to create a fast path encode or decode implementation
  132. // for common maps and slices, by by-passing reflection altogether.
  133. fastpathEnabled = true
  134. // if checkStructForEmptyValue, check structs fields to see if an empty value.
  135. // This could be an expensive call, so possibly disable it.
  136. checkStructForEmptyValue = false
  137. // if derefForIsEmptyValue, deref pointers and interfaces when checking isEmptyValue
  138. derefForIsEmptyValue = false
  139. // if resetSliceElemToZeroValue, then on decoding a slice, reset the element to a zero value first.
  140. // Only concern is that, if the slice already contained some garbage, we will decode into that garbage.
  141. // The chances of this are slim, so leave this "optimization".
  142. // TODO: should this be true, to ensure that we always decode into a "zero" "empty" value?
  143. resetSliceElemToZeroValue bool = false
  144. )
  145. var oneByteArr = [1]byte{0}
  146. var zeroByteSlice = oneByteArr[:0:0]
  147. type charEncoding uint8
  148. const (
  149. c_RAW charEncoding = iota
  150. c_UTF8
  151. c_UTF16LE
  152. c_UTF16BE
  153. c_UTF32LE
  154. c_UTF32BE
  155. )
  156. // valueType is the stream type
  157. type valueType uint8
  158. const (
  159. valueTypeUnset valueType = iota
  160. valueTypeNil
  161. valueTypeInt
  162. valueTypeUint
  163. valueTypeFloat
  164. valueTypeBool
  165. valueTypeString
  166. valueTypeSymbol
  167. valueTypeBytes
  168. valueTypeMap
  169. valueTypeArray
  170. valueTypeTimestamp
  171. valueTypeExt
  172. // valueTypeInvalid = 0xff
  173. )
  174. type seqType uint8
  175. const (
  176. _ seqType = iota
  177. seqTypeArray
  178. seqTypeSlice
  179. seqTypeChan
  180. )
  181. // note that containerMapStart and containerArraySend are not sent.
  182. // This is because the ReadXXXStart and EncodeXXXStart already does these.
  183. type containerState uint8
  184. const (
  185. _ containerState = iota
  186. containerMapStart // slot left open, since Driver method already covers it
  187. containerMapKey
  188. containerMapValue
  189. containerMapEnd
  190. containerArrayStart // slot left open, since Driver methods already cover it
  191. containerArrayElem
  192. containerArrayEnd
  193. )
  194. type containerStateRecv interface {
  195. sendContainerState(containerState)
  196. }
  197. // mirror json.Marshaler and json.Unmarshaler here,
  198. // so we don't import the encoding/json package
  199. type jsonMarshaler interface {
  200. MarshalJSON() ([]byte, error)
  201. }
  202. type jsonUnmarshaler interface {
  203. UnmarshalJSON([]byte) error
  204. }
  205. var (
  206. bigen = binary.BigEndian
  207. structInfoFieldName = "_struct"
  208. mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
  209. mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil))
  210. intfSliceTyp = reflect.TypeOf([]interface{}(nil))
  211. intfTyp = intfSliceTyp.Elem()
  212. stringTyp = reflect.TypeOf("")
  213. timeTyp = reflect.TypeOf(time.Time{})
  214. rawExtTyp = reflect.TypeOf(RawExt{})
  215. uint8SliceTyp = reflect.TypeOf([]uint8(nil))
  216. mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem()
  217. binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
  218. binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
  219. textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
  220. textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
  221. jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem()
  222. jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem()
  223. selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem()
  224. uint8SliceTypId = reflect.ValueOf(uint8SliceTyp).Pointer()
  225. rawExtTypId = reflect.ValueOf(rawExtTyp).Pointer()
  226. intfTypId = reflect.ValueOf(intfTyp).Pointer()
  227. timeTypId = reflect.ValueOf(timeTyp).Pointer()
  228. stringTypId = reflect.ValueOf(stringTyp).Pointer()
  229. mapStrIntfTypId = reflect.ValueOf(mapStrIntfTyp).Pointer()
  230. mapIntfIntfTypId = reflect.ValueOf(mapIntfIntfTyp).Pointer()
  231. intfSliceTypId = reflect.ValueOf(intfSliceTyp).Pointer()
  232. // mapBySliceTypId = reflect.ValueOf(mapBySliceTyp).Pointer()
  233. intBitsize uint8 = uint8(reflect.TypeOf(int(0)).Bits())
  234. uintBitsize uint8 = uint8(reflect.TypeOf(uint(0)).Bits())
  235. bsAll0x00 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
  236. bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  237. chkOvf checkOverflow
  238. noFieldNameToStructFieldInfoErr = errors.New("no field name passed to parseStructFieldInfo")
  239. )
  240. var defTypeInfos = NewTypeInfos([]string{"codec", "json"})
  241. // Selfer defines methods by which a value can encode or decode itself.
  242. //
  243. // Any type which implements Selfer will be able to encode or decode itself.
  244. // Consequently, during (en|de)code, this takes precedence over
  245. // (text|binary)(M|Unm)arshal or extension support.
  246. type Selfer interface {
  247. CodecEncodeSelf(*Encoder)
  248. CodecDecodeSelf(*Decoder)
  249. }
  250. // MapBySlice represents a slice which should be encoded as a map in the stream.
  251. // The slice contains a sequence of key-value pairs.
  252. // This affords storing a map in a specific sequence in the stream.
  253. //
  254. // The support of MapBySlice affords the following:
  255. // - A slice type which implements MapBySlice will be encoded as a map
  256. // - A slice can be decoded from a map in the stream
  257. type MapBySlice interface {
  258. MapBySlice()
  259. }
  260. // WARNING: DO NOT USE DIRECTLY. EXPORTED FOR GODOC BENEFIT. WILL BE REMOVED.
  261. //
  262. // BasicHandle encapsulates the common options and extension functions.
  263. type BasicHandle struct {
  264. // TypeInfos is used to get the type info for any type.
  265. //
  266. // If not configured, the default TypeInfos is used, which uses struct tag keys: codec, json
  267. TypeInfos *TypeInfos
  268. extHandle
  269. EncodeOptions
  270. DecodeOptions
  271. }
  272. func (x *BasicHandle) getBasicHandle() *BasicHandle {
  273. return x
  274. }
  275. func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
  276. if x.TypeInfos != nil {
  277. return x.TypeInfos.get(rtid, rt)
  278. }
  279. return defTypeInfos.get(rtid, rt)
  280. }
  281. // Handle is the interface for a specific encoding format.
  282. //
  283. // Typically, a Handle is pre-configured before first time use,
  284. // and not modified while in use. Such a pre-configured Handle
  285. // is safe for concurrent access.
  286. type Handle interface {
  287. getBasicHandle() *BasicHandle
  288. newEncDriver(w *Encoder) encDriver
  289. newDecDriver(r *Decoder) decDriver
  290. isBinary() bool
  291. }
  292. // RawExt represents raw unprocessed extension data.
  293. // Some codecs will decode extension data as a *RawExt if there is no registered extension for the tag.
  294. //
  295. // Only one of Data or Value is nil. If Data is nil, then the content of the RawExt is in the Value.
  296. type RawExt struct {
  297. Tag uint64
  298. // Data is the []byte which represents the raw ext. If Data is nil, ext is exposed in Value.
  299. // Data is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types
  300. Data []byte
  301. // Value represents the extension, if Data is nil.
  302. // Value is used by codecs (e.g. cbor) which use the format to do custom serialization of the types.
  303. Value interface{}
  304. }
  305. // BytesExt handles custom (de)serialization of types to/from []byte.
  306. // It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
  307. type BytesExt interface {
  308. // WriteExt converts a value to a []byte.
  309. //
  310. // Note: v *may* be a pointer to the extension type, if the extension type was a struct or array.
  311. WriteExt(v interface{}) []byte
  312. // ReadExt updates a value from a []byte.
  313. ReadExt(dst interface{}, src []byte)
  314. }
  315. // InterfaceExt handles custom (de)serialization of types to/from another interface{} value.
  316. // The Encoder or Decoder will then handle the further (de)serialization of that known type.
  317. //
  318. // It is used by codecs (e.g. cbor, json) which use the format to do custom serialization of the types.
  319. type InterfaceExt interface {
  320. // ConvertExt converts a value into a simpler interface for easy encoding e.g. convert time.Time to int64.
  321. //
  322. // Note: v *may* be a pointer to the extension type, if the extension type was a struct or array.
  323. ConvertExt(v interface{}) interface{}
  324. // UpdateExt updates a value from a simpler interface for easy decoding e.g. convert int64 to time.Time.
  325. UpdateExt(dst interface{}, src interface{})
  326. }
  327. // Ext handles custom (de)serialization of custom types / extensions.
  328. type Ext interface {
  329. BytesExt
  330. InterfaceExt
  331. }
  332. // addExtWrapper is a wrapper implementation to support former AddExt exported method.
  333. type addExtWrapper struct {
  334. encFn func(reflect.Value) ([]byte, error)
  335. decFn func(reflect.Value, []byte) error
  336. }
  337. func (x addExtWrapper) WriteExt(v interface{}) []byte {
  338. bs, err := x.encFn(reflect.ValueOf(v))
  339. if err != nil {
  340. panic(err)
  341. }
  342. return bs
  343. }
  344. func (x addExtWrapper) ReadExt(v interface{}, bs []byte) {
  345. if err := x.decFn(reflect.ValueOf(v), bs); err != nil {
  346. panic(err)
  347. }
  348. }
  349. func (x addExtWrapper) ConvertExt(v interface{}) interface{} {
  350. return x.WriteExt(v)
  351. }
  352. func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) {
  353. x.ReadExt(dest, v.([]byte))
  354. }
  355. type setExtWrapper struct {
  356. b BytesExt
  357. i InterfaceExt
  358. }
  359. func (x *setExtWrapper) WriteExt(v interface{}) []byte {
  360. if x.b == nil {
  361. panic("BytesExt.WriteExt is not supported")
  362. }
  363. return x.b.WriteExt(v)
  364. }
  365. func (x *setExtWrapper) ReadExt(v interface{}, bs []byte) {
  366. if x.b == nil {
  367. panic("BytesExt.WriteExt is not supported")
  368. }
  369. x.b.ReadExt(v, bs)
  370. }
  371. func (x *setExtWrapper) ConvertExt(v interface{}) interface{} {
  372. if x.i == nil {
  373. panic("InterfaceExt.ConvertExt is not supported")
  374. }
  375. return x.i.ConvertExt(v)
  376. }
  377. func (x *setExtWrapper) UpdateExt(dest interface{}, v interface{}) {
  378. if x.i == nil {
  379. panic("InterfaceExxt.UpdateExt is not supported")
  380. }
  381. x.i.UpdateExt(dest, v)
  382. }
  383. // type errorString string
  384. // func (x errorString) Error() string { return string(x) }
  385. type binaryEncodingType struct{}
  386. func (_ binaryEncodingType) isBinary() bool { return true }
  387. type textEncodingType struct{}
  388. func (_ textEncodingType) isBinary() bool { return false }
  389. // noBuiltInTypes is embedded into many types which do not support builtins
  390. // e.g. msgpack, simple, cbor.
  391. type noBuiltInTypes struct{}
  392. func (_ noBuiltInTypes) IsBuiltinType(rt uintptr) bool { return false }
  393. func (_ noBuiltInTypes) EncodeBuiltin(rt uintptr, v interface{}) {}
  394. func (_ noBuiltInTypes) DecodeBuiltin(rt uintptr, v interface{}) {}
  395. type noStreamingCodec struct{}
  396. func (_ noStreamingCodec) CheckBreak() bool { return false }
  397. // bigenHelper.
  398. // Users must already slice the x completely, because we will not reslice.
  399. type bigenHelper struct {
  400. x []byte // must be correctly sliced to appropriate len. slicing is a cost.
  401. w encWriter
  402. }
  403. func (z bigenHelper) writeUint16(v uint16) {
  404. bigen.PutUint16(z.x, v)
  405. z.w.writeb(z.x)
  406. }
  407. func (z bigenHelper) writeUint32(v uint32) {
  408. bigen.PutUint32(z.x, v)
  409. z.w.writeb(z.x)
  410. }
  411. func (z bigenHelper) writeUint64(v uint64) {
  412. bigen.PutUint64(z.x, v)
  413. z.w.writeb(z.x)
  414. }
  415. type extTypeTagFn struct {
  416. rtid uintptr
  417. rt reflect.Type
  418. tag uint64
  419. ext Ext
  420. }
  421. type extHandle []extTypeTagFn
  422. // DEPRECATED: Use SetBytesExt or SetInterfaceExt on the Handle instead.
  423. //
  424. // AddExt registes an encode and decode function for a reflect.Type.
  425. // AddExt internally calls SetExt.
  426. // To deregister an Ext, call AddExt with nil encfn and/or nil decfn.
  427. func (o *extHandle) AddExt(
  428. rt reflect.Type, tag byte,
  429. encfn func(reflect.Value) ([]byte, error), decfn func(reflect.Value, []byte) error,
  430. ) (err error) {
  431. if encfn == nil || decfn == nil {
  432. return o.SetExt(rt, uint64(tag), nil)
  433. }
  434. return o.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn})
  435. }
  436. // DEPRECATED: Use SetBytesExt or SetInterfaceExt on the Handle instead.
  437. //
  438. // Note that the type must be a named type, and specifically not
  439. // a pointer or Interface. An error is returned if that is not honored.
  440. //
  441. // To Deregister an ext, call SetExt with nil Ext
  442. func (o *extHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) {
  443. // o is a pointer, because we may need to initialize it
  444. if rt.PkgPath() == "" || rt.Kind() == reflect.Interface {
  445. err = fmt.Errorf("codec.Handle.AddExt: Takes named type, especially not a pointer or interface: %T",
  446. reflect.Zero(rt).Interface())
  447. return
  448. }
  449. rtid := reflect.ValueOf(rt).Pointer()
  450. for _, v := range *o {
  451. if v.rtid == rtid {
  452. v.tag, v.ext = tag, ext
  453. return
  454. }
  455. }
  456. if *o == nil {
  457. *o = make([]extTypeTagFn, 0, 4)
  458. }
  459. *o = append(*o, extTypeTagFn{rtid, rt, tag, ext})
  460. return
  461. }
  462. func (o extHandle) getExt(rtid uintptr) *extTypeTagFn {
  463. var v *extTypeTagFn
  464. for i := range o {
  465. v = &o[i]
  466. if v.rtid == rtid {
  467. return v
  468. }
  469. }
  470. return nil
  471. }
  472. func (o extHandle) getExtForTag(tag uint64) *extTypeTagFn {
  473. var v *extTypeTagFn
  474. for i := range o {
  475. v = &o[i]
  476. if v.tag == tag {
  477. return v
  478. }
  479. }
  480. return nil
  481. }
  482. type structFieldInfo struct {
  483. encName string // encode name
  484. // only one of 'i' or 'is' can be set. If 'i' is -1, then 'is' has been set.
  485. is []int // (recursive/embedded) field index in struct
  486. i int16 // field index in struct
  487. omitEmpty bool
  488. toArray bool // if field is _struct, is the toArray set?
  489. }
  490. // func (si *structFieldInfo) isZero() bool {
  491. // return si.encName == "" && len(si.is) == 0 && si.i == 0 && !si.omitEmpty && !si.toArray
  492. // }
  493. // rv returns the field of the struct.
  494. // If anonymous, it returns an Invalid
  495. func (si *structFieldInfo) field(v reflect.Value, update bool) (rv2 reflect.Value) {
  496. if si.i != -1 {
  497. v = v.Field(int(si.i))
  498. return v
  499. }
  500. // replicate FieldByIndex
  501. for _, x := range si.is {
  502. for v.Kind() == reflect.Ptr {
  503. if v.IsNil() {
  504. if !update {
  505. return
  506. }
  507. v.Set(reflect.New(v.Type().Elem()))
  508. }
  509. v = v.Elem()
  510. }
  511. v = v.Field(x)
  512. }
  513. return v
  514. }
  515. func (si *structFieldInfo) setToZeroValue(v reflect.Value) {
  516. if si.i != -1 {
  517. v = v.Field(int(si.i))
  518. v.Set(reflect.Zero(v.Type()))
  519. // v.Set(reflect.New(v.Type()).Elem())
  520. // v.Set(reflect.New(v.Type()))
  521. } else {
  522. // replicate FieldByIndex
  523. for _, x := range si.is {
  524. for v.Kind() == reflect.Ptr {
  525. if v.IsNil() {
  526. return
  527. }
  528. v = v.Elem()
  529. }
  530. v = v.Field(x)
  531. }
  532. v.Set(reflect.Zero(v.Type()))
  533. }
  534. }
  535. func parseStructFieldInfo(fname string, stag string) *structFieldInfo {
  536. // if fname == "" {
  537. // panic(noFieldNameToStructFieldInfoErr)
  538. // }
  539. si := structFieldInfo{
  540. encName: fname,
  541. }
  542. if stag != "" {
  543. for i, s := range strings.Split(stag, ",") {
  544. if i == 0 {
  545. if s != "" {
  546. si.encName = s
  547. }
  548. } else {
  549. if s == "omitempty" {
  550. si.omitEmpty = true
  551. } else if s == "toarray" {
  552. si.toArray = true
  553. }
  554. }
  555. }
  556. }
  557. // si.encNameBs = []byte(si.encName)
  558. return &si
  559. }
  560. type sfiSortedByEncName []*structFieldInfo
  561. func (p sfiSortedByEncName) Len() int {
  562. return len(p)
  563. }
  564. func (p sfiSortedByEncName) Less(i, j int) bool {
  565. return p[i].encName < p[j].encName
  566. }
  567. func (p sfiSortedByEncName) Swap(i, j int) {
  568. p[i], p[j] = p[j], p[i]
  569. }
  570. // typeInfo keeps information about each type referenced in the encode/decode sequence.
  571. //
  572. // During an encode/decode sequence, we work as below:
  573. // - If base is a built in type, en/decode base value
  574. // - If base is registered as an extension, en/decode base value
  575. // - If type is binary(M/Unm)arshaler, call Binary(M/Unm)arshal method
  576. // - If type is text(M/Unm)arshaler, call Text(M/Unm)arshal method
  577. // - Else decode appropriately based on the reflect.Kind
  578. type typeInfo struct {
  579. sfi []*structFieldInfo // sorted. Used when enc/dec struct to map.
  580. sfip []*structFieldInfo // unsorted. Used when enc/dec struct to array.
  581. rt reflect.Type
  582. rtid uintptr
  583. numMeth uint16 // number of methods
  584. // baseId gives pointer to the base reflect.Type, after deferencing
  585. // the pointers. E.g. base type of ***time.Time is time.Time.
  586. base reflect.Type
  587. baseId uintptr
  588. baseIndir int8 // number of indirections to get to base
  589. mbs bool // base type (T or *T) is a MapBySlice
  590. bm bool // base type (T or *T) is a binaryMarshaler
  591. bunm bool // base type (T or *T) is a binaryUnmarshaler
  592. bmIndir int8 // number of indirections to get to binaryMarshaler type
  593. bunmIndir int8 // number of indirections to get to binaryUnmarshaler type
  594. tm bool // base type (T or *T) is a textMarshaler
  595. tunm bool // base type (T or *T) is a textUnmarshaler
  596. tmIndir int8 // number of indirections to get to textMarshaler type
  597. tunmIndir int8 // number of indirections to get to textUnmarshaler type
  598. jm bool // base type (T or *T) is a jsonMarshaler
  599. junm bool // base type (T or *T) is a jsonUnmarshaler
  600. jmIndir int8 // number of indirections to get to jsonMarshaler type
  601. junmIndir int8 // number of indirections to get to jsonUnmarshaler type
  602. cs bool // base type (T or *T) is a Selfer
  603. csIndir int8 // number of indirections to get to Selfer type
  604. toArray bool // whether this (struct) type should be encoded as an array
  605. }
  606. func (ti *typeInfo) indexForEncName(name string) int {
  607. //tisfi := ti.sfi
  608. const binarySearchThreshold = 16
  609. if sfilen := len(ti.sfi); sfilen < binarySearchThreshold {
  610. // linear search. faster than binary search in my testing up to 16-field structs.
  611. for i, si := range ti.sfi {
  612. if si.encName == name {
  613. return i
  614. }
  615. }
  616. } else {
  617. // binary search. adapted from sort/search.go.
  618. h, i, j := 0, 0, sfilen
  619. for i < j {
  620. h = i + (j-i)/2
  621. if ti.sfi[h].encName < name {
  622. i = h + 1
  623. } else {
  624. j = h
  625. }
  626. }
  627. if i < sfilen && ti.sfi[i].encName == name {
  628. return i
  629. }
  630. }
  631. return -1
  632. }
  633. // TypeInfos caches typeInfo for each type on first inspection.
  634. //
  635. // It is configured with a set of tag keys, which are used to get
  636. // configuration for the type.
  637. type TypeInfos struct {
  638. infos map[uintptr]*typeInfo
  639. mu sync.RWMutex
  640. tags []string
  641. }
  642. // NewTypeInfos creates a TypeInfos given a set of struct tags keys.
  643. //
  644. // This allows users customize the struct tag keys which contain configuration
  645. // of their types.
  646. func NewTypeInfos(tags []string) *TypeInfos {
  647. return &TypeInfos{tags: tags, infos: make(map[uintptr]*typeInfo, 64)}
  648. }
  649. func (x *TypeInfos) structTag(t reflect.StructTag) (s string) {
  650. // check for tags: codec, json, in that order.
  651. // this allows seamless support for many configured structs.
  652. for _, x := range x.tags {
  653. s = t.Get(x)
  654. if s != "" {
  655. return s
  656. }
  657. }
  658. return
  659. }
  660. func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
  661. var ok bool
  662. x.mu.RLock()
  663. pti, ok = x.infos[rtid]
  664. x.mu.RUnlock()
  665. if ok {
  666. return
  667. }
  668. // do not hold lock while computing this.
  669. // it may lead to duplication, but that's ok.
  670. ti := typeInfo{rt: rt, rtid: rtid}
  671. ti.numMeth = uint16(rt.NumMethod())
  672. var indir int8
  673. if ok, indir = implementsIntf(rt, binaryMarshalerTyp); ok {
  674. ti.bm, ti.bmIndir = true, indir
  675. }
  676. if ok, indir = implementsIntf(rt, binaryUnmarshalerTyp); ok {
  677. ti.bunm, ti.bunmIndir = true, indir
  678. }
  679. if ok, indir = implementsIntf(rt, textMarshalerTyp); ok {
  680. ti.tm, ti.tmIndir = true, indir
  681. }
  682. if ok, indir = implementsIntf(rt, textUnmarshalerTyp); ok {
  683. ti.tunm, ti.tunmIndir = true, indir
  684. }
  685. if ok, indir = implementsIntf(rt, jsonMarshalerTyp); ok {
  686. ti.jm, ti.jmIndir = true, indir
  687. }
  688. if ok, indir = implementsIntf(rt, jsonUnmarshalerTyp); ok {
  689. ti.junm, ti.junmIndir = true, indir
  690. }
  691. if ok, indir = implementsIntf(rt, selferTyp); ok {
  692. ti.cs, ti.csIndir = true, indir
  693. }
  694. if ok, _ = implementsIntf(rt, mapBySliceTyp); ok {
  695. ti.mbs = true
  696. }
  697. pt := rt
  698. var ptIndir int8
  699. // for ; pt.Kind() == reflect.Ptr; pt, ptIndir = pt.Elem(), ptIndir+1 { }
  700. for pt.Kind() == reflect.Ptr {
  701. pt = pt.Elem()
  702. ptIndir++
  703. }
  704. if ptIndir == 0 {
  705. ti.base = rt
  706. ti.baseId = rtid
  707. } else {
  708. ti.base = pt
  709. ti.baseId = reflect.ValueOf(pt).Pointer()
  710. ti.baseIndir = ptIndir
  711. }
  712. if rt.Kind() == reflect.Struct {
  713. var siInfo *structFieldInfo
  714. if f, ok := rt.FieldByName(structInfoFieldName); ok {
  715. siInfo = parseStructFieldInfo(structInfoFieldName, x.structTag(f.Tag))
  716. ti.toArray = siInfo.toArray
  717. }
  718. sfip := make([]*structFieldInfo, 0, rt.NumField())
  719. x.rget(rt, nil, make(map[string]bool, 16), &sfip, siInfo)
  720. ti.sfip = make([]*structFieldInfo, len(sfip))
  721. ti.sfi = make([]*structFieldInfo, len(sfip))
  722. copy(ti.sfip, sfip)
  723. sort.Sort(sfiSortedByEncName(sfip))
  724. copy(ti.sfi, sfip)
  725. }
  726. // sfi = sfip
  727. x.mu.Lock()
  728. if pti, ok = x.infos[rtid]; !ok {
  729. pti = &ti
  730. x.infos[rtid] = pti
  731. }
  732. x.mu.Unlock()
  733. return
  734. }
  735. func (x *TypeInfos) rget(rt reflect.Type, indexstack []int, fnameToHastag map[string]bool,
  736. sfi *[]*structFieldInfo, siInfo *structFieldInfo,
  737. ) {
  738. for j := 0; j < rt.NumField(); j++ {
  739. f := rt.Field(j)
  740. fkind := f.Type.Kind()
  741. // skip if a func type, or is unexported, or structTag value == "-"
  742. if fkind == reflect.Func {
  743. continue
  744. }
  745. // if r1, _ := utf8.DecodeRuneInString(f.Name); r1 == utf8.RuneError || !unicode.IsUpper(r1) {
  746. if f.PkgPath != "" && !f.Anonymous { // unexported, not embedded
  747. continue
  748. }
  749. stag := x.structTag(f.Tag)
  750. if stag == "-" {
  751. continue
  752. }
  753. var si *structFieldInfo
  754. // if anonymous and no struct tag (or it's blank), and a struct (or pointer to struct), inline it.
  755. if f.Anonymous && fkind != reflect.Interface {
  756. doInline := stag == ""
  757. if !doInline {
  758. si = parseStructFieldInfo("", stag)
  759. doInline = si.encName == ""
  760. // doInline = si.isZero()
  761. }
  762. if doInline {
  763. ft := f.Type
  764. for ft.Kind() == reflect.Ptr {
  765. ft = ft.Elem()
  766. }
  767. if ft.Kind() == reflect.Struct {
  768. indexstack2 := make([]int, len(indexstack)+1, len(indexstack)+4)
  769. copy(indexstack2, indexstack)
  770. indexstack2[len(indexstack)] = j
  771. // indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
  772. x.rget(ft, indexstack2, fnameToHastag, sfi, siInfo)
  773. continue
  774. }
  775. }
  776. }
  777. // after the anonymous dance: if an unexported field, skip
  778. if f.PkgPath != "" { // unexported
  779. continue
  780. }
  781. // do not let fields with same name in embedded structs override field at higher level.
  782. // this must be done after anonymous check, to allow anonymous field
  783. // still include their child fields
  784. if _, ok := fnameToHastag[f.Name]; ok {
  785. continue
  786. }
  787. if f.Name == "" {
  788. panic(noFieldNameToStructFieldInfoErr)
  789. }
  790. if si == nil {
  791. si = parseStructFieldInfo(f.Name, stag)
  792. } else if si.encName == "" {
  793. si.encName = f.Name
  794. }
  795. // si.ikind = int(f.Type.Kind())
  796. if len(indexstack) == 0 {
  797. si.i = int16(j)
  798. } else {
  799. si.i = -1
  800. si.is = append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
  801. }
  802. if siInfo != nil {
  803. if siInfo.omitEmpty {
  804. si.omitEmpty = true
  805. }
  806. }
  807. *sfi = append(*sfi, si)
  808. fnameToHastag[f.Name] = stag != ""
  809. }
  810. }
  811. func panicToErr(err *error) {
  812. if recoverPanicToErr {
  813. if x := recover(); x != nil {
  814. //debug.PrintStack()
  815. panicValToErr(x, err)
  816. }
  817. }
  818. }
  819. // func doPanic(tag string, format string, params ...interface{}) {
  820. // params2 := make([]interface{}, len(params)+1)
  821. // params2[0] = tag
  822. // copy(params2[1:], params)
  823. // panic(fmt.Errorf("%s: "+format, params2...))
  824. // }
  825. func isImmutableKind(k reflect.Kind) (v bool) {
  826. return false ||
  827. k == reflect.Int ||
  828. k == reflect.Int8 ||
  829. k == reflect.Int16 ||
  830. k == reflect.Int32 ||
  831. k == reflect.Int64 ||
  832. k == reflect.Uint ||
  833. k == reflect.Uint8 ||
  834. k == reflect.Uint16 ||
  835. k == reflect.Uint32 ||
  836. k == reflect.Uint64 ||
  837. k == reflect.Uintptr ||
  838. k == reflect.Float32 ||
  839. k == reflect.Float64 ||
  840. k == reflect.Bool ||
  841. k == reflect.String
  842. }
  843. // these functions must be inlinable, and not call anybody
  844. type checkOverflow struct{}
  845. func (_ checkOverflow) Float32(f float64) (overflow bool) {
  846. if f < 0 {
  847. f = -f
  848. }
  849. return math.MaxFloat32 < f && f <= math.MaxFloat64
  850. }
  851. func (_ checkOverflow) Uint(v uint64, bitsize uint8) (overflow bool) {
  852. if bitsize == 0 || bitsize >= 64 || v == 0 {
  853. return
  854. }
  855. if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc {
  856. overflow = true
  857. }
  858. return
  859. }
  860. func (_ checkOverflow) Int(v int64, bitsize uint8) (overflow bool) {
  861. if bitsize == 0 || bitsize >= 64 || v == 0 {
  862. return
  863. }
  864. if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc {
  865. overflow = true
  866. }
  867. return
  868. }
  869. func (_ checkOverflow) SignedInt(v uint64) (i int64, overflow bool) {
  870. //e.g. -127 to 128 for int8
  871. pos := (v >> 63) == 0
  872. ui2 := v & 0x7fffffffffffffff
  873. if pos {
  874. if ui2 > math.MaxInt64 {
  875. overflow = true
  876. return
  877. }
  878. } else {
  879. if ui2 > math.MaxInt64-1 {
  880. overflow = true
  881. return
  882. }
  883. }
  884. i = int64(v)
  885. return
  886. }
  887. // ------------------ SORT -----------------
  888. func isNaN(f float64) bool { return f != f }
  889. // -----------------------
  890. type intSlice []int64
  891. type uintSlice []uint64
  892. type floatSlice []float64
  893. type boolSlice []bool
  894. type stringSlice []string
  895. type bytesSlice [][]byte
  896. func (p intSlice) Len() int { return len(p) }
  897. func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
  898. func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  899. func (p uintSlice) Len() int { return len(p) }
  900. func (p uintSlice) Less(i, j int) bool { return p[i] < p[j] }
  901. func (p uintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  902. func (p floatSlice) Len() int { return len(p) }
  903. func (p floatSlice) Less(i, j int) bool {
  904. return p[i] < p[j] || isNaN(p[i]) && !isNaN(p[j])
  905. }
  906. func (p floatSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  907. func (p stringSlice) Len() int { return len(p) }
  908. func (p stringSlice) Less(i, j int) bool { return p[i] < p[j] }
  909. func (p stringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  910. func (p bytesSlice) Len() int { return len(p) }
  911. func (p bytesSlice) Less(i, j int) bool { return bytes.Compare(p[i], p[j]) == -1 }
  912. func (p bytesSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  913. func (p boolSlice) Len() int { return len(p) }
  914. func (p boolSlice) Less(i, j int) bool { return !p[i] && p[j] }
  915. func (p boolSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  916. // ---------------------
  917. type intRv struct {
  918. v int64
  919. r reflect.Value
  920. }
  921. type intRvSlice []intRv
  922. type uintRv struct {
  923. v uint64
  924. r reflect.Value
  925. }
  926. type uintRvSlice []uintRv
  927. type floatRv struct {
  928. v float64
  929. r reflect.Value
  930. }
  931. type floatRvSlice []floatRv
  932. type boolRv struct {
  933. v bool
  934. r reflect.Value
  935. }
  936. type boolRvSlice []boolRv
  937. type stringRv struct {
  938. v string
  939. r reflect.Value
  940. }
  941. type stringRvSlice []stringRv
  942. type bytesRv struct {
  943. v []byte
  944. r reflect.Value
  945. }
  946. type bytesRvSlice []bytesRv
  947. func (p intRvSlice) Len() int { return len(p) }
  948. func (p intRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
  949. func (p intRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  950. func (p uintRvSlice) Len() int { return len(p) }
  951. func (p uintRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
  952. func (p uintRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  953. func (p floatRvSlice) Len() int { return len(p) }
  954. func (p floatRvSlice) Less(i, j int) bool {
  955. return p[i].v < p[j].v || isNaN(p[i].v) && !isNaN(p[j].v)
  956. }
  957. func (p floatRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  958. func (p stringRvSlice) Len() int { return len(p) }
  959. func (p stringRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
  960. func (p stringRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  961. func (p bytesRvSlice) Len() int { return len(p) }
  962. func (p bytesRvSlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
  963. func (p bytesRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  964. func (p boolRvSlice) Len() int { return len(p) }
  965. func (p boolRvSlice) Less(i, j int) bool { return !p[i].v && p[j].v }
  966. func (p boolRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  967. // -----------------
  968. type bytesI struct {
  969. v []byte
  970. i interface{}
  971. }
  972. type bytesISlice []bytesI
  973. func (p bytesISlice) Len() int { return len(p) }
  974. func (p bytesISlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
  975. func (p bytesISlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }