github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/gen.generated.go (about)

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