gen-dec-map.go.tmpl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. {{var "v"}} := *{{ .Varname }}
  2. {{var "l"}} := z.DecReadMapStart()
  3. if {{var "l"}} == codecSelferDecContainerLenNil{{xs}} {
  4. *{{ .Varname }} = nil
  5. } else {
  6. if {{var "v"}} == nil {
  7. {{var "rl"}} := z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  8. {{var "v"}} = make(map[{{ .KTyp }}]{{ .Typ }}, {{var "rl"}})
  9. *{{ .Varname }} = {{var "v"}}
  10. }
  11. var {{var "mk"}} {{ .KTyp }}
  12. var {{var "mv"}} {{ .Typ }}
  13. var {{var "mg"}}, {{var "mdn"}} {{if decElemKindPtr}}, {{var "ms"}}, {{var "mok"}}{{end}} bool
  14. if z.DecBasicHandle().MapValueReset {
  15. {{if decElemKindPtr}}{{var "mg"}} = true
  16. {{else if decElemKindIntf}}if !z.DecBasicHandle().InterfaceReset { {{var "mg"}} = true }
  17. {{else if not decElemKindImmutable}}{{var "mg"}} = true
  18. {{end}} }
  19. if {{var "l"}} != 0 {
  20. {{var "hl"}} := {{var "l"}} > 0
  21. for {{var "j"}} := 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || z.DecCheckBreak()); {{var "j"}}++ {
  22. z.DecReadMapElemKey()
  23. {{ $x := printf "%vmk%v" .TempVar .Rand }}{{ decLineVarK $x -}}
  24. {{ if eq .KTyp "interface{}" }}{{/* // special case if a byte array. */ -}}
  25. if {{var "bv"}}, {{var "bok"}} := {{var "mk"}}.([]byte); {{var "bok"}} {
  26. {{var "mk"}} = string({{var "bv"}})
  27. }
  28. {{ end -}}
  29. {{if decElemKindPtr -}}
  30. {{var "ms"}} = true
  31. {{end -}}
  32. if {{var "mg"}} {
  33. {{if decElemKindPtr -}}
  34. {{var "mv"}}, {{var "mok"}} = {{var "v"}}[{{var "mk"}}]
  35. if {{var "mok"}} {
  36. {{var "ms"}} = false
  37. }
  38. {{else -}}
  39. {{var "mv"}} = {{var "v"}}[{{var "mk"}}]
  40. {{end -}}
  41. } {{if not decElemKindImmutable}}else { {{var "mv"}} = {{decElemZero}} }{{end}}
  42. z.DecReadMapElemValue()
  43. {{var "mdn"}} = false
  44. {{ $x := printf "%vmv%v" .TempVar .Rand }}{{ $y := printf "%vmdn%v" .TempVar .Rand }}{{ decLineVar $x $y -}}
  45. if {{var "mdn"}} {
  46. if z.DecBasicHandle().DeleteOnNilMapValue { delete({{var "v"}}, {{var "mk"}}) } else { {{var "v"}}[{{var "mk"}}] = {{decElemZero}} }
  47. } else if {{if decElemKindPtr}} {{var "ms"}} && {{end}} {{var "v"}} != nil {
  48. {{var "v"}}[{{var "mk"}}] = {{var "mv"}}
  49. }
  50. }
  51. } // else len==0: TODO: Should we clear map entries?
  52. z.DecReadMapEnd()
  53. }