gen.generated.go 6.0 KB

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