gen-helper.go.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // comment this out // + build ignore
  2. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. // Code generated from gen-helper.go.tmpl - DO NOT EDIT.
  5. package codec
  6. import (
  7. "encoding"
  8. "reflect"
  9. )
  10. // GenVersion is the current version of codecgen.
  11. const GenVersion = {{ .Version }}
  12. // This file is used to generate helper code for codecgen.
  13. // The values here i.e. genHelper(En|De)coder are not to be used directly by
  14. // library users. They WILL change continuously and without notice.
  15. //
  16. // To help enforce this, we create an unexported type with exported members.
  17. // The only way to get the type is via the one exported type that we control (somewhat).
  18. //
  19. // When static codecs are created for types, they will use this value
  20. // to perform encoding or decoding of primitives or known slice or map types.
  21. // GenHelperEncoder is exported so that it can be used externally by codecgen.
  22. //
  23. // Library users: DO NOT USE IT DIRECTLY. IT WILL CHANGE CONTINOUSLY WITHOUT NOTICE.
  24. func GenHelperEncoder(e *Encoder) (ge genHelperEncoder, ee genHelperEncDriver) {
  25. ge = genHelperEncoder{e: e}
  26. ee = genHelperEncDriver{encDriver: e.e}
  27. return
  28. }
  29. // GenHelperDecoder is exported so that it can be used externally by codecgen.
  30. //
  31. // Library users: DO NOT USE IT DIRECTLY. IT WILL CHANGE CONTINOUSLY WITHOUT NOTICE.
  32. func GenHelperDecoder(d *Decoder) (gd genHelperDecoder, dd genHelperDecDriver) {
  33. gd = genHelperDecoder{d: d}
  34. dd = genHelperDecDriver{decDriver: d.d}
  35. return
  36. }
  37. type genHelperEncDriver struct {
  38. encDriver
  39. }
  40. func (x genHelperEncDriver) EncodeBuiltin(rt uintptr, v interface{}) {}
  41. func (x genHelperEncDriver) EncStructFieldKey(keyType valueType, s string) {
  42. encStructFieldKey(s, x.encDriver, nil, keyType, false, false)
  43. }
  44. func (x genHelperEncDriver) EncodeSymbol(s string) {
  45. x.encDriver.EncodeStringEnc(cUTF8, s)
  46. }
  47. type genHelperDecDriver struct {
  48. decDriver
  49. C checkOverflow
  50. }
  51. func (x genHelperDecDriver) DecodeBuiltin(rt uintptr, v interface{}) {}
  52. func (x genHelperDecDriver) DecStructFieldKey(keyType valueType, buf *[decScratchByteArrayLen]byte) []byte {
  53. return decStructFieldKey(x.decDriver, keyType, buf)
  54. }
  55. func (x genHelperDecDriver) DecodeInt(bitsize uint8) (i int64) {
  56. return x.C.IntV(x.decDriver.DecodeInt64(), bitsize)
  57. }
  58. func (x genHelperDecDriver) DecodeUint(bitsize uint8) (ui uint64) {
  59. return x.C.UintV(x.decDriver.DecodeUint64(), bitsize)
  60. }
  61. func (x genHelperDecDriver) DecodeFloat(chkOverflow32 bool) (f float64) {
  62. f = x.DecodeFloat64()
  63. if chkOverflow32 && chkOvf.Float32(f) {
  64. panicv.errorf("float32 overflow: %v", f)
  65. }
  66. return
  67. }
  68. func (x genHelperDecDriver) DecodeFloat32As64() (f float64) {
  69. f = x.DecodeFloat64()
  70. if chkOvf.Float32(f) {
  71. panicv.errorf("float32 overflow: %v", f)
  72. }
  73. return
  74. }
  75. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  76. type genHelperEncoder struct {
  77. M must
  78. e *Encoder
  79. F fastpathT
  80. }
  81. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  82. type genHelperDecoder struct {
  83. C checkOverflow
  84. d *Decoder
  85. F fastpathT
  86. }
  87. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  88. func (f genHelperEncoder) EncBasicHandle() *BasicHandle {
  89. return f.e.h
  90. }
  91. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  92. func (f genHelperEncoder) EncBinary() bool {
  93. return f.e.be // f.e.hh.isBinaryEncoding()
  94. }
  95. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  96. func (f genHelperEncoder) IsJSONHandle() bool {
  97. return f.e.js
  98. }
  99. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  100. func (f genHelperEncoder) EncFallback(iv interface{}) {
  101. // println(">>>>>>>>> EncFallback")
  102. // f.e.encodeI(iv, false, false)
  103. f.e.encodeValue(reflect.ValueOf(iv), nil, false)
  104. }
  105. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  106. func (f genHelperEncoder) EncTextMarshal(iv encoding.TextMarshaler) {
  107. bs, fnerr := iv.MarshalText()
  108. f.e.marshalUtf8(bs, fnerr)
  109. }
  110. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  111. func (f genHelperEncoder) EncJSONMarshal(iv jsonMarshaler) {
  112. bs, fnerr := iv.MarshalJSON()
  113. f.e.marshalAsis(bs, fnerr)
  114. }
  115. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  116. func (f genHelperEncoder) EncBinaryMarshal(iv encoding.BinaryMarshaler) {
  117. bs, fnerr := iv.MarshalBinary()
  118. f.e.marshalRaw(bs, fnerr)
  119. }
  120. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  121. func (f genHelperEncoder) EncRaw(iv Raw) { f.e.rawBytes(iv) }
  122. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  123. //
  124. // Deprecated: builtin no longer supported - so we make this method a no-op,
  125. // but leave in-place so that old generated files continue to work without regeneration.
  126. func (f genHelperEncoder) TimeRtidIfBinc() (v uintptr) { return }
  127. // func (f genHelperEncoder) TimeRtidIfBinc() uintptr {
  128. // if _, ok := f.e.hh.(*BincHandle); ok {
  129. // return timeTypId
  130. // }
  131. // }
  132. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  133. func (f genHelperEncoder) I2Rtid(v interface{}) uintptr {
  134. return i2rtid(v)
  135. }
  136. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  137. func (f genHelperEncoder) Extension(rtid uintptr) (xfn *extTypeTagFn) {
  138. return f.e.h.getExt(rtid)
  139. }
  140. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  141. func (f genHelperEncoder) EncExtension(v interface{}, xfFn *extTypeTagFn) {
  142. f.e.e.EncodeExt(v, xfFn.tag, xfFn.ext, f.e)
  143. }
  144. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  145. func (f genHelperEncoder) WriteStr(s string) {
  146. f.e.w.writestr(s)
  147. }
  148. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  149. func (f genHelperEncoder) BytesView(v string) []byte { return bytesView(v) }
  150. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  151. //
  152. // Deprecated: No longer used,
  153. // but leave in-place so that old generated files continue to work without regeneration.
  154. func (f genHelperEncoder) HasExtensions() bool {
  155. return len(f.e.h.extHandle) != 0
  156. }
  157. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  158. //
  159. // Deprecated: No longer used,
  160. // but leave in-place so that old generated files continue to work without regeneration.
  161. func (f genHelperEncoder) EncExt(v interface{}) (r bool) {
  162. if xfFn := f.e.h.getExt(i2rtid(v)); xfFn != nil {
  163. f.e.e.EncodeExt(v, xfFn.tag, xfFn.ext, f.e)
  164. return true
  165. }
  166. return false
  167. }
  168. // ---------------- DECODER FOLLOWS -----------------
  169. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  170. func (f genHelperDecoder) DecBasicHandle() *BasicHandle {
  171. return f.d.h
  172. }
  173. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  174. func (f genHelperDecoder) DecBinary() bool {
  175. return f.d.be // f.d.hh.isBinaryEncoding()
  176. }
  177. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  178. func (f genHelperDecoder) DecSwallow() { f.d.swallow() }
  179. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  180. func (f genHelperDecoder) DecScratchBuffer() []byte {
  181. return f.d.b[:]
  182. }
  183. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  184. func (f genHelperDecoder) DecScratchArrayBuffer() *[decScratchByteArrayLen]byte {
  185. return &f.d.b
  186. }
  187. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  188. func (f genHelperDecoder) DecFallback(iv interface{}, chkPtr bool) {
  189. // println(">>>>>>>>> DecFallback")
  190. rv := reflect.ValueOf(iv)
  191. if chkPtr {
  192. rv = f.d.ensureDecodeable(rv)
  193. }
  194. f.d.decodeValue(rv, nil, false)
  195. // f.d.decodeValueFallback(rv)
  196. }
  197. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  198. func (f genHelperDecoder) DecSliceHelperStart() (decSliceHelper, int) {
  199. return f.d.decSliceHelperStart()
  200. }
  201. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  202. func (f genHelperDecoder) DecStructFieldNotFound(index int, name string) {
  203. f.d.structFieldNotFound(index, name)
  204. }
  205. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  206. func (f genHelperDecoder) DecArrayCannotExpand(sliceLen, streamLen int) {
  207. f.d.arrayCannotExpand(sliceLen, streamLen)
  208. }
  209. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  210. func (f genHelperDecoder) DecTextUnmarshal(tm encoding.TextUnmarshaler) {
  211. fnerr := tm.UnmarshalText(f.d.d.DecodeStringAsBytes())
  212. if fnerr != nil {
  213. panic(fnerr)
  214. }
  215. }
  216. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  217. func (f genHelperDecoder) DecJSONUnmarshal(tm jsonUnmarshaler) {
  218. // bs := f.dd.DecodeStringAsBytes()
  219. // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
  220. fnerr := tm.UnmarshalJSON(f.d.nextValueBytes())
  221. if fnerr != nil {
  222. panic(fnerr)
  223. }
  224. }
  225. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  226. func (f genHelperDecoder) DecBinaryUnmarshal(bm encoding.BinaryUnmarshaler) {
  227. fnerr := bm.UnmarshalBinary(f.d.d.DecodeBytes(nil, true))
  228. if fnerr != nil {
  229. panic(fnerr)
  230. }
  231. }
  232. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  233. func (f genHelperDecoder) DecRaw() []byte { return f.d.rawBytes() }
  234. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  235. //
  236. // Deprecated: builtin no longer supported - so we make this method a no-op,
  237. // but leave in-place so that old generated files continue to work without regeneration.
  238. func (f genHelperDecoder) TimeRtidIfBinc() (v uintptr) { return }
  239. // func (f genHelperDecoder) TimeRtidIfBinc() uintptr {
  240. // // Note: builtin is no longer supported - so make this a no-op
  241. // if _, ok := f.d.hh.(*BincHandle); ok {
  242. // return timeTypId
  243. // }
  244. // return 0
  245. // }
  246. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  247. func (f genHelperDecoder) IsJSONHandle() bool {
  248. return f.d.js
  249. }
  250. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  251. func (f genHelperDecoder) I2Rtid(v interface{}) uintptr {
  252. return i2rtid(v)
  253. }
  254. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  255. func (f genHelperDecoder) Extension(rtid uintptr) (xfn *extTypeTagFn) {
  256. return f.d.h.getExt(rtid)
  257. }
  258. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  259. func (f genHelperDecoder) DecExtension(v interface{}, xfFn *extTypeTagFn) {
  260. f.d.d.DecodeExt(v, xfFn.tag, xfFn.ext)
  261. }
  262. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  263. //
  264. // Deprecated: No longer used,
  265. // but leave in-place so that old generated files continue to work without regeneration.
  266. func (f genHelperDecoder) HasExtensions() bool {
  267. return len(f.d.h.extHandle) != 0
  268. }
  269. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  270. //
  271. // Deprecated: No longer used,
  272. // but leave in-place so that old generated files continue to work without regeneration.
  273. func (f genHelperDecoder) DecExt(v interface{}) (r bool) {
  274. if xfFn := f.d.h.getExt(i2rtid(v)); xfFn != nil {
  275. f.d.d.DecodeExt(v, xfFn.tag, xfFn.ext)
  276. return true
  277. }
  278. return false
  279. }
  280. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  281. func (f genHelperDecoder) DecInferLen(clen, maxlen, unit int) (rvlen int) {
  282. return decInferLen(clen, maxlen, unit)
  283. }
  284. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  285. //
  286. // Deprecated: no longer used,
  287. // but leave in-place so that old generated files continue to work without regeneration.
  288. func (f genHelperDecoder) StringView(v []byte) string { return stringView(v) }