gen-helper.go.tmpl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // //+build ignore
  2. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  3. // Use of this source code is governed by a MIT license found in the LICENSE file.
  4. // ************************************************************
  5. // DO NOT EDIT.
  6. // THIS FILE IS AUTO-GENERATED from gen-helper.go.tmpl
  7. // ************************************************************
  8. package codec
  9. import (
  10. "encoding"
  11. "reflect"
  12. )
  13. // This file is used to generate helper code for codecgen.
  14. // The values here i.e. genHelper(En|De)coder are not to be used directly by
  15. // library users. They WILL change continously and without notice.
  16. //
  17. // To help enforce this, we create an unexported type with exported members.
  18. // The only way to get the type is via the one exported type that we control (somewhat).
  19. //
  20. // When static codecs are created for types, they will use this value
  21. // to perform encoding or decoding of primitives or known slice or map types.
  22. // GenHelperEncoder is exported so that it can be used externally by codecgen.
  23. // Library users: DO NOT USE IT DIRECTLY. IT WILL CHANGE CONTINOUSLY WITHOUT NOTICE.
  24. func GenHelperEncoder(e *Encoder) (genHelperEncoder, encDriver) {
  25. return genHelperEncoder{e:e}, e.e
  26. }
  27. // GenHelperDecoder is exported so that it can be used externally by codecgen.
  28. // Library users: DO NOT USE IT DIRECTLY. IT WILL CHANGE CONTINOUSLY WITHOUT NOTICE.
  29. func GenHelperDecoder(d *Decoder) (genHelperDecoder, decDriver) {
  30. return genHelperDecoder{d:d}, d.d
  31. }
  32. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  33. type genHelperEncoder struct {
  34. e *Encoder
  35. F fastpathT
  36. }
  37. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  38. type genHelperDecoder struct {
  39. d *Decoder
  40. F fastpathT
  41. }
  42. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  43. func (f genHelperEncoder) EncBasicHandle() *BasicHandle {
  44. return f.e.h
  45. }
  46. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  47. func (f genHelperEncoder) EncBinary() bool {
  48. return f.e.be // f.e.hh.isBinaryEncoding()
  49. }
  50. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  51. func (f genHelperEncoder) EncFallback(iv interface{}) {
  52. // println(">>>>>>>>> EncFallback")
  53. f.e.encodeI(iv, false, false)
  54. }
  55. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  56. func (f genHelperEncoder) EncTextMarshal(iv encoding.TextMarshaler) {
  57. bs, fnerr := iv.MarshalText()
  58. f.e.marshal(bs, fnerr, false, c_UTF8)
  59. }
  60. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  61. func (f genHelperEncoder) EncJSONMarshal(iv jsonMarshaler) {
  62. bs, fnerr := iv.MarshalJSON()
  63. f.e.marshal(bs, fnerr, true, c_UTF8)
  64. }
  65. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  66. func (f genHelperEncoder) EncBinaryMarshal(iv encoding.BinaryMarshaler) {
  67. bs, fnerr := iv.MarshalBinary()
  68. f.e.marshal(bs, fnerr, false, c_RAW)
  69. }
  70. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  71. func (f genHelperEncoder) TimeRtidIfBinc() uintptr {
  72. if _, ok := f.e.hh.(*BincHandle); ok {
  73. return timeTypId
  74. }
  75. return 0
  76. }
  77. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  78. func (f genHelperEncoder) IsJSONHandle() bool {
  79. return f.e.js
  80. }
  81. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  82. func (f genHelperEncoder) HasExtensions() bool {
  83. return len(f.e.h.extHandle) != 0
  84. }
  85. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  86. func (f genHelperEncoder) EncExt(v interface{}) (r bool) {
  87. rt := reflect.TypeOf(v)
  88. if rt.Kind() == reflect.Ptr {
  89. rt = rt.Elem()
  90. }
  91. rtid := reflect.ValueOf(rt).Pointer()
  92. if xfFn := f.e.h.getExt(rtid); xfFn != nil {
  93. f.e.e.EncodeExt(v, xfFn.tag, xfFn.ext, f.e)
  94. return true
  95. }
  96. return false
  97. }
  98. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  99. func (f genHelperEncoder) EncSendContainerState(c containerState) {
  100. if f.e.cr != nil {
  101. f.e.cr.sendContainerState(c)
  102. }
  103. }
  104. // ---------------- DECODER FOLLOWS -----------------
  105. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  106. func (f genHelperDecoder) DecBasicHandle() *BasicHandle {
  107. return f.d.h
  108. }
  109. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  110. func (f genHelperDecoder) DecBinary() bool {
  111. return f.d.be // f.d.hh.isBinaryEncoding()
  112. }
  113. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  114. func (f genHelperDecoder) DecSwallow() {
  115. f.d.swallow()
  116. }
  117. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  118. func (f genHelperDecoder) DecScratchBuffer() []byte {
  119. return f.d.b[:]
  120. }
  121. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  122. func (f genHelperDecoder) DecFallback(iv interface{}, chkPtr bool) {
  123. // println(">>>>>>>>> DecFallback")
  124. f.d.decodeI(iv, chkPtr, false, false, false)
  125. }
  126. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  127. func (f genHelperDecoder) DecSliceHelperStart() (decSliceHelper, int) {
  128. return f.d.decSliceHelperStart()
  129. }
  130. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  131. func (f genHelperDecoder) DecStructFieldNotFound(index int, name string) {
  132. f.d.structFieldNotFound(index, name)
  133. }
  134. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  135. func (f genHelperDecoder) DecArrayCannotExpand(sliceLen, streamLen int) {
  136. f.d.arrayCannotExpand(sliceLen, streamLen)
  137. }
  138. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  139. func (f genHelperDecoder) DecTextUnmarshal(tm encoding.TextUnmarshaler) {
  140. fnerr := tm.UnmarshalText(f.d.d.DecodeBytes(f.d.b[:], true, true))
  141. if fnerr != nil {
  142. panic(fnerr)
  143. }
  144. }
  145. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  146. func (f genHelperDecoder) DecJSONUnmarshal(tm jsonUnmarshaler) {
  147. // bs := f.dd.DecodeBytes(f.d.b[:], true, true)
  148. // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
  149. fnerr := tm.UnmarshalJSON(f.d.nextValueBytes())
  150. if fnerr != nil {
  151. panic(fnerr)
  152. }
  153. }
  154. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  155. func (f genHelperDecoder) DecBinaryUnmarshal(bm encoding.BinaryUnmarshaler) {
  156. fnerr := bm.UnmarshalBinary(f.d.d.DecodeBytes(nil, false, true))
  157. if fnerr != nil {
  158. panic(fnerr)
  159. }
  160. }
  161. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  162. func (f genHelperDecoder) TimeRtidIfBinc() uintptr {
  163. if _, ok := f.d.hh.(*BincHandle); ok {
  164. return timeTypId
  165. }
  166. return 0
  167. }
  168. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  169. func (f genHelperDecoder) IsJSONHandle() bool {
  170. return f.d.js
  171. }
  172. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  173. func (f genHelperDecoder) HasExtensions() bool {
  174. return len(f.d.h.extHandle) != 0
  175. }
  176. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  177. func (f genHelperDecoder) DecExt(v interface{}) (r bool) {
  178. rt := reflect.TypeOf(v).Elem()
  179. rtid := reflect.ValueOf(rt).Pointer()
  180. if xfFn := f.d.h.getExt(rtid); xfFn != nil {
  181. f.d.d.DecodeExt(v, xfFn.tag, xfFn.ext)
  182. return true
  183. }
  184. return false
  185. }
  186. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  187. func (f genHelperDecoder) DecInferLen(clen, maxlen, unit int) (rvlen int, truncated bool) {
  188. return decInferLen(clen, maxlen, unit)
  189. }
  190. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  191. func (f genHelperDecoder) DecSendContainerState(c containerState) {
  192. if f.d.cr != nil {
  193. f.d.cr.sendContainerState(c)
  194. }
  195. }
  196. {{/*
  197. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  198. func (f genHelperEncoder) EncDriver() encDriver {
  199. return f.e.e
  200. }
  201. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  202. func (f genHelperDecoder) DecDriver() decDriver {
  203. return f.d.d
  204. }
  205. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  206. func (f genHelperEncoder) EncNil() {
  207. f.e.e.EncodeNil()
  208. }
  209. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  210. func (f genHelperEncoder) EncBytes(v []byte) {
  211. f.e.e.EncodeStringBytes(c_RAW, v)
  212. }
  213. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  214. func (f genHelperEncoder) EncArrayStart(length int) {
  215. f.e.e.EncodeArrayStart(length)
  216. }
  217. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  218. func (f genHelperEncoder) EncArrayEnd() {
  219. f.e.e.EncodeArrayEnd()
  220. }
  221. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  222. func (f genHelperEncoder) EncArrayEntrySeparator() {
  223. f.e.e.EncodeArrayEntrySeparator()
  224. }
  225. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  226. func (f genHelperEncoder) EncMapStart(length int) {
  227. f.e.e.EncodeMapStart(length)
  228. }
  229. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  230. func (f genHelperEncoder) EncMapEnd() {
  231. f.e.e.EncodeMapEnd()
  232. }
  233. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  234. func (f genHelperEncoder) EncMapEntrySeparator() {
  235. f.e.e.EncodeMapEntrySeparator()
  236. }
  237. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  238. func (f genHelperEncoder) EncMapKVSeparator() {
  239. f.e.e.EncodeMapKVSeparator()
  240. }
  241. // ---------
  242. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  243. func (f genHelperDecoder) DecBytes(v *[]byte) {
  244. *v = f.d.d.DecodeBytes(*v)
  245. }
  246. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  247. func (f genHelperDecoder) DecTryNil() bool {
  248. return f.d.d.TryDecodeAsNil()
  249. }
  250. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  251. func (f genHelperDecoder) DecContainerIsNil() (b bool) {
  252. return f.d.d.IsContainerType(valueTypeNil)
  253. }
  254. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  255. func (f genHelperDecoder) DecContainerIsMap() (b bool) {
  256. return f.d.d.IsContainerType(valueTypeMap)
  257. }
  258. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  259. func (f genHelperDecoder) DecContainerIsArray() (b bool) {
  260. return f.d.d.IsContainerType(valueTypeArray)
  261. }
  262. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  263. func (f genHelperDecoder) DecCheckBreak() bool {
  264. return f.d.d.CheckBreak()
  265. }
  266. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  267. func (f genHelperDecoder) DecMapStart() int {
  268. return f.d.d.ReadMapStart()
  269. }
  270. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  271. func (f genHelperDecoder) DecArrayStart() int {
  272. return f.d.d.ReadArrayStart()
  273. }
  274. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  275. func (f genHelperDecoder) DecMapEnd() {
  276. f.d.d.ReadMapEnd()
  277. }
  278. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  279. func (f genHelperDecoder) DecArrayEnd() {
  280. f.d.d.ReadArrayEnd()
  281. }
  282. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  283. func (f genHelperDecoder) DecArrayEntrySeparator() {
  284. f.d.d.ReadArrayEntrySeparator()
  285. }
  286. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  287. func (f genHelperDecoder) DecMapEntrySeparator() {
  288. f.d.d.ReadMapEntrySeparator()
  289. }
  290. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  291. func (f genHelperDecoder) DecMapKVSeparator() {
  292. f.d.d.ReadMapKVSeparator()
  293. }
  294. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  295. func (f genHelperDecoder) ReadStringAsBytes(bs []byte) []byte {
  296. return f.d.d.DecodeStringAsBytes(bs)
  297. }
  298. // -- encode calls (primitives)
  299. {{range .Values}}{{if .Primitive }}{{if ne .Primitive "interface{}" }}
  300. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  301. func (f genHelperEncoder) {{ .MethodNamePfx "Enc" true }}(v {{ .Primitive }}) {
  302. ee := f.e.e
  303. {{ encmd .Primitive "v" }}
  304. }
  305. {{ end }}{{ end }}{{ end }}
  306. // -- decode calls (primitives)
  307. {{range .Values}}{{if .Primitive }}{{if ne .Primitive "interface{}" }}
  308. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  309. func (f genHelperDecoder) {{ .MethodNamePfx "Dec" true }}(vp *{{ .Primitive }}) {
  310. dd := f.d.d
  311. *vp = {{ decmd .Primitive }}
  312. }
  313. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  314. func (f genHelperDecoder) {{ .MethodNamePfx "Read" true }}() (v {{ .Primitive }}) {
  315. dd := f.d.d
  316. v = {{ decmd .Primitive }}
  317. return
  318. }
  319. {{ end }}{{ end }}{{ end }}
  320. // -- encode calls (slices/maps)
  321. {{range .Values}}{{if not .Primitive }}{{if .Slice }}
  322. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  323. func (f genHelperEncoder) {{ .MethodNamePfx "Enc" false }}(v []{{ .Elem }}) { {{ else }}
  324. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  325. func (f genHelperEncoder) {{ .MethodNamePfx "Enc" false }}(v map[{{ .MapKey }}]{{ .Elem }}) { {{end}}
  326. f.F.{{ .MethodNamePfx "Enc" false }}V(v, false, f.e)
  327. }
  328. {{ end }}{{ end }}
  329. // -- decode calls (slices/maps)
  330. {{range .Values}}{{if not .Primitive }}
  331. // FOR USE BY CODECGEN ONLY. IT *WILL* CHANGE WITHOUT NOTICE. *DO NOT USE*
  332. {{if .Slice }}func (f genHelperDecoder) {{ .MethodNamePfx "Dec" false }}(vp *[]{{ .Elem }}) {
  333. {{else}}func (f genHelperDecoder) {{ .MethodNamePfx "Dec" false }}(vp *map[{{ .MapKey }}]{{ .Elem }}) { {{end}}
  334. v, changed := f.F.{{ .MethodNamePfx "Dec" false }}V(*vp, false, true, f.d)
  335. if changed {
  336. *vp = v
  337. }
  338. }
  339. {{ end }}{{ end }}
  340. */}}