MCPcopy
hub / github.com/tinylib/msgp / structAsMap

Method structAsMap

gen/decode.go:236–327  ·  view source on GitHub ↗
(s *Struct)

Source from the content-addressed store, hash-verified

234}
235
236func (d *decodeGen) structAsMap(s *Struct) {
237 d.needsField()
238 sz := randIdent()
239 d.p.declare(sz, u32)
240 d.assignMap(sz, mapHeader, 0)
241
242 oeCount := s.CountFieldTagPart("omitempty") + s.CountFieldTagPart("omitzero")
243 if !d.ctx.clearOmitted {
244 oeCount = 0
245 }
246 bm := bmask{
247 bitlen: oeCount,
248 varname: sz + "Mask",
249 }
250 if oeCount > 0 {
251 // Declare mask
252 d.p.printf("\n%s", bm.typeDecl())
253 d.p.printf("\n_ = %s", bm.varname)
254 }
255 // Index to field idx of each emitted
256 oeEmittedIdx := []int{}
257
258 d.p.printf("\nfor %s > 0 {\n%s--", sz, sz)
259 d.assignAndCheck("field", mapKey)
260 d.p.print("\nswitch msgp.UnsafeString(field) {")
261 for i := range s.Fields {
262 d.ctx.PushString(s.Fields[i].FieldName)
263 d.p.printf("\ncase %q:", s.Fields[i].FieldTag)
264 fieldElem := s.Fields[i].FieldElem
265 anField := s.Fields[i].HasTagPart("allownil") && fieldElem.AllowNil()
266
267 // Set field-specific limits in context based on struct field's FieldLimit
268 if s.Fields[i].FieldLimit > 0 {
269 // Apply same limit to both arrays and maps for this field
270 d.ctx.SetFieldLimits(s.Fields[i].FieldLimit, s.Fields[i].FieldLimit)
271 } else {
272 d.ctx.ClearFieldLimits()
273 }
274
275 if anField {
276 d.p.print("\nif dc.IsNil() {")
277 d.p.print("\nerr = dc.ReadNil()")
278 d.p.wrapErrCheck(d.ctx.ArgsStr())
279 d.p.printf("\n%s = nil\n} else {", fieldElem.Varname())
280 }
281 SetIsAllowNil(fieldElem, anField)
282 setTypeParams(fieldElem, s.typeParams)
283 next(d, fieldElem)
284
285 // Clear field limits after processing
286 d.ctx.ClearFieldLimits()
287
288 if oeCount > 0 && (s.Fields[i].HasTagPart("omitempty") || s.Fields[i].HasTagPart("omitzero")) {
289 d.p.printf("\n%s", bm.setStmt(len(oeEmittedIdx)))
290 oeEmittedIdx = append(oeEmittedIdx, i)
291 }
292 d.ctx.Pop()
293 if !d.p.ok() {

Callers 1

gStructMethod · 0.95

Calls 15

needsFieldMethod · 0.95
assignMapMethod · 0.95
typeDeclMethod · 0.95
assignAndCheckMethod · 0.95
setStmtMethod · 0.95
notAllSetMethod · 0.95
readExprMethod · 0.95
randIdentFunction · 0.85
SetIsAllowNilFunction · 0.85
setTypeParamsFunction · 0.85
nextFunction · 0.85
declareMethod · 0.80

Tested by

no test coverage detected