2018-01-26 23:51:00 +00:00
|
|
|
// +build codecgen.exec
|
|
|
|
|
2015-06-29 21:50:55 +00:00
|
|
|
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
|
2015-08-19 01:12:51 +00:00
|
|
|
// Use of this source code is governed by a MIT license found in the LICENSE file.
|
2015-06-29 21:50:55 +00:00
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
// DO NOT EDIT. THIS FILE IS AUTO-GENERATED FROM gen-dec-(map|array).go.tmpl
|
|
|
|
|
|
|
|
const genDecMapTmpl = `
|
|
|
|
{{var "v"}} := *{{ .Varname }}
|
|
|
|
{{var "l"}} := r.ReadMapStart()
|
2015-10-15 21:52:18 +00:00
|
|
|
{{var "bh"}} := z.DecBasicHandle()
|
2015-06-29 21:50:55 +00:00
|
|
|
if {{var "v"}} == nil {
|
2017-10-27 19:06:04 +00:00
|
|
|
{{var "rl"}} := z.DecInferLen({{var "l"}}, {{var "bh"}}.MaxInitLen, {{ .Size }})
|
2015-10-07 20:10:00 +00:00
|
|
|
{{var "v"}} = make(map[{{ .KTyp }}]{{ .Typ }}, {{var "rl"}})
|
2015-06-29 21:50:55 +00:00
|
|
|
*{{ .Varname }} = {{var "v"}}
|
|
|
|
}
|
2015-10-15 21:52:18 +00:00
|
|
|
var {{var "mk"}} {{ .KTyp }}
|
|
|
|
var {{var "mv"}} {{ .Typ }}
|
2017-10-27 19:06:04 +00:00
|
|
|
var {{var "mg"}}, {{var "mdn"}} {{if decElemKindPtr}}, {{var "ms"}}, {{var "mok"}}{{end}} bool
|
2015-10-15 21:52:18 +00:00
|
|
|
if {{var "bh"}}.MapValueReset {
|
|
|
|
{{if decElemKindPtr}}{{var "mg"}} = true
|
|
|
|
{{else if decElemKindIntf}}if !{{var "bh"}}.InterfaceReset { {{var "mg"}} = true }
|
|
|
|
{{else if not decElemKindImmutable}}{{var "mg"}} = true
|
|
|
|
{{end}} }
|
2017-10-27 19:06:04 +00:00
|
|
|
if {{var "l"}} != 0 {
|
|
|
|
{{var "hl"}} := {{var "l"}} > 0
|
|
|
|
for {{var "j"}} := 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ {
|
|
|
|
r.ReadMapElemKey() {{/* z.DecSendContainerState(codecSelfer_containerMapKey{{ .Sfx }}) */}}
|
2015-06-29 21:50:55 +00:00
|
|
|
{{ $x := printf "%vmk%v" .TempVar .Rand }}{{ decLineVarK $x }}
|
2015-10-15 21:52:18 +00:00
|
|
|
{{ if eq .KTyp "interface{}" }}{{/* // special case if a byte array. */}}if {{var "bv"}}, {{var "bok"}} := {{var "mk"}}.([]byte); {{var "bok"}} {
|
2015-06-29 21:50:55 +00:00
|
|
|
{{var "mk"}} = string({{var "bv"}})
|
2015-10-30 22:07:00 +00:00
|
|
|
}{{ end }}{{if decElemKindPtr}}
|
|
|
|
{{var "ms"}} = true{{end}}
|
2015-10-15 21:52:18 +00:00
|
|
|
if {{var "mg"}} {
|
2015-10-30 22:07:00 +00:00
|
|
|
{{if decElemKindPtr}}{{var "mv"}}, {{var "mok"}} = {{var "v"}}[{{var "mk"}}]
|
|
|
|
if {{var "mok"}} {
|
|
|
|
{{var "ms"}} = false
|
|
|
|
} {{else}}{{var "mv"}} = {{var "v"}}[{{var "mk"}}] {{end}}
|
2015-10-15 21:52:18 +00:00
|
|
|
} {{if not decElemKindImmutable}}else { {{var "mv"}} = {{decElemZero}} }{{end}}
|
2017-10-27 19:06:04 +00:00
|
|
|
r.ReadMapElemValue() {{/* z.DecSendContainerState(codecSelfer_containerMapValue{{ .Sfx }}) */}}
|
|
|
|
{{var "mdn"}} = false
|
|
|
|
{{ $x := printf "%vmv%v" .TempVar .Rand }}{{ $y := printf "%vmdn%v" .TempVar .Rand }}{{ decLineVar $x $y }}
|
|
|
|
if {{var "mdn"}} {
|
|
|
|
if {{ var "bh" }}.DeleteOnNilMapValue { delete({{var "v"}}, {{var "mk"}}) } else { {{var "v"}}[{{var "mk"}}] = {{decElemZero}} }
|
|
|
|
} else if {{if decElemKindPtr}} {{var "ms"}} && {{end}} {{var "v"}} != nil {
|
2015-06-29 21:50:55 +00:00
|
|
|
{{var "v"}}[{{var "mk"}}] = {{var "mv"}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // else len==0: TODO: Should we clear map entries?
|
2017-10-27 19:06:04 +00:00
|
|
|
r.ReadMapEnd() {{/* z.DecSendContainerState(codecSelfer_containerMapEnd{{ .Sfx }}) */}}
|
2015-06-29 21:50:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
const genDecListTmpl = `
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "v"}} := {{if not isArray}}*{{end}}{{ .Varname }}
|
2016-04-26 00:18:04 +00:00
|
|
|
{{var "h"}}, {{var "l"}} := z.DecSliceHelperStart() {{/* // helper, containerLenS */}}{{if not isArray}}
|
2015-10-30 22:07:00 +00:00
|
|
|
var {{var "c"}} bool {{/* // changed */}}
|
2016-04-26 00:18:04 +00:00
|
|
|
_ = {{var "c"}}{{end}}
|
2015-10-30 22:07:00 +00:00
|
|
|
if {{var "l"}} == 0 {
|
|
|
|
{{if isSlice }}if {{var "v"}} == nil {
|
|
|
|
{{var "v"}} = []{{ .Typ }}{}
|
|
|
|
{{var "c"}} = true
|
|
|
|
} else if len({{var "v"}}) != 0 {
|
|
|
|
{{var "v"}} = {{var "v"}}[:0]
|
|
|
|
{{var "c"}} = true
|
2018-07-06 16:09:34 +00:00
|
|
|
} {{else if isChan }}if {{var "v"}} == nil {
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "v"}} = make({{ .CTyp }}, 0)
|
|
|
|
{{var "c"}} = true
|
|
|
|
} {{end}}
|
2017-10-27 19:06:04 +00:00
|
|
|
} else {
|
|
|
|
{{var "hl"}} := {{var "l"}} > 0
|
2018-07-06 16:09:34 +00:00
|
|
|
var {{var "rl"}} int
|
|
|
|
_ = {{var "rl"}}
|
2017-10-27 19:06:04 +00:00
|
|
|
{{if isSlice }} if {{var "hl"}} {
|
2015-06-29 21:50:55 +00:00
|
|
|
if {{var "l"}} > cap({{var "v"}}) {
|
2017-10-27 19:06:04 +00:00
|
|
|
{{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
|
|
|
|
if {{var "rl"}} <= cap({{var "v"}}) {
|
|
|
|
{{var "v"}} = {{var "v"}}[:{{var "rl"}}]
|
2015-10-30 22:07:00 +00:00
|
|
|
} else {
|
|
|
|
{{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "c"}} = true
|
2017-10-27 19:06:04 +00:00
|
|
|
} else if {{var "l"}} != len({{var "v"}}) {
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "v"}} = {{var "v"}}[:{{var "l"}}]
|
|
|
|
{{var "c"}} = true
|
2015-10-07 20:10:00 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
} {{end}}
|
|
|
|
var {{var "j"}} int
|
|
|
|
// var {{var "dn"}} bool
|
|
|
|
for ; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ {
|
2018-07-06 16:09:34 +00:00
|
|
|
{{if not isArray}} if {{var "j"}} == 0 && {{var "v"}} == nil {
|
2017-10-27 19:06:04 +00:00
|
|
|
if {{var "hl"}} {
|
|
|
|
{{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
|
|
|
|
} else {
|
2018-07-06 16:09:34 +00:00
|
|
|
{{var "rl"}} = {{if isSlice}}8{{else if isChan}}64{{end}}
|
2017-10-27 19:06:04 +00:00
|
|
|
}
|
2018-07-06 16:09:34 +00:00
|
|
|
{{var "v"}} = make({{if isSlice}}[]{{ .Typ }}{{else if isChan}}{{.CTyp}}{{end}}, {{var "rl"}})
|
2017-10-27 19:06:04 +00:00
|
|
|
{{var "c"}} = true
|
|
|
|
}{{end}}
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "h"}}.ElemContainerState({{var "j"}})
|
2018-07-06 16:09:34 +00:00
|
|
|
{{/* {{var "dn"}} = r.TryDecodeAsNil() */}}{{/* commented out, as decLineVar handles this already each time */}}
|
|
|
|
{{if isChan}}{{ $x := printf "%[1]vvcx%[2]v" .TempVar .Rand }}var {{$x}} {{ .Typ }}
|
2017-10-27 19:06:04 +00:00
|
|
|
{{ decLineVar $x }}
|
|
|
|
{{var "v"}} <- {{ $x }}
|
2018-07-06 16:09:34 +00:00
|
|
|
// println(">>>> sending ", {{ $x }}, " into ", {{var "v"}}) // TODO: remove this
|
|
|
|
{{else}}{{/* // if indefinite, etc, then expand the slice if necessary */}}
|
2017-10-27 19:06:04 +00:00
|
|
|
var {{var "db"}} bool
|
2015-10-30 22:07:00 +00:00
|
|
|
if {{var "j"}} >= len({{var "v"}}) {
|
2018-07-06 16:09:34 +00:00
|
|
|
{{if isSlice }} {{var "v"}} = append({{var "v"}}, {{ zero }})
|
|
|
|
{{var "c"}} = true
|
2017-10-27 19:06:04 +00:00
|
|
|
{{else}} z.DecArrayCannotExpand(len(v), {{var "j"}}+1); {{var "db"}} = true
|
|
|
|
{{end}}
|
2015-10-30 22:07:00 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
if {{var "db"}} {
|
2015-06-29 21:50:55 +00:00
|
|
|
z.DecSwallow()
|
2017-10-27 19:06:04 +00:00
|
|
|
} else {
|
|
|
|
{{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
{{end}}
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2017-10-27 19:06:04 +00:00
|
|
|
{{if isSlice}} if {{var "j"}} < len({{var "v"}}) {
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "v"}} = {{var "v"}}[:{{var "j"}}]
|
|
|
|
{{var "c"}} = true
|
|
|
|
} else if {{var "j"}} == 0 && {{var "v"}} == nil {
|
2017-10-27 19:06:04 +00:00
|
|
|
{{var "v"}} = make([]{{ .Typ }}, 0)
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "c"}} = true
|
2017-10-27 19:06:04 +00:00
|
|
|
} {{end}}
|
2015-06-29 21:50:55 +00:00
|
|
|
}
|
2015-10-30 22:07:00 +00:00
|
|
|
{{var "h"}}.End()
|
|
|
|
{{if not isArray }}if {{var "c"}} {
|
2015-06-29 21:50:55 +00:00
|
|
|
*{{ .Varname }} = {{var "v"}}
|
2015-10-30 22:07:00 +00:00
|
|
|
}{{end}}
|
2018-07-06 16:09:34 +00:00
|
|
|
`
|
2017-10-27 19:06:04 +00:00
|
|
|
|
2018-07-06 16:09:34 +00:00
|
|
|
const genEncChanTmpl = `
|
|
|
|
{{.Label}}:
|
|
|
|
switch timeout{{.Sfx}} := z.EncBasicHandle().ChanRecvTimeout; {
|
|
|
|
case timeout{{.Sfx}} == 0: // only consume available
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b{{.Sfx}} := <-{{.Chan}}:
|
|
|
|
{{ .Slice }} = append({{.Slice}}, b{{.Sfx}})
|
|
|
|
default:
|
|
|
|
break {{.Label}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case timeout{{.Sfx}} > 0: // consume until timeout
|
|
|
|
tt{{.Sfx}} := time.NewTimer(timeout{{.Sfx}})
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case b{{.Sfx}} := <-{{.Chan}}:
|
|
|
|
{{.Slice}} = append({{.Slice}}, b{{.Sfx}})
|
|
|
|
case <-tt{{.Sfx}}.C:
|
|
|
|
// close(tt.C)
|
|
|
|
break {{.Label}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: // consume until close
|
|
|
|
for b{{.Sfx}} := range {{.Chan}} {
|
|
|
|
{{.Slice}} = append({{.Slice}}, b{{.Sfx}})
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 21:50:55 +00:00
|
|
|
`
|