gen.go 64 KB

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