gen.generated.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 */}}
  68. var {{var "c"}} bool {{/* // changed */}}
  69. if {{var "l"}} == 0 {
  70. {{if isSlice }}if {{var "v"}} == nil {
  71. {{var "v"}} = []{{ .Typ }}{}
  72. {{var "c"}} = true
  73. } else if len({{var "v"}}) != 0 {
  74. {{var "v"}} = {{var "v"}}[:0]
  75. {{var "c"}} = true
  76. } {{end}} {{if isChan }}if {{var "v"}} == nil {
  77. {{var "v"}} = make({{ .CTyp }}, 0)
  78. {{var "c"}} = true
  79. } {{end}}
  80. } else if {{var "l"}} > 0 {
  81. {{if isChan }}if {{var "v"}} == nil {
  82. {{var "rl"}}, _ = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  83. {{var "v"}} = make({{ .CTyp }}, {{var "rl"}})
  84. {{var "c"}} = true
  85. }
  86. for {{var "r"}} := 0; {{var "r"}} < {{var "l"}}; {{var "r"}}++ {
  87. {{var "h"}}.ElemContainerState({{var "r"}})
  88. var {{var "t"}} {{ .Typ }}
  89. {{ $x := printf "%st%s" .TempVar .Rand }}{{ decLineVar $x }}
  90. {{var "v"}} <- {{var "t"}}
  91. }
  92. {{ else }} var {{var "rr"}}, {{var "rl"}} int {{/* // num2read, length of slice/array/chan */}}
  93. var {{var "rt"}} bool {{/* truncated */}}
  94. if {{var "l"}} > cap({{var "v"}}) {
  95. {{if isArray }}z.DecArrayCannotExpand(len({{var "v"}}), {{var "l"}})
  96. {{ else }}{{if not .Immutable }}
  97. {{var "rg"}} := len({{var "v"}}) > 0
  98. {{var "v2"}} := {{var "v"}} {{end}}
  99. {{var "rl"}}, {{var "rt"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  100. if {{var "rt"}} {
  101. if {{var "rl"}} <= cap({{var "v"}}) {
  102. {{var "v"}} = {{var "v"}}[:{{var "rl"}}]
  103. } else {
  104. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  105. }
  106. } else {
  107. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  108. }
  109. {{var "c"}} = true
  110. {{var "rr"}} = len({{var "v"}}) {{if not .Immutable }}
  111. if {{var "rg"}} { copy({{var "v"}}, {{var "v2"}}) } {{end}} {{end}}{{/* end not Immutable, isArray */}}
  112. } {{if isSlice }} else if {{var "l"}} != len({{var "v"}}) {
  113. {{var "v"}} = {{var "v"}}[:{{var "l"}}]
  114. {{var "c"}} = true
  115. } {{end}} {{/* end isSlice:47 */}}
  116. {{var "j"}} := 0
  117. for ; {{var "j"}} < {{var "rr"}} ; {{var "j"}}++ {
  118. {{var "h"}}.ElemContainerState({{var "j"}})
  119. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  120. }
  121. {{if isArray }}for ; {{var "j"}} < {{var "l"}} ; {{var "j"}}++ {
  122. {{var "h"}}.ElemContainerState({{var "j"}})
  123. z.DecSwallow()
  124. }
  125. {{ else }}if {{var "rt"}} {
  126. for ; {{var "j"}} < {{var "l"}} ; {{var "j"}}++ {
  127. {{var "v"}} = append({{var "v"}}, {{ zero}})
  128. {{var "h"}}.ElemContainerState({{var "j"}})
  129. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  130. }
  131. } {{end}} {{/* end isArray:56 */}}
  132. {{end}} {{/* end isChan:16 */}}
  133. } else { {{/* len < 0 */}}
  134. {{var "j"}} := 0
  135. for ; !r.CheckBreak(); {{var "j"}}++ {
  136. {{if isChan }}
  137. {{var "h"}}.ElemContainerState({{var "j"}})
  138. var {{var "t"}} {{ .Typ }}
  139. {{ $x := printf "%st%s" .TempVar .Rand }}{{ decLineVar $x }}
  140. {{var "v"}} <- {{var "t"}}
  141. {{ else }}
  142. if {{var "j"}} >= len({{var "v"}}) {
  143. {{if isArray }}z.DecArrayCannotExpand(len({{var "v"}}), {{var "j"}}+1)
  144. {{ else }}{{var "v"}} = append({{var "v"}}, {{zero}})// var {{var "z"}} {{ .Typ }}
  145. {{var "c"}} = true {{end}}
  146. }
  147. {{var "h"}}.ElemContainerState({{var "j"}})
  148. if {{var "j"}} < len({{var "v"}}) {
  149. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  150. } else {
  151. z.DecSwallow()
  152. }
  153. {{end}}
  154. }
  155. {{if isSlice }}if {{var "j"}} < len({{var "v"}}) {
  156. {{var "v"}} = {{var "v"}}[:{{var "j"}}]
  157. {{var "c"}} = true
  158. } else if {{var "j"}} == 0 && {{var "v"}} == nil {
  159. {{var "v"}} = []{{ .Typ }}{}
  160. {{var "c"}} = true
  161. }{{end}}
  162. }
  163. {{var "h"}}.End()
  164. {{if not isArray }}if {{var "c"}} {
  165. *{{ .Varname }} = {{var "v"}}
  166. }{{end}}
  167. `