另客网go项目公用的代码库

simple.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // Copyright (c) 2012-2018 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. "math"
  6. "reflect"
  7. "time"
  8. )
  9. const (
  10. _ uint8 = iota
  11. simpleVdNil = 1
  12. simpleVdFalse = 2
  13. simpleVdTrue = 3
  14. simpleVdFloat32 = 4
  15. simpleVdFloat64 = 5
  16. // each lasts for 4 (ie n, n+1, n+2, n+3)
  17. simpleVdPosInt = 8
  18. simpleVdNegInt = 12
  19. simpleVdTime = 24
  20. // containers: each lasts for 4 (ie n, n+1, n+2, ... n+7)
  21. simpleVdString = 216
  22. simpleVdByteArray = 224
  23. simpleVdArray = 232
  24. simpleVdMap = 240
  25. simpleVdExt = 248
  26. )
  27. type simpleEncDriver struct {
  28. noBuiltInTypes
  29. // encNoSeparator
  30. e *Encoder
  31. h *SimpleHandle
  32. w *encWriterSwitch
  33. b [8]byte
  34. // c containerState
  35. encDriverTrackContainerWriter
  36. // encDriverNoopContainerWriter
  37. _ [3]uint64 // padding
  38. }
  39. func (e *simpleEncDriver) EncodeNil() {
  40. e.w.writen1(simpleVdNil)
  41. }
  42. func (e *simpleEncDriver) EncodeBool(b bool) {
  43. if e.h.EncZeroValuesAsNil && e.c != containerMapKey && !b {
  44. e.EncodeNil()
  45. return
  46. }
  47. if b {
  48. e.w.writen1(simpleVdTrue)
  49. } else {
  50. e.w.writen1(simpleVdFalse)
  51. }
  52. }
  53. func (e *simpleEncDriver) EncodeFloat32(f float32) {
  54. if e.h.EncZeroValuesAsNil && e.c != containerMapKey && f == 0.0 {
  55. e.EncodeNil()
  56. return
  57. }
  58. e.w.writen1(simpleVdFloat32)
  59. bigenHelper{e.b[:4], e.w}.writeUint32(math.Float32bits(f))
  60. }
  61. func (e *simpleEncDriver) EncodeFloat64(f float64) {
  62. if e.h.EncZeroValuesAsNil && e.c != containerMapKey && f == 0.0 {
  63. e.EncodeNil()
  64. return
  65. }
  66. e.w.writen1(simpleVdFloat64)
  67. bigenHelper{e.b[:8], e.w}.writeUint64(math.Float64bits(f))
  68. }
  69. func (e *simpleEncDriver) EncodeInt(v int64) {
  70. if v < 0 {
  71. e.encUint(uint64(-v), simpleVdNegInt)
  72. } else {
  73. e.encUint(uint64(v), simpleVdPosInt)
  74. }
  75. }
  76. func (e *simpleEncDriver) EncodeUint(v uint64) {
  77. e.encUint(v, simpleVdPosInt)
  78. }
  79. func (e *simpleEncDriver) encUint(v uint64, bd uint8) {
  80. if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == 0 {
  81. e.EncodeNil()
  82. return
  83. }
  84. if v <= math.MaxUint8 {
  85. e.w.writen2(bd, uint8(v))
  86. } else if v <= math.MaxUint16 {
  87. e.w.writen1(bd + 1)
  88. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(v))
  89. } else if v <= math.MaxUint32 {
  90. e.w.writen1(bd + 2)
  91. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(v))
  92. } else { // if v <= math.MaxUint64 {
  93. e.w.writen1(bd + 3)
  94. bigenHelper{e.b[:8], e.w}.writeUint64(v)
  95. }
  96. }
  97. func (e *simpleEncDriver) encLen(bd byte, length int) {
  98. if length == 0 {
  99. e.w.writen1(bd)
  100. } else if length <= math.MaxUint8 {
  101. e.w.writen1(bd + 1)
  102. e.w.writen1(uint8(length))
  103. } else if length <= math.MaxUint16 {
  104. e.w.writen1(bd + 2)
  105. bigenHelper{e.b[:2], e.w}.writeUint16(uint16(length))
  106. } else if int64(length) <= math.MaxUint32 {
  107. e.w.writen1(bd + 3)
  108. bigenHelper{e.b[:4], e.w}.writeUint32(uint32(length))
  109. } else {
  110. e.w.writen1(bd + 4)
  111. bigenHelper{e.b[:8], e.w}.writeUint64(uint64(length))
  112. }
  113. }
  114. func (e *simpleEncDriver) EncodeExt(rv interface{}, xtag uint64, ext Ext, _ *Encoder) {
  115. bs := ext.WriteExt(rv)
  116. if bs == nil {
  117. e.EncodeNil()
  118. return
  119. }
  120. e.encodeExtPreamble(uint8(xtag), len(bs))
  121. e.w.writeb(bs)
  122. }
  123. func (e *simpleEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
  124. e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
  125. e.w.writeb(re.Data)
  126. }
  127. func (e *simpleEncDriver) encodeExtPreamble(xtag byte, length int) {
  128. e.encLen(simpleVdExt, length)
  129. e.w.writen1(xtag)
  130. }
  131. func (e *simpleEncDriver) WriteArrayStart(length int) {
  132. e.c = containerArrayStart
  133. e.encLen(simpleVdArray, length)
  134. }
  135. func (e *simpleEncDriver) WriteMapStart(length int) {
  136. e.c = containerMapStart
  137. e.encLen(simpleVdMap, length)
  138. }
  139. // func (e *simpleEncDriver) EncodeSymbol(v string) {
  140. // e.EncodeString(cUTF8, v)
  141. // }
  142. func (e *simpleEncDriver) EncodeStringEnc(c charEncoding, v string) {
  143. if false && e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == "" {
  144. e.EncodeNil()
  145. return
  146. }
  147. e.encLen(simpleVdString, len(v))
  148. e.w.writestr(v)
  149. }
  150. func (e *simpleEncDriver) EncodeString(c charEncoding, v string) {
  151. e.EncodeStringEnc(c, v)
  152. }
  153. func (e *simpleEncDriver) EncodeStringBytes(c charEncoding, v []byte) {
  154. e.EncodeStringBytesRaw(v)
  155. }
  156. func (e *simpleEncDriver) EncodeStringBytesRaw(v []byte) {
  157. // if e.h.EncZeroValuesAsNil && e.c != containerMapKey && v == nil {
  158. if v == nil {
  159. e.EncodeNil()
  160. return
  161. }
  162. e.encLen(simpleVdByteArray, len(v))
  163. e.w.writeb(v)
  164. }
  165. func (e *simpleEncDriver) EncodeTime(t time.Time) {
  166. // if e.h.EncZeroValuesAsNil && e.c != containerMapKey && t.IsZero() {
  167. if t.IsZero() {
  168. e.EncodeNil()
  169. return
  170. }
  171. v, err := t.MarshalBinary()
  172. if err != nil {
  173. e.e.errorv(err)
  174. return
  175. }
  176. // time.Time marshalbinary takes about 14 bytes.
  177. e.w.writen2(simpleVdTime, uint8(len(v)))
  178. e.w.writeb(v)
  179. }
  180. //------------------------------------
  181. type simpleDecDriver struct {
  182. d *Decoder
  183. h *SimpleHandle
  184. r *decReaderSwitch
  185. bdRead bool
  186. bd byte
  187. br bool // a bytes reader?
  188. c containerState
  189. // b [scratchByteArrayLen]byte
  190. noBuiltInTypes
  191. // noStreamingCodec
  192. decDriverNoopContainerReader
  193. // _ [3]uint64 // padding
  194. }
  195. func (d *simpleDecDriver) readNextBd() {
  196. d.bd = d.r.readn1()
  197. d.bdRead = true
  198. }
  199. func (d *simpleDecDriver) uncacheRead() {
  200. if d.bdRead {
  201. d.r.unreadn1()
  202. d.bdRead = false
  203. }
  204. }
  205. func (d *simpleDecDriver) ContainerType() (vt valueType) {
  206. if !d.bdRead {
  207. d.readNextBd()
  208. }
  209. switch d.bd {
  210. case simpleVdNil:
  211. return valueTypeNil
  212. case simpleVdByteArray, simpleVdByteArray + 1,
  213. simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  214. return valueTypeBytes
  215. case simpleVdString, simpleVdString + 1,
  216. simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  217. return valueTypeString
  218. case simpleVdArray, simpleVdArray + 1,
  219. simpleVdArray + 2, simpleVdArray + 3, simpleVdArray + 4:
  220. return valueTypeArray
  221. case simpleVdMap, simpleVdMap + 1,
  222. simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  223. return valueTypeMap
  224. // case simpleVdTime:
  225. // return valueTypeTime
  226. }
  227. // else {
  228. // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
  229. // }
  230. return valueTypeUnset
  231. }
  232. func (d *simpleDecDriver) TryDecodeAsNil() bool {
  233. if !d.bdRead {
  234. d.readNextBd()
  235. }
  236. if d.bd == simpleVdNil {
  237. d.bdRead = false
  238. return true
  239. }
  240. return false
  241. }
  242. func (d *simpleDecDriver) decCheckInteger() (ui uint64, neg bool) {
  243. if !d.bdRead {
  244. d.readNextBd()
  245. }
  246. switch d.bd {
  247. case simpleVdPosInt:
  248. ui = uint64(d.r.readn1())
  249. case simpleVdPosInt + 1:
  250. ui = uint64(bigen.Uint16(d.r.readx(2)))
  251. case simpleVdPosInt + 2:
  252. ui = uint64(bigen.Uint32(d.r.readx(4)))
  253. case simpleVdPosInt + 3:
  254. ui = uint64(bigen.Uint64(d.r.readx(8)))
  255. case simpleVdNegInt:
  256. ui = uint64(d.r.readn1())
  257. neg = true
  258. case simpleVdNegInt + 1:
  259. ui = uint64(bigen.Uint16(d.r.readx(2)))
  260. neg = true
  261. case simpleVdNegInt + 2:
  262. ui = uint64(bigen.Uint32(d.r.readx(4)))
  263. neg = true
  264. case simpleVdNegInt + 3:
  265. ui = uint64(bigen.Uint64(d.r.readx(8)))
  266. neg = true
  267. default:
  268. d.d.errorf("integer only valid from pos/neg integer1..8. Invalid descriptor: %v", d.bd)
  269. return
  270. }
  271. // don't do this check, because callers may only want the unsigned value.
  272. // if ui > math.MaxInt64 {
  273. // d.d.errorf("decIntAny: Integer out of range for signed int64: %v", ui)
  274. // return
  275. // }
  276. return
  277. }
  278. func (d *simpleDecDriver) DecodeInt64() (i int64) {
  279. ui, neg := d.decCheckInteger()
  280. i = chkOvf.SignedIntV(ui)
  281. if neg {
  282. i = -i
  283. }
  284. d.bdRead = false
  285. return
  286. }
  287. func (d *simpleDecDriver) DecodeUint64() (ui uint64) {
  288. ui, neg := d.decCheckInteger()
  289. if neg {
  290. d.d.errorf("assigning negative signed value to unsigned type")
  291. return
  292. }
  293. d.bdRead = false
  294. return
  295. }
  296. func (d *simpleDecDriver) DecodeFloat64() (f float64) {
  297. if !d.bdRead {
  298. d.readNextBd()
  299. }
  300. if d.bd == simpleVdFloat32 {
  301. f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
  302. } else if d.bd == simpleVdFloat64 {
  303. f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
  304. } else {
  305. if d.bd >= simpleVdPosInt && d.bd <= simpleVdNegInt+3 {
  306. f = float64(d.DecodeInt64())
  307. } else {
  308. d.d.errorf("float only valid from float32/64: Invalid descriptor: %v", d.bd)
  309. return
  310. }
  311. }
  312. d.bdRead = false
  313. return
  314. }
  315. // bool can be decoded from bool only (single byte).
  316. func (d *simpleDecDriver) DecodeBool() (b bool) {
  317. if !d.bdRead {
  318. d.readNextBd()
  319. }
  320. if d.bd == simpleVdTrue {
  321. b = true
  322. } else if d.bd == simpleVdFalse {
  323. } else {
  324. d.d.errorf("cannot decode bool - %s: %x", msgBadDesc, d.bd)
  325. return
  326. }
  327. d.bdRead = false
  328. return
  329. }
  330. func (d *simpleDecDriver) ReadMapStart() (length int) {
  331. if !d.bdRead {
  332. d.readNextBd()
  333. }
  334. d.bdRead = false
  335. d.c = containerMapStart
  336. return d.decLen()
  337. }
  338. func (d *simpleDecDriver) ReadArrayStart() (length int) {
  339. if !d.bdRead {
  340. d.readNextBd()
  341. }
  342. d.bdRead = false
  343. d.c = containerArrayStart
  344. return d.decLen()
  345. }
  346. func (d *simpleDecDriver) ReadArrayElem() {
  347. d.c = containerArrayElem
  348. }
  349. func (d *simpleDecDriver) ReadArrayEnd() {
  350. d.c = containerArrayEnd
  351. }
  352. func (d *simpleDecDriver) ReadMapElemKey() {
  353. d.c = containerMapKey
  354. }
  355. func (d *simpleDecDriver) ReadMapElemValue() {
  356. d.c = containerMapValue
  357. }
  358. func (d *simpleDecDriver) ReadMapEnd() {
  359. d.c = containerMapEnd
  360. }
  361. func (d *simpleDecDriver) decLen() int {
  362. switch d.bd % 8 {
  363. case 0:
  364. return 0
  365. case 1:
  366. return int(d.r.readn1())
  367. case 2:
  368. return int(bigen.Uint16(d.r.readx(2)))
  369. case 3:
  370. ui := uint64(bigen.Uint32(d.r.readx(4)))
  371. if chkOvf.Uint(ui, intBitsize) {
  372. d.d.errorf("overflow integer: %v", ui)
  373. return 0
  374. }
  375. return int(ui)
  376. case 4:
  377. ui := bigen.Uint64(d.r.readx(8))
  378. if chkOvf.Uint(ui, intBitsize) {
  379. d.d.errorf("overflow integer: %v", ui)
  380. return 0
  381. }
  382. return int(ui)
  383. }
  384. d.d.errorf("cannot read length: bd%%8 must be in range 0..4. Got: %d", d.bd%8)
  385. return -1
  386. }
  387. func (d *simpleDecDriver) DecodeString() (s string) {
  388. return string(d.DecodeBytes(d.d.b[:], true))
  389. }
  390. func (d *simpleDecDriver) DecodeStringAsBytes() (s []byte) {
  391. return d.DecodeBytes(d.d.b[:], true)
  392. }
  393. func (d *simpleDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
  394. if !d.bdRead {
  395. d.readNextBd()
  396. }
  397. if d.bd == simpleVdNil {
  398. d.bdRead = false
  399. return
  400. }
  401. // check if an "array" of uint8's (see ContainerType for how to infer if an array)
  402. if d.bd >= simpleVdArray && d.bd <= simpleVdMap+4 {
  403. if len(bs) == 0 && zerocopy {
  404. bs = d.d.b[:]
  405. }
  406. bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
  407. return
  408. }
  409. clen := d.decLen()
  410. d.bdRead = false
  411. if zerocopy {
  412. if d.br {
  413. return d.r.readx(uint(clen))
  414. } else if len(bs) == 0 {
  415. bs = d.d.b[:]
  416. }
  417. }
  418. return decByteSlice(d.r, clen, d.d.h.MaxInitLen, bs)
  419. }
  420. func (d *simpleDecDriver) DecodeTime() (t time.Time) {
  421. if !d.bdRead {
  422. d.readNextBd()
  423. }
  424. if d.bd == simpleVdNil {
  425. d.bdRead = false
  426. return
  427. }
  428. if d.bd != simpleVdTime {
  429. d.d.errorf("invalid descriptor for time.Time - expect 0x%x, received 0x%x", simpleVdTime, d.bd)
  430. return
  431. }
  432. d.bdRead = false
  433. clen := int(d.r.readn1())
  434. b := d.r.readx(uint(clen))
  435. if err := (&t).UnmarshalBinary(b); err != nil {
  436. d.d.errorv(err)
  437. }
  438. return
  439. }
  440. func (d *simpleDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
  441. if xtag > 0xff {
  442. d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
  443. return
  444. }
  445. realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
  446. realxtag = uint64(realxtag1)
  447. if ext == nil {
  448. re := rv.(*RawExt)
  449. re.Tag = realxtag
  450. re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
  451. } else {
  452. ext.ReadExt(rv, xbs)
  453. }
  454. return
  455. }
  456. func (d *simpleDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
  457. if !d.bdRead {
  458. d.readNextBd()
  459. }
  460. switch d.bd {
  461. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  462. l := d.decLen()
  463. xtag = d.r.readn1()
  464. if verifyTag && xtag != tag {
  465. d.d.errorf("wrong extension tag. Got %b. Expecting: %v", xtag, tag)
  466. return
  467. }
  468. if d.br {
  469. xbs = d.r.readx(uint(l))
  470. } else {
  471. xbs = decByteSlice(d.r, l, d.d.h.MaxInitLen, d.d.b[:])
  472. }
  473. case simpleVdByteArray, simpleVdByteArray + 1,
  474. simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  475. xbs = d.DecodeBytes(nil, true)
  476. default:
  477. d.d.errorf("ext - %s - expecting extensions/bytearray, got: 0x%x", msgBadDesc, d.bd)
  478. return
  479. }
  480. d.bdRead = false
  481. return
  482. }
  483. func (d *simpleDecDriver) DecodeNaked() {
  484. if !d.bdRead {
  485. d.readNextBd()
  486. }
  487. n := d.d.naked()
  488. var decodeFurther bool
  489. switch d.bd {
  490. case simpleVdNil:
  491. n.v = valueTypeNil
  492. case simpleVdFalse:
  493. n.v = valueTypeBool
  494. n.b = false
  495. case simpleVdTrue:
  496. n.v = valueTypeBool
  497. n.b = true
  498. case simpleVdPosInt, simpleVdPosInt + 1, simpleVdPosInt + 2, simpleVdPosInt + 3:
  499. if d.h.SignedInteger {
  500. n.v = valueTypeInt
  501. n.i = d.DecodeInt64()
  502. } else {
  503. n.v = valueTypeUint
  504. n.u = d.DecodeUint64()
  505. }
  506. case simpleVdNegInt, simpleVdNegInt + 1, simpleVdNegInt + 2, simpleVdNegInt + 3:
  507. n.v = valueTypeInt
  508. n.i = d.DecodeInt64()
  509. case simpleVdFloat32:
  510. n.v = valueTypeFloat
  511. n.f = d.DecodeFloat64()
  512. case simpleVdFloat64:
  513. n.v = valueTypeFloat
  514. n.f = d.DecodeFloat64()
  515. case simpleVdTime:
  516. n.v = valueTypeTime
  517. n.t = d.DecodeTime()
  518. case simpleVdString, simpleVdString + 1,
  519. simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
  520. n.v = valueTypeString
  521. n.s = d.DecodeString()
  522. case simpleVdByteArray, simpleVdByteArray + 1,
  523. simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
  524. n.v = valueTypeBytes
  525. n.l = d.DecodeBytes(nil, false)
  526. case simpleVdExt, simpleVdExt + 1, simpleVdExt + 2, simpleVdExt + 3, simpleVdExt + 4:
  527. n.v = valueTypeExt
  528. l := d.decLen()
  529. n.u = uint64(d.r.readn1())
  530. if d.br {
  531. n.l = d.r.readx(uint(l))
  532. } else {
  533. n.l = decByteSlice(d.r, l, d.d.h.MaxInitLen, d.d.b[:])
  534. }
  535. case simpleVdArray, simpleVdArray + 1, simpleVdArray + 2,
  536. simpleVdArray + 3, simpleVdArray + 4:
  537. n.v = valueTypeArray
  538. decodeFurther = true
  539. case simpleVdMap, simpleVdMap + 1, simpleVdMap + 2, simpleVdMap + 3, simpleVdMap + 4:
  540. n.v = valueTypeMap
  541. decodeFurther = true
  542. default:
  543. d.d.errorf("cannot infer value - %s 0x%x", msgBadDesc, d.bd)
  544. }
  545. if !decodeFurther {
  546. d.bdRead = false
  547. }
  548. }
  549. //------------------------------------
  550. // SimpleHandle is a Handle for a very simple encoding format.
  551. //
  552. // simple is a simplistic codec similar to binc, but not as compact.
  553. // - Encoding of a value is always preceded by the descriptor byte (bd)
  554. // - True, false, nil are encoded fully in 1 byte (the descriptor)
  555. // - Integers (intXXX, uintXXX) are encoded in 1, 2, 4 or 8 bytes (plus a descriptor byte).
  556. // There are positive (uintXXX and intXXX >= 0) and negative (intXXX < 0) integers.
  557. // - Floats are encoded in 4 or 8 bytes (plus a descriptor byte)
  558. // - Length of containers (strings, bytes, array, map, extensions)
  559. // are encoded in 0, 1, 2, 4 or 8 bytes.
  560. // Zero-length containers have no length encoded.
  561. // For others, the number of bytes is given by pow(2, bd%3)
  562. // - maps are encoded as [bd] [length] [[key][value]]...
  563. // - arrays are encoded as [bd] [length] [value]...
  564. // - extensions are encoded as [bd] [length] [tag] [byte]...
  565. // - strings/bytearrays are encoded as [bd] [length] [byte]...
  566. // - time.Time are encoded as [bd] [length] [byte]...
  567. //
  568. // The full spec will be published soon.
  569. type SimpleHandle struct {
  570. BasicHandle
  571. binaryEncodingType
  572. noElemSeparators
  573. // EncZeroValuesAsNil says to encode zero values for numbers, bool, string, etc as nil
  574. EncZeroValuesAsNil bool
  575. // _ [1]uint64 // padding
  576. }
  577. // Name returns the name of the handle: simple
  578. func (h *SimpleHandle) Name() string { return "simple" }
  579. // SetBytesExt sets an extension
  580. func (h *SimpleHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
  581. return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}})
  582. }
  583. func (h *SimpleHandle) hasElemSeparators() bool { return true } // as it implements Write(Map|Array)XXX
  584. func (h *SimpleHandle) newEncDriver(e *Encoder) encDriver {
  585. return &simpleEncDriver{e: e, w: e.w, h: h}
  586. }
  587. func (h *SimpleHandle) newDecDriver(d *Decoder) decDriver {
  588. return &simpleDecDriver{d: d, h: h, r: d.r, br: d.bytes}
  589. }
  590. func (e *simpleEncDriver) reset() {
  591. e.c = 0
  592. e.w = e.e.w
  593. }
  594. func (d *simpleDecDriver) reset() {
  595. d.c = 0
  596. d.r, d.br = d.d.r, d.d.bytes
  597. d.bd, d.bdRead = 0, false
  598. }
  599. var _ decDriver = (*simpleDecDriver)(nil)
  600. var _ encDriver = (*simpleEncDriver)(nil)