encode.go 22 KB

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