encode.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. import (
  5. "encoding"
  6. "io"
  7. "reflect"
  8. )
  9. const (
  10. // Some tagging information for error messages.
  11. msgTagEnc = "codec.encoder"
  12. defEncByteBufSize = 1 << 6 // 4:16, 6:64, 8:256, 10:1024
  13. // maxTimeSecs32 = math.MaxInt32 / 60 / 24 / 366
  14. )
  15. // AsSymbolFlag defines what should be encoded as symbols.
  16. type AsSymbolFlag uint8
  17. const (
  18. // AsSymbolDefault is default.
  19. // Currently, this means only encode struct field names as symbols.
  20. // The default is subject to change.
  21. AsSymbolDefault AsSymbolFlag = iota
  22. // AsSymbolAll means encode anything which could be a symbol as a symbol.
  23. AsSymbolAll = 0xfe
  24. // AsSymbolNone means do not encode anything as a symbol.
  25. AsSymbolNone = 1 << iota
  26. // AsSymbolMapStringKeys means encode keys in map[string]XXX as symbols.
  27. AsSymbolMapStringKeysFlag
  28. // AsSymbolStructFieldName means encode struct field names as symbols.
  29. AsSymbolStructFieldNameFlag
  30. )
  31. // fastpathsEnc holds the rtid (reflect.Type Pointer) to fast encode function for a selected slice/map type.
  32. var fastpathsEnc = make(map[uintptr]func(*encFnInfo, reflect.Value))
  33. // encWriter abstracts writing to a byte array or to an io.Writer.
  34. type encWriter interface {
  35. writeUint16(uint16)
  36. writeUint32(uint32)
  37. writeUint64(uint64)
  38. writeb([]byte)
  39. writestr(string)
  40. writen1(byte)
  41. writen2(byte, byte)
  42. atEndOfEncode()
  43. }
  44. // encDriver abstracts the actual codec (binc vs msgpack, etc)
  45. type encDriver interface {
  46. isBuiltinType(rt uintptr) bool
  47. encodeBuiltin(rt uintptr, v interface{})
  48. encodeNil()
  49. encodeInt(i int64)
  50. encodeUint(i uint64)
  51. encodeBool(b bool)
  52. encodeFloat32(f float32)
  53. encodeFloat64(f float64)
  54. // encodeExtPreamble(xtag byte, length int)
  55. encodeRawExt(re *RawExt, e *Encoder)
  56. encodeExt(rv reflect.Value, xtag uint64, ext Ext, e *Encoder)
  57. encodeArrayStart(length int)
  58. encodeArrayEnd()
  59. encodeArrayEntrySeparator()
  60. encodeMapStart(length int)
  61. encodeMapEnd()
  62. encodeMapEntrySeparator()
  63. encodeMapKVSeparator()
  64. encodeString(c charEncoding, v string)
  65. encodeSymbol(v string)
  66. encodeStringBytes(c charEncoding, v []byte)
  67. //TODO
  68. //encBignum(f *big.Int)
  69. //encStringRunes(c charEncoding, v []rune)
  70. }
  71. type encNoMapArrayEnd struct{}
  72. func (_ encNoMapArrayEnd) encodeMapEnd() {}
  73. func (_ encNoMapArrayEnd) encodeArrayEnd() {}
  74. type encNoMapArraySeparator struct{}
  75. func (_ encNoMapArraySeparator) encodeArrayEntrySeparator() {}
  76. func (_ encNoMapArraySeparator) encodeMapEntrySeparator() {}
  77. func (_ encNoMapArraySeparator) encodeMapKVSeparator() {}
  78. type ioEncWriterWriter interface {
  79. WriteByte(c byte) error
  80. WriteString(s string) (n int, err error)
  81. Write(p []byte) (n int, err error)
  82. }
  83. type ioEncStringWriter interface {
  84. WriteString(s string) (n int, err error)
  85. }
  86. type EncodeOptions struct {
  87. // Encode a struct as an array, and not as a map.
  88. StructToArray bool
  89. // AsSymbols defines what should be encoded as symbols.
  90. //
  91. // Encoding as symbols can reduce the encoded size significantly.
  92. //
  93. // However, during decoding, each string to be encoded as a symbol must
  94. // be checked to see if it has been seen before. Consequently, encoding time
  95. // will increase if using symbols, because string comparisons has a clear cost.
  96. //
  97. // Sample values:
  98. // AsSymbolNone
  99. // AsSymbolAll
  100. // AsSymbolMapStringKeys
  101. // AsSymbolMapStringKeysFlag | AsSymbolStructFieldNameFlag
  102. AsSymbols AsSymbolFlag
  103. }
  104. // ---------------------------------------------
  105. type simpleIoEncWriterWriter struct {
  106. w io.Writer
  107. bw io.ByteWriter
  108. sw ioEncStringWriter
  109. }
  110. func (o *simpleIoEncWriterWriter) WriteByte(c byte) (err error) {
  111. if o.bw != nil {
  112. return o.bw.WriteByte(c)
  113. }
  114. _, err = o.w.Write([]byte{c})
  115. return
  116. }
  117. func (o *simpleIoEncWriterWriter) WriteString(s string) (n int, err error) {
  118. if o.sw != nil {
  119. return o.sw.WriteString(s)
  120. }
  121. return o.w.Write([]byte(s))
  122. }
  123. func (o *simpleIoEncWriterWriter) Write(p []byte) (n int, err error) {
  124. return o.w.Write(p)
  125. }
  126. // ----------------------------------------
  127. // ioEncWriter implements encWriter and can write to an io.Writer implementation
  128. type ioEncWriter struct {
  129. w ioEncWriterWriter
  130. x [8]byte // temp byte array re-used internally for efficiency
  131. }
  132. func (z *ioEncWriter) writeUint16(v uint16) {
  133. bigen.PutUint16(z.x[:2], v)
  134. z.writeb(z.x[:2])
  135. }
  136. func (z *ioEncWriter) writeUint32(v uint32) {
  137. bigen.PutUint32(z.x[:4], v)
  138. z.writeb(z.x[:4])
  139. }
  140. func (z *ioEncWriter) writeUint64(v uint64) {
  141. bigen.PutUint64(z.x[:8], v)
  142. z.writeb(z.x[:8])
  143. }
  144. func (z *ioEncWriter) writeb(bs []byte) {
  145. if len(bs) == 0 {
  146. return
  147. }
  148. n, err := z.w.Write(bs)
  149. if err != nil {
  150. panic(err)
  151. }
  152. if n != len(bs) {
  153. encErr("incorrect num bytes written. Expecting: %v, Wrote: %v", len(bs), n)
  154. }
  155. }
  156. func (z *ioEncWriter) writestr(s string) {
  157. n, err := z.w.WriteString(s)
  158. if err != nil {
  159. panic(err)
  160. }
  161. if n != len(s) {
  162. encErr("incorrect num bytes written. Expecting: %v, Wrote: %v", len(s), n)
  163. }
  164. }
  165. func (z *ioEncWriter) writen1(b byte) {
  166. if err := z.w.WriteByte(b); err != nil {
  167. panic(err)
  168. }
  169. }
  170. func (z *ioEncWriter) writen2(b1 byte, b2 byte) {
  171. z.writen1(b1)
  172. z.writen1(b2)
  173. }
  174. func (z *ioEncWriter) atEndOfEncode() {}
  175. // ----------------------------------------
  176. // bytesEncWriter implements encWriter and can write to an byte slice.
  177. // It is used by Marshal function.
  178. type bytesEncWriter struct {
  179. b []byte
  180. c int // cursor
  181. out *[]byte // write out on atEndOfEncode
  182. }
  183. func (z *bytesEncWriter) writeUint16(v uint16) {
  184. c := z.grow(2)
  185. z.b[c] = byte(v >> 8)
  186. z.b[c+1] = byte(v)
  187. }
  188. func (z *bytesEncWriter) writeUint32(v uint32) {
  189. c := z.grow(4)
  190. z.b[c] = byte(v >> 24)
  191. z.b[c+1] = byte(v >> 16)
  192. z.b[c+2] = byte(v >> 8)
  193. z.b[c+3] = byte(v)
  194. }
  195. func (z *bytesEncWriter) writeUint64(v uint64) {
  196. c := z.grow(8)
  197. z.b[c] = byte(v >> 56)
  198. z.b[c+1] = byte(v >> 48)
  199. z.b[c+2] = byte(v >> 40)
  200. z.b[c+3] = byte(v >> 32)
  201. z.b[c+4] = byte(v >> 24)
  202. z.b[c+5] = byte(v >> 16)
  203. z.b[c+6] = byte(v >> 8)
  204. z.b[c+7] = byte(v)
  205. }
  206. func (z *bytesEncWriter) writeb(s []byte) {
  207. if len(s) == 0 {
  208. return
  209. }
  210. c := z.grow(len(s))
  211. copy(z.b[c:], s)
  212. }
  213. func (z *bytesEncWriter) writestr(s string) {
  214. c := z.grow(len(s))
  215. copy(z.b[c:], s)
  216. }
  217. func (z *bytesEncWriter) writen1(b1 byte) {
  218. c := z.grow(1)
  219. z.b[c] = b1
  220. }
  221. func (z *bytesEncWriter) writen2(b1 byte, b2 byte) {
  222. c := z.grow(2)
  223. z.b[c] = b1
  224. z.b[c+1] = b2
  225. }
  226. func (z *bytesEncWriter) atEndOfEncode() {
  227. *(z.out) = z.b[:z.c]
  228. }
  229. func (z *bytesEncWriter) grow(n int) (oldcursor int) {
  230. oldcursor = z.c
  231. z.c = oldcursor + n
  232. if z.c > cap(z.b) {
  233. // Tried using appendslice logic: (if cap < 1024, *2, else *1.25).
  234. // However, it was too expensive, causing too many iterations of copy.
  235. // Using bytes.Buffer model was much better (2*cap + n)
  236. bs := make([]byte, 2*cap(z.b)+n)
  237. copy(bs, z.b[:oldcursor])
  238. z.b = bs
  239. } else if z.c > len(z.b) {
  240. z.b = z.b[:cap(z.b)]
  241. }
  242. return
  243. }
  244. // ---------------------------------------------
  245. type encFnInfo struct {
  246. ti *typeInfo
  247. e *Encoder
  248. ee encDriver
  249. xfFn Ext
  250. xfTag uint64
  251. array bool
  252. }
  253. func (f *encFnInfo) builtin(rv reflect.Value) {
  254. f.ee.encodeBuiltin(f.ti.rtid, rv.Interface())
  255. }
  256. func (f *encFnInfo) rawExt(rv reflect.Value) {
  257. f.ee.encodeRawExt(rv.Interface().(*RawExt), f.e)
  258. }
  259. func (f *encFnInfo) ext(rv reflect.Value) {
  260. f.ee.encodeExt(rv, f.xfTag, f.xfFn, f.e)
  261. }
  262. func (f *encFnInfo) binaryMarshal(rv reflect.Value) {
  263. var bm encoding.BinaryMarshaler
  264. if f.ti.bmIndir == 0 {
  265. bm = rv.Interface().(encoding.BinaryMarshaler)
  266. } else if f.ti.bmIndir == -1 {
  267. bm = rv.Addr().Interface().(encoding.BinaryMarshaler)
  268. } else {
  269. for j, k := int8(0), f.ti.bmIndir; j < k; j++ {
  270. if rv.IsNil() {
  271. f.ee.encodeNil()
  272. return
  273. }
  274. rv = rv.Elem()
  275. }
  276. bm = rv.Interface().(encoding.BinaryMarshaler)
  277. }
  278. // debugf(">>>> encoding.BinaryMarshaler: %T", rv.Interface())
  279. bs, fnerr := bm.MarshalBinary()
  280. if fnerr != nil {
  281. panic(fnerr)
  282. }
  283. if bs == nil {
  284. f.ee.encodeNil()
  285. } else {
  286. f.ee.encodeStringBytes(c_RAW, bs)
  287. }
  288. }
  289. func (f *encFnInfo) textMarshal(rv reflect.Value) {
  290. var tm encoding.TextMarshaler
  291. if f.ti.tmIndir == 0 {
  292. tm = rv.Interface().(encoding.TextMarshaler)
  293. } else if f.ti.tmIndir == -1 {
  294. tm = rv.Addr().Interface().(encoding.TextMarshaler)
  295. } else {
  296. for j, k := int8(0), f.ti.tmIndir; j < k; j++ {
  297. if rv.IsNil() {
  298. f.ee.encodeNil()
  299. return
  300. }
  301. rv = rv.Elem()
  302. }
  303. tm = rv.Interface().(encoding.TextMarshaler)
  304. }
  305. // debugf(">>>> encoding.TextMarshaler: %T", rv.Interface())
  306. bs, fnerr := tm.MarshalText()
  307. if fnerr != nil {
  308. panic(fnerr)
  309. }
  310. if bs == nil {
  311. f.ee.encodeNil()
  312. } else {
  313. f.ee.encodeStringBytes(c_UTF8, bs)
  314. }
  315. }
  316. func (f *encFnInfo) kBool(rv reflect.Value) {
  317. f.ee.encodeBool(rv.Bool())
  318. }
  319. func (f *encFnInfo) kString(rv reflect.Value) {
  320. f.ee.encodeString(c_UTF8, rv.String())
  321. }
  322. func (f *encFnInfo) kFloat64(rv reflect.Value) {
  323. f.ee.encodeFloat64(rv.Float())
  324. }
  325. func (f *encFnInfo) kFloat32(rv reflect.Value) {
  326. f.ee.encodeFloat32(float32(rv.Float()))
  327. }
  328. func (f *encFnInfo) kInt(rv reflect.Value) {
  329. f.ee.encodeInt(rv.Int())
  330. }
  331. func (f *encFnInfo) kUint(rv reflect.Value) {
  332. f.ee.encodeUint(rv.Uint())
  333. }
  334. func (f *encFnInfo) kInvalid(rv reflect.Value) {
  335. f.ee.encodeNil()
  336. }
  337. func (f *encFnInfo) kErr(rv reflect.Value) {
  338. encErr("unsupported kind %s, for %#v", rv.Kind(), rv)
  339. }
  340. func (f *encFnInfo) kSlice(rv reflect.Value) {
  341. // array may be non-addressable, so we have to manage with care (don't call rv.Bytes, rv.Slice, etc).
  342. // E.g. type struct S{B [2]byte}; Encode(S{}) will bomb on "panic: slice of unaddressable array".
  343. if !f.array {
  344. if rv.IsNil() {
  345. f.ee.encodeNil()
  346. return
  347. }
  348. // If in this method, then there was no extension function defined.
  349. // So it's okay to treat as []byte.
  350. if f.ti.rtid == uint8SliceTypId {
  351. f.ee.encodeStringBytes(c_RAW, rv.Bytes())
  352. return
  353. }
  354. }
  355. rtelem := f.ti.rt.Elem()
  356. l := rv.Len()
  357. if rtelem.Kind() == reflect.Uint8 {
  358. if f.array {
  359. // if l == 0 { f.ee.encodeStringBytes(c_RAW, nil) } else
  360. if rv.CanAddr() {
  361. f.ee.encodeStringBytes(c_RAW, rv.Slice(0, l).Bytes())
  362. } else {
  363. bs := make([]byte, l)
  364. for i := 0; i < l; i++ {
  365. bs[i] = byte(rv.Index(i).Uint())
  366. }
  367. f.ee.encodeStringBytes(c_RAW, bs)
  368. }
  369. } else {
  370. f.ee.encodeStringBytes(c_RAW, rv.Bytes())
  371. }
  372. return
  373. }
  374. if f.ti.mbs {
  375. if l%2 == 1 {
  376. encErr("mapBySlice requires even slice length, but got %v", l)
  377. }
  378. f.ee.encodeMapStart(l / 2)
  379. } else {
  380. f.ee.encodeArrayStart(l)
  381. }
  382. if l > 0 {
  383. for rtelem.Kind() == reflect.Ptr {
  384. rtelem = rtelem.Elem()
  385. }
  386. fn := f.e.getEncFn(rtelem)
  387. for j := 0; j < l; j++ {
  388. // TODO: Consider perf implication of encoding odd index values as symbols if type is string
  389. if j > 0 {
  390. if f.ti.mbs {
  391. if j%2 == 0 {
  392. f.ee.encodeMapEntrySeparator()
  393. } else {
  394. f.ee.encodeMapKVSeparator()
  395. }
  396. } else {
  397. f.ee.encodeArrayEntrySeparator()
  398. }
  399. }
  400. f.e.encodeValue(rv.Index(j), fn)
  401. }
  402. }
  403. if f.ti.mbs {
  404. f.ee.encodeMapEnd()
  405. } else {
  406. f.ee.encodeArrayEnd()
  407. }
  408. }
  409. func (f *encFnInfo) kStruct(rv reflect.Value) {
  410. fti := f.ti
  411. newlen := len(fti.sfi)
  412. rvals := make([]reflect.Value, newlen)
  413. var encnames []string
  414. e := f.e
  415. tisfi := fti.sfip
  416. toMap := !(fti.toArray || e.h.StructToArray)
  417. // if toMap, use the sorted array. If toArray, use unsorted array (to match sequence in struct)
  418. if toMap {
  419. tisfi = fti.sfi
  420. encnames = make([]string, newlen)
  421. }
  422. newlen = 0
  423. for _, si := range tisfi {
  424. if si.i != -1 {
  425. rvals[newlen] = rv.Field(int(si.i))
  426. } else {
  427. rvals[newlen] = rv.FieldByIndex(si.is)
  428. }
  429. if toMap {
  430. if si.omitEmpty && isEmptyValue(rvals[newlen]) {
  431. continue
  432. }
  433. encnames[newlen] = si.encName
  434. } else {
  435. if si.omitEmpty && isEmptyValue(rvals[newlen]) {
  436. rvals[newlen] = reflect.Value{} //encode as nil
  437. }
  438. }
  439. newlen++
  440. }
  441. // debugf(">>>> kStruct: newlen: %v", newlen)
  442. if toMap {
  443. ee := f.ee //don't dereference everytime
  444. ee.encodeMapStart(newlen)
  445. // asSymbols := e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
  446. asSymbols := e.h.AsSymbols == AsSymbolDefault || e.h.AsSymbols&AsSymbolStructFieldNameFlag != 0
  447. for j := 0; j < newlen; j++ {
  448. if j > 0 {
  449. ee.encodeMapEntrySeparator()
  450. }
  451. if asSymbols {
  452. ee.encodeSymbol(encnames[j])
  453. } else {
  454. ee.encodeString(c_UTF8, encnames[j])
  455. }
  456. ee.encodeMapKVSeparator()
  457. e.encodeValue(rvals[j], encFn{})
  458. }
  459. ee.encodeMapEnd()
  460. } else {
  461. f.ee.encodeArrayStart(newlen)
  462. for j := 0; j < newlen; j++ {
  463. if j > 0 {
  464. f.ee.encodeArrayEntrySeparator()
  465. }
  466. e.encodeValue(rvals[j], encFn{})
  467. }
  468. f.ee.encodeArrayEnd()
  469. }
  470. }
  471. // func (f *encFnInfo) kPtr(rv reflect.Value) {
  472. // debugf(">>>>>>> ??? encode kPtr called - shouldn't get called")
  473. // if rv.IsNil() {
  474. // f.ee.encodeNil()
  475. // return
  476. // }
  477. // f.e.encodeValue(rv.Elem())
  478. // }
  479. func (f *encFnInfo) kInterface(rv reflect.Value) {
  480. if rv.IsNil() {
  481. f.ee.encodeNil()
  482. return
  483. }
  484. f.e.encodeValue(rv.Elem(), encFn{})
  485. }
  486. func (f *encFnInfo) kMap(rv reflect.Value) {
  487. if rv.IsNil() {
  488. f.ee.encodeNil()
  489. return
  490. }
  491. l := rv.Len()
  492. f.ee.encodeMapStart(l)
  493. if l == 0 {
  494. f.ee.encodeMapEnd()
  495. return
  496. }
  497. var asSymbols bool
  498. // determine the underlying key and val encFn's for the map.
  499. // This eliminates some work which is done for each loop iteration i.e.
  500. // rv.Type(), ref.ValueOf(rt).Pointer(), then check map/list for fn.
  501. var keyFn, valFn encFn
  502. rtkey := f.ti.rt.Key()
  503. rtval := f.ti.rt.Elem()
  504. // keyTypeIsString := f.ti.rt.Key().Kind() == reflect.String
  505. var keyTypeIsString = rtkey == stringTyp
  506. if keyTypeIsString {
  507. asSymbols = f.e.h.AsSymbols&AsSymbolMapStringKeysFlag != 0
  508. } else {
  509. for rtkey.Kind() == reflect.Ptr {
  510. rtkey = rtkey.Elem()
  511. }
  512. keyFn = f.e.getEncFn(rtkey)
  513. }
  514. for rtval.Kind() == reflect.Ptr {
  515. rtval = rtval.Elem()
  516. }
  517. valFn = f.e.getEncFn(rtval)
  518. mks := rv.MapKeys()
  519. // for j, lmks := 0, len(mks); j < lmks; j++ {
  520. ee := f.ee //don't dereference everytime
  521. for j := range mks {
  522. if j > 0 {
  523. ee.encodeMapEntrySeparator()
  524. }
  525. if keyTypeIsString {
  526. if asSymbols {
  527. ee.encodeSymbol(mks[j].String())
  528. } else {
  529. ee.encodeString(c_UTF8, mks[j].String())
  530. }
  531. } else {
  532. f.e.encodeValue(mks[j], keyFn)
  533. }
  534. ee.encodeMapKVSeparator()
  535. f.e.encodeValue(rv.MapIndex(mks[j]), valFn)
  536. }
  537. ee.encodeMapEnd()
  538. }
  539. // --------------------------------------------------
  540. // encFn encapsulates the captured variables and the encode function.
  541. // This way, we only do some calculations one times, and pass to the
  542. // code block that should be called (encapsulated in a function)
  543. // instead of executing the checks every time.
  544. type encFn struct {
  545. i *encFnInfo
  546. f func(*encFnInfo, reflect.Value)
  547. }
  548. // --------------------------------------------------
  549. // An Encoder writes an object to an output stream in the codec format.
  550. type Encoder struct {
  551. w encWriter
  552. e encDriver
  553. h *BasicHandle
  554. hh Handle
  555. f map[uintptr]encFn
  556. x []uintptr
  557. s []encFn
  558. }
  559. // NewEncoder returns an Encoder for encoding into an io.Writer.
  560. //
  561. // For efficiency, Users are encouraged to pass in a memory buffered writer
  562. // (eg bufio.Writer, bytes.Buffer).
  563. func NewEncoder(w io.Writer, h Handle) *Encoder {
  564. ww, ok := w.(ioEncWriterWriter)
  565. if !ok {
  566. sww := simpleIoEncWriterWriter{w: w}
  567. sww.bw, _ = w.(io.ByteWriter)
  568. sww.sw, _ = w.(ioEncStringWriter)
  569. ww = &sww
  570. //ww = bufio.NewWriterSize(w, defEncByteBufSize)
  571. }
  572. z := ioEncWriter{
  573. w: ww,
  574. }
  575. return &Encoder{w: &z, hh: h, h: h.getBasicHandle(), e: h.newEncDriver(&z)}
  576. }
  577. // NewEncoderBytes returns an encoder for encoding directly and efficiently
  578. // into a byte slice, using zero-copying to temporary slices.
  579. //
  580. // It will potentially replace the output byte slice pointed to.
  581. // After encoding, the out parameter contains the encoded contents.
  582. func NewEncoderBytes(out *[]byte, h Handle) *Encoder {
  583. in := *out
  584. if in == nil {
  585. in = make([]byte, defEncByteBufSize)
  586. }
  587. z := bytesEncWriter{
  588. b: in,
  589. out: out,
  590. }
  591. return &Encoder{w: &z, hh: h, h: h.getBasicHandle(), e: h.newEncDriver(&z)}
  592. }
  593. // Encode writes an object into a stream in the codec format.
  594. //
  595. // Encoding can be configured via the "codec" struct tag for the fields.
  596. //
  597. // The "codec" key in struct field's tag value is the key name,
  598. // followed by an optional comma and options.
  599. //
  600. // To set an option on all fields (e.g. omitempty on all fields), you
  601. // can create a field called _struct, and set flags on it.
  602. //
  603. // Struct values "usually" encode as maps. Each exported struct field is encoded unless:
  604. // - the field's codec tag is "-", OR
  605. // - the field is empty and its codec tag specifies the "omitempty" option.
  606. //
  607. // When encoding as a map, the first string in the tag (before the comma)
  608. // is the map key string to use when encoding.
  609. //
  610. // However, struct values may encode as arrays. This happens when:
  611. // - StructToArray Encode option is set, OR
  612. // - the codec tag on the _struct field sets the "toarray" option
  613. //
  614. // Values with types that implement MapBySlice are encoded as stream maps.
  615. //
  616. // The empty values (for omitempty option) are false, 0, any nil pointer
  617. // or interface value, and any array, slice, map, or string of length zero.
  618. //
  619. // Anonymous fields are encoded inline if no struct tag is present.
  620. // Else they are encoded as regular fields.
  621. //
  622. // Examples:
  623. //
  624. // type MyStruct struct {
  625. // _struct bool `codec:",omitempty"` //set omitempty for every field
  626. // Field1 string `codec:"-"` //skip this field
  627. // Field2 int `codec:"myName"` //Use key "myName" in encode stream
  628. // Field3 int32 `codec:",omitempty"` //use key "Field3". Omit if empty.
  629. // Field4 bool `codec:"f4,omitempty"` //use key "f4". Omit if empty.
  630. // ...
  631. // }
  632. //
  633. // type MyStruct struct {
  634. // _struct bool `codec:",omitempty,toarray"` //set omitempty for every field
  635. // //and encode struct as an array
  636. // }
  637. //
  638. // The mode of encoding is based on the type of the value. When a value is seen:
  639. // - If an extension is registered for it, call that extension function
  640. // - If it implements BinaryMarshaler, call its MarshalBinary() (data []byte, err error)
  641. // - Else encode it based on its reflect.Kind
  642. //
  643. // Note that struct field names and keys in map[string]XXX will be treated as symbols.
  644. // Some formats support symbols (e.g. binc) and will properly encode the string
  645. // only once in the stream, and use a tag to refer to it thereafter.
  646. func (e *Encoder) Encode(v interface{}) (err error) {
  647. defer panicToErr(&err)
  648. e.encode(v)
  649. e.w.atEndOfEncode()
  650. return
  651. }
  652. // MustEncode is like Encode, but panics if unable to Encode.
  653. // This provides insight to the code location that triggered the error.
  654. func (e *Encoder) MustEncode(v interface{}) {
  655. e.encode(v)
  656. e.w.atEndOfEncode()
  657. }
  658. func (e *Encoder) Write(bs []byte) (err error) {
  659. defer panicToErr(&err)
  660. e.w.writeb(bs)
  661. return
  662. }
  663. func (e *Encoder) MustWrite(bs []byte) {
  664. e.w.writeb(bs)
  665. }
  666. func (e *Encoder) encode(iv interface{}) {
  667. switch v := iv.(type) {
  668. case nil:
  669. e.e.encodeNil()
  670. case reflect.Value:
  671. e.encodeValue(v, encFn{})
  672. case string:
  673. e.e.encodeString(c_UTF8, v)
  674. case bool:
  675. e.e.encodeBool(v)
  676. case int:
  677. e.e.encodeInt(int64(v))
  678. case int8:
  679. e.e.encodeInt(int64(v))
  680. case int16:
  681. e.e.encodeInt(int64(v))
  682. case int32:
  683. e.e.encodeInt(int64(v))
  684. case int64:
  685. e.e.encodeInt(v)
  686. case uint:
  687. e.e.encodeUint(uint64(v))
  688. case uint8:
  689. e.e.encodeUint(uint64(v))
  690. case uint16:
  691. e.e.encodeUint(uint64(v))
  692. case uint32:
  693. e.e.encodeUint(uint64(v))
  694. case uint64:
  695. e.e.encodeUint(v)
  696. case float32:
  697. e.e.encodeFloat32(v)
  698. case float64:
  699. e.e.encodeFloat64(v)
  700. case []uint8:
  701. e.e.encodeStringBytes(c_RAW, v)
  702. case *string:
  703. e.e.encodeString(c_UTF8, *v)
  704. case *bool:
  705. e.e.encodeBool(*v)
  706. case *int:
  707. e.e.encodeInt(int64(*v))
  708. case *int8:
  709. e.e.encodeInt(int64(*v))
  710. case *int16:
  711. e.e.encodeInt(int64(*v))
  712. case *int32:
  713. e.e.encodeInt(int64(*v))
  714. case *int64:
  715. e.e.encodeInt(*v)
  716. case *uint:
  717. e.e.encodeUint(uint64(*v))
  718. case *uint8:
  719. e.e.encodeUint(uint64(*v))
  720. case *uint16:
  721. e.e.encodeUint(uint64(*v))
  722. case *uint32:
  723. e.e.encodeUint(uint64(*v))
  724. case *uint64:
  725. e.e.encodeUint(*v)
  726. case *float32:
  727. e.e.encodeFloat32(*v)
  728. case *float64:
  729. e.e.encodeFloat64(*v)
  730. case *[]uint8:
  731. e.e.encodeStringBytes(c_RAW, *v)
  732. default:
  733. e.encodeValue(reflect.ValueOf(iv), encFn{})
  734. }
  735. }
  736. func (e *Encoder) encodeValue(rv reflect.Value, fn encFn) {
  737. // if a valid fn is passed, it MUST BE for the dereferenced type of rv
  738. for rv.Kind() == reflect.Ptr {
  739. if rv.IsNil() {
  740. e.e.encodeNil()
  741. return
  742. }
  743. rv = rv.Elem()
  744. }
  745. if !rv.IsValid() {
  746. e.e.encodeNil()
  747. return
  748. }
  749. if fn.i == nil {
  750. fn = e.getEncFn(rv.Type())
  751. }
  752. fn.f(fn.i, rv)
  753. }
  754. func (e *Encoder) getEncFn(rt reflect.Type) (fn encFn) {
  755. // if e.f == nil && e.s == nil { debugf("---->Creating new enc f map for type: %v\n", rt) }
  756. rtid := reflect.ValueOf(rt).Pointer()
  757. var ok bool
  758. if useMapForCodecCache {
  759. fn, ok = e.f[rtid]
  760. } else {
  761. // if len(e.x) > 0 && len(e.x)%10 == 0 {
  762. // println("len(e.x) ", len(e.x))
  763. // }
  764. for i, v := range e.x {
  765. if v == rtid {
  766. fn, ok = e.s[i], true
  767. break
  768. }
  769. }
  770. }
  771. if !ok {
  772. // debugf("\tCreating new enc fn for type: %v\n", rt)
  773. fi := encFnInfo{ti: getTypeInfo(rtid, rt), e: e, ee: e.e}
  774. fn.i = &fi
  775. if rtid == rawExtTypId {
  776. fn.f = (*encFnInfo).rawExt
  777. } else if e.e.isBuiltinType(rtid) {
  778. fn.f = (*encFnInfo).builtin
  779. } else if xfFn := e.h.getExt(rtid); xfFn != nil {
  780. fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
  781. fn.f = (*encFnInfo).ext
  782. } else if supportMarshalInterfaces && e.hh.isBinaryEncoding() && fi.ti.bm {
  783. fn.f = (*encFnInfo).binaryMarshal
  784. } else if supportMarshalInterfaces && !e.hh.isBinaryEncoding() && fi.ti.tm {
  785. fn.f = (*encFnInfo).textMarshal
  786. } else {
  787. rk := rt.Kind()
  788. if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice) {
  789. if fn.f, ok = fastpathsEnc[rtid]; !ok && rt.PkgPath() != "" {
  790. // use mapping for underlying type if there
  791. var rtu reflect.Type
  792. if rk == reflect.Map {
  793. rtu = reflect.MapOf(rt.Key(), rt.Elem())
  794. } else {
  795. rtu = reflect.SliceOf(rt.Elem())
  796. }
  797. rtuid := reflect.ValueOf(rtu).Pointer()
  798. if fn.f, ok = fastpathsEnc[rtuid]; ok {
  799. xfnf := fn.f
  800. xrt := fastpathsTyp[rtuid]
  801. fn.f = func(xf *encFnInfo, xrv reflect.Value) {
  802. xfnf(xf, xrv.Convert(xrt))
  803. }
  804. }
  805. }
  806. }
  807. if fn.f == nil {
  808. switch rk {
  809. case reflect.Bool:
  810. fn.f = (*encFnInfo).kBool
  811. case reflect.String:
  812. fn.f = (*encFnInfo).kString
  813. case reflect.Float64:
  814. fn.f = (*encFnInfo).kFloat64
  815. case reflect.Float32:
  816. fn.f = (*encFnInfo).kFloat32
  817. case reflect.Int, reflect.Int8, reflect.Int64, reflect.Int32, reflect.Int16:
  818. fn.f = (*encFnInfo).kInt
  819. case reflect.Uint8, reflect.Uint64, reflect.Uint, reflect.Uint32, reflect.Uint16:
  820. fn.f = (*encFnInfo).kUint
  821. case reflect.Invalid:
  822. fn.f = (*encFnInfo).kInvalid
  823. case reflect.Slice:
  824. fn.f = (*encFnInfo).kSlice
  825. case reflect.Array:
  826. fi.array = true
  827. fn.f = (*encFnInfo).kSlice
  828. case reflect.Struct:
  829. fn.f = (*encFnInfo).kStruct
  830. // case reflect.Ptr:
  831. // fn.f = (*encFnInfo).kPtr
  832. case reflect.Interface:
  833. fn.f = (*encFnInfo).kInterface
  834. case reflect.Map:
  835. fn.f = (*encFnInfo).kMap
  836. default:
  837. fn.f = (*encFnInfo).kErr
  838. }
  839. }
  840. }
  841. if useMapForCodecCache {
  842. if e.f == nil {
  843. e.f = make(map[uintptr]encFn, 64)
  844. }
  845. e.f[rtid] = fn
  846. } else {
  847. e.s = append(e.s, fn)
  848. e.x = append(e.x, rtid)
  849. }
  850. }
  851. return
  852. }
  853. // ----------------------------------------
  854. func encErr(format string, params ...interface{}) {
  855. doPanic(msgTagEnc, format, params...)
  856. }