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

gen.generated.go 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // DO NOT EDIT. THIS FILE IS AUTO-GENERATED FROM gen-dec-(map|array).go.tmpl
  6. const genDecMapTmpl = `
  7. {{var "v"}} := *{{ .Varname }}
  8. {{var "l"}} := r.ReadMapStart()
  9. {{var "bh"}} := z.DecBasicHandle()
  10. if {{var "v"}} == nil {
  11. {{var "rl"}} := z.DecInferLen({{var "l"}}, {{var "bh"}}.MaxInitLen, {{ .Size }})
  12. {{var "v"}} = make(map[{{ .KTyp }}]{{ .Typ }}, {{var "rl"}})
  13. *{{ .Varname }} = {{var "v"}}
  14. }
  15. var {{var "mk"}} {{ .KTyp }}
  16. var {{var "mv"}} {{ .Typ }}
  17. var {{var "mg"}}, {{var "mdn"}} {{if decElemKindPtr}}, {{var "ms"}}, {{var "mok"}}{{end}} bool
  18. if {{var "bh"}}.MapValueReset {
  19. {{if decElemKindPtr}}{{var "mg"}} = true
  20. {{else if decElemKindIntf}}if !{{var "bh"}}.InterfaceReset { {{var "mg"}} = true }
  21. {{else if not decElemKindImmutable}}{{var "mg"}} = true
  22. {{end}} }
  23. if {{var "l"}} != 0 {
  24. {{var "hl"}} := {{var "l"}} > 0
  25. for {{var "j"}} := 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ {
  26. r.ReadMapElemKey() {{/* z.DecSendContainerState(codecSelfer_containerMapKey{{ .Sfx }}) */}}
  27. {{ $x := printf "%vmk%v" .TempVar .Rand }}{{ decLineVarK $x }}
  28. {{ if eq .KTyp "interface{}" }}{{/* // special case if a byte array. */}}if {{var "bv"}}, {{var "bok"}} := {{var "mk"}}.([]byte); {{var "bok"}} {
  29. {{var "mk"}} = string({{var "bv"}})
  30. }{{ end }}{{if decElemKindPtr}}
  31. {{var "ms"}} = true{{end}}
  32. if {{var "mg"}} {
  33. {{if decElemKindPtr}}{{var "mv"}}, {{var "mok"}} = {{var "v"}}[{{var "mk"}}]
  34. if {{var "mok"}} {
  35. {{var "ms"}} = false
  36. } {{else}}{{var "mv"}} = {{var "v"}}[{{var "mk"}}] {{end}}
  37. } {{if not decElemKindImmutable}}else { {{var "mv"}} = {{decElemZero}} }{{end}}
  38. r.ReadMapElemValue() {{/* z.DecSendContainerState(codecSelfer_containerMapValue{{ .Sfx }}) */}}
  39. {{var "mdn"}} = false
  40. {{ $x := printf "%vmv%v" .TempVar .Rand }}{{ $y := printf "%vmdn%v" .TempVar .Rand }}{{ decLineVar $x $y }}
  41. if {{var "mdn"}} {
  42. if {{ var "bh" }}.DeleteOnNilMapValue { delete({{var "v"}}, {{var "mk"}}) } else { {{var "v"}}[{{var "mk"}}] = {{decElemZero}} }
  43. } else if {{if decElemKindPtr}} {{var "ms"}} && {{end}} {{var "v"}} != nil {
  44. {{var "v"}}[{{var "mk"}}] = {{var "mv"}}
  45. }
  46. }
  47. } // else len==0: TODO: Should we clear map entries?
  48. r.ReadMapEnd() {{/* z.DecSendContainerState(codecSelfer_containerMapEnd{{ .Sfx }}) */}}
  49. `
  50. const genDecListTmpl = `
  51. {{var "v"}} := {{if not isArray}}*{{end}}{{ .Varname }}
  52. {{var "h"}}, {{var "l"}} := z.DecSliceHelperStart() {{/* // helper, containerLenS */}}{{if not isArray}}
  53. var {{var "c"}} bool {{/* // changed */}}
  54. _ = {{var "c"}}{{end}}
  55. if {{var "l"}} == 0 {
  56. {{if isSlice }}if {{var "v"}} == nil {
  57. {{var "v"}} = []{{ .Typ }}{}
  58. {{var "c"}} = true
  59. } else if len({{var "v"}}) != 0 {
  60. {{var "v"}} = {{var "v"}}[:0]
  61. {{var "c"}} = true
  62. } {{else if isChan }}if {{var "v"}} == nil {
  63. {{var "v"}} = make({{ .CTyp }}, 0)
  64. {{var "c"}} = true
  65. } {{end}}
  66. } else {
  67. {{var "hl"}} := {{var "l"}} > 0
  68. var {{var "rl"}} int
  69. _ = {{var "rl"}}
  70. {{if isSlice }} if {{var "hl"}} {
  71. if {{var "l"}} > cap({{var "v"}}) {
  72. {{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  73. if {{var "rl"}} <= cap({{var "v"}}) {
  74. {{var "v"}} = {{var "v"}}[:{{var "rl"}}]
  75. } else {
  76. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  77. }
  78. {{var "c"}} = true
  79. } else if {{var "l"}} != len({{var "v"}}) {
  80. {{var "v"}} = {{var "v"}}[:{{var "l"}}]
  81. {{var "c"}} = true
  82. }
  83. } {{end}}
  84. var {{var "j"}} int
  85. // var {{var "dn"}} bool
  86. for {{var "j"}} = 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ { // bounds-check-elimination
  87. {{if not isArray}} if {{var "j"}} == 0 && {{var "v"}} == nil {
  88. if {{var "hl"}} {
  89. {{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  90. } else {
  91. {{var "rl"}} = {{if isSlice}}8{{else if isChan}}64{{end}}
  92. }
  93. {{var "v"}} = make({{if isSlice}}[]{{ .Typ }}{{else if isChan}}{{.CTyp}}{{end}}, {{var "rl"}})
  94. {{var "c"}} = true
  95. }{{end}}
  96. {{var "h"}}.ElemContainerState({{var "j"}})
  97. {{/* {{var "dn"}} = r.TryDecodeAsNil() */}}{{/* commented out, as decLineVar handles this already each time */}}
  98. {{if isChan}}{{ $x := printf "%[1]vvcx%[2]v" .TempVar .Rand }}var {{$x}} {{ .Typ }}
  99. {{ decLineVar $x }}
  100. {{var "v"}} <- {{ $x }}
  101. // println(">>>> sending ", {{ $x }}, " into ", {{var "v"}}) // TODO: remove this
  102. {{else}}{{/* // if indefinite, etc, then expand the slice if necessary */}}
  103. var {{var "db"}} bool
  104. if {{var "j"}} >= len({{var "v"}}) {
  105. {{if isSlice }} {{var "v"}} = append({{var "v"}}, {{ zero }})
  106. {{var "c"}} = true
  107. {{else}} z.DecArrayCannotExpand(len(v), {{var "j"}}+1); {{var "db"}} = true
  108. {{end}}
  109. }
  110. if {{var "db"}} {
  111. z.DecSwallow()
  112. } else {
  113. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  114. }
  115. {{end}}
  116. }
  117. {{if isSlice}} if {{var "j"}} < len({{var "v"}}) {
  118. {{var "v"}} = {{var "v"}}[:{{var "j"}}]
  119. {{var "c"}} = true
  120. } else if {{var "j"}} == 0 && {{var "v"}} == nil {
  121. {{var "v"}} = make([]{{ .Typ }}, 0)
  122. {{var "c"}} = true
  123. } {{end}}
  124. }
  125. {{var "h"}}.End()
  126. {{if not isArray }}if {{var "c"}} {
  127. *{{ .Varname }} = {{var "v"}}
  128. }{{end}}
  129. `
  130. const genEncChanTmpl = `
  131. {{.Label}}:
  132. switch timeout{{.Sfx}} := z.EncBasicHandle().ChanRecvTimeout; {
  133. case timeout{{.Sfx}} == 0: // only consume available
  134. for {
  135. select {
  136. case b{{.Sfx}} := <-{{.Chan}}:
  137. {{ .Slice }} = append({{.Slice}}, b{{.Sfx}})
  138. default:
  139. break {{.Label}}
  140. }
  141. }
  142. case timeout{{.Sfx}} > 0: // consume until timeout
  143. tt{{.Sfx}} := time.NewTimer(timeout{{.Sfx}})
  144. for {
  145. select {
  146. case b{{.Sfx}} := <-{{.Chan}}:
  147. {{.Slice}} = append({{.Slice}}, b{{.Sfx}})
  148. case <-tt{{.Sfx}}.C:
  149. // close(tt.C)
  150. break {{.Label}}
  151. }
  152. }
  153. default: // consume until close
  154. for b{{.Sfx}} := range {{.Chan}} {
  155. {{.Slice}} = append({{.Slice}}, b{{.Sfx}})
  156. }
  157. }
  158. `