MCPcopy Create free account
hub / github.com/apache/fory / writeSliceReadElements

Function writeSliceReadElements

go/fory/codegen/decoder.go:582–633  ·  view source on GitHub ↗

writeSliceReadElements generates the element reading code for a slice with specified indentation

(buf *bytes.Buffer, sliceType *types.Slice, elemType types.Type, fieldAccess string, elemIsReferencable bool, indent string)

Source from the content-addressed store, hash-verified

580
581// writeSliceReadElements generates the element reading code for a slice with specified indentation
582func writeSliceReadElements(buf *bytes.Buffer, sliceType *types.Slice, elemType types.Type, fieldAccess string, elemIsReferencable bool, indent string) error {
583 // ReadData collection header
584 fmt.Fprintf(buf, "%scollectFlag := buf.ReadInt8(err)\n", indent)
585 fmt.Fprintf(buf, "%sif ctx.HasError() {\n", indent)
586 fmt.Fprintf(buf, "%s\treturn ctx.TakeError()\n", indent)
587 fmt.Fprintf(buf, "%s}\n", indent)
588 fmt.Fprintf(buf, "%s// Check if CollectionIsDeclElementType is set (bit 2, value 4)\n", indent)
589 fmt.Fprintf(buf, "%shasDeclType := (collectFlag & 4) != 0\n", indent)
590 if elemIsReferencable {
591 fmt.Fprintf(buf, "%s// Check if CollectionTrackingRef is set (bit 0, value 1)\n", indent)
592 fmt.Fprintf(buf, "%strackRefs := (collectFlag & 1) != 0\n", indent)
593 }
594
595 // Create slice
596 fmt.Fprintf(buf, "%sif !buf.CheckReadable(sliceLen, err) {\n", indent)
597 fmt.Fprintf(buf, "%s\treturn ctx.TakeError()\n", indent)
598 fmt.Fprintf(buf, "%s}\n", indent)
599 fmt.Fprintf(buf, "%s%s = make(%s, sliceLen)\n", indent, fieldAccess, sliceType.String())
600
601 // ReadData elements based on whether CollectionIsDeclElementType is set
602 fmt.Fprintf(buf, "%sif hasDeclType {\n", indent)
603 fmt.Fprintf(buf, "%s\t// Elements are written directly without type IDs\n", indent)
604 fmt.Fprintf(buf, "%s\tfor i := 0; i < sliceLen; i++ {\n", indent)
605 if elemIsReferencable {
606 fmt.Fprintf(buf, "%s\t\tif trackRefs {\n", indent)
607 fmt.Fprintf(buf, "%s\t\t\t_ = buf.ReadInt8(err) // Read ref flag (NotNullValueFlag)\n", indent)
608 fmt.Fprintf(buf, "%s\t\t}\n", indent)
609 }
610 if err := generateSliceElementReadDirectIndented(buf, elemType, fmt.Sprintf("%s[i]", fieldAccess), indent+"\t\t"); err != nil {
611 return err
612 }
613 fmt.Fprintf(buf, "%s\t}\n", indent)
614 fmt.Fprintf(buf, "%s} else {\n", indent)
615 fmt.Fprintf(buf, "%s\t// Need to read type ID once if CollectionIsSameType is set\n", indent)
616 fmt.Fprintf(buf, "%s\tif (collectFlag & 8) != 0 {\n", indent)
617 fmt.Fprintf(buf, "%s\t\t// ReadData element type ID once for all elements\n", indent)
618 fmt.Fprintf(buf, "%s\t\t_ = buf.ReadVarUint32(err)\n", indent)
619 fmt.Fprintf(buf, "%s\t}\n", indent)
620 fmt.Fprintf(buf, "%s\tfor i := 0; i < sliceLen; i++ {\n", indent)
621 if elemIsReferencable {
622 fmt.Fprintf(buf, "%s\t\tif trackRefs {\n", indent)
623 fmt.Fprintf(buf, "%s\t\t\t_ = buf.ReadInt8(err) // Read ref flag (NotNullValueFlag)\n", indent)
624 fmt.Fprintf(buf, "%s\t\t}\n", indent)
625 }
626 if err := generateSliceElementReadDirectIndented(buf, elemType, fmt.Sprintf("%s[i]", fieldAccess), indent+"\t\t"); err != nil {
627 return err
628 }
629 fmt.Fprintf(buf, "%s\t}\n", indent)
630 fmt.Fprintf(buf, "%s}\n", indent)
631
632 return nil
633}
634
635// generateElementTypeIDReadInline generates element type ID verification
636func generateElementTypeIDReadInline(buf *bytes.Buffer, elemType types.Type) error {

Callers 2

generateSliceReadInlineFunction · 0.85

Calls 2

StringMethod · 0.45

Tested by

no test coverage detected