1// +build codecgen.exec 2 3// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved. 4// Use of this source code is governed by a MIT license found in the LICENSE file. 5 6package codec 7 8// DO NOT EDIT. THIS FILE IS AUTO-GENERATED FROM gen-dec-(map|array).go.tmpl 9 10const genDecMapTmpl = ` 11{{var "v"}} := *{{ .Varname }} 12{{var "l"}} := r.ReadMapStart() 13{{var "bh"}} := z.DecBasicHandle() 14if {{var "v"}} == nil { 15 {{var "rl"}} := z.DecInferLen({{var "l"}}, {{var "bh"}}.MaxInitLen, {{ .Size }}) 16 {{var "v"}} = make(map[{{ .KTyp }}]{{ .Typ }}, {{var "rl"}}) 17 *{{ .Varname }} = {{var "v"}} 18} 19var {{var "mk"}} {{ .KTyp }} 20var {{var "mv"}} {{ .Typ }} 21var {{var "mg"}}, {{var "mdn"}} {{if decElemKindPtr}}, {{var "ms"}}, {{var "mok"}}{{end}} bool 22if {{var "bh"}}.MapValueReset { 23 {{if decElemKindPtr}}{{var "mg"}} = true 24 {{else if decElemKindIntf}}if !{{var "bh"}}.InterfaceReset { {{var "mg"}} = true } 25 {{else if not decElemKindImmutable}}{{var "mg"}} = true 26 {{end}} } 27if {{var "l"}} != 0 { 28{{var "hl"}} := {{var "l"}} > 0 29 for {{var "j"}} := 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ { 30 r.ReadMapElemKey() {{/* z.DecSendContainerState(codecSelfer_containerMapKey{{ .Sfx }}) */}} 31 {{ $x := printf "%vmk%v" .TempVar .Rand }}{{ decLineVarK $x }} 32{{ if eq .KTyp "interface{}" }}{{/* // special case if a byte array. */}}if {{var "bv"}}, {{var "bok"}} := {{var "mk"}}.([]byte); {{var "bok"}} { 33 {{var "mk"}} = string({{var "bv"}}) 34 }{{ end }}{{if decElemKindPtr}} 35 {{var "ms"}} = true{{end}} 36 if {{var "mg"}} { 37 {{if decElemKindPtr}}{{var "mv"}}, {{var "mok"}} = {{var "v"}}[{{var "mk"}}] 38 if {{var "mok"}} { 39 {{var "ms"}} = false 40 } {{else}}{{var "mv"}} = {{var "v"}}[{{var "mk"}}] {{end}} 41 } {{if not decElemKindImmutable}}else { {{var "mv"}} = {{decElemZero}} }{{end}} 42 r.ReadMapElemValue() {{/* z.DecSendContainerState(codecSelfer_containerMapValue{{ .Sfx }}) */}} 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 {{ var "bh" }}.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? 52r.ReadMapEnd() {{/* z.DecSendContainerState(codecSelfer_containerMapEnd{{ .Sfx }}) */}} 53` 54 55const genDecListTmpl = ` 56{{var "v"}} := {{if not isArray}}*{{end}}{{ .Varname }} 57{{var "h"}}, {{var "l"}} := z.DecSliceHelperStart() {{/* // helper, containerLenS */}}{{if not isArray}} 58var {{var "c"}} bool {{/* // changed */}} 59_ = {{var "c"}}{{end}} 60if {{var "l"}} == 0 { 61 {{if isSlice }}if {{var "v"}} == nil { 62 {{var "v"}} = []{{ .Typ }}{} 63 {{var "c"}} = true 64 } else if len({{var "v"}}) != 0 { 65 {{var "v"}} = {{var "v"}}[:0] 66 {{var "c"}} = true 67 } {{else if isChan }}if {{var "v"}} == nil { 68 {{var "v"}} = make({{ .CTyp }}, 0) 69 {{var "c"}} = true 70 } {{end}} 71} else { 72 {{var "hl"}} := {{var "l"}} > 0 73 var {{var "rl"}} int 74 _ = {{var "rl"}} 75 {{if isSlice }} if {{var "hl"}} { 76 if {{var "l"}} > cap({{var "v"}}) { 77 {{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }}) 78 if {{var "rl"}} <= cap({{var "v"}}) { 79 {{var "v"}} = {{var "v"}}[:{{var "rl"}}] 80 } else { 81 {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}}) 82 } 83 {{var "c"}} = true 84 } else if {{var "l"}} != len({{var "v"}}) { 85 {{var "v"}} = {{var "v"}}[:{{var "l"}}] 86 {{var "c"}} = true 87 } 88 } {{end}} 89 var {{var "j"}} int 90 // var {{var "dn"}} bool 91 for {{var "j"}} = 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ { // bounds-check-elimination 92 {{if not isArray}} if {{var "j"}} == 0 && {{var "v"}} == nil { 93 if {{var "hl"}} { 94 {{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }}) 95 } else { 96 {{var "rl"}} = {{if isSlice}}8{{else if isChan}}64{{end}} 97 } 98 {{var "v"}} = make({{if isSlice}}[]{{ .Typ }}{{else if isChan}}{{.CTyp}}{{end}}, {{var "rl"}}) 99 {{var "c"}} = true 100 }{{end}} 101 {{var "h"}}.ElemContainerState({{var "j"}}) 102 {{/* {{var "dn"}} = r.TryDecodeAsNil() */}}{{/* commented out, as decLineVar handles this already each time */}} 103 {{if isChan}}{{ $x := printf "%[1]vvcx%[2]v" .TempVar .Rand }}var {{$x}} {{ .Typ }} 104 {{ decLineVar $x }} 105 {{var "v"}} <- {{ $x }} 106 // println(">>>> sending ", {{ $x }}, " into ", {{var "v"}}) // TODO: remove this 107 {{else}}{{/* // if indefinite, etc, then expand the slice if necessary */}} 108 var {{var "db"}} bool 109 if {{var "j"}} >= len({{var "v"}}) { 110 {{if isSlice }} {{var "v"}} = append({{var "v"}}, {{ zero }}) 111 {{var "c"}} = true 112 {{else}} z.DecArrayCannotExpand(len(v), {{var "j"}}+1); {{var "db"}} = true 113 {{end}} 114 } 115 if {{var "db"}} { 116 z.DecSwallow() 117 } else { 118 {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }} 119 } 120 {{end}} 121 } 122 {{if isSlice}} if {{var "j"}} < len({{var "v"}}) { 123 {{var "v"}} = {{var "v"}}[:{{var "j"}}] 124 {{var "c"}} = true 125 } else if {{var "j"}} == 0 && {{var "v"}} == nil { 126 {{var "v"}} = make([]{{ .Typ }}, 0) 127 {{var "c"}} = true 128 } {{end}} 129} 130{{var "h"}}.End() 131{{if not isArray }}if {{var "c"}} { 132 *{{ .Varname }} = {{var "v"}} 133}{{end}} 134` 135 136const genEncChanTmpl = ` 137{{.Label}}: 138switch timeout{{.Sfx}} := z.EncBasicHandle().ChanRecvTimeout; { 139case timeout{{.Sfx}} == 0: // only consume available 140 for { 141 select { 142 case b{{.Sfx}} := <-{{.Chan}}: 143 {{ .Slice }} = append({{.Slice}}, b{{.Sfx}}) 144 default: 145 break {{.Label}} 146 } 147 } 148case timeout{{.Sfx}} > 0: // consume until timeout 149 tt{{.Sfx}} := time.NewTimer(timeout{{.Sfx}}) 150 for { 151 select { 152 case b{{.Sfx}} := <-{{.Chan}}: 153 {{.Slice}} = append({{.Slice}}, b{{.Sfx}}) 154 case <-tt{{.Sfx}}.C: 155 // close(tt.C) 156 break {{.Label}} 157 } 158 } 159default: // consume until close 160 for b{{.Sfx}} := range {{.Chan}} { 161 {{.Slice}} = append({{.Slice}}, b{{.Sfx}}) 162 } 163} 164` 165