encode.go 22 KB

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