gen.generated.go 5.9 KB

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