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

Function generateSliceReadInline

go/fory/codegen/decoder.go:488–535  ·  view source on GitHub ↗

generateSliceReadInline generates inline slice deserialization code to match encoder behavior exactly Uses error-aware methods for deferred error checking

(buf *bytes.Buffer, sliceType *types.Slice, fieldAccess string)

Source from the content-addressed store, hash-verified

486// generateSliceReadInline generates inline slice deserialization code to match encoder behavior exactly
487// Uses error-aware methods for deferred error checking
488func generateSliceReadInline(buf *bytes.Buffer, sliceType *types.Slice, fieldAccess string) error {
489 elemType := sliceType.Elem()
490
491 // Check if element type is referencable (needs ref tracking)
492 elemIsReferencable := isReferencableType(elemType)
493
494 // In xlang mode, slices are NOT nullable by default (only pointer types are nullable).
495 // In native Go mode, slices can be nil and need null flags.
496 // Generate conditional code that respects the mode at runtime.
497
498 // ReadData slice with conditional null flag - use block scope to avoid variable name conflicts
499 fmt.Fprintf(buf, "\t{\n")
500 fmt.Fprintf(buf, "\t\tisXlang := ctx.TypeResolver().IsXlang()\n")
501 fmt.Fprintf(buf, "\t\tif isXlang {\n")
502 fmt.Fprintf(buf, "\t\t\t// xlang mode: slices are not nullable, read directly without null flag\n")
503 fmt.Fprintf(buf, "\t\t\tsliceLen := ctx.ReadCollectionLength()\n")
504 fmt.Fprintf(buf, "\t\t\tif sliceLen == 0 {\n")
505 fmt.Fprintf(buf, "\t\t\t\t%s = make(%s, 0)\n", fieldAccess, sliceType.String())
506 fmt.Fprintf(buf, "\t\t\t} else {\n")
507 // ReadData collection header in xlang mode
508 if err := writeSliceReadElements(buf, sliceType, elemType, fieldAccess, elemIsReferencable, "\t\t\t\t"); err != nil {
509 return err
510 }
511 fmt.Fprintf(buf, "\t\t\t}\n") // end else (sliceLen > 0)
512 fmt.Fprintf(buf, "\t\t} else {\n")
513 fmt.Fprintf(buf, "\t\t\t// Native Go mode: slices are nullable, read null flag\n")
514 fmt.Fprintf(buf, "\t\t\tnullFlag := buf.ReadInt8(err)\n")
515 fmt.Fprintf(buf, "\t\t\tif nullFlag == -3 {\n") // NullFlag
516 fmt.Fprintf(buf, "\t\t\t\t%s = nil\n", fieldAccess)
517 fmt.Fprintf(buf, "\t\t\t} else {\n")
518 fmt.Fprintf(buf, "\t\t\t\tsliceLen := ctx.ReadCollectionLength()\n")
519 fmt.Fprintf(buf, "\t\t\t\tif ctx.HasError() {\n")
520 fmt.Fprintf(buf, "\t\t\t\t\treturn ctx.TakeError()\n")
521 fmt.Fprintf(buf, "\t\t\t\t}\n")
522 fmt.Fprintf(buf, "\t\t\t\tif sliceLen == 0 {\n")
523 fmt.Fprintf(buf, "\t\t\t\t\t%s = make(%s, 0)\n", fieldAccess, sliceType.String())
524 fmt.Fprintf(buf, "\t\t\t\t} else {\n")
525 // ReadData collection header in native mode
526 if err := writeSliceReadElements(buf, sliceType, elemType, fieldAccess, elemIsReferencable, "\t\t\t\t\t"); err != nil {
527 return err
528 }
529 fmt.Fprintf(buf, "\t\t\t\t}\n") // end else (sliceLen > 0)
530 fmt.Fprintf(buf, "\t\t\t}\n") // end else (not null)
531 fmt.Fprintf(buf, "\t\t}\n") // end else (native mode)
532 fmt.Fprintf(buf, "\t}\n") // end block scope
533
534 return nil
535}
536
537func generateSliceReadInlineNoNull(buf *bytes.Buffer, sliceType *types.Slice, fieldAccess string) error {
538 elemType := sliceType.Elem()

Callers 1

generateFieldReadTypedFunction · 0.85

Calls 3

isReferencableTypeFunction · 0.85
writeSliceReadElementsFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected