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

gen.go 64KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. // +build codecgen.exec
  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. package codec
  5. import (
  6. "bytes"
  7. "encoding/base64"
  8. "errors"
  9. "fmt"
  10. "go/format"
  11. "io"
  12. "io/ioutil"
  13. "math/rand"
  14. "reflect"
  15. "regexp"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. "text/template"
  21. "time"
  22. "unicode"
  23. "unicode/utf8"
  24. )
  25. // ---------------------------------------------------
  26. // codecgen supports the full cycle of reflection-based codec:
  27. // - RawExt
  28. // - Raw
  29. // - Extensions
  30. // - (Binary|Text|JSON)(Unm|M)arshal
  31. // - generic by-kind
  32. //
  33. // This means that, for dynamic things, we MUST use reflection to at least get the reflect.Type.
  34. // In those areas, we try to only do reflection or interface-conversion when NECESSARY:
  35. // - Extensions, only if Extensions are configured.
  36. //
  37. // However, codecgen doesn't support the following:
  38. // - Canonical option. (codecgen IGNORES it currently)
  39. // This is just because it has not been implemented.
  40. // - MissingFielder implementation.
  41. // If a type implements MissingFielder, it is completely ignored by codecgen.
  42. //
  43. // During encode/decode, Selfer takes precedence.
  44. // A type implementing Selfer will know how to encode/decode itself statically.
  45. //
  46. // The following field types are supported:
  47. // array: [n]T
  48. // slice: []T
  49. // map: map[K]V
  50. // primitive: [u]int[n], float(32|64), bool, string
  51. // struct
  52. //
  53. // ---------------------------------------------------
  54. // Note that a Selfer cannot call (e|d).(En|De)code on itself,
  55. // as this will cause a circular reference, as (En|De)code will call Selfer methods.
  56. // Any type that implements Selfer must implement completely and not fallback to (En|De)code.
  57. //
  58. // In addition, code in this file manages the generation of fast-path implementations of
  59. // encode/decode of slices/maps of primitive keys/values.
  60. //
  61. // Users MUST re-generate their implementations whenever the code shape changes.
  62. // The generated code will panic if it was generated with a version older than the supporting library.
  63. // ---------------------------------------------------
  64. //
  65. // codec framework is very feature rich.
  66. // When encoding or decoding into an interface, it depends on the runtime type of the interface.
  67. // The type of the interface may be a named type, an extension, etc.
  68. // Consequently, we fallback to runtime codec for encoding/decoding interfaces.
  69. // In addition, we fallback for any value which cannot be guaranteed at runtime.
  70. // This allows us support ANY value, including any named types, specifically those which
  71. // do not implement our interfaces (e.g. Selfer).
  72. //
  73. // This explains some slowness compared to other code generation codecs (e.g. msgp).
  74. // This reduction in speed is only seen when your refers to interfaces,
  75. // e.g. type T struct { A interface{}; B []interface{}; C map[string]interface{} }
  76. //
  77. // codecgen will panic if the file was generated with an old version of the library in use.
  78. //
  79. // Note:
  80. // It was a conscious decision to have gen.go always explicitly call EncodeNil or TryDecodeAsNil.
  81. // This way, there isn't a function call overhead just to see that we should not enter a block of code.
  82. //
  83. // Note:
  84. // codecgen-generated code depends on the variables defined by fast-path.generated.go.
  85. // consequently, you cannot run with tags "codecgen notfastpath".
  86. // GenVersion is the current version of codecgen.
  87. //
  88. // NOTE: Increment this value each time codecgen changes fundamentally.
  89. // Fundamental changes are:
  90. // - helper methods change (signature change, new ones added, some removed, etc)
  91. // - codecgen command line changes
  92. //
  93. // v1: Initial Version
  94. // v2:
  95. // v3: Changes for Kubernetes:
  96. // changes in signature of some unpublished helper methods and codecgen cmdline arguments.
  97. // v4: Removed separator support from (en|de)cDriver, and refactored codec(gen)
  98. // v5: changes to support faster json decoding. Let encoder/decoder maintain state of collections.
  99. // v6: removed unsafe from gen, and now uses codecgen.exec tag
  100. // v7:
  101. // v8: current - we now maintain compatibility with old generated code.
  102. // v9: skipped
  103. // v10: modified encDriver and decDriver interfaces. Remove deprecated methods after Jan 1, 2019
  104. const genVersion = 10
  105. const (
  106. genCodecPkg = "codec1978"
  107. genTempVarPfx = "yy"
  108. genTopLevelVarName = "x"
  109. // ignore canBeNil parameter, and always set to true.
  110. // This is because nil can appear anywhere, so we should always check.
  111. genAnythingCanBeNil = true
  112. // if genUseOneFunctionForDecStructMap, make a single codecDecodeSelferFromMap function;
  113. // else make codecDecodeSelferFromMap{LenPrefix,CheckBreak} so that conditionals
  114. // are not executed a lot.
  115. //
  116. // From testing, it didn't make much difference in runtime, so keep as true (one function only)
  117. genUseOneFunctionForDecStructMap = true
  118. )
  119. type genStructMapStyle uint8
  120. const (
  121. genStructMapStyleConsolidated genStructMapStyle = iota
  122. genStructMapStyleLenPrefix
  123. genStructMapStyleCheckBreak
  124. )
  125. var (
  126. errGenAllTypesSamePkg = errors.New("All types must be in the same package")
  127. errGenExpectArrayOrMap = errors.New("unexpected type. Expecting array/map/slice")
  128. genBase64enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__")
  129. genQNameRegex = regexp.MustCompile(`[A-Za-z_.]+`)
  130. )
  131. type genBuf struct {
  132. buf []byte
  133. }
  134. func (x *genBuf) s(s string) *genBuf { x.buf = append(x.buf, s...); return x }
  135. func (x *genBuf) b(s []byte) *genBuf { x.buf = append(x.buf, s...); return x }
  136. func (x *genBuf) v() string { return string(x.buf) }
  137. func (x *genBuf) f(s string, args ...interface{}) { x.s(fmt.Sprintf(s, args...)) }
  138. func (x *genBuf) reset() {
  139. if x.buf != nil {
  140. x.buf = x.buf[:0]
  141. }
  142. }
  143. // genRunner holds some state used during a Gen run.
  144. type genRunner struct {
  145. w io.Writer // output
  146. c uint64 // counter used for generating varsfx
  147. t []reflect.Type // list of types to run selfer on
  148. tc reflect.Type // currently running selfer on this type
  149. te map[uintptr]bool // types for which the encoder has been created
  150. td map[uintptr]bool // types for which the decoder has been created
  151. cp string // codec import path
  152. im map[string]reflect.Type // imports to add
  153. imn map[string]string // package names of imports to add
  154. imc uint64 // counter for import numbers
  155. is map[reflect.Type]struct{} // types seen during import search
  156. bp string // base PkgPath, for which we are generating for
  157. cpfx string // codec package prefix
  158. tm map[reflect.Type]struct{} // types for which enc/dec must be generated
  159. ts []reflect.Type // types for which enc/dec must be generated
  160. xs string // top level variable/constant suffix
  161. hn string // fn helper type name
  162. ti *TypeInfos
  163. // rr *rand.Rand // random generator for file-specific types
  164. nx bool // no extensions
  165. }
  166. // Gen will write a complete go file containing Selfer implementations for each
  167. // type passed. All the types must be in the same package.
  168. //
  169. // Library users: DO NOT USE IT DIRECTLY. IT WILL CHANGE CONTINUOUSLY WITHOUT NOTICE.
  170. func Gen(w io.Writer, buildTags, pkgName, uid string, noExtensions bool,
  171. ti *TypeInfos, typ ...reflect.Type) {
  172. // All types passed to this method do not have a codec.Selfer method implemented directly.
  173. // codecgen already checks the AST and skips any types that define the codec.Selfer methods.
  174. // Consequently, there's no need to check and trim them if they implement codec.Selfer
  175. if len(typ) == 0 {
  176. return
  177. }
  178. x := genRunner{
  179. w: w,
  180. t: typ,
  181. te: make(map[uintptr]bool),
  182. td: make(map[uintptr]bool),
  183. im: make(map[string]reflect.Type),
  184. imn: make(map[string]string),
  185. is: make(map[reflect.Type]struct{}),
  186. tm: make(map[reflect.Type]struct{}),
  187. ts: []reflect.Type{},
  188. bp: genImportPath(typ[0]),
  189. xs: uid,
  190. ti: ti,
  191. nx: noExtensions,
  192. }
  193. if x.ti == nil {
  194. x.ti = defTypeInfos
  195. }
  196. if x.xs == "" {
  197. rr := rand.New(rand.NewSource(time.Now().UnixNano()))
  198. x.xs = strconv.FormatInt(rr.Int63n(9999), 10)
  199. }
  200. // gather imports first:
  201. x.cp = genImportPath(reflect.TypeOf(x))
  202. x.imn[x.cp] = genCodecPkg
  203. for _, t := range typ {
  204. // fmt.Printf("###########: PkgPath: '%v', Name: '%s'\n", genImportPath(t), t.Name())
  205. if genImportPath(t) != x.bp {
  206. panic(errGenAllTypesSamePkg)
  207. }
  208. x.genRefPkgs(t)
  209. }
  210. if buildTags != "" {
  211. x.line("// +build " + buildTags)
  212. x.line("")
  213. }
  214. x.line(`
  215. // Code generated by codecgen - DO NOT EDIT.
  216. `)
  217. x.line("package " + pkgName)
  218. x.line("")
  219. x.line("import (")
  220. if x.cp != x.bp {
  221. x.cpfx = genCodecPkg + "."
  222. x.linef("%s \"%s\"", genCodecPkg, x.cp)
  223. }
  224. // use a sorted set of im keys, so that we can get consistent output
  225. imKeys := make([]string, 0, len(x.im))
  226. for k := range x.im {
  227. imKeys = append(imKeys, k)
  228. }
  229. sort.Strings(imKeys)
  230. for _, k := range imKeys { // for k, _ := range x.im {
  231. if k == x.imn[k] {
  232. x.linef("\"%s\"", k)
  233. } else {
  234. x.linef("%s \"%s\"", x.imn[k], k)
  235. }
  236. }
  237. // add required packages
  238. for _, k := range [...]string{"runtime", "errors", "strconv"} { // "reflect", "fmt"
  239. if _, ok := x.im[k]; !ok {
  240. x.line("\"" + k + "\"")
  241. }
  242. }
  243. x.line(")")
  244. x.line("")
  245. x.line("const (")
  246. x.linef("// ----- content types ----")
  247. x.linef("codecSelferCcUTF8%s = %v", x.xs, int64(cUTF8))
  248. x.linef("codecSelferCcRAW%s = %v", x.xs, int64(cRAW))
  249. x.linef("// ----- value types used ----")
  250. for _, vt := range [...]valueType{
  251. valueTypeArray, valueTypeMap, valueTypeString,
  252. valueTypeInt, valueTypeUint, valueTypeFloat} {
  253. x.linef("codecSelferValueType%s%s = %v", vt.String(), x.xs, int64(vt))
  254. }
  255. x.linef("codecSelferBitsize%s = uint8(32 << (^uint(0) >> 63))", x.xs)
  256. x.line(")")
  257. x.line("var (")
  258. x.line("errCodecSelferOnlyMapOrArrayEncodeToStruct" + x.xs + " = errors.New(`only encoded map or array can be decoded into a struct`)")
  259. x.line(")")
  260. x.line("")
  261. x.hn = "codecSelfer" + x.xs
  262. x.line("type " + x.hn + " struct{}")
  263. x.line("")
  264. x.varsfxreset()
  265. x.line("func init() {")
  266. x.linef("if %sGenVersion != %v {", x.cpfx, genVersion)
  267. x.line("_, file, _, _ := runtime.Caller(0)")
  268. x.outf(`panic("codecgen version mismatch: current: %v, need " + strconv.FormatInt(int64(%sGenVersion), 10) + ". Re-generate file: " + file)`, genVersion, x.cpfx)
  269. // x.out(`panic(fmt.Errorf("codecgen version mismatch: current: %v, need %v. Re-generate file: %v", `)
  270. // x.linef(`%v, %sGenVersion, file))`, genVersion, x.cpfx)
  271. x.linef("}")
  272. x.line("if false { var _ byte = 0; // reference the types, but skip this branch at build/run time")
  273. // x.line("_ = strconv.ParseInt")
  274. var n int
  275. // for k, t := range x.im {
  276. for _, k := range imKeys {
  277. t := x.im[k]
  278. x.linef("var v%v %s.%s", n, x.imn[k], t.Name())
  279. n++
  280. }
  281. if n > 0 {
  282. x.out("_")
  283. for i := 1; i < n; i++ {
  284. x.out(", _")
  285. }
  286. x.out(" = v0")
  287. for i := 1; i < n; i++ {
  288. x.outf(", v%v", i)
  289. }
  290. }
  291. x.line("} ") // close if false
  292. x.line("}") // close init
  293. x.line("")
  294. // generate rest of type info
  295. for _, t := range typ {
  296. x.tc = t
  297. x.selfer(true)
  298. x.selfer(false)
  299. }
  300. for _, t := range x.ts {
  301. rtid := rt2id(t)
  302. // generate enc functions for all these slice/map types.
  303. x.varsfxreset()
  304. x.linef("func (x %s) enc%s(v %s%s, e *%sEncoder) {", x.hn, x.genMethodNameT(t), x.arr2str(t, "*"), x.genTypeName(t), x.cpfx)
  305. x.genRequiredMethodVars(true)
  306. switch t.Kind() {
  307. case reflect.Array, reflect.Slice, reflect.Chan:
  308. x.encListFallback("v", t)
  309. case reflect.Map:
  310. x.encMapFallback("v", t)
  311. default:
  312. panic(errGenExpectArrayOrMap)
  313. }
  314. x.line("}")
  315. x.line("")
  316. // generate dec functions for all these slice/map types.
  317. x.varsfxreset()
  318. x.linef("func (x %s) dec%s(v *%s, d *%sDecoder) {", x.hn, x.genMethodNameT(t), x.genTypeName(t), x.cpfx)
  319. x.genRequiredMethodVars(false)
  320. switch t.Kind() {
  321. case reflect.Array, reflect.Slice, reflect.Chan:
  322. x.decListFallback("v", rtid, t)
  323. case reflect.Map:
  324. x.decMapFallback("v", rtid, t)
  325. default:
  326. panic(errGenExpectArrayOrMap)
  327. }
  328. x.line("}")
  329. x.line("")
  330. }
  331. x.line("")
  332. }
  333. func (x *genRunner) checkForSelfer(t reflect.Type, varname string) bool {
  334. // return varname != genTopLevelVarName && t != x.tc
  335. // the only time we checkForSelfer is if we are not at the TOP of the generated code.
  336. return varname != genTopLevelVarName
  337. }
  338. func (x *genRunner) arr2str(t reflect.Type, s string) string {
  339. if t.Kind() == reflect.Array {
  340. return s
  341. }
  342. return ""
  343. }
  344. func (x *genRunner) genRequiredMethodVars(encode bool) {
  345. x.line("var h " + x.hn)
  346. if encode {
  347. x.line("z, r := " + x.cpfx + "GenHelperEncoder(e)")
  348. } else {
  349. x.line("z, r := " + x.cpfx + "GenHelperDecoder(d)")
  350. }
  351. x.line("_, _, _ = h, z, r")
  352. }
  353. func (x *genRunner) genRefPkgs(t reflect.Type) {
  354. if _, ok := x.is[t]; ok {
  355. return
  356. }
  357. x.is[t] = struct{}{}
  358. tpkg, tname := genImportPath(t), t.Name()
  359. if tpkg != "" && tpkg != x.bp && tpkg != x.cp && tname != "" && tname[0] >= 'A' && tname[0] <= 'Z' {
  360. if _, ok := x.im[tpkg]; !ok {
  361. x.im[tpkg] = t
  362. if idx := strings.LastIndex(tpkg, "/"); idx < 0 {
  363. x.imn[tpkg] = tpkg
  364. } else {
  365. x.imc++
  366. x.imn[tpkg] = "pkg" + strconv.FormatUint(x.imc, 10) + "_" + genGoIdentifier(tpkg[idx+1:], false)
  367. }
  368. }
  369. }
  370. switch t.Kind() {
  371. case reflect.Array, reflect.Slice, reflect.Ptr, reflect.Chan:
  372. x.genRefPkgs(t.Elem())
  373. case reflect.Map:
  374. x.genRefPkgs(t.Elem())
  375. x.genRefPkgs(t.Key())
  376. case reflect.Struct:
  377. for i := 0; i < t.NumField(); i++ {
  378. if fname := t.Field(i).Name; fname != "" && fname[0] >= 'A' && fname[0] <= 'Z' {
  379. x.genRefPkgs(t.Field(i).Type)
  380. }
  381. }
  382. }
  383. }
  384. func (x *genRunner) varsfx() string {
  385. x.c++
  386. return strconv.FormatUint(x.c, 10)
  387. }
  388. func (x *genRunner) varsfxreset() {
  389. x.c = 0
  390. }
  391. func (x *genRunner) out(s string) {
  392. _, err := io.WriteString(x.w, s)
  393. if err != nil {
  394. panic(err)
  395. }
  396. }
  397. func (x *genRunner) outf(s string, params ...interface{}) {
  398. _, err := fmt.Fprintf(x.w, s, params...)
  399. if err != nil {
  400. panic(err)
  401. }
  402. }
  403. func (x *genRunner) line(s string) {
  404. x.out(s)
  405. if len(s) == 0 || s[len(s)-1] != '\n' {
  406. x.out("\n")
  407. }
  408. }
  409. func (x *genRunner) linef(s string, params ...interface{}) {
  410. x.outf(s, params...)
  411. if len(s) == 0 || s[len(s)-1] != '\n' {
  412. x.out("\n")
  413. }
  414. }
  415. func (x *genRunner) genTypeName(t reflect.Type) (n string) {
  416. // defer func() { fmt.Printf(">>>> ####: genTypeName: t: %v, name: '%s'\n", t, n) }()
  417. // if the type has a PkgPath, which doesn't match the current package,
  418. // then include it.
  419. // We cannot depend on t.String() because it includes current package,
  420. // or t.PkgPath because it includes full import path,
  421. //
  422. var ptrPfx string
  423. for t.Kind() == reflect.Ptr {
  424. ptrPfx += "*"
  425. t = t.Elem()
  426. }
  427. if tn := t.Name(); tn != "" {
  428. return ptrPfx + x.genTypeNamePrim(t)
  429. }
  430. switch t.Kind() {
  431. case reflect.Map:
  432. return ptrPfx + "map[" + x.genTypeName(t.Key()) + "]" + x.genTypeName(t.Elem())
  433. case reflect.Slice:
  434. return ptrPfx + "[]" + x.genTypeName(t.Elem())
  435. case reflect.Array:
  436. return ptrPfx + "[" + strconv.FormatInt(int64(t.Len()), 10) + "]" + x.genTypeName(t.Elem())
  437. case reflect.Chan:
  438. return ptrPfx + t.ChanDir().String() + " " + x.genTypeName(t.Elem())
  439. default:
  440. if t == intfTyp {
  441. return ptrPfx + "interface{}"
  442. } else {
  443. return ptrPfx + x.genTypeNamePrim(t)
  444. }
  445. }
  446. }
  447. func (x *genRunner) genTypeNamePrim(t reflect.Type) (n string) {
  448. if t.Name() == "" {
  449. return t.String()
  450. } else if genImportPath(t) == "" || genImportPath(t) == genImportPath(x.tc) {
  451. return t.Name()
  452. } else {
  453. return x.imn[genImportPath(t)] + "." + t.Name()
  454. // return t.String() // best way to get the package name inclusive
  455. }
  456. }
  457. func (x *genRunner) genZeroValueR(t reflect.Type) string {
  458. // if t is a named type, w
  459. switch t.Kind() {
  460. case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func,
  461. reflect.Slice, reflect.Map, reflect.Invalid:
  462. return "nil"
  463. case reflect.Bool:
  464. return "false"
  465. case reflect.String:
  466. return `""`
  467. case reflect.Struct, reflect.Array:
  468. return x.genTypeName(t) + "{}"
  469. default: // all numbers
  470. return "0"
  471. }
  472. }
  473. func (x *genRunner) genMethodNameT(t reflect.Type) (s string) {
  474. return genMethodNameT(t, x.tc)
  475. }
  476. func (x *genRunner) selfer(encode bool) {
  477. t := x.tc
  478. t0 := t
  479. // always make decode use a pointer receiver,
  480. // and structs/arrays always use a ptr receiver (encode|decode)
  481. isptr := !encode || t.Kind() == reflect.Array || (t.Kind() == reflect.Struct && t != timeTyp)
  482. x.varsfxreset()
  483. fnSigPfx := "func (" + genTopLevelVarName + " "
  484. if isptr {
  485. fnSigPfx += "*"
  486. }
  487. fnSigPfx += x.genTypeName(t)
  488. x.out(fnSigPfx)
  489. if isptr {
  490. t = reflect.PtrTo(t)
  491. }
  492. if encode {
  493. x.line(") CodecEncodeSelf(e *" + x.cpfx + "Encoder) {")
  494. x.genRequiredMethodVars(true)
  495. x.encVar(genTopLevelVarName, t)
  496. } else {
  497. x.line(") CodecDecodeSelf(d *" + x.cpfx + "Decoder) {")
  498. x.genRequiredMethodVars(false)
  499. // do not use decVar, as there is no need to check TryDecodeAsNil
  500. // or way to elegantly handle that, and also setting it to a
  501. // non-nil value doesn't affect the pointer passed.
  502. // x.decVar(genTopLevelVarName, t, false)
  503. x.dec(genTopLevelVarName, t0, true)
  504. }
  505. x.line("}")
  506. x.line("")
  507. if encode || t0.Kind() != reflect.Struct {
  508. return
  509. }
  510. // write is containerMap
  511. if genUseOneFunctionForDecStructMap {
  512. x.out(fnSigPfx)
  513. x.line(") codecDecodeSelfFromMap(l int, d *" + x.cpfx + "Decoder) {")
  514. x.genRequiredMethodVars(false)
  515. x.decStructMap(genTopLevelVarName, "l", rt2id(t0), t0, genStructMapStyleConsolidated)
  516. x.line("}")
  517. x.line("")
  518. } else {
  519. x.out(fnSigPfx)
  520. x.line(") codecDecodeSelfFromMapLenPrefix(l int, d *" + x.cpfx + "Decoder) {")
  521. x.genRequiredMethodVars(false)
  522. x.decStructMap(genTopLevelVarName, "l", rt2id(t0), t0, genStructMapStyleLenPrefix)
  523. x.line("}")
  524. x.line("")
  525. x.out(fnSigPfx)
  526. x.line(") codecDecodeSelfFromMapCheckBreak(l int, d *" + x.cpfx + "Decoder) {")
  527. x.genRequiredMethodVars(false)
  528. x.decStructMap(genTopLevelVarName, "l", rt2id(t0), t0, genStructMapStyleCheckBreak)
  529. x.line("}")
  530. x.line("")
  531. }
  532. // write containerArray
  533. x.out(fnSigPfx)
  534. x.line(") codecDecodeSelfFromArray(l int, d *" + x.cpfx + "Decoder) {")
  535. x.genRequiredMethodVars(false)
  536. x.decStructArray(genTopLevelVarName, "l", "return", rt2id(t0), t0)
  537. x.line("}")
  538. x.line("")
  539. }
  540. // used for chan, array, slice, map
  541. func (x *genRunner) xtraSM(varname string, t reflect.Type, encode, isptr bool) {
  542. var ptrPfx, addrPfx string
  543. if isptr {
  544. ptrPfx = "*"
  545. } else {
  546. addrPfx = "&"
  547. }
  548. if encode {
  549. x.linef("h.enc%s((%s%s)(%s), e)", x.genMethodNameT(t), ptrPfx, x.genTypeName(t), varname)
  550. } else {
  551. x.linef("h.dec%s((*%s)(%s%s), d)", x.genMethodNameT(t), x.genTypeName(t), addrPfx, varname)
  552. }
  553. x.registerXtraT(t)
  554. }
  555. func (x *genRunner) registerXtraT(t reflect.Type) {
  556. // recursively register the types
  557. if _, ok := x.tm[t]; ok {
  558. return
  559. }
  560. var tkey reflect.Type
  561. switch t.Kind() {
  562. case reflect.Chan, reflect.Slice, reflect.Array:
  563. case reflect.Map:
  564. tkey = t.Key()
  565. default:
  566. return
  567. }
  568. x.tm[t] = struct{}{}
  569. x.ts = append(x.ts, t)
  570. // check if this refers to any xtra types eg. a slice of array: add the array
  571. x.registerXtraT(t.Elem())
  572. if tkey != nil {
  573. x.registerXtraT(tkey)
  574. }
  575. }
  576. // encVar will encode a variable.
  577. // The parameter, t, is the reflect.Type of the variable itself
  578. func (x *genRunner) encVar(varname string, t reflect.Type) {
  579. // fmt.Printf(">>>>>> varname: %s, t: %v\n", varname, t)
  580. var checkNil bool
  581. switch t.Kind() {
  582. case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map, reflect.Chan:
  583. checkNil = true
  584. }
  585. if checkNil {
  586. x.linef("if %s == nil { r.EncodeNil() } else { ", varname)
  587. }
  588. switch t.Kind() {
  589. case reflect.Ptr:
  590. telem := t.Elem()
  591. tek := telem.Kind()
  592. if tek == reflect.Array || (tek == reflect.Struct && telem != timeTyp) {
  593. x.enc(varname, genNonPtr(t))
  594. break
  595. }
  596. i := x.varsfx()
  597. x.line(genTempVarPfx + i + " := *" + varname)
  598. x.enc(genTempVarPfx+i, genNonPtr(t))
  599. case reflect.Struct, reflect.Array:
  600. if t == timeTyp {
  601. x.enc(varname, t)
  602. break
  603. }
  604. i := x.varsfx()
  605. x.line(genTempVarPfx + i + " := &" + varname)
  606. x.enc(genTempVarPfx+i, t)
  607. default:
  608. x.enc(varname, t)
  609. }
  610. if checkNil {
  611. x.line("}")
  612. }
  613. }
  614. // enc will encode a variable (varname) of type t, where t represents T.
  615. // if t is !time.Time and t is of kind reflect.Struct or reflect.Array, varname is of type *T
  616. // (to prevent copying),
  617. // else t is of type T
  618. func (x *genRunner) enc(varname string, t reflect.Type) {
  619. rtid := rt2id(t)
  620. ti2 := x.ti.get(rtid, t)
  621. // We call CodecEncodeSelf if one of the following are honored:
  622. // - the type already implements Selfer, call that
  623. // - the type has a Selfer implementation just created, use that
  624. // - the type is in the list of the ones we will generate for, but it is not currently being generated
  625. mi := x.varsfx()
  626. // tptr := reflect.PtrTo(t)
  627. tk := t.Kind()
  628. if x.checkForSelfer(t, varname) {
  629. if tk == reflect.Array || (tk == reflect.Struct && rtid != timeTypId) { // varname is of type *T
  630. // if tptr.Implements(selferTyp) || t.Implements(selferTyp) {
  631. if ti2.isFlag(typeInfoFlagIsZeroerPtr) || ti2.isFlag(typeInfoFlagIsZeroer) {
  632. x.line(varname + ".CodecEncodeSelf(e)")
  633. return
  634. }
  635. } else { // varname is of type T
  636. if ti2.cs { // t.Implements(selferTyp) {
  637. x.line(varname + ".CodecEncodeSelf(e)")
  638. return
  639. } else if ti2.csp { // tptr.Implements(selferTyp) {
  640. x.linef("%ssf%s := &%s", genTempVarPfx, mi, varname)
  641. x.linef("%ssf%s.CodecEncodeSelf(e)", genTempVarPfx, mi)
  642. return
  643. }
  644. }
  645. if _, ok := x.te[rtid]; ok {
  646. x.line(varname + ".CodecEncodeSelf(e)")
  647. return
  648. }
  649. }
  650. inlist := false
  651. for _, t0 := range x.t {
  652. if t == t0 {
  653. inlist = true
  654. if x.checkForSelfer(t, varname) {
  655. x.line(varname + ".CodecEncodeSelf(e)")
  656. return
  657. }
  658. break
  659. }
  660. }
  661. var rtidAdded bool
  662. if t == x.tc {
  663. x.te[rtid] = true
  664. rtidAdded = true
  665. }
  666. // check if
  667. // - type is time.Time, RawExt, Raw
  668. // - the type implements (Text|JSON|Binary)(Unm|M)arshal
  669. x.line("if false {") //start if block
  670. defer func() { x.line("}") }() //end if block
  671. if t == timeTyp {
  672. x.linef("} else if !z.EncBasicHandle().TimeNotBuiltin { r.EncodeTime(%s)", varname)
  673. // return
  674. }
  675. if t == rawTyp {
  676. x.linef("} else { z.EncRaw(%s)", varname)
  677. return
  678. }
  679. if t == rawExtTyp {
  680. x.linef("} else { r.EncodeRawExt(%s, e)", varname)
  681. return
  682. }
  683. // only check for extensions if the type is named, and has a packagePath.
  684. var arrayOrStruct = tk == reflect.Array || tk == reflect.Struct // meaning varname if of type *T
  685. if !x.nx && genImportPath(t) != "" && t.Name() != "" {
  686. yy := fmt.Sprintf("%sxt%s", genTempVarPfx, mi)
  687. x.linef("} else if %s := z.Extension(z.I2Rtid(%s)); %s != nil { z.EncExtension(%s, %s) ", yy, varname, yy, varname, yy)
  688. }
  689. if arrayOrStruct { // varname is of type *T
  690. if ti2.bm || ti2.bmp { // t.Implements(binaryMarshalerTyp) || tptr.Implements(binaryMarshalerTyp) {
  691. x.linef("} else if z.EncBinary() { z.EncBinaryMarshal(%v) ", varname)
  692. }
  693. if ti2.jm || ti2.jmp { // t.Implements(jsonMarshalerTyp) || tptr.Implements(jsonMarshalerTyp) {
  694. x.linef("} else if !z.EncBinary() && z.IsJSONHandle() { z.EncJSONMarshal(%v) ", varname)
  695. } else if ti2.tm || ti2.tmp { // t.Implements(textMarshalerTyp) || tptr.Implements(textMarshalerTyp) {
  696. x.linef("} else if !z.EncBinary() { z.EncTextMarshal(%v) ", varname)
  697. }
  698. } else { // varname is of type T
  699. if ti2.bm { // t.Implements(binaryMarshalerTyp) {
  700. x.linef("} else if z.EncBinary() { z.EncBinaryMarshal(%v) ", varname)
  701. } else if ti2.bmp { // tptr.Implements(binaryMarshalerTyp) {
  702. x.linef("} else if z.EncBinary() { z.EncBinaryMarshal(&%v) ", varname)
  703. }
  704. if ti2.jm { // t.Implements(jsonMarshalerTyp) {
  705. x.linef("} else if !z.EncBinary() && z.IsJSONHandle() { z.EncJSONMarshal(%v) ", varname)
  706. } else if ti2.jmp { // tptr.Implements(jsonMarshalerTyp) {
  707. x.linef("} else if !z.EncBinary() && z.IsJSONHandle() { z.EncJSONMarshal(&%v) ", varname)
  708. } else if ti2.tm { // t.Implements(textMarshalerTyp) {
  709. x.linef("} else if !z.EncBinary() { z.EncTextMarshal(%v) ", varname)
  710. } else if ti2.tmp { // tptr.Implements(textMarshalerTyp) {
  711. x.linef("} else if !z.EncBinary() { z.EncTextMarshal(&%v) ", varname)
  712. }
  713. }
  714. x.line("} else {")
  715. switch t.Kind() {
  716. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  717. x.line("r.EncodeInt(int64(" + varname + "))")
  718. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  719. x.line("r.EncodeUint(uint64(" + varname + "))")
  720. case reflect.Float32:
  721. x.line("r.EncodeFloat32(float32(" + varname + "))")
  722. case reflect.Float64:
  723. x.line("r.EncodeFloat64(float64(" + varname + "))")
  724. case reflect.Bool:
  725. x.line("r.EncodeBool(bool(" + varname + "))")
  726. case reflect.String:
  727. x.line("r.EncodeStringEnc(codecSelferCcUTF8" + x.xs + ", string(" + varname + "))")
  728. case reflect.Chan:
  729. x.xtraSM(varname, t, true, false)
  730. // x.encListFallback(varname, rtid, t)
  731. case reflect.Array:
  732. x.xtraSM(varname, t, true, true)
  733. case reflect.Slice:
  734. // if nil, call dedicated function
  735. // if a []uint8, call dedicated function
  736. // if a known fastpath slice, call dedicated function
  737. // else write encode function in-line.
  738. // - if elements are primitives or Selfers, call dedicated function on each member.
  739. // - else call Encoder.encode(XXX) on it.
  740. if rtid == uint8SliceTypId {
  741. x.line("r.EncodeStringBytesRaw([]byte(" + varname + "))")
  742. } else if fastpathAV.index(rtid) != -1 {
  743. g := x.newGenV(t)
  744. x.line("z.F." + g.MethodNamePfx("Enc", false) + "V(" + varname + ", e)")
  745. } else {
  746. x.xtraSM(varname, t, true, false)
  747. // x.encListFallback(varname, rtid, t)
  748. }
  749. case reflect.Map:
  750. // if nil, call dedicated function
  751. // if a known fastpath map, call dedicated function
  752. // else write encode function in-line.
  753. // - if elements are primitives or Selfers, call dedicated function on each member.
  754. // - else call Encoder.encode(XXX) on it.
  755. // x.line("if " + varname + " == nil { \nr.EncodeNil()\n } else { ")
  756. if fastpathAV.index(rtid) != -1 {
  757. g := x.newGenV(t)
  758. x.line("z.F." + g.MethodNamePfx("Enc", false) + "V(" + varname + ", e)")
  759. } else {
  760. x.xtraSM(varname, t, true, false)
  761. // x.encMapFallback(varname, rtid, t)
  762. }
  763. case reflect.Struct:
  764. if !inlist {
  765. delete(x.te, rtid)
  766. x.line("z.EncFallback(" + varname + ")")
  767. break
  768. }
  769. x.encStruct(varname, rtid, t)
  770. default:
  771. if rtidAdded {
  772. delete(x.te, rtid)
  773. }
  774. x.line("z.EncFallback(" + varname + ")")
  775. }
  776. }
  777. func (x *genRunner) encZero(t reflect.Type) {
  778. switch t.Kind() {
  779. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  780. x.line("r.EncodeInt(0)")
  781. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  782. x.line("r.EncodeUint(0)")
  783. case reflect.Float32:
  784. x.line("r.EncodeFloat32(0)")
  785. case reflect.Float64:
  786. x.line("r.EncodeFloat64(0)")
  787. case reflect.Bool:
  788. x.line("r.EncodeBool(false)")
  789. case reflect.String:
  790. x.line("r.EncodeStringEnc(codecSelferCcUTF8" + x.xs + `, "")`)
  791. default:
  792. x.line("r.EncodeNil()")
  793. }
  794. }
  795. func (x *genRunner) encOmitEmptyLine(t2 reflect.StructField, varname string, buf *genBuf) {
  796. // smartly check omitEmpty on a struct type, as it may contain uncomparable map/slice/etc.
  797. // also, for maps/slices/arrays, check if len ! 0 (not if == zero value)
  798. varname2 := varname + "." + t2.Name
  799. switch t2.Type.Kind() {
  800. case reflect.Struct:
  801. rtid2 := rt2id(t2.Type)
  802. ti2 := x.ti.get(rtid2, t2.Type)
  803. // fmt.Printf(">>>> structfield: omitempty: type: %s, field: %s\n", t2.Type.Name(), t2.Name)
  804. if ti2.rtid == timeTypId {
  805. buf.s("!(").s(varname2).s(".IsZero())")
  806. break
  807. }
  808. if ti2.isFlag(typeInfoFlagIsZeroerPtr) || ti2.isFlag(typeInfoFlagIsZeroer) {
  809. buf.s("!(").s(varname2).s(".IsZero())")
  810. break
  811. }
  812. if ti2.isFlag(typeInfoFlagComparable) {
  813. buf.s(varname2).s(" != ").s(x.genZeroValueR(t2.Type))
  814. break
  815. }
  816. // buf.s("(")
  817. buf.s("false")
  818. for i, n := 0, t2.Type.NumField(); i < n; i++ {
  819. f := t2.Type.Field(i)
  820. if f.PkgPath != "" { // unexported
  821. continue
  822. }
  823. buf.s(" || ")
  824. x.encOmitEmptyLine(f, varname2, buf)
  825. }
  826. //buf.s(")")
  827. case reflect.Bool:
  828. buf.s(varname2)
  829. case reflect.Map, reflect.Slice, reflect.Array, reflect.Chan:
  830. buf.s("len(").s(varname2).s(") != 0")
  831. default:
  832. buf.s(varname2).s(" != ").s(x.genZeroValueR(t2.Type))
  833. }
  834. }
  835. func (x *genRunner) encStruct(varname string, rtid uintptr, t reflect.Type) {
  836. // Use knowledge from structfieldinfo (mbs, encodable fields. Ignore omitempty. )
  837. // replicate code in kStruct i.e. for each field, deref type to non-pointer, and call x.enc on it
  838. // if t === type currently running selfer on, do for all
  839. ti := x.ti.get(rtid, t)
  840. i := x.varsfx()
  841. sepVarname := genTempVarPfx + "sep" + i
  842. numfieldsvar := genTempVarPfx + "q" + i
  843. ti2arrayvar := genTempVarPfx + "r" + i
  844. struct2arrvar := genTempVarPfx + "2arr" + i
  845. x.line(sepVarname + " := !z.EncBinary()")
  846. x.linef("%s := z.EncBasicHandle().StructToArray", struct2arrvar)
  847. x.linef("_, _ = %s, %s", sepVarname, struct2arrvar)
  848. x.linef("const %s bool = %v // struct tag has 'toArray'", ti2arrayvar, ti.toArray)
  849. tisfi := ti.sfiSrc // always use sequence from file. decStruct expects same thing.
  850. // var nn int
  851. // due to omitEmpty, we need to calculate the
  852. // number of non-empty things we write out first.
  853. // This is required as we need to pre-determine the size of the container,
  854. // to support length-prefixing.
  855. if ti.anyOmitEmpty {
  856. x.linef("var %s = [%v]bool{ // should field at this index be written?", numfieldsvar, len(tisfi))
  857. for j, si := range tisfi {
  858. _ = j
  859. if !si.omitEmpty() {
  860. // x.linef("%s[%v] = true // %s", numfieldsvar, j, si.fieldName)
  861. x.linef("true, // %s", si.fieldName)
  862. // nn++
  863. continue
  864. }
  865. var t2 reflect.StructField
  866. var omitline genBuf
  867. {
  868. t2typ := t
  869. varname3 := varname
  870. // go through the loop, record the t2 field explicitly,
  871. // and gather the omit line if embedded in pointers.
  872. for ij, ix := range si.is {
  873. if uint8(ij) == si.nis {
  874. break
  875. }
  876. for t2typ.Kind() == reflect.Ptr {
  877. t2typ = t2typ.Elem()
  878. }
  879. t2 = t2typ.Field(int(ix))
  880. t2typ = t2.Type
  881. varname3 = varname3 + "." + t2.Name
  882. // do not include actual field in the omit line.
  883. // that is done subsequently (right after - below).
  884. if uint8(ij+1) < si.nis && t2typ.Kind() == reflect.Ptr {
  885. omitline.s(varname3).s(" != nil && ")
  886. }
  887. }
  888. }
  889. x.encOmitEmptyLine(t2, varname, &omitline)
  890. x.linef("%s, // %s", omitline.v(), si.fieldName)
  891. }
  892. x.line("}")
  893. x.linef("_ = %s", numfieldsvar)
  894. }
  895. // x.linef("var %snn%s int", genTempVarPfx, i)
  896. x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) // if ti.toArray {
  897. x.linef("r.WriteArrayStart(%d)", len(tisfi))
  898. x.linef("} else {") // if not ti.toArray
  899. if ti.anyOmitEmpty {
  900. // nn = 0
  901. // x.linef("var %snn%s = %v", genTempVarPfx, i, nn)
  902. x.linef("var %snn%s int", genTempVarPfx, i)
  903. x.linef("for _, b := range %s { if b { %snn%s++ } }", numfieldsvar, genTempVarPfx, i)
  904. x.linef("r.WriteMapStart(%snn%s)", genTempVarPfx, i)
  905. x.linef("%snn%s = %v", genTempVarPfx, i, 0)
  906. } else {
  907. x.linef("r.WriteMapStart(%d)", len(tisfi))
  908. }
  909. x.line("}") // close if not StructToArray
  910. for j, si := range tisfi {
  911. i := x.varsfx()
  912. isNilVarName := genTempVarPfx + "n" + i
  913. var labelUsed bool
  914. var t2 reflect.StructField
  915. {
  916. t2typ := t
  917. varname3 := varname
  918. for ij, ix := range si.is {
  919. if uint8(ij) == si.nis {
  920. break
  921. }
  922. for t2typ.Kind() == reflect.Ptr {
  923. t2typ = t2typ.Elem()
  924. }
  925. t2 = t2typ.Field(int(ix))
  926. t2typ = t2.Type
  927. varname3 = varname3 + "." + t2.Name
  928. if t2typ.Kind() == reflect.Ptr {
  929. if !labelUsed {
  930. x.line("var " + isNilVarName + " bool")
  931. }
  932. x.line("if " + varname3 + " == nil { " + isNilVarName + " = true ")
  933. x.line("goto LABEL" + i)
  934. x.line("}")
  935. labelUsed = true
  936. // "varname3 = new(" + x.genTypeName(t3.Elem()) + ") }")
  937. }
  938. }
  939. // t2 = t.FieldByIndex(si.is)
  940. }
  941. if labelUsed {
  942. x.line("LABEL" + i + ":")
  943. }
  944. // if the type of the field is a Selfer, or one of the ones
  945. x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) // if ti.toArray
  946. if labelUsed {
  947. x.linef("if %s { r.WriteArrayElem(); r.EncodeNil() } else { ", isNilVarName)
  948. }
  949. x.line("r.WriteArrayElem()")
  950. if si.omitEmpty() {
  951. x.linef("if %s[%v] {", numfieldsvar, j)
  952. }
  953. x.encVar(varname+"."+t2.Name, t2.Type)
  954. if si.omitEmpty() {
  955. x.linef("} else {")
  956. x.encZero(t2.Type)
  957. x.linef("}")
  958. }
  959. if labelUsed {
  960. x.line("}")
  961. }
  962. x.linef("} else {") // if not ti.toArray
  963. if si.omitEmpty() {
  964. x.linef("if %s[%v] {", numfieldsvar, j)
  965. }
  966. x.line("r.WriteMapElemKey()")
  967. // x.line("r.EncodeStringEnc(codecSelferCcUTF8" + x.xs + ", `" + si.encName + "`)")
  968. // emulate EncStructFieldKey
  969. switch ti.keyType {
  970. case valueTypeInt:
  971. x.linef("r.EncodeInt(z.M.Int(strconv.ParseInt(`%s`, 10, 64)))", si.encName)
  972. case valueTypeUint:
  973. x.linef("r.EncodeUint(z.M.Uint(strconv.ParseUint(`%s`, 10, 64)))", si.encName)
  974. case valueTypeFloat:
  975. x.linef("r.EncodeFloat64(z.M.Float(strconv.ParseFloat(`%s`, 64)))", si.encName)
  976. default: // string
  977. if si.encNameAsciiAlphaNum {
  978. x.linef(`if z.IsJSONHandle() { z.WriteStr("\"%s\"") } else { `, si.encName)
  979. }
  980. x.linef("r.EncodeStringEnc(codecSelferCcUTF8%s, `%s`)", x.xs, si.encName)
  981. if si.encNameAsciiAlphaNum {
  982. x.linef("}")
  983. }
  984. }
  985. // x.linef("r.EncStructFieldKey(codecSelferValueType%s%s, `%s`)", ti.keyType.String(), x.xs, si.encName)
  986. x.line("r.WriteMapElemValue()")
  987. if labelUsed {
  988. x.line("if " + isNilVarName + " { r.EncodeNil() } else { ")
  989. x.encVar(varname+"."+t2.Name, t2.Type)
  990. x.line("}")
  991. } else {
  992. x.encVar(varname+"."+t2.Name, t2.Type)
  993. }
  994. if si.omitEmpty() {
  995. x.line("}")
  996. }
  997. x.linef("} ") // end if/else ti.toArray
  998. }
  999. x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) // if ti.toArray {
  1000. x.line("r.WriteArrayEnd()")
  1001. x.line("} else {")
  1002. x.line("r.WriteMapEnd()")
  1003. x.line("}")
  1004. }
  1005. func (x *genRunner) encListFallback(varname string, t reflect.Type) {
  1006. elemBytes := t.Elem().Kind() == reflect.Uint8
  1007. if t.AssignableTo(uint8SliceTyp) {
  1008. x.linef("r.EncodeStringBytesRaw([]byte(%s))", varname)
  1009. return
  1010. }
  1011. if t.Kind() == reflect.Array && elemBytes {
  1012. x.linef("r.EncodeStringBytesRaw(((*[%d]byte)(%s))[:])", t.Len(), varname)
  1013. return
  1014. }
  1015. i := x.varsfx()
  1016. if t.Kind() == reflect.Chan {
  1017. type ts struct {
  1018. Label, Chan, Slice, Sfx string
  1019. }
  1020. tm, err := template.New("").Parse(genEncChanTmpl)
  1021. if err != nil {
  1022. panic(err)
  1023. }
  1024. x.linef("if %s == nil { r.EncodeNil() } else { ", varname)
  1025. x.linef("var sch%s []%s", i, x.genTypeName(t.Elem()))
  1026. err = tm.Execute(x.w, &ts{"Lsch" + i, varname, "sch" + i, i})
  1027. if err != nil {
  1028. panic(err)
  1029. }
  1030. // x.linef("%s = sch%s", varname, i)
  1031. if elemBytes {
  1032. x.linef("r.EncodeStringBytesRaw([]byte(%s))", "sch"+i)
  1033. x.line("}")
  1034. return
  1035. }
  1036. varname = "sch" + i
  1037. }
  1038. x.line("r.WriteArrayStart(len(" + varname + "))")
  1039. x.linef("for _, %sv%s := range %s {", genTempVarPfx, i, varname)
  1040. x.line("r.WriteArrayElem()")
  1041. x.encVar(genTempVarPfx+"v"+i, t.Elem())
  1042. x.line("}")
  1043. x.line("r.WriteArrayEnd()")
  1044. if t.Kind() == reflect.Chan {
  1045. x.line("}")
  1046. }
  1047. }
  1048. func (x *genRunner) encMapFallback(varname string, t reflect.Type) {
  1049. // TODO: expand this to handle canonical.
  1050. i := x.varsfx()
  1051. x.line("r.WriteMapStart(len(" + varname + "))")
  1052. x.linef("for %sk%s, %sv%s := range %s {", genTempVarPfx, i, genTempVarPfx, i, varname)
  1053. x.line("r.WriteMapElemKey()")
  1054. x.encVar(genTempVarPfx+"k"+i, t.Key())
  1055. x.line("r.WriteMapElemValue()")
  1056. x.encVar(genTempVarPfx+"v"+i, t.Elem())
  1057. x.line("}")
  1058. x.line("r.WriteMapEnd()")
  1059. }
  1060. func (x *genRunner) decVarInitPtr(varname, nilvar string, t reflect.Type, si *structFieldInfo,
  1061. newbuf, nilbuf *genBuf) (t2 reflect.StructField) {
  1062. //we must accommodate anonymous fields, where the embedded field is a nil pointer in the value.
  1063. // t2 = t.FieldByIndex(si.is)
  1064. t2typ := t
  1065. varname3 := varname
  1066. t2kind := t2typ.Kind()
  1067. var nilbufed bool
  1068. if si != nil {
  1069. for ij, ix := range si.is {
  1070. if uint8(ij) == si.nis {
  1071. break
  1072. }
  1073. for t2typ.Kind() == reflect.Ptr {
  1074. t2typ = t2typ.Elem()
  1075. }
  1076. t2 = t2typ.Field(int(ix))
  1077. t2typ = t2.Type
  1078. varname3 = varname3 + "." + t2.Name
  1079. t2kind = t2typ.Kind()
  1080. if t2kind != reflect.Ptr {
  1081. continue
  1082. }
  1083. if newbuf != nil {
  1084. newbuf.f("if %s == nil { %s = new(%s) }\n", varname3, varname3, x.genTypeName(t2typ.Elem()))
  1085. }
  1086. if nilbuf != nil {
  1087. if !nilbufed {
  1088. nilbuf.s("if true")
  1089. nilbufed = true
  1090. }
  1091. nilbuf.s(" && ").s(varname3).s(" != nil")
  1092. }
  1093. }
  1094. }
  1095. // if t2typ.Kind() == reflect.Ptr {
  1096. // varname3 = varname3 + t2.Name
  1097. // }
  1098. if nilbuf != nil {
  1099. if nilbufed {
  1100. nilbuf.s(" { ")
  1101. }
  1102. if nilvar != "" {
  1103. nilbuf.s(nilvar).s(" = true")
  1104. } else if tk := t2typ.Kind(); tk == reflect.Ptr {
  1105. if strings.IndexByte(varname3, '.') != -1 || strings.IndexByte(varname3, '[') != -1 {
  1106. nilbuf.s(varname3).s(" = nil")
  1107. } else {
  1108. nilbuf.s("*").s(varname3).s(" = ").s(x.genZeroValueR(t2typ.Elem()))
  1109. }
  1110. } else {
  1111. nilbuf.s(varname3).s(" = ").s(x.genZeroValueR(t2typ))
  1112. }
  1113. if nilbufed {
  1114. nilbuf.s("}")
  1115. }
  1116. }
  1117. return t2
  1118. }
  1119. // decVar takes a variable called varname, of type t
  1120. func (x *genRunner) decVarMain(varname, rand string, t reflect.Type, checkNotNil bool) {
  1121. // We only encode as nil if a nillable value.
  1122. // This removes some of the wasted checks for TryDecodeAsNil.
  1123. // We need to think about this more, to see what happens if omitempty, etc
  1124. // cause a nil value to be stored when something is expected.
  1125. // This could happen when decoding from a struct encoded as an array.
  1126. // For that, decVar should be called with canNil=true, to force true as its value.
  1127. var varname2 string
  1128. if t.Kind() != reflect.Ptr {
  1129. if t.PkgPath() != "" || !x.decTryAssignPrimitive(varname, t, false) {
  1130. x.dec(varname, t, false)
  1131. }
  1132. } else {
  1133. if checkNotNil {
  1134. x.linef("if %s == nil { %s = new(%s) }", varname, varname, x.genTypeName(t.Elem()))
  1135. }
  1136. // Ensure we set underlying ptr to a non-nil value (so we can deref to it later).
  1137. // There's a chance of a **T in here which is nil.
  1138. var ptrPfx string
  1139. for t = t.Elem(); t.Kind() == reflect.Ptr; t = t.Elem() {
  1140. ptrPfx += "*"
  1141. if checkNotNil {
  1142. x.linef("if %s%s == nil { %s%s = new(%s)}",
  1143. ptrPfx, varname, ptrPfx, varname, x.genTypeName(t))
  1144. }
  1145. }
  1146. // Should we create temp var if a slice/map indexing? No. dec(...) can now handle it.
  1147. if ptrPfx == "" {
  1148. x.dec(varname, t, true)
  1149. } else {
  1150. varname2 = genTempVarPfx + "z" + rand
  1151. x.line(varname2 + " := " + ptrPfx + varname)
  1152. x.dec(varname2, t, true)
  1153. }
  1154. }
  1155. }
  1156. // decVar takes a variable called varname, of type t
  1157. func (x *genRunner) decVar(varname, nilvar string, t reflect.Type, canBeNil, checkNotNil bool) {
  1158. i := x.varsfx()
  1159. // We only encode as nil if a nillable value.
  1160. // This removes some of the wasted checks for TryDecodeAsNil.
  1161. // We need to think about this more, to see what happens if omitempty, etc
  1162. // cause a nil value to be stored when something is expected.
  1163. // This could happen when decoding from a struct encoded as an array.
  1164. // For that, decVar should be called with canNil=true, to force true as its value.
  1165. if !canBeNil {
  1166. canBeNil = genAnythingCanBeNil || !genIsImmutable(t)
  1167. }
  1168. if canBeNil {
  1169. var buf genBuf
  1170. x.decVarInitPtr(varname, nilvar, t, nil, nil, &buf)
  1171. x.linef("if r.TryDecodeAsNil() { %s } else {", buf.buf)
  1172. } else {
  1173. x.line("// cannot be nil")
  1174. }
  1175. x.decVarMain(varname, i, t, checkNotNil)
  1176. if canBeNil {
  1177. x.line("} ")
  1178. }
  1179. }
  1180. // dec will decode a variable (varname) of type t or ptrTo(t) if isptr==true.
  1181. // t is always a basetype (i.e. not of kind reflect.Ptr).
  1182. func (x *genRunner) dec(varname string, t reflect.Type, isptr bool) {
  1183. // assumptions:
  1184. // - the varname is to a pointer already. No need to take address of it
  1185. // - t is always a baseType T (not a *T, etc).
  1186. rtid := rt2id(t)
  1187. ti2 := x.ti.get(rtid, t)
  1188. // tptr := reflect.PtrTo(t)
  1189. if x.checkForSelfer(t, varname) {
  1190. if ti2.cs || ti2.csp { // t.Implements(selferTyp) || tptr.Implements(selferTyp) {
  1191. x.line(varname + ".CodecDecodeSelf(d)")
  1192. return
  1193. }
  1194. if _, ok := x.td[rtid]; ok {
  1195. x.line(varname + ".CodecDecodeSelf(d)")
  1196. return
  1197. }
  1198. }
  1199. inlist := false
  1200. for _, t0 := range x.t {
  1201. if t == t0 {
  1202. inlist = true
  1203. if x.checkForSelfer(t, varname) {
  1204. x.line(varname + ".CodecDecodeSelf(d)")
  1205. return
  1206. }
  1207. break
  1208. }
  1209. }
  1210. var rtidAdded bool
  1211. if t == x.tc {
  1212. x.td[rtid] = true
  1213. rtidAdded = true
  1214. }
  1215. // check if
  1216. // - type is time.Time, Raw, RawExt
  1217. // - the type implements (Text|JSON|Binary)(Unm|M)arshal
  1218. mi := x.varsfx()
  1219. // x.linef("%sm%s := z.DecBinary()", genTempVarPfx, mi)
  1220. // x.linef("_ = %sm%s", genTempVarPfx, mi)
  1221. x.line("if false {") //start if block
  1222. defer func() { x.line("}") }() //end if block
  1223. var ptrPfx, addrPfx string
  1224. if isptr {
  1225. ptrPfx = "*"
  1226. } else {
  1227. addrPfx = "&"
  1228. }
  1229. if t == timeTyp {
  1230. x.linef("} else if !z.DecBasicHandle().TimeNotBuiltin { %s%v = r.DecodeTime()", ptrPfx, varname)
  1231. // return
  1232. }
  1233. if t == rawTyp {
  1234. x.linef("} else { %s%v = z.DecRaw()", ptrPfx, varname)
  1235. return
  1236. }
  1237. if t == rawExtTyp {
  1238. x.linef("} else { r.DecodeExt(%s%v, 0, nil)", addrPfx, varname)
  1239. return
  1240. }
  1241. // only check for extensions if the type is named, and has a packagePath.
  1242. if !x.nx && genImportPath(t) != "" && t.Name() != "" {
  1243. // first check if extensions are configued, before doing the interface conversion
  1244. // x.linef("} else if z.HasExtensions() && z.DecExt(%s) {", varname)
  1245. yy := fmt.Sprintf("%sxt%s", genTempVarPfx, mi)
  1246. x.linef("} else if %s := z.Extension(z.I2Rtid(%s)); %s != nil { z.DecExtension(%s, %s) ", yy, varname, yy, varname, yy)
  1247. }
  1248. if ti2.bu || ti2.bup { // t.Implements(binaryUnmarshalerTyp) || tptr.Implements(binaryUnmarshalerTyp) {
  1249. x.linef("} else if z.DecBinary() { z.DecBinaryUnmarshal(%s%v) ", addrPfx, varname)
  1250. }
  1251. if ti2.ju || ti2.jup { // t.Implements(jsonUnmarshalerTyp) || tptr.Implements(jsonUnmarshalerTyp) {
  1252. x.linef("} else if !z.DecBinary() && z.IsJSONHandle() { z.DecJSONUnmarshal(%s%v)", addrPfx, varname)
  1253. } else if ti2.tu || ti2.tup { // t.Implements(textUnmarshalerTyp) || tptr.Implements(textUnmarshalerTyp) {
  1254. x.linef("} else if !z.DecBinary() { z.DecTextUnmarshal(%s%v)", addrPfx, varname)
  1255. }
  1256. x.line("} else {")
  1257. if x.decTryAssignPrimitive(varname, t, isptr) {
  1258. return
  1259. }
  1260. switch t.Kind() {
  1261. case reflect.Array, reflect.Chan:
  1262. x.xtraSM(varname, t, false, isptr)
  1263. case reflect.Slice:
  1264. // if a []uint8, call dedicated function
  1265. // if a known fastpath slice, call dedicated function
  1266. // else write encode function in-line.
  1267. // - if elements are primitives or Selfers, call dedicated function on each member.
  1268. // - else call Encoder.encode(XXX) on it.
  1269. if rtid == uint8SliceTypId {
  1270. x.linef("%s%s = r.DecodeBytes(%s(%s[]byte)(%s), false)",
  1271. ptrPfx, varname, ptrPfx, ptrPfx, varname)
  1272. } else if fastpathAV.index(rtid) != -1 {
  1273. g := x.newGenV(t)
  1274. x.linef("z.F.%sX(%s%s, d)", g.MethodNamePfx("Dec", false), addrPfx, varname)
  1275. } else {
  1276. x.xtraSM(varname, t, false, isptr)
  1277. // x.decListFallback(varname, rtid, false, t)
  1278. }
  1279. case reflect.Map:
  1280. // if a known fastpath map, call dedicated function
  1281. // else write encode function in-line.
  1282. // - if elements are primitives or Selfers, call dedicated function on each member.
  1283. // - else call Encoder.encode(XXX) on it.
  1284. if fastpathAV.index(rtid) != -1 {
  1285. g := x.newGenV(t)
  1286. x.linef("z.F.%sX(%s%s, d)", g.MethodNamePfx("Dec", false), addrPfx, varname)
  1287. } else {
  1288. x.xtraSM(varname, t, false, isptr)
  1289. // x.decMapFallback(varname, rtid, t)
  1290. }
  1291. case reflect.Struct:
  1292. if inlist {
  1293. // no need to create temp variable if isptr, or x.F or x[F]
  1294. if isptr || strings.IndexByte(varname, '.') != -1 || strings.IndexByte(varname, '[') != -1 {
  1295. x.decStruct(varname, rtid, t)
  1296. } else {
  1297. varname2 := genTempVarPfx + "j" + mi
  1298. x.line(varname2 + " := &" + varname)
  1299. x.decStruct(varname2, rtid, t)
  1300. }
  1301. } else {
  1302. // delete(x.td, rtid)
  1303. x.line("z.DecFallback(" + addrPfx + varname + ", false)")
  1304. }
  1305. default:
  1306. if rtidAdded {
  1307. delete(x.te, rtid)
  1308. }
  1309. x.line("z.DecFallback(" + addrPfx + varname + ", true)")
  1310. }
  1311. }
  1312. func (x *genRunner) decTryAssignPrimitive(varname string, t reflect.Type, isptr bool) (done bool) {
  1313. // This should only be used for exact primitives (ie un-named types).
  1314. // Named types may be implementations of Selfer, Unmarshaler, etc.
  1315. // They should be handled by dec(...)
  1316. var ptr string
  1317. if isptr {
  1318. ptr = "*"
  1319. }
  1320. switch t.Kind() {
  1321. case reflect.Int:
  1322. x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), codecSelferBitsize%s))", ptr, varname, x.genTypeName(t), x.xs)
  1323. case reflect.Int8:
  1324. x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), 8))", ptr, varname, x.genTypeName(t))
  1325. case reflect.Int16:
  1326. x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), 16))", ptr, varname, x.genTypeName(t))
  1327. case reflect.Int32:
  1328. x.linef("%s%s = (%s)(z.C.IntV(r.DecodeInt64(), 32))", ptr, varname, x.genTypeName(t))
  1329. case reflect.Int64:
  1330. x.linef("%s%s = (%s)(r.DecodeInt64())", ptr, varname, x.genTypeName(t))
  1331. case reflect.Uint:
  1332. x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), codecSelferBitsize%s))", ptr, varname, x.genTypeName(t), x.xs)
  1333. case reflect.Uint8:
  1334. x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), 8))", ptr, varname, x.genTypeName(t))
  1335. case reflect.Uint16:
  1336. x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), 16))", ptr, varname, x.genTypeName(t))
  1337. case reflect.Uint32:
  1338. x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), 32))", ptr, varname, x.genTypeName(t))
  1339. case reflect.Uint64:
  1340. x.linef("%s%s = (%s)(r.DecodeUint64())", ptr, varname, x.genTypeName(t))
  1341. case reflect.Uintptr:
  1342. x.linef("%s%s = (%s)(z.C.UintV(r.DecodeUint64(), codecSelferBitsize%s))", ptr, varname, x.genTypeName(t), x.xs)
  1343. case reflect.Float32:
  1344. x.linef("%s%s = (%s)(r.DecodeFloat32As64())", ptr, varname, x.genTypeName(t))
  1345. case reflect.Float64:
  1346. x.linef("%s%s = (%s)(r.DecodeFloat64())", ptr, varname, x.genTypeName(t))
  1347. case reflect.Bool:
  1348. x.linef("%s%s = (%s)(r.DecodeBool())", ptr, varname, x.genTypeName(t))
  1349. case reflect.String:
  1350. x.linef("%s%s = (%s)(r.DecodeString())", ptr, varname, x.genTypeName(t))
  1351. default:
  1352. return false
  1353. }
  1354. return true
  1355. }
  1356. func (x *genRunner) decListFallback(varname string, rtid uintptr, t reflect.Type) {
  1357. if t.AssignableTo(uint8SliceTyp) {
  1358. x.line("*" + varname + " = r.DecodeBytes(*((*[]byte)(" + varname + ")), false)")
  1359. return
  1360. }
  1361. if t.Kind() == reflect.Array && t.Elem().Kind() == reflect.Uint8 {
  1362. x.linef("r.DecodeBytes( ((*[%d]byte)(%s))[:], true)", t.Len(), varname)
  1363. return
  1364. }
  1365. type tstruc struct {
  1366. TempVar string
  1367. Rand string
  1368. Varname string
  1369. CTyp string
  1370. Typ string
  1371. Immutable bool
  1372. Size int
  1373. }
  1374. telem := t.Elem()
  1375. ts := tstruc{genTempVarPfx, x.varsfx(), varname, x.genTypeName(t), x.genTypeName(telem), genIsImmutable(telem), int(telem.Size())}
  1376. funcs := make(template.FuncMap)
  1377. funcs["decLineVar"] = func(varname string) string {
  1378. x.decVar(varname, "", telem, false, true)
  1379. return ""
  1380. }
  1381. funcs["var"] = func(s string) string {
  1382. return ts.TempVar + s + ts.Rand
  1383. }
  1384. funcs["zero"] = func() string {
  1385. return x.genZeroValueR(telem)
  1386. }
  1387. funcs["isArray"] = func() bool {
  1388. return t.Kind() == reflect.Array
  1389. }
  1390. funcs["isSlice"] = func() bool {
  1391. return t.Kind() == reflect.Slice
  1392. }
  1393. funcs["isChan"] = func() bool {
  1394. return t.Kind() == reflect.Chan
  1395. }
  1396. tm, err := template.New("").Funcs(funcs).Parse(genDecListTmpl)
  1397. if err != nil {
  1398. panic(err)
  1399. }
  1400. if err = tm.Execute(x.w, &ts); err != nil {
  1401. panic(err)
  1402. }
  1403. }
  1404. func (x *genRunner) decMapFallback(varname string, rtid uintptr, t reflect.Type) {
  1405. type tstruc struct {
  1406. TempVar string
  1407. Sfx string
  1408. Rand string
  1409. Varname string
  1410. KTyp string
  1411. Typ string
  1412. Size int
  1413. }
  1414. telem := t.Elem()
  1415. tkey := t.Key()
  1416. ts := tstruc{
  1417. genTempVarPfx, x.xs, x.varsfx(), varname, x.genTypeName(tkey),
  1418. x.genTypeName(telem), int(telem.Size() + tkey.Size()),
  1419. }
  1420. funcs := make(template.FuncMap)
  1421. funcs["decElemZero"] = func() string {
  1422. return x.genZeroValueR(telem)
  1423. }
  1424. funcs["decElemKindImmutable"] = func() bool {
  1425. return genIsImmutable(telem)
  1426. }
  1427. funcs["decElemKindPtr"] = func() bool {
  1428. return telem.Kind() == reflect.Ptr
  1429. }
  1430. funcs["decElemKindIntf"] = func() bool {
  1431. return telem.Kind() == reflect.Interface
  1432. }
  1433. funcs["decLineVarK"] = func(varname string) string {
  1434. x.decVar(varname, "", tkey, false, true)
  1435. return ""
  1436. }
  1437. funcs["decLineVar"] = func(varname, decodedNilVarname string) string {
  1438. x.decVar(varname, decodedNilVarname, telem, false, true)
  1439. return ""
  1440. }
  1441. funcs["var"] = func(s string) string {
  1442. return ts.TempVar + s + ts.Rand
  1443. }
  1444. tm, err := template.New("").Funcs(funcs).Parse(genDecMapTmpl)
  1445. if err != nil {
  1446. panic(err)
  1447. }
  1448. if err = tm.Execute(x.w, &ts); err != nil {
  1449. panic(err)
  1450. }
  1451. }
  1452. func (x *genRunner) decStructMapSwitch(kName string, varname string, rtid uintptr, t reflect.Type) {
  1453. ti := x.ti.get(rtid, t)
  1454. tisfi := ti.sfiSrc // always use sequence from file. decStruct expects same thing.
  1455. x.line("switch (" + kName + ") {")
  1456. var newbuf, nilbuf genBuf
  1457. for _, si := range tisfi {
  1458. x.line("case \"" + si.encName + "\":")
  1459. newbuf.reset()
  1460. nilbuf.reset()
  1461. t2 := x.decVarInitPtr(varname, "", t, si, &newbuf, &nilbuf)
  1462. x.linef("if r.TryDecodeAsNil() { %s } else { %s", nilbuf.buf, newbuf.buf)
  1463. x.decVarMain(varname+"."+t2.Name, x.varsfx(), t2.Type, false)
  1464. x.line("}")
  1465. }
  1466. x.line("default:")
  1467. // pass the slice here, so that the string will not escape, and maybe save allocation
  1468. x.line("z.DecStructFieldNotFound(-1, " + kName + ")")
  1469. x.line("} // end switch " + kName)
  1470. }
  1471. func (x *genRunner) decStructMap(varname, lenvarname string, rtid uintptr, t reflect.Type, style genStructMapStyle) {
  1472. tpfx := genTempVarPfx
  1473. ti := x.ti.get(rtid, t)
  1474. i := x.varsfx()
  1475. kName := tpfx + "s" + i
  1476. switch style {
  1477. case genStructMapStyleLenPrefix:
  1478. x.linef("for %sj%s := 0; %sj%s < %s; %sj%s++ {", tpfx, i, tpfx, i, lenvarname, tpfx, i)
  1479. case genStructMapStyleCheckBreak:
  1480. x.linef("for %sj%s := 0; !r.CheckBreak(); %sj%s++ {", tpfx, i, tpfx, i)
  1481. default: // 0, otherwise.
  1482. x.linef("var %shl%s bool = %s >= 0", tpfx, i, lenvarname) // has length
  1483. x.linef("for %sj%s := 0; ; %sj%s++ {", tpfx, i, tpfx, i)
  1484. x.linef("if %shl%s { if %sj%s >= %s { break }", tpfx, i, tpfx, i, lenvarname)
  1485. x.line("} else { if r.CheckBreak() { break }; }")
  1486. }
  1487. x.line("r.ReadMapElemKey()")
  1488. // emulate decstructfieldkey
  1489. switch ti.keyType {
  1490. case valueTypeInt:
  1491. x.linef("%s := z.StringView(strconv.AppendInt(z.DecScratchArrayBuffer()[:0], r.DecodeInt64(), 10))", kName)
  1492. case valueTypeUint:
  1493. x.linef("%s := z.StringView(strconv.AppendUint(z.DecScratchArrayBuffer()[:0], r.DecodeUint64(), 10))", kName)
  1494. case valueTypeFloat:
  1495. x.linef("%s := z.StringView(strconv.AppendFloat(z.DecScratchArrayBuffer()[:0], r.DecodeFloat64(), 'f', -1, 64))", kName)
  1496. default: // string
  1497. x.linef("%s := z.StringView(r.DecodeStringAsBytes())", kName)
  1498. }
  1499. // x.linef("%s := z.StringView(r.DecStructFieldKey(codecSelferValueType%s%s, z.DecScratchArrayBuffer()))", kName, ti.keyType.String(), x.xs)
  1500. x.line("r.ReadMapElemValue()")
  1501. x.decStructMapSwitch(kName, varname, rtid, t)
  1502. x.line("} // end for " + tpfx + "j" + i)
  1503. x.line("r.ReadMapEnd()")
  1504. }
  1505. func (x *genRunner) decStructArray(varname, lenvarname, breakString string, rtid uintptr, t reflect.Type) {
  1506. tpfx := genTempVarPfx
  1507. i := x.varsfx()
  1508. ti := x.ti.get(rtid, t)
  1509. tisfi := ti.sfiSrc // always use sequence from file. decStruct expects same thing.
  1510. x.linef("var %sj%s int", tpfx, i)
  1511. x.linef("var %sb%s bool", tpfx, i) // break
  1512. x.linef("var %shl%s bool = %s >= 0", tpfx, i, lenvarname) // has length
  1513. var newbuf, nilbuf genBuf
  1514. for _, si := range tisfi {
  1515. x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = r.CheckBreak() }",
  1516. tpfx, i, tpfx, i, tpfx, i,
  1517. tpfx, i, lenvarname, tpfx, i)
  1518. x.linef("if %sb%s { r.ReadArrayEnd(); %s }", tpfx, i, breakString)
  1519. x.line("r.ReadArrayElem()")
  1520. newbuf.reset()
  1521. nilbuf.reset()
  1522. t2 := x.decVarInitPtr(varname, "", t, si, &newbuf, &nilbuf)
  1523. x.linef("if r.TryDecodeAsNil() { %s } else { %s", nilbuf.buf, newbuf.buf)
  1524. x.decVarMain(varname+"."+t2.Name, x.varsfx(), t2.Type, false)
  1525. x.line("}")
  1526. }
  1527. // read remaining values and throw away.
  1528. x.line("for {")
  1529. x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = r.CheckBreak() }",
  1530. tpfx, i, tpfx, i, tpfx, i,
  1531. tpfx, i, lenvarname, tpfx, i)
  1532. x.linef("if %sb%s { break }", tpfx, i)
  1533. x.line("r.ReadArrayElem()")
  1534. x.linef(`z.DecStructFieldNotFound(%sj%s - 1, "")`, tpfx, i)
  1535. x.line("}")
  1536. x.line("r.ReadArrayEnd()")
  1537. }
  1538. func (x *genRunner) decStruct(varname string, rtid uintptr, t reflect.Type) {
  1539. // varname MUST be a ptr, or a struct field or a slice element.
  1540. i := x.varsfx()
  1541. x.linef("%sct%s := r.ContainerType()", genTempVarPfx, i)
  1542. x.linef("if %sct%s == codecSelferValueTypeMap%s {", genTempVarPfx, i, x.xs)
  1543. x.line(genTempVarPfx + "l" + i + " := r.ReadMapStart()")
  1544. x.linef("if %sl%s == 0 {", genTempVarPfx, i)
  1545. x.line("r.ReadMapEnd()")
  1546. if genUseOneFunctionForDecStructMap {
  1547. x.line("} else { ")
  1548. x.linef("%s.codecDecodeSelfFromMap(%sl%s, d)", varname, genTempVarPfx, i)
  1549. } else {
  1550. x.line("} else if " + genTempVarPfx + "l" + i + " > 0 { ")
  1551. x.line(varname + ".codecDecodeSelfFromMapLenPrefix(" + genTempVarPfx + "l" + i + ", d)")
  1552. x.line("} else {")
  1553. x.line(varname + ".codecDecodeSelfFromMapCheckBreak(" + genTempVarPfx + "l" + i + ", d)")
  1554. }
  1555. x.line("}")
  1556. // else if container is array
  1557. x.linef("} else if %sct%s == codecSelferValueTypeArray%s {", genTempVarPfx, i, x.xs)
  1558. x.line(genTempVarPfx + "l" + i + " := r.ReadArrayStart()")
  1559. x.linef("if %sl%s == 0 {", genTempVarPfx, i)
  1560. x.line("r.ReadArrayEnd()")
  1561. x.line("} else { ")
  1562. x.linef("%s.codecDecodeSelfFromArray(%sl%s, d)", varname, genTempVarPfx, i)
  1563. x.line("}")
  1564. // else panic
  1565. x.line("} else { ")
  1566. x.line("panic(errCodecSelferOnlyMapOrArrayEncodeToStruct" + x.xs + ")")
  1567. x.line("} ")
  1568. }
  1569. // --------
  1570. type genV struct {
  1571. // genV is either a primitive (Primitive != "") or a map (MapKey != "") or a slice
  1572. MapKey string
  1573. Elem string
  1574. Primitive string
  1575. Size int
  1576. }
  1577. func (x *genRunner) newGenV(t reflect.Type) (v genV) {
  1578. switch t.Kind() {
  1579. case reflect.Slice, reflect.Array:
  1580. te := t.Elem()
  1581. v.Elem = x.genTypeName(te)
  1582. v.Size = int(te.Size())
  1583. case reflect.Map:
  1584. te, tk := t.Elem(), t.Key()
  1585. v.Elem = x.genTypeName(te)
  1586. v.MapKey = x.genTypeName(tk)
  1587. v.Size = int(te.Size() + tk.Size())
  1588. default:
  1589. panic("unexpected type for newGenV. Requires map or slice type")
  1590. }
  1591. return
  1592. }
  1593. func (x *genV) MethodNamePfx(prefix string, prim bool) string {
  1594. var name []byte
  1595. if prefix != "" {
  1596. name = append(name, prefix...)
  1597. }
  1598. if prim {
  1599. name = append(name, genTitleCaseName(x.Primitive)...)
  1600. } else {
  1601. if x.MapKey == "" {
  1602. name = append(name, "Slice"...)
  1603. } else {
  1604. name = append(name, "Map"...)
  1605. name = append(name, genTitleCaseName(x.MapKey)...)
  1606. }
  1607. name = append(name, genTitleCaseName(x.Elem)...)
  1608. }
  1609. return string(name)
  1610. }
  1611. // genImportPath returns import path of a non-predeclared named typed, or an empty string otherwise.
  1612. //
  1613. // This handles the misbehaviour that occurs when 1.5-style vendoring is enabled,
  1614. // where PkgPath returns the full path, including the vendoring pre-fix that should have been stripped.
  1615. // We strip it here.
  1616. func genImportPath(t reflect.Type) (s string) {
  1617. s = t.PkgPath()
  1618. if genCheckVendor {
  1619. // HACK: always handle vendoring. It should be typically on in go 1.6, 1.7
  1620. s = genStripVendor(s)
  1621. }
  1622. return
  1623. }
  1624. // A go identifier is (letter|_)[letter|number|_]*
  1625. func genGoIdentifier(s string, checkFirstChar bool) string {
  1626. b := make([]byte, 0, len(s))
  1627. t := make([]byte, 4)
  1628. var n int
  1629. for i, r := range s {
  1630. if checkFirstChar && i == 0 && !unicode.IsLetter(r) {
  1631. b = append(b, '_')
  1632. }
  1633. // r must be unicode_letter, unicode_digit or _
  1634. if unicode.IsLetter(r) || unicode.IsDigit(r) {
  1635. n = utf8.EncodeRune(t, r)
  1636. b = append(b, t[:n]...)
  1637. } else {
  1638. b = append(b, '_')
  1639. }
  1640. }
  1641. return string(b)
  1642. }
  1643. func genNonPtr(t reflect.Type) reflect.Type {
  1644. for t.Kind() == reflect.Ptr {
  1645. t = t.Elem()
  1646. }
  1647. return t
  1648. }
  1649. func genTitleCaseName(s string) string {
  1650. switch s {
  1651. case "interface{}", "interface {}":
  1652. return "Intf"
  1653. default:
  1654. return strings.ToUpper(s[0:1]) + s[1:]
  1655. }
  1656. }
  1657. func genMethodNameT(t reflect.Type, tRef reflect.Type) (n string) {
  1658. var ptrPfx string
  1659. for t.Kind() == reflect.Ptr {
  1660. ptrPfx += "Ptrto"
  1661. t = t.Elem()
  1662. }
  1663. tstr := t.String()
  1664. if tn := t.Name(); tn != "" {
  1665. if tRef != nil && genImportPath(t) == genImportPath(tRef) {
  1666. return ptrPfx + tn
  1667. } else {
  1668. if genQNameRegex.MatchString(tstr) {
  1669. return ptrPfx + strings.Replace(tstr, ".", "_", 1000)
  1670. } else {
  1671. return ptrPfx + genCustomTypeName(tstr)
  1672. }
  1673. }
  1674. }
  1675. switch t.Kind() {
  1676. case reflect.Map:
  1677. return ptrPfx + "Map" + genMethodNameT(t.Key(), tRef) + genMethodNameT(t.Elem(), tRef)
  1678. case reflect.Slice:
  1679. return ptrPfx + "Slice" + genMethodNameT(t.Elem(), tRef)
  1680. case reflect.Array:
  1681. return ptrPfx + "Array" + strconv.FormatInt(int64(t.Len()), 10) + genMethodNameT(t.Elem(), tRef)
  1682. case reflect.Chan:
  1683. var cx string
  1684. switch t.ChanDir() {
  1685. case reflect.SendDir:
  1686. cx = "ChanSend"
  1687. case reflect.RecvDir:
  1688. cx = "ChanRecv"
  1689. default:
  1690. cx = "Chan"
  1691. }
  1692. return ptrPfx + cx + genMethodNameT(t.Elem(), tRef)
  1693. default:
  1694. if t == intfTyp {
  1695. return ptrPfx + "Interface"
  1696. } else {
  1697. if tRef != nil && genImportPath(t) == genImportPath(tRef) {
  1698. if t.Name() != "" {
  1699. return ptrPfx + t.Name()
  1700. } else {
  1701. return ptrPfx + genCustomTypeName(tstr)
  1702. }
  1703. } else {
  1704. // best way to get the package name inclusive
  1705. // return ptrPfx + strings.Replace(tstr, ".", "_", 1000)
  1706. // return ptrPfx + genBase64enc.EncodeToString([]byte(tstr))
  1707. if t.Name() != "" && genQNameRegex.MatchString(tstr) {
  1708. return ptrPfx + strings.Replace(tstr, ".", "_", 1000)
  1709. } else {
  1710. return ptrPfx + genCustomTypeName(tstr)
  1711. }
  1712. }
  1713. }
  1714. }
  1715. }
  1716. // genCustomNameForType base64encodes the t.String() value in such a way
  1717. // that it can be used within a function name.
  1718. func genCustomTypeName(tstr string) string {
  1719. len2 := genBase64enc.EncodedLen(len(tstr))
  1720. bufx := make([]byte, len2)
  1721. genBase64enc.Encode(bufx, []byte(tstr))
  1722. for i := len2 - 1; i >= 0; i-- {
  1723. if bufx[i] == '=' {
  1724. len2--
  1725. } else {
  1726. break
  1727. }
  1728. }
  1729. return string(bufx[:len2])
  1730. }
  1731. func genIsImmutable(t reflect.Type) (v bool) {
  1732. return isImmutableKind(t.Kind())
  1733. }
  1734. type genInternal struct {
  1735. Version int
  1736. Values []genV
  1737. }
  1738. func (x genInternal) FastpathLen() (l int) {
  1739. for _, v := range x.Values {
  1740. if v.Primitive == "" && !(v.MapKey == "" && v.Elem == "uint8") {
  1741. l++
  1742. }
  1743. }
  1744. return
  1745. }
  1746. func genInternalZeroValue(s string) string {
  1747. switch s {
  1748. case "interface{}", "interface {}":
  1749. return "nil"
  1750. case "bool":
  1751. return "false"
  1752. case "string":
  1753. return `""`
  1754. default:
  1755. return "0"
  1756. }
  1757. }
  1758. var genInternalNonZeroValueIdx [5]uint64
  1759. var genInternalNonZeroValueStrs = [2][5]string{
  1760. {`"string-is-an-interface"`, "true", `"some-string"`, "11.1", "33"},
  1761. {`"string-is-an-interface-2"`, "true", `"some-string-2"`, "22.2", "44"},
  1762. }
  1763. func genInternalNonZeroValue(s string) string {
  1764. switch s {
  1765. case "interface{}", "interface {}":
  1766. genInternalNonZeroValueIdx[0]++
  1767. return genInternalNonZeroValueStrs[genInternalNonZeroValueIdx[0]%2][0] // return string, to remove ambiguity
  1768. case "bool":
  1769. genInternalNonZeroValueIdx[1]++
  1770. return genInternalNonZeroValueStrs[genInternalNonZeroValueIdx[1]%2][1]
  1771. case "string":
  1772. genInternalNonZeroValueIdx[2]++
  1773. return genInternalNonZeroValueStrs[genInternalNonZeroValueIdx[2]%2][2]
  1774. case "float32", "float64", "float", "double":
  1775. genInternalNonZeroValueIdx[3]++
  1776. return genInternalNonZeroValueStrs[genInternalNonZeroValueIdx[3]%2][3]
  1777. default:
  1778. genInternalNonZeroValueIdx[4]++
  1779. return genInternalNonZeroValueStrs[genInternalNonZeroValueIdx[4]%2][4]
  1780. }
  1781. }
  1782. func genInternalEncCommandAsString(s string, vname string) string {
  1783. switch s {
  1784. case "uint", "uint8", "uint16", "uint32", "uint64":
  1785. return "ee.EncodeUint(uint64(" + vname + "))"
  1786. case "int", "int8", "int16", "int32", "int64":
  1787. return "ee.EncodeInt(int64(" + vname + "))"
  1788. case "string":
  1789. return "ee.EncodeStringEnc(cUTF8, " + vname + ")"
  1790. case "float32":
  1791. return "ee.EncodeFloat32(" + vname + ")"
  1792. case "float64":
  1793. return "ee.EncodeFloat64(" + vname + ")"
  1794. case "bool":
  1795. return "ee.EncodeBool(" + vname + ")"
  1796. // case "symbol":
  1797. // return "ee.EncodeSymbol(" + vname + ")"
  1798. default:
  1799. return "e.encode(" + vname + ")"
  1800. }
  1801. }
  1802. func genInternalDecCommandAsString(s string) string {
  1803. switch s {
  1804. case "uint":
  1805. return "uint(chkOvf.UintV(dd.DecodeUint64(), uintBitsize))"
  1806. case "uint8":
  1807. return "uint8(chkOvf.UintV(dd.DecodeUint64(), 8))"
  1808. case "uint16":
  1809. return "uint16(chkOvf.UintV(dd.DecodeUint64(), 16))"
  1810. case "uint32":
  1811. return "uint32(chkOvf.UintV(dd.DecodeUint64(), 32))"
  1812. case "uint64":
  1813. return "dd.DecodeUint64()"
  1814. case "uintptr":
  1815. return "uintptr(chkOvf.UintV(dd.DecodeUint64(), uintBitsize))"
  1816. case "int":
  1817. return "int(chkOvf.IntV(dd.DecodeInt64(), intBitsize))"
  1818. case "int8":
  1819. return "int8(chkOvf.IntV(dd.DecodeInt64(), 8))"
  1820. case "int16":
  1821. return "int16(chkOvf.IntV(dd.DecodeInt64(), 16))"
  1822. case "int32":
  1823. return "int32(chkOvf.IntV(dd.DecodeInt64(), 32))"
  1824. case "int64":
  1825. return "dd.DecodeInt64()"
  1826. case "string":
  1827. return "dd.DecodeString()"
  1828. case "float32":
  1829. return "float32(chkOvf.Float32V(dd.DecodeFloat64()))"
  1830. case "float64":
  1831. return "dd.DecodeFloat64()"
  1832. case "bool":
  1833. return "dd.DecodeBool()"
  1834. default:
  1835. panic(errors.New("gen internal: unknown type for decode: " + s))
  1836. }
  1837. }
  1838. func genInternalSortType(s string, elem bool) string {
  1839. for _, v := range [...]string{"int", "uint", "float", "bool", "string"} {
  1840. if strings.HasPrefix(s, v) {
  1841. if elem {
  1842. if v == "int" || v == "uint" || v == "float" {
  1843. return v + "64"
  1844. } else {
  1845. return v
  1846. }
  1847. }
  1848. return v + "Slice"
  1849. }
  1850. }
  1851. panic("sorttype: unexpected type: " + s)
  1852. }
  1853. func genStripVendor(s string) string {
  1854. // HACK: Misbehaviour occurs in go 1.5. May have to re-visit this later.
  1855. // if s contains /vendor/ OR startsWith vendor/, then return everything after it.
  1856. const vendorStart = "vendor/"
  1857. const vendorInline = "/vendor/"
  1858. if i := strings.LastIndex(s, vendorInline); i >= 0 {
  1859. s = s[i+len(vendorInline):]
  1860. } else if strings.HasPrefix(s, vendorStart) {
  1861. s = s[len(vendorStart):]
  1862. }
  1863. return s
  1864. }
  1865. // var genInternalMu sync.Mutex
  1866. var genInternalV = genInternal{Version: genVersion}
  1867. var genInternalTmplFuncs template.FuncMap
  1868. var genInternalOnce sync.Once
  1869. func genInternalInit() {
  1870. types := [...]string{
  1871. "interface{}",
  1872. "string",
  1873. "float32",
  1874. "float64",
  1875. "uint",
  1876. "uint8",
  1877. "uint16",
  1878. "uint32",
  1879. "uint64",
  1880. "uintptr",
  1881. "int",
  1882. "int8",
  1883. "int16",
  1884. "int32",
  1885. "int64",
  1886. "bool",
  1887. }
  1888. // keep as slice, so it is in specific iteration order.
  1889. // Initial order was uint64, string, interface{}, int, int64
  1890. mapvaltypes := [...]string{
  1891. "interface{}",
  1892. "string",
  1893. "uint",
  1894. "uint8",
  1895. "uint16",
  1896. "uint32",
  1897. "uint64",
  1898. "uintptr",
  1899. "int",
  1900. "int8",
  1901. "int16",
  1902. "int32",
  1903. "int64",
  1904. "float32",
  1905. "float64",
  1906. "bool",
  1907. }
  1908. wordSizeBytes := int(intBitsize) / 8
  1909. mapvaltypes2 := map[string]int{
  1910. "interface{}": 2 * wordSizeBytes,
  1911. "string": 2 * wordSizeBytes,
  1912. "uint": 1 * wordSizeBytes,
  1913. "uint8": 1,
  1914. "uint16": 2,
  1915. "uint32": 4,
  1916. "uint64": 8,
  1917. "uintptr": 1 * wordSizeBytes,
  1918. "int": 1 * wordSizeBytes,
  1919. "int8": 1,
  1920. "int16": 2,
  1921. "int32": 4,
  1922. "int64": 8,
  1923. "float32": 4,
  1924. "float64": 8,
  1925. "bool": 1,
  1926. }
  1927. var gt = genInternal{Version: genVersion}
  1928. // For each slice or map type, there must be a (symmetrical) Encode and Decode fast-path function
  1929. for _, s := range types {
  1930. gt.Values = append(gt.Values, genV{Primitive: s, Size: mapvaltypes2[s]})
  1931. // if s != "uint8" { // do not generate fast path for slice of bytes. Treat specially already.
  1932. // gt.Values = append(gt.Values, genV{Elem: s, Size: mapvaltypes2[s]})
  1933. // }
  1934. gt.Values = append(gt.Values, genV{Elem: s, Size: mapvaltypes2[s]})
  1935. if _, ok := mapvaltypes2[s]; !ok {
  1936. gt.Values = append(gt.Values, genV{MapKey: s, Elem: s, Size: 2 * mapvaltypes2[s]})
  1937. }
  1938. for _, ms := range mapvaltypes {
  1939. gt.Values = append(gt.Values, genV{MapKey: s, Elem: ms, Size: mapvaltypes2[s] + mapvaltypes2[ms]})
  1940. }
  1941. }
  1942. funcs := make(template.FuncMap)
  1943. // funcs["haspfx"] = strings.HasPrefix
  1944. funcs["encmd"] = genInternalEncCommandAsString
  1945. funcs["decmd"] = genInternalDecCommandAsString
  1946. funcs["zerocmd"] = genInternalZeroValue
  1947. funcs["nonzerocmd"] = genInternalNonZeroValue
  1948. funcs["hasprefix"] = strings.HasPrefix
  1949. funcs["sorttype"] = genInternalSortType
  1950. genInternalV = gt
  1951. genInternalTmplFuncs = funcs
  1952. }
  1953. // genInternalGoFile is used to generate source files from templates.
  1954. // It is run by the program author alone.
  1955. // Unfortunately, it has to be exported so that it can be called from a command line tool.
  1956. // *** DO NOT USE ***
  1957. func genInternalGoFile(r io.Reader, w io.Writer) (err error) {
  1958. genInternalOnce.Do(genInternalInit)
  1959. gt := genInternalV
  1960. t := template.New("").Funcs(genInternalTmplFuncs)
  1961. tmplstr, err := ioutil.ReadAll(r)
  1962. if err != nil {
  1963. return
  1964. }
  1965. if t, err = t.Parse(string(tmplstr)); err != nil {
  1966. return
  1967. }
  1968. var out bytes.Buffer
  1969. err = t.Execute(&out, gt)
  1970. if err != nil {
  1971. return
  1972. }
  1973. bout, err := format.Source(out.Bytes())
  1974. if err != nil {
  1975. w.Write(out.Bytes()) // write out if error, so we can still see.
  1976. // w.Write(bout) // write out if error, as much as possible, so we can still see.
  1977. return
  1978. }
  1979. w.Write(bout)
  1980. return
  1981. }