encode.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. import (
  5. "bytes"
  6. "encoding"
  7. "fmt"
  8. "io"
  9. "reflect"
  10. "sort"
  11. "sync"
  12. )
  13. const (
  14. defEncByteBufSize = 1 << 6 // 4:16, 6:64, 8:256, 10:1024
  15. )
  16. // AsSymbolFlag defines what should be encoded as symbols.
  17. type AsSymbolFlag uint8
  18. const (
  19. // AsSymbolDefault is default.
  20. // Currently, this means only encode struct field names as symbols.
  21. // The default is subject to change.
  22. AsSymbolDefault AsSymbolFlag = iota
  23. // AsSymbolAll means encode anything which could be a symbol as a symbol.
  24. AsSymbolAll = 0xfe
  25. // AsSymbolNone means do not encode anything as a symbol.
  26. AsSymbolNone = 1 << iota
  27. // AsSymbolMapStringKeys means encode keys in map[string]XXX as symbols.
  28. AsSymbolMapStringKeysFlag
  29. // AsSymbolStructFieldName means encode struct field names as symbols.
  30. AsSymbolStructFieldNameFlag
  31. )
  32. // encWriter abstracts writing to a byte array or to an io.Writer.
  33. type encWriter interface {
  34. writeb([]byte)
  35. writestr(string)
  36. writen1(byte)
  37. writen2(byte, byte)
  38. atEndOfEncode()
  39. }
  40. // encDriver abstracts the actual codec (binc vs msgpack, etc)
  41. type encDriver interface {
  42. IsBuiltinType(rt uintptr) bool
  43. EncodeBuiltin(rt uintptr, v interface{})
  44. EncodeNil()
  45. EncodeInt(i int64)
  46. EncodeUint(i uint64)
  47. EncodeBool(b bool)
  48. EncodeFloat32(f float32)
  49. EncodeFloat64(f float64)
  50. // encodeExtPreamble(xtag byte, length int)
  51. EncodeRawExt(re *RawExt, e *Encoder)
  52. EncodeExt(v interface{}, xtag uint64, ext Ext, e *Encoder)
  53. EncodeArrayStart(length int)
  54. EncodeMapStart(length int)
  55. EncodeEnd()
  56. EncodeString(c charEncoding, v string)
  57. EncodeSymbol(v string)
  58. EncodeStringBytes(c charEncoding, v []byte)
  59. //TODO
  60. //encBignum(f *big.Int)
  61. //encStringRunes(c charEncoding, v []rune)
  62. }
  63. type encDriverAsis interface {
  64. EncodeAsis(v []byte)
  65. }
  66. type encNoSeparator struct{}
  67. func (_ encNoSeparator) EncodeEnd() {}
  68. type encStructFieldBytesV struct {
  69. b []byte
  70. v reflect.Value
  71. }
  72. type encStructFieldBytesVslice []encStructFieldBytesV
  73. func (p encStructFieldBytesVslice) Len() int { return len(p) }
  74. func (p encStructFieldBytesVslice) Less(i, j int) bool { return bytes.Compare(p[i].b, p[j].b) == -1 }
  75. func (p encStructFieldBytesVslice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  76. type ioEncWriterWriter interface {
  77. WriteByte(c byte) error
  78. WriteString(s string) (n int, err error)
  79. Write(p []byte) (n int, err error)
  80. }
  81. type ioEncStringWriter interface {
  82. WriteString(s string) (n int, err error)
  83. }
  84. type EncodeOptions struct {
  85. // Encode a struct as an array, and not as a map
  86. StructToArray bool
  87. // Canonical representation means that encoding a value will always result in the same
  88. // sequence of bytes.
  89. //
  90. // This only affects maps, as the iteration order for maps is random.
  91. // In this case, the map keys will first be encoded into []byte, and then sorted,
  92. // before writing the sorted keys and the corresponding map values to the stream.
  93. Canonical bool
  94. // AsSymbols defines what should be encoded as symbols.
  95. //
  96. // Encoding as symbols can reduce the encoded size significantly.
  97. //
  98. // However, during decoding, each string to be encoded as a symbol must
  99. // be checked to see if it has been seen before. Consequently, encoding time
  100. // will increase if using symbols, because string comparisons has a clear cost.
  101. //
  102. // Sample values:
  103. // AsSymbolNone
  104. // AsSymbolAll
  105. // AsSymbolMapStringKeys
  106. // AsSymbolMapStringKeysFlag | AsSymbolStructFieldNameFlag
  107. AsSymbols AsSymbolFlag
  108. }
  109. // ---------------------------------------------
  110. type simpleIoEncWriterWriter struct {
  111. w io.Writer
  112. bw io.ByteWriter
  113. sw ioEncStringWriter
  114. }
  115. func (o *simpleIoEncWriterWriter) WriteByte(c byte) (err error) {
  116. if o.bw != nil {
  117. return o.bw.WriteByte(c)
  118. }
  119. _, err = o.w.Write([]byte{c})
  120. return
  121. }
  122. func (o *simpleIoEncWriterWriter) WriteString(s string) (n int, err error) {
  123. if o.sw != nil {
  124. return o.sw.WriteString(s)
  125. }
  126. // return o.w.Write([]byte(s))
  127. return o.w.Write(bytesView(s))
  128. }
  129. func (o *simpleIoEncWriterWriter) Write(p []byte) (n int, err error) {
  130. return o.w.Write(p)
  131. }
  132. // ----------------------------------------
  133. // ioEncWriter implements encWriter and can write to an io.Writer implementation
  134. type ioEncWriter struct {
  135. w ioEncWriterWriter
  136. // x [8]byte // temp byte array re-used internally for efficiency
  137. }
  138. func (z *ioEncWriter) writeb(bs []byte) {
  139. if len(bs) == 0 {
  140. return
  141. }
  142. n, err := z.w.Write(bs)
  143. if err != nil {
  144. panic(err)
  145. }
  146. if n != len(bs) {
  147. panic(fmt.Errorf("incorrect num bytes written. Expecting: %v, Wrote: %v", len(bs), n))
  148. }
  149. }
  150. func (z *ioEncWriter) writestr(s string) {
  151. n, err := z.w.WriteString(s)
  152. if err != nil {
  153. panic(err)
  154. }
  155. if n != len(s) {
  156. panic(fmt.Errorf("incorrect num bytes written. Expecting: %v, Wrote: %v", len(s), n))
  157. }
  158. }
  159. func (z *ioEncWriter) writen1(b byte) {
  160. if err := z.w.WriteByte(b); err != nil {
  161. panic(err)
  162. }
  163. }
  164. func (z *ioEncWriter) writen2(b1 byte, b2 byte) {
  165. z.writen1(b1)
  166. z.writen1(b2)
  167. }
  168. func (z *ioEncWriter) atEndOfEncode() {}
  169. // ----------------------------------------
  170. // bytesEncWriter implements encWriter and can write to an byte slice.
  171. // It is used by Marshal function.
  172. type bytesEncWriter struct {
  173. b []byte
  174. c int // cursor
  175. out *[]byte // write out on atEndOfEncode
  176. }
  177. func (z *bytesEncWriter) writeb(s []byte) {
  178. if len(s) > 0 {
  179. c := z.grow(len(s))
  180. copy(z.b[c:], s)
  181. }
  182. }
  183. func (z *bytesEncWriter) writestr(s string) {
  184. if len(s) > 0 {
  185. c := z.grow(len(s))
  186. copy(z.b[c:], s)
  187. }
  188. }
  189. func (z *bytesEncWriter) writen1(b1 byte) {
  190. c := z.grow(1)
  191. z.b[c] = b1
  192. }
  193. func (z *bytesEncWriter) writen2(b1 byte, b2 byte) {
  194. c := z.grow(2)
  195. z.b[c] = b1
  196. z.b[c+1] = b2
  197. }
  198. func (z *bytesEncWriter) atEndOfEncode() {
  199. *(z.out) = z.b[:z.c]
  200. }
  201. func (z *bytesEncWriter) grow(n int) (oldcursor int) {
  202. oldcursor = z.c
  203. z.c = oldcursor + n
  204. if z.c > len(z.b) {
  205. if z.c > cap(z.b) {
  206. // appendslice logic (if cap < 1024, *2, else *1.25): more expensive. many copy calls.
  207. // bytes.Buffer model (2*cap + n): much better
  208. // bs := make([]byte, 2*cap(z.b)+n)
  209. bs := make([]byte, growCap(cap(z.b), 1, n))
  210. copy(bs, z.b[:oldcursor])
  211. z.b = bs
  212. } else {
  213. z.b = z.b[:cap(z.b)]
  214. }
  215. }
  216. return
  217. }
  218. // ---------------------------------------------
  219. type encFnInfo struct {
  220. e *Encoder
  221. ti *typeInfo
  222. xfFn Ext
  223. xfTag uint64
  224. seq seqType
  225. }
  226. func (f *encFnInfo) builtin(rv reflect.Value) {
  227. f.e.e.EncodeBuiltin(f.ti.rtid, rv.Interface())
  228. }
  229. func (f *encFnInfo) rawExt(rv reflect.Value) {
  230. // rev := rv.Interface().(RawExt)
  231. // f.e.e.EncodeRawExt(&rev, f.e)
  232. var re *RawExt
  233. if rv.CanAddr() {
  234. re = rv.Addr().Interface().(*RawExt)
  235. } else {
  236. rev := rv.Interface().(RawExt)
  237. re = &rev
  238. }
  239. f.e.e.EncodeRawExt(re, f.e)
  240. }
  241. func (f *encFnInfo) ext(rv reflect.Value) {
  242. // if this is a struct|array and it was addressable, then pass the address directly (not the value)
  243. if k := rv.Kind(); (k == reflect.Struct || k == reflect.Array) && rv.CanAddr() {
  244. rv = rv.Addr()
  245. }
  246. f.e.e.EncodeExt(rv.Interface(), f.xfTag, f.xfFn, f.e)
  247. }
  248. func (f *encFnInfo) getValueForMarshalInterface(rv reflect.Value, indir int8) (v interface{}, proceed bool) {
  249. if indir == 0 {
  250. v = rv.Interface()
  251. } else if indir == -1 {
  252. // If a non-pointer was passed to Encode(), then that value is not addressable.
  253. // Take addr if addresable, else copy value to an addressable value.
  254. if rv.CanAddr() {
  255. v = rv.Addr().Interface()
  256. } else {
  257. rv2 := reflect.New(rv.Type())
  258. rv2.Elem().Set(rv)
  259. v = rv2.Interface()
  260. // fmt.Printf("rv.Type: %v, rv2.Type: %v, v: %v\n", rv.Type(), rv2.Type(), v)
  261. }
  262. } else {
  263. for j := int8(0); j < indir; j++ {
  264. if rv.IsNil() {
  265. f.e.e.EncodeNil()
  266. return
  267. }
  268. rv = rv.Elem()
  269. }
  270. v = rv.Interface()
  271. }
  272. return v, true
  273. }
  274. func (f *encFnInfo) selferMarshal(rv reflect.Value) {
  275. if v, proceed := f.getValueForMarshalInterface(rv, f.ti.csIndir); proceed {
  276. v.(Selfer).CodecEncodeSelf(f.e)
  277. }
  278. }
  279. func (f *encFnInfo) binaryMarshal(rv reflect.Value) {
  280. if v, proceed := f.getValueForMarshalInterface(rv, f.ti.bmIndir); proceed {
  281. bs, fnerr := v.(encoding.BinaryMarshaler).MarshalBinary()
  282. f.e.marshal(bs, fnerr, false, c_RAW)
  283. }
  284. }
  285. func (f *encFnInfo) textMarshal(rv reflect.Value) {
  286. if v, proceed := f.getValueForMarshalInterface(rv, f.ti.tmIndir); proceed {
  287. // debugf(">>>> encoding.TextMarshaler: %T", rv.Interface())
  288. bs, fnerr := v.(encoding.TextMarshaler).MarshalText()
  289. f.e.marshal(bs, fnerr, false, c_UTF8)
  290. }
  291. }
  292. func (f *encFnInfo) jsonMarshal(rv reflect.Value) {
  293. if v, proceed := f.getValueForMarshalInterface(rv, f.ti.jmIndir); proceed {
  294. bs, fnerr := v.(jsonMarshaler).MarshalJSON()
  295. f.e.marshal(bs, fnerr, true, c_UTF8)
  296. }
  297. }
  298. func (f *encFnInfo) kBool(rv reflect.Value) {
  299. f.e.e.EncodeBool(rv.Bool())
  300. }
  301. func (f *encFnInfo) kString(rv reflect.Value) {
  302. f.e.e.EncodeString(c_UTF8, rv.String())
  303. }
  304. func (f *encFnInfo) kFloat64(rv reflect.Value) {
  305. f.e.e.EncodeFloat64(rv.Float())
  306. }
  307. func (f *encFnInfo) kFloat32(rv reflect.Value) {
  308. f.e.e.EncodeFloat32(float32(rv.Float()))
  309. }
  310. func (f *encFnInfo) kInt(rv reflect.Value) {
  311. f.e.e.EncodeInt(rv.Int())
  312. }
  313. func (f *encFnInfo) kUint(rv reflect.Value) {
  314. f.e.e.EncodeUint(rv.Uint())
  315. }
  316. func (f *encFnInfo) kInvalid(rv reflect.Value) {
  317. f.e.e.EncodeNil()
  318. }
  319. func (f *encFnInfo) kErr(rv reflect.Value) {
  320. f.e.errorf("unsupported kind %s, for %#v", rv.Kind(), rv)
  321. }
  322. func (f *encFnInfo) kSlice(rv reflect.Value) {
  323. ti := f.ti
  324. // array may be non-addressable, so we have to manage with care
  325. // (don't call rv.Bytes, rv.Slice, etc).
  326. // E.g. type struct S{B [2]byte};
  327. // Encode(S{}) will bomb on "panic: slice of unaddressable array".
  328. if f.seq != seqTypeArray {
  329. if rv.IsNil() {
  330. f.e.e.EncodeNil()
  331. return
  332. }
  333. // If in this method, then there was no extension function defined.
  334. // So it's okay to treat as []byte.
  335. if ti.rtid == uint8SliceTypId {
  336. f.e.e.EncodeStringBytes(c_RAW, rv.Bytes())
  337. return
  338. }
  339. }
  340. rtelem := ti.rt.Elem()
  341. l := rv.Len()
  342. if rtelem.Kind() == reflect.Uint8 {
  343. switch f.seq {
  344. case seqTypeArray:
  345. // if l == 0 { f.e.e.encodeStringBytes(c_RAW, nil) } else
  346. if rv.CanAddr() {
  347. f.e.e.EncodeStringBytes(c_RAW, rv.Slice(0, l).Bytes())
  348. } else {
  349. var bs []byte
  350. if l <= cap(f.e.b) {
  351. bs = f.e.b[:l]
  352. } else {
  353. bs = make([]byte, l)
  354. }
  355. reflect.Copy(reflect.ValueOf(bs), rv)
  356. // TODO: Test that reflect.Copy works instead of manual one-by-one
  357. // for i := 0; i < l; i++ {
  358. // bs[i] = byte(rv.Index(i).Uint())
  359. // }
  360. f.e.e.EncodeStringBytes(c_RAW, bs)
  361. }
  362. case seqTypeSlice:
  363. f.e.e.EncodeStringBytes(c_RAW, rv.Bytes())
  364. case seqTypeChan:
  365. bs := f.e.b[:0]
  366. // do not use range, so that the number of elements encoded
  367. // does not change, and encoding does not hang waiting on someone to close chan.
  368. // for b := range rv.Interface().(<-chan byte) {
  369. // bs = append(bs, b)
  370. // }
  371. ch := rv.Interface().(<-chan byte)
  372. for i := 0; i < l; i++ {
  373. bs = append(bs, <-ch)
  374. }
  375. f.e.e.EncodeStringBytes(c_RAW, bs)
  376. }
  377. return
  378. }
  379. if ti.mbs {
  380. if l%2 == 1 {
  381. f.e.errorf("mapBySlice requires even slice length, but got %v", l)
  382. return
  383. }
  384. f.e.e.EncodeMapStart(l / 2)
  385. } else {
  386. f.e.e.EncodeArrayStart(l)
  387. }
  388. e := f.e
  389. if l > 0 {
  390. for rtelem.Kind() == reflect.Ptr {
  391. rtelem = rtelem.Elem()
  392. }
  393. // if kind is reflect.Interface, do not pre-determine the
  394. // encoding type, because preEncodeValue may break it down to
  395. // a concrete type and kInterface will bomb.
  396. var fn *encFn
  397. if rtelem.Kind() != reflect.Interface {
  398. rtelemid := reflect.ValueOf(rtelem).Pointer()
  399. fn = e.getEncFn(rtelemid, rtelem, true, true)
  400. }
  401. // TODO: Consider perf implication of encoding odd index values as symbols if type is string
  402. for j := 0; j < l; j++ {
  403. if f.seq == seqTypeChan {
  404. if rv2, ok2 := rv.Recv(); ok2 {
  405. e.encodeValue(rv2, fn)
  406. }
  407. } else {
  408. e.encodeValue(rv.Index(j), fn)
  409. }
  410. }
  411. }
  412. f.e.e.EncodeEnd()
  413. }
  414. func (f *encFnInfo) kStruct(rv reflect.Value) {
  415. fti := f.ti
  416. e := f.e
  417. tisfi := fti.sfip
  418. toMap := !(fti.toArray || e.h.StructToArray)
  419. newlen := len(fti.sfi)
  420. // Use sync.Pool to reduce allocating slices unnecessarily.
  421. // The cost of the occasional locking is less than the cost of locking.
  422. pool, poolv, fkvs := encStructPoolGet(newlen)
  423. // if toMap, use the sorted array. If toArray, use unsorted array (to match sequence in struct)
  424. if toMap {
  425. tisfi = fti.sfi
  426. }
  427. newlen = 0
  428. var kv encStructFieldKV
  429. for _, si := range tisfi {
  430. kv.v = si.field(rv, false)
  431. // if si.i != -1 {
  432. // rvals[newlen] = rv.Field(int(si.i))
  433. // } else {
  434. // rvals[newlen] = rv.FieldByIndex(si.is)
  435. // }
  436. if toMap {
  437. if si.omitEmpty && isEmptyValue(kv.v) {
  438. continue
  439. }
  440. kv.k = si.encName
  441. } else {
  442. // use the zero value.
  443. // if a reference or struct, set to nil (so you do not output too much)
  444. if si.omitEmpty && isEmptyValue(kv.v) {
  445. switch kv.v.Kind() {
  446. case reflect.Struct, reflect.Interface, reflect.Ptr, reflect.Array,
  447. reflect.Map, reflect.Slice:
  448. kv.v = reflect.Value{} //encode as nil
  449. }
  450. }
  451. }
  452. fkvs[newlen] = kv
  453. newlen++
  454. }
  455. // debugf(">>>> kStruct: newlen: %v", newlen)
  456. // sep := !e.be
  457. ee := f.e.e //don't dereference everytime
  458. if toMap {
  459. ee.EncodeMapStart(newlen)
  460. // asSymbols := e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
  461. asSymbols := e.h.AsSymbols == AsSymbolDefault || e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
  462. for j := 0; j < newlen; j++ {
  463. kv = fkvs[j]
  464. if asSymbols {
  465. ee.EncodeSymbol(kv.k)
  466. } else {
  467. ee.EncodeString(c_UTF8, kv.k)
  468. }
  469. e.encodeValue(kv.v, nil)
  470. }
  471. } else {
  472. ee.EncodeArrayStart(newlen)
  473. for j := 0; j < newlen; j++ {
  474. kv = fkvs[j]
  475. e.encodeValue(kv.v, nil)
  476. }
  477. }
  478. ee.EncodeEnd()
  479. // do not use defer. Instead, use explicit pool return at end of function.
  480. // defer has a cost we are trying to avoid.
  481. // If there is a panic and these slices are not returned, it is ok.
  482. if pool != nil {
  483. pool.Put(poolv)
  484. }
  485. }
  486. // func (f *encFnInfo) kPtr(rv reflect.Value) {
  487. // debugf(">>>>>>> ??? encode kPtr called - shouldn't get called")
  488. // if rv.IsNil() {
  489. // f.e.e.encodeNil()
  490. // return
  491. // }
  492. // f.e.encodeValue(rv.Elem())
  493. // }
  494. func (f *encFnInfo) kInterface(rv reflect.Value) {
  495. if rv.IsNil() {
  496. f.e.e.EncodeNil()
  497. return
  498. }
  499. f.e.encodeValue(rv.Elem(), nil)
  500. }
  501. func (f *encFnInfo) kMap(rv reflect.Value) {
  502. ee := f.e.e
  503. if rv.IsNil() {
  504. ee.EncodeNil()
  505. return
  506. }
  507. l := rv.Len()
  508. ee.EncodeMapStart(l)
  509. e := f.e
  510. if l == 0 {
  511. ee.EncodeEnd()
  512. return
  513. }
  514. var asSymbols bool
  515. // determine the underlying key and val encFn's for the map.
  516. // This eliminates some work which is done for each loop iteration i.e.
  517. // rv.Type(), ref.ValueOf(rt).Pointer(), then check map/list for fn.
  518. //
  519. // However, if kind is reflect.Interface, do not pre-determine the
  520. // encoding type, because preEncodeValue may break it down to
  521. // a concrete type and kInterface will bomb.
  522. var keyFn, valFn *encFn
  523. ti := f.ti
  524. rtkey := ti.rt.Key()
  525. rtval := ti.rt.Elem()
  526. rtkeyid := reflect.ValueOf(rtkey).Pointer()
  527. // keyTypeIsString := f.ti.rt.Key().Kind() == reflect.String
  528. var keyTypeIsString = rtkeyid == stringTypId
  529. if keyTypeIsString {
  530. asSymbols = e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0
  531. } else {
  532. for rtkey.Kind() == reflect.Ptr {
  533. rtkey = rtkey.Elem()
  534. }
  535. if rtkey.Kind() != reflect.Interface {
  536. rtkeyid = reflect.ValueOf(rtkey).Pointer()
  537. keyFn = e.getEncFn(rtkeyid, rtkey, true, true)
  538. }
  539. }
  540. for rtval.Kind() == reflect.Ptr {
  541. rtval = rtval.Elem()
  542. }
  543. if rtval.Kind() != reflect.Interface {
  544. rtvalid := reflect.ValueOf(rtval).Pointer()
  545. valFn = e.getEncFn(rtvalid, rtval, true, true)
  546. }
  547. mks := rv.MapKeys()
  548. // for j, lmks := 0, len(mks); j < lmks; j++ {
  549. if e.h.Canonical {
  550. // first encode each key to a []byte first, then sort them, then record
  551. // println(">>>>>>>> CANONICAL <<<<<<<<")
  552. var mksv []byte = make([]byte, 0, len(mks)*16) // temporary byte slice for the encoding
  553. e2 := NewEncoderBytes(&mksv, e.hh)
  554. mksbv := make([]encStructFieldBytesV, len(mks))
  555. for i, k := range mks {
  556. l := len(mksv)
  557. e2.MustEncode(k)
  558. mksbv[i].v = k
  559. mksbv[i].b = mksv[l:]
  560. // fmt.Printf(">>>>> %s\n", mksv[l:])
  561. }
  562. sort.Sort(encStructFieldBytesVslice(mksbv))
  563. for j := range mksbv {
  564. e.asis(mksbv[j].b)
  565. e.encodeValue(rv.MapIndex(mksbv[j].v), valFn)
  566. }
  567. } else {
  568. for j := range mks {
  569. if keyTypeIsString {
  570. if asSymbols {
  571. ee.EncodeSymbol(mks[j].String())
  572. } else {
  573. ee.EncodeString(c_UTF8, mks[j].String())
  574. }
  575. } else {
  576. e.encodeValue(mks[j], keyFn)
  577. }
  578. e.encodeValue(rv.MapIndex(mks[j]), valFn)
  579. }
  580. }
  581. ee.EncodeEnd()
  582. }
  583. // --------------------------------------------------
  584. // encFn encapsulates the captured variables and the encode function.
  585. // This way, we only do some calculations one times, and pass to the
  586. // code block that should be called (encapsulated in a function)
  587. // instead of executing the checks every time.
  588. type encFn struct {
  589. i encFnInfo
  590. f func(*encFnInfo, reflect.Value)
  591. }
  592. // --------------------------------------------------
  593. type encRtidFn struct {
  594. rtid uintptr
  595. fn encFn
  596. }
  597. // An Encoder writes an object to an output stream in the codec format.
  598. type Encoder struct {
  599. // hopefully, reduce derefencing cost by laying the encWriter inside the Encoder
  600. e encDriver
  601. // NOTE: Encoder shouldn't call it's write methods,
  602. // as the handler MAY need to do some coordination.
  603. w encWriter
  604. s []encRtidFn
  605. be bool // is binary encoding
  606. js bool // is json handle
  607. wi ioEncWriter
  608. wb bytesEncWriter
  609. h *BasicHandle
  610. as encDriverAsis
  611. hh Handle
  612. f map[uintptr]*encFn
  613. b [scratchByteArrayLen]byte
  614. }
  615. // NewEncoder returns an Encoder for encoding into an io.Writer.
  616. //
  617. // For efficiency, Users are encouraged to pass in a memory buffered writer
  618. // (eg bufio.Writer, bytes.Buffer).
  619. func NewEncoder(w io.Writer, h Handle) *Encoder {
  620. e := &Encoder{hh: h, h: h.getBasicHandle(), be: h.isBinary()}
  621. ww, ok := w.(ioEncWriterWriter)
  622. if !ok {
  623. sww := simpleIoEncWriterWriter{w: w}
  624. sww.bw, _ = w.(io.ByteWriter)
  625. sww.sw, _ = w.(ioEncStringWriter)
  626. ww = &sww
  627. //ww = bufio.NewWriterSize(w, defEncByteBufSize)
  628. }
  629. e.wi.w = ww
  630. e.w = &e.wi
  631. _, e.js = h.(*JsonHandle)
  632. e.e = h.newEncDriver(e)
  633. e.as, _ = e.e.(encDriverAsis)
  634. return e
  635. }
  636. // NewEncoderBytes returns an encoder for encoding directly and efficiently
  637. // into a byte slice, using zero-copying to temporary slices.
  638. //
  639. // It will potentially replace the output byte slice pointed to.
  640. // After encoding, the out parameter contains the encoded contents.
  641. func NewEncoderBytes(out *[]byte, h Handle) *Encoder {
  642. e := &Encoder{hh: h, h: h.getBasicHandle(), be: h.isBinary()}
  643. in := *out
  644. if in == nil {
  645. in = make([]byte, defEncByteBufSize)
  646. }
  647. e.wb.b, e.wb.out = in, out
  648. e.w = &e.wb
  649. _, e.js = h.(*JsonHandle)
  650. e.e = h.newEncDriver(e)
  651. e.as, _ = e.e.(encDriverAsis)
  652. return e
  653. }
  654. // Encode writes an object into a stream.
  655. //
  656. // Encoding can be configured via the struct tag for the fields.
  657. // The "codec" key in struct field's tag value is the key name,
  658. // followed by an optional comma and options.
  659. // Note that the "json" key is used in the absence of the "codec" key.
  660. //
  661. // To set an option on all fields (e.g. omitempty on all fields), you
  662. // can create a field called _struct, and set flags on it.
  663. //
  664. // Struct values "usually" encode as maps. Each exported struct field is encoded unless:
  665. // - the field's tag is "-", OR
  666. // - the field is empty (empty or the zero value) and its tag specifies the "omitempty" option.
  667. //
  668. // When encoding as a map, the first string in the tag (before the comma)
  669. // is the map key string to use when encoding.
  670. //
  671. // However, struct values may encode as arrays. This happens when:
  672. // - StructToArray Encode option is set, OR
  673. // - the tag on the _struct field sets the "toarray" option
  674. //
  675. // Values with types that implement MapBySlice are encoded as stream maps.
  676. //
  677. // The empty values (for omitempty option) are false, 0, any nil pointer
  678. // or interface value, and any array, slice, map, or string of length zero.
  679. //
  680. // Anonymous fields are encoded inline except:
  681. // - the struct tag specifies a replacement name (first value)
  682. // - the field is of an interface type
  683. //
  684. // Examples:
  685. //
  686. // // NOTE: 'json:' can be used as struct tag key, in place 'codec:' below.
  687. // type MyStruct struct {
  688. // _struct bool `codec:",omitempty"` //set omitempty for every field
  689. // Field1 string `codec:"-"` //skip this field
  690. // Field2 int `codec:"myName"` //Use key "myName" in encode stream
  691. // Field3 int32 `codec:",omitempty"` //use key "Field3". Omit if empty.
  692. // Field4 bool `codec:"f4,omitempty"` //use key "f4". Omit if empty.
  693. // io.Reader //use key "Reader".
  694. // MyStruct `codec:"my1" //use key "my1".
  695. // MyStruct //inline it
  696. // ...
  697. // }
  698. //
  699. // type MyStruct struct {
  700. // _struct bool `codec:",omitempty,toarray"` //set omitempty for every field
  701. // //and encode struct as an array
  702. // }
  703. //
  704. // The mode of encoding is based on the type of the value. When a value is seen:
  705. // - If a Selfer, call its CodecEncodeSelf method
  706. // - If an extension is registered for it, call that extension function
  707. // - If it implements encoding.(Binary|Text|JSON)Marshaler, call its Marshal(Binary|Text|JSON) method
  708. // - Else encode it based on its reflect.Kind
  709. //
  710. // Note that struct field names and keys in map[string]XXX will be treated as symbols.
  711. // Some formats support symbols (e.g. binc) and will properly encode the string
  712. // only once in the stream, and use a tag to refer to it thereafter.
  713. func (e *Encoder) Encode(v interface{}) (err error) {
  714. defer panicToErr(&err)
  715. e.encode(v)
  716. e.w.atEndOfEncode()
  717. return
  718. }
  719. // MustEncode is like Encode, but panics if unable to Encode.
  720. // This provides insight to the code location that triggered the error.
  721. func (e *Encoder) MustEncode(v interface{}) {
  722. e.encode(v)
  723. e.w.atEndOfEncode()
  724. }
  725. // comment out these (Must)Write methods. They were only put there to support cbor.
  726. // However, users already have access to the streams, and can write directly.
  727. //
  728. // // Write allows users write to the Encoder stream directly.
  729. // func (e *Encoder) Write(bs []byte) (err error) {
  730. // defer panicToErr(&err)
  731. // e.w.writeb(bs)
  732. // return
  733. // }
  734. // // MustWrite is like write, but panics if unable to Write.
  735. // func (e *Encoder) MustWrite(bs []byte) {
  736. // e.w.writeb(bs)
  737. // }
  738. func (e *Encoder) encode(iv interface{}) {
  739. // if ics, ok := iv.(Selfer); ok {
  740. // ics.CodecEncodeSelf(e)
  741. // return
  742. // }
  743. switch v := iv.(type) {
  744. case nil:
  745. e.e.EncodeNil()
  746. case Selfer:
  747. v.CodecEncodeSelf(e)
  748. case reflect.Value:
  749. e.encodeValue(v, nil)
  750. case string:
  751. e.e.EncodeString(c_UTF8, v)
  752. case bool:
  753. e.e.EncodeBool(v)
  754. case int:
  755. e.e.EncodeInt(int64(v))
  756. case int8:
  757. e.e.EncodeInt(int64(v))
  758. case int16:
  759. e.e.EncodeInt(int64(v))
  760. case int32:
  761. e.e.EncodeInt(int64(v))
  762. case int64:
  763. e.e.EncodeInt(v)
  764. case uint:
  765. e.e.EncodeUint(uint64(v))
  766. case uint8:
  767. e.e.EncodeUint(uint64(v))
  768. case uint16:
  769. e.e.EncodeUint(uint64(v))
  770. case uint32:
  771. e.e.EncodeUint(uint64(v))
  772. case uint64:
  773. e.e.EncodeUint(v)
  774. case float32:
  775. e.e.EncodeFloat32(v)
  776. case float64:
  777. e.e.EncodeFloat64(v)
  778. case []uint8:
  779. e.e.EncodeStringBytes(c_RAW, v)
  780. case *string:
  781. e.e.EncodeString(c_UTF8, *v)
  782. case *bool:
  783. e.e.EncodeBool(*v)
  784. case *int:
  785. e.e.EncodeInt(int64(*v))
  786. case *int8:
  787. e.e.EncodeInt(int64(*v))
  788. case *int16:
  789. e.e.EncodeInt(int64(*v))
  790. case *int32:
  791. e.e.EncodeInt(int64(*v))
  792. case *int64:
  793. e.e.EncodeInt(*v)
  794. case *uint:
  795. e.e.EncodeUint(uint64(*v))
  796. case *uint8:
  797. e.e.EncodeUint(uint64(*v))
  798. case *uint16:
  799. e.e.EncodeUint(uint64(*v))
  800. case *uint32:
  801. e.e.EncodeUint(uint64(*v))
  802. case *uint64:
  803. e.e.EncodeUint(*v)
  804. case *float32:
  805. e.e.EncodeFloat32(*v)
  806. case *float64:
  807. e.e.EncodeFloat64(*v)
  808. case *[]uint8:
  809. e.e.EncodeStringBytes(c_RAW, *v)
  810. default:
  811. // canonical mode is not supported for fastpath of maps (but is fine for slices)
  812. const checkCodecSelfer1 = true // in case T is passed, where *T is a Selfer, still checkCodecSelfer
  813. if e.h.Canonical {
  814. if !fastpathEncodeTypeSwitchSlice(iv, e) {
  815. e.encodeI(iv, false, checkCodecSelfer1)
  816. }
  817. } else {
  818. if !fastpathEncodeTypeSwitch(iv, e) {
  819. e.encodeI(iv, false, checkCodecSelfer1)
  820. }
  821. }
  822. }
  823. }
  824. func (e *Encoder) encodeI(iv interface{}, checkFastpath, checkCodecSelfer bool) {
  825. if rv, proceed := e.preEncodeValue(reflect.ValueOf(iv)); proceed {
  826. rt := rv.Type()
  827. rtid := reflect.ValueOf(rt).Pointer()
  828. fn := e.getEncFn(rtid, rt, checkFastpath, checkCodecSelfer)
  829. fn.f(&fn.i, rv)
  830. }
  831. }
  832. func (e *Encoder) preEncodeValue(rv reflect.Value) (rv2 reflect.Value, proceed bool) {
  833. LOOP:
  834. for {
  835. switch rv.Kind() {
  836. case reflect.Ptr, reflect.Interface:
  837. if rv.IsNil() {
  838. e.e.EncodeNil()
  839. return
  840. }
  841. rv = rv.Elem()
  842. continue LOOP
  843. case reflect.Slice, reflect.Map:
  844. if rv.IsNil() {
  845. e.e.EncodeNil()
  846. return
  847. }
  848. case reflect.Invalid, reflect.Func:
  849. e.e.EncodeNil()
  850. return
  851. }
  852. break
  853. }
  854. return rv, true
  855. }
  856. func (e *Encoder) encodeValue(rv reflect.Value, fn *encFn) {
  857. // if a valid fn is passed, it MUST BE for the dereferenced type of rv
  858. if rv, proceed := e.preEncodeValue(rv); proceed {
  859. if fn == nil {
  860. rt := rv.Type()
  861. rtid := reflect.ValueOf(rt).Pointer()
  862. fn = e.getEncFn(rtid, rt, true, true)
  863. }
  864. fn.f(&fn.i, rv)
  865. }
  866. }
  867. func (e *Encoder) getEncFn(rtid uintptr, rt reflect.Type, checkFastpath, checkCodecSelfer bool) (fn *encFn) {
  868. // rtid := reflect.ValueOf(rt).Pointer()
  869. var ok bool
  870. if useMapForCodecCache {
  871. fn, ok = e.f[rtid]
  872. } else {
  873. for i := range e.s {
  874. v := &(e.s[i])
  875. if v.rtid == rtid {
  876. fn, ok = &(v.fn), true
  877. break
  878. }
  879. }
  880. }
  881. if ok {
  882. return
  883. }
  884. if useMapForCodecCache {
  885. if e.f == nil {
  886. e.f = make(map[uintptr]*encFn, initCollectionCap)
  887. }
  888. fn = new(encFn)
  889. e.f[rtid] = fn
  890. } else {
  891. if e.s == nil {
  892. e.s = make([]encRtidFn, 0, initCollectionCap)
  893. }
  894. e.s = append(e.s, encRtidFn{rtid: rtid})
  895. fn = &(e.s[len(e.s)-1]).fn
  896. }
  897. ti := getTypeInfo(rtid, rt)
  898. fi := &(fn.i)
  899. fi.e = e
  900. fi.ti = ti
  901. if checkCodecSelfer && ti.cs {
  902. fn.f = (*encFnInfo).selferMarshal
  903. } else if rtid == rawExtTypId {
  904. fn.f = (*encFnInfo).rawExt
  905. } else if e.e.IsBuiltinType(rtid) {
  906. fn.f = (*encFnInfo).builtin
  907. } else if xfFn := e.h.getExt(rtid); xfFn != nil {
  908. fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
  909. fn.f = (*encFnInfo).ext
  910. } else if supportMarshalInterfaces && e.be && ti.bm {
  911. fn.f = (*encFnInfo).binaryMarshal
  912. } else if supportMarshalInterfaces && !e.be && e.js && ti.jm {
  913. //If JSON, we should check JSONMarshal before textMarshal
  914. fn.f = (*encFnInfo).jsonMarshal
  915. } else if supportMarshalInterfaces && !e.be && ti.tm {
  916. fn.f = (*encFnInfo).textMarshal
  917. } else {
  918. rk := rt.Kind()
  919. // if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) {
  920. if fastpathEnabled && checkFastpath && (rk == reflect.Slice || (rk == reflect.Map && !e.h.Canonical)) {
  921. if rt.PkgPath() == "" {
  922. if idx := fastpathAV.index(rtid); idx != -1 {
  923. fn.f = fastpathAV[idx].encfn
  924. }
  925. } else {
  926. ok = false
  927. // use mapping for underlying type if there
  928. var rtu reflect.Type
  929. if rk == reflect.Map {
  930. rtu = reflect.MapOf(rt.Key(), rt.Elem())
  931. } else {
  932. rtu = reflect.SliceOf(rt.Elem())
  933. }
  934. rtuid := reflect.ValueOf(rtu).Pointer()
  935. if idx := fastpathAV.index(rtuid); idx != -1 {
  936. xfnf := fastpathAV[idx].encfn
  937. xrt := fastpathAV[idx].rt
  938. fn.f = func(xf *encFnInfo, xrv reflect.Value) {
  939. xfnf(xf, xrv.Convert(xrt))
  940. }
  941. }
  942. }
  943. }
  944. if fn.f == nil {
  945. switch rk {
  946. case reflect.Bool:
  947. fn.f = (*encFnInfo).kBool
  948. case reflect.String:
  949. fn.f = (*encFnInfo).kString
  950. case reflect.Float64:
  951. fn.f = (*encFnInfo).kFloat64
  952. case reflect.Float32:
  953. fn.f = (*encFnInfo).kFloat32
  954. case reflect.Int, reflect.Int8, reflect.Int64, reflect.Int32, reflect.Int16:
  955. fn.f = (*encFnInfo).kInt
  956. case reflect.Uint8, reflect.Uint64, reflect.Uint, reflect.Uint32, reflect.Uint16, reflect.Uintptr:
  957. fn.f = (*encFnInfo).kUint
  958. case reflect.Invalid:
  959. fn.f = (*encFnInfo).kInvalid
  960. case reflect.Chan:
  961. fi.seq = seqTypeChan
  962. fn.f = (*encFnInfo).kSlice
  963. case reflect.Slice:
  964. fi.seq = seqTypeSlice
  965. fn.f = (*encFnInfo).kSlice
  966. case reflect.Array:
  967. fi.seq = seqTypeArray
  968. fn.f = (*encFnInfo).kSlice
  969. case reflect.Struct:
  970. fn.f = (*encFnInfo).kStruct
  971. // case reflect.Ptr:
  972. // fn.f = (*encFnInfo).kPtr
  973. case reflect.Interface:
  974. fn.f = (*encFnInfo).kInterface
  975. case reflect.Map:
  976. fn.f = (*encFnInfo).kMap
  977. default:
  978. fn.f = (*encFnInfo).kErr
  979. }
  980. }
  981. }
  982. return
  983. }
  984. func (e *Encoder) marshal(bs []byte, fnerr error, asis bool, c charEncoding) {
  985. if fnerr != nil {
  986. panic(fnerr)
  987. }
  988. if bs == nil {
  989. e.e.EncodeNil()
  990. } else if asis {
  991. e.asis(bs)
  992. } else {
  993. e.e.EncodeStringBytes(c, bs)
  994. }
  995. }
  996. func (e *Encoder) asis(v []byte) {
  997. if e.as == nil {
  998. e.w.writeb(v)
  999. } else {
  1000. e.as.EncodeAsis(v)
  1001. }
  1002. }
  1003. func (e *Encoder) errorf(format string, params ...interface{}) {
  1004. err := fmt.Errorf(format, params...)
  1005. panic(err)
  1006. }
  1007. // ----------------------------------------
  1008. type encStructFieldKV struct {
  1009. k string
  1010. v reflect.Value
  1011. }
  1012. const encStructPoolLen = 5
  1013. // encStructPool is an array of sync.Pool.
  1014. // Each element of the array pools one of encStructPool(8|16|32|64).
  1015. // It allows the re-use of slices up to 64 in length.
  1016. // A performance cost of encoding structs was collecting
  1017. // which values were empty and should be omitted.
  1018. // We needed slices of reflect.Value and string to collect them.
  1019. // This shared pool reduces the amount of unnecessary creation we do.
  1020. // The cost is that of locking sometimes, but sync.Pool is efficient
  1021. // enough to reduce thread contention.
  1022. var encStructPool [encStructPoolLen]sync.Pool
  1023. func init() {
  1024. encStructPool[0].New = func() interface{} { return new([8]encStructFieldKV) }
  1025. encStructPool[1].New = func() interface{} { return new([16]encStructFieldKV) }
  1026. encStructPool[2].New = func() interface{} { return new([32]encStructFieldKV) }
  1027. encStructPool[3].New = func() interface{} { return new([64]encStructFieldKV) }
  1028. encStructPool[4].New = func() interface{} { return new([128]encStructFieldKV) }
  1029. }
  1030. func encStructPoolGet(newlen int) (p *sync.Pool, v interface{}, s []encStructFieldKV) {
  1031. // if encStructPoolLen != 5 { // constant chec, so removed at build time.
  1032. // panic(errors.New("encStructPoolLen must be equal to 4")) // defensive, in case it is changed
  1033. // }
  1034. // idxpool := newlen / 8
  1035. // if pool == nil {
  1036. // fkvs = make([]encStructFieldKV, newlen)
  1037. // } else {
  1038. // poolv = pool.Get()
  1039. // switch vv := poolv.(type) {
  1040. // case *[8]encStructFieldKV:
  1041. // fkvs = vv[:newlen]
  1042. // case *[16]encStructFieldKV:
  1043. // fkvs = vv[:newlen]
  1044. // case *[32]encStructFieldKV:
  1045. // fkvs = vv[:newlen]
  1046. // case *[64]encStructFieldKV:
  1047. // fkvs = vv[:newlen]
  1048. // case *[128]encStructFieldKV:
  1049. // fkvs = vv[:newlen]
  1050. // }
  1051. // }
  1052. if newlen <= 8 {
  1053. p = &encStructPool[0]
  1054. v = p.Get()
  1055. s = v.(*[8]encStructFieldKV)[:newlen]
  1056. } else if newlen <= 16 {
  1057. p = &encStructPool[1]
  1058. v = p.Get()
  1059. s = v.(*[16]encStructFieldKV)[:newlen]
  1060. } else if newlen <= 32 {
  1061. p = &encStructPool[2]
  1062. v = p.Get()
  1063. s = v.(*[32]encStructFieldKV)[:newlen]
  1064. } else if newlen <= 64 {
  1065. p = &encStructPool[3]
  1066. v = p.Get()
  1067. s = v.(*[64]encStructFieldKV)[:newlen]
  1068. } else if newlen <= 128 {
  1069. p = &encStructPool[4]
  1070. v = p.Get()
  1071. s = v.(*[128]encStructFieldKV)[:newlen]
  1072. } else {
  1073. s = make([]encStructFieldKV, newlen)
  1074. }
  1075. return
  1076. }
  1077. // ----------------------------------------
  1078. // func encErr(format string, params ...interface{}) {
  1079. // doPanic(msgTagEnc, format, params...)
  1080. // }