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

Function generateMapReadInline

go/fory/codegen/decoder.go:808–865  ·  view source on GitHub ↗

generateMapReadInline generates inline map deserialization code following the chunk-based format Uses error-aware methods for deferred error checking

(buf *bytes.Buffer, mapType *types.Map, fieldAccess string)

Source from the content-addressed store, hash-verified

806// generateMapReadInline generates inline map deserialization code following the chunk-based format
807// Uses error-aware methods for deferred error checking
808func generateMapReadInline(buf *bytes.Buffer, mapType *types.Map, fieldAccess string) error {
809 keyType := mapType.Key()
810 valueType := mapType.Elem()
811
812 // Check if key or value types are any (unwrap aliases like 'any')
813 keyIsInterface := false
814 valueIsInterface := false
815 unwrappedKey := types.Unalias(keyType)
816 unwrappedValue := types.Unalias(valueType)
817 if iface, ok := unwrappedKey.(*types.Interface); ok && iface.Empty() {
818 keyIsInterface = true
819 }
820 if iface, ok := unwrappedValue.(*types.Interface); ok && iface.Empty() {
821 valueIsInterface = true
822 }
823
824 // In xlang mode, maps are NOT nullable by default (only pointer types are nullable).
825 // In native Go mode, maps can be nil and need null flags.
826 // Generate conditional code that respects the mode at runtime.
827
828 // ReadData map with conditional null flag
829 fmt.Fprintf(buf, "\t{\n")
830 fmt.Fprintf(buf, "\t\tisXlang := ctx.TypeResolver().IsXlang()\n")
831 fmt.Fprintf(buf, "\t\tif isXlang {\n")
832 fmt.Fprintf(buf, "\t\t\t// xlang mode: maps are not nullable, read directly without null flag\n")
833 fmt.Fprintf(buf, "\t\t\tmapLen := ctx.ReadCollectionLength()\n")
834 fmt.Fprintf(buf, "\t\t\tif mapLen == 0 {\n")
835 fmt.Fprintf(buf, "\t\t\t\t%s = make(%s)\n", fieldAccess, mapType.String())
836 fmt.Fprintf(buf, "\t\t\t} else {\n")
837 // Read map chunks in xlang mode
838 if err := writeMapReadChunks(buf, mapType, fieldAccess, keyType, valueType, keyIsInterface, valueIsInterface, "\t\t\t\t"); err != nil {
839 return err
840 }
841 fmt.Fprintf(buf, "\t\t\t}\n") // end else (mapLen > 0)
842 fmt.Fprintf(buf, "\t\t} else {\n")
843 fmt.Fprintf(buf, "\t\t\t// Native Go mode: maps are nullable, read null flag\n")
844 fmt.Fprintf(buf, "\t\t\tnullFlag := buf.ReadInt8(err)\n")
845 fmt.Fprintf(buf, "\t\t\tif nullFlag == -3 {\n") // NullFlag
846 fmt.Fprintf(buf, "\t\t\t\t%s = nil\n", fieldAccess)
847 fmt.Fprintf(buf, "\t\t\t} else {\n")
848 fmt.Fprintf(buf, "\t\t\t\tmapLen := ctx.ReadCollectionLength()\n")
849 fmt.Fprintf(buf, "\t\t\t\tif ctx.HasError() {\n")
850 fmt.Fprintf(buf, "\t\t\t\t\treturn ctx.TakeError()\n")
851 fmt.Fprintf(buf, "\t\t\t\t}\n")
852 fmt.Fprintf(buf, "\t\t\t\tif mapLen == 0 {\n")
853 fmt.Fprintf(buf, "\t\t\t\t\t%s = make(%s)\n", fieldAccess, mapType.String())
854 fmt.Fprintf(buf, "\t\t\t\t} else {\n")
855 // Read map chunks in native mode
856 if err := writeMapReadChunks(buf, mapType, fieldAccess, keyType, valueType, keyIsInterface, valueIsInterface, "\t\t\t\t\t"); err != nil {
857 return err
858 }
859 fmt.Fprintf(buf, "\t\t\t\t}\n") // end else (mapLen > 0)
860 fmt.Fprintf(buf, "\t\t\t}\n") // end else (not null)
861 fmt.Fprintf(buf, "\t\t}\n") // end else (native mode)
862 fmt.Fprintf(buf, "\t}\n") // end block scope
863
864 return nil
865}

Callers 1

generateFieldReadTypedFunction · 0.85

Calls 3

writeMapReadChunksFunction · 0.85
EmptyMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected