gen.generated.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. // DO NOT EDIT. THIS FILE IS AUTO-GENERATED FROM gen-dec-(map|array).go.tmpl
  5. const genDecMapTmpl = `
  6. {{var "v"}} := *{{ .Varname }}
  7. {{var "l"}} := r.ReadMapStart()
  8. {{var "bh"}} := z.DecBasicHandle()
  9. if {{var "v"}} == nil {
  10. {{var "rl"}}, _ := z.DecInferLen({{var "l"}}, {{var "bh"}}.MaxInitLen, {{ .Size }})
  11. {{var "v"}} = make(map[{{ .KTyp }}]{{ .Typ }}, {{var "rl"}})
  12. *{{ .Varname }} = {{var "v"}}
  13. }
  14. var {{var "mk"}} {{ .KTyp }}
  15. var {{var "mv"}} {{ .Typ }}
  16. var {{var "mg"}} {{if decElemKindPtr}}, {{var "ms"}}, {{var "mok"}}{{end}} bool
  17. if {{var "bh"}}.MapValueReset {
  18. {{if decElemKindPtr}}{{var "mg"}} = true
  19. {{else if decElemKindIntf}}if !{{var "bh"}}.InterfaceReset { {{var "mg"}} = true }
  20. {{else if not decElemKindImmutable}}{{var "mg"}} = true
  21. {{end}} }
  22. if {{var "l"}} > 0 {
  23. for {{var "j"}} := 0; {{var "j"}} < {{var "l"}}; {{var "j"}}++ {
  24. z.DecSendContainerState(codecSelfer_containerMapKey{{ .Sfx }})
  25. {{ $x := printf "%vmk%v" .TempVar .Rand }}{{ decLineVarK $x }}
  26. {{ if eq .KTyp "interface{}" }}{{/* // special case if a byte array. */}}if {{var "bv"}}, {{var "bok"}} := {{var "mk"}}.([]byte); {{var "bok"}} {
  27. {{var "mk"}} = string({{var "bv"}})
  28. }{{ end }}{{if decElemKindPtr}}
  29. {{var "ms"}} = true{{end}}
  30. if {{var "mg"}} {
  31. {{if decElemKindPtr}}{{var "mv"}}, {{var "mok"}} = {{var "v"}}[{{var "mk"}}]
  32. if {{var "mok"}} {
  33. {{var "ms"}} = false
  34. } {{else}}{{var "mv"}} = {{var "v"}}[{{var "mk"}}] {{end}}
  35. } {{if not decElemKindImmutable}}else { {{var "mv"}} = {{decElemZero}} }{{end}}
  36. z.DecSendContainerState(codecSelfer_containerMapValue{{ .Sfx }})
  37. {{ $x := printf "%vmv%v" .TempVar .Rand }}{{ decLineVar $x }}
  38. if {{if decElemKindPtr}} {{var "ms"}} && {{end}} {{var "v"}} != nil {
  39. {{var "v"}}[{{var "mk"}}] = {{var "mv"}}
  40. }
  41. }
  42. } else if {{var "l"}} < 0 {
  43. for {{var "j"}} := 0; !r.CheckBreak(); {{var "j"}}++ {
  44. z.DecSendContainerState(codecSelfer_containerMapKey{{ .Sfx }})
  45. {{ $x := printf "%vmk%v" .TempVar .Rand }}{{ decLineVarK $x }}
  46. {{ if eq .KTyp "interface{}" }}{{/* // special case if a byte array. */}}if {{var "bv"}}, {{var "bok"}} := {{var "mk"}}.([]byte); {{var "bok"}} {
  47. {{var "mk"}} = string({{var "bv"}})
  48. }{{ end }}{{if decElemKindPtr}}
  49. {{var "ms"}} = true {{ end }}
  50. if {{var "mg"}} {
  51. {{if decElemKindPtr}}{{var "mv"}}, {{var "mok"}} = {{var "v"}}[{{var "mk"}}]
  52. if {{var "mok"}} {
  53. {{var "ms"}} = false
  54. } {{else}}{{var "mv"}} = {{var "v"}}[{{var "mk"}}] {{end}}
  55. } {{if not decElemKindImmutable}}else { {{var "mv"}} = {{decElemZero}} }{{end}}
  56. z.DecSendContainerState(codecSelfer_containerMapValue{{ .Sfx }})
  57. {{ $x := printf "%vmv%v" .TempVar .Rand }}{{ decLineVar $x }}
  58. if {{if decElemKindPtr}} {{var "ms"}} && {{end}} {{var "v"}} != nil {
  59. {{var "v"}}[{{var "mk"}}] = {{var "mv"}}
  60. }
  61. }
  62. } // else len==0: TODO: Should we clear map entries?
  63. z.DecSendContainerState(codecSelfer_containerMapEnd{{ .Sfx }})
  64. `
  65. const genDecListTmpl = `
  66. {{var "v"}} := {{if not isArray}}*{{end}}{{ .Varname }}
  67. {{var "h"}}, {{var "l"}} := z.DecSliceHelperStart() {{/* // helper, containerLenS */}}{{if not isArray}}
  68. var {{var "c"}} bool {{/* // changed */}}
  69. _ = {{var "c"}}{{end}}
  70. if {{var "l"}} == 0 {
  71. {{if isSlice }}if {{var "v"}} == nil {
  72. {{var "v"}} = []{{ .Typ }}{}
  73. {{var "c"}} = true
  74. } else if len({{var "v"}}) != 0 {
  75. {{var "v"}} = {{var "v"}}[:0]
  76. {{var "c"}} = true
  77. } {{end}} {{if isChan }}if {{var "v"}} == nil {
  78. {{var "v"}} = make({{ .CTyp }}, 0)
  79. {{var "c"}} = true
  80. } {{end}}
  81. } else if {{var "l"}} > 0 {
  82. {{if isChan }}if {{var "v"}} == nil {
  83. {{var "rl"}}, _ = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  84. {{var "v"}} = make({{ .CTyp }}, {{var "rl"}})
  85. {{var "c"}} = true
  86. }
  87. for {{var "r"}} := 0; {{var "r"}} < {{var "l"}}; {{var "r"}}++ {
  88. {{var "h"}}.ElemContainerState({{var "r"}})
  89. var {{var "t"}} {{ .Typ }}
  90. {{ $x := printf "%st%s" .TempVar .Rand }}{{ decLineVar $x }}
  91. {{var "v"}} <- {{var "t"}}
  92. }
  93. {{ else }} var {{var "rr"}}, {{var "rl"}} int {{/* // num2read, length of slice/array/chan */}}
  94. var {{var "rt"}} bool {{/* truncated */}}
  95. _, _ = {{var "rl"}}, {{var "rt"}}
  96. {{var "rr"}} = {{var "l"}} // len({{var "v"}})
  97. if {{var "l"}} > cap({{var "v"}}) {
  98. {{if isArray }}z.DecArrayCannotExpand(len({{var "v"}}), {{var "l"}})
  99. {{ else }}{{if not .Immutable }}
  100. {{var "rg"}} := len({{var "v"}}) > 0
  101. {{var "v2"}} := {{var "v"}} {{end}}
  102. {{var "rl"}}, {{var "rt"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  103. if {{var "rt"}} {
  104. if {{var "rl"}} <= cap({{var "v"}}) {
  105. {{var "v"}} = {{var "v"}}[:{{var "rl"}}]
  106. } else {
  107. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  108. }
  109. } else {
  110. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  111. }
  112. {{var "c"}} = true
  113. {{var "rr"}} = len({{var "v"}}) {{if not .Immutable }}
  114. if {{var "rg"}} { copy({{var "v"}}, {{var "v2"}}) } {{end}} {{end}}{{/* end not Immutable, isArray */}}
  115. } {{if isSlice }} else if {{var "l"}} != len({{var "v"}}) {
  116. {{var "v"}} = {{var "v"}}[:{{var "l"}}]
  117. {{var "c"}} = true
  118. } {{end}} {{/* end isSlice:47 */}}
  119. {{var "j"}} := 0
  120. for ; {{var "j"}} < {{var "rr"}} ; {{var "j"}}++ {
  121. {{var "h"}}.ElemContainerState({{var "j"}})
  122. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  123. }
  124. {{if isArray }}for ; {{var "j"}} < {{var "l"}} ; {{var "j"}}++ {
  125. {{var "h"}}.ElemContainerState({{var "j"}})
  126. z.DecSwallow()
  127. }
  128. {{ else }}if {{var "rt"}} {
  129. for ; {{var "j"}} < {{var "l"}} ; {{var "j"}}++ {
  130. {{var "v"}} = append({{var "v"}}, {{ zero}})
  131. {{var "h"}}.ElemContainerState({{var "j"}})
  132. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  133. }
  134. } {{end}} {{/* end isArray:56 */}}
  135. {{end}} {{/* end isChan:16 */}}
  136. } else { {{/* len < 0 */}}
  137. {{var "j"}} := 0
  138. for ; !r.CheckBreak(); {{var "j"}}++ {
  139. {{if isChan }}
  140. {{var "h"}}.ElemContainerState({{var "j"}})
  141. var {{var "t"}} {{ .Typ }}
  142. {{ $x := printf "%st%s" .TempVar .Rand }}{{ decLineVar $x }}
  143. {{var "v"}} <- {{var "t"}}
  144. {{ else }}
  145. if {{var "j"}} >= len({{var "v"}}) {
  146. {{if isArray }}z.DecArrayCannotExpand(len({{var "v"}}), {{var "j"}}+1)
  147. {{ else }}{{var "v"}} = append({{var "v"}}, {{zero}})// var {{var "z"}} {{ .Typ }}
  148. {{var "c"}} = true {{end}}
  149. }
  150. {{var "h"}}.ElemContainerState({{var "j"}})
  151. if {{var "j"}} < len({{var "v"}}) {
  152. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  153. } else {
  154. z.DecSwallow()
  155. }
  156. {{end}}
  157. }
  158. {{if isSlice }}if {{var "j"}} < len({{var "v"}}) {
  159. {{var "v"}} = {{var "v"}}[:{{var "j"}}]
  160. {{var "c"}} = true
  161. } else if {{var "j"}} == 0 && {{var "v"}} == nil {
  162. {{var "v"}} = []{{ .Typ }}{}
  163. {{var "c"}} = true
  164. }{{end}}
  165. }
  166. {{var "h"}}.End()
  167. {{if not isArray }}if {{var "c"}} {
  168. *{{ .Varname }} = {{var "v"}}
  169. }{{end}}
  170. `