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

Function generateSliceElementWriteInlineIndented

go/fory/codegen/encoder.go:914–959  ·  view source on GitHub ↗

generateSliceElementWriteInlineIndented generates code to write a single slice element value with custom indentation

(buf *bytes.Buffer, elemType types.Type, elemAccess string, indent string)

Source from the content-addressed store, hash-verified

912
913// generateSliceElementWriteInlineIndented generates code to write a single slice element value with custom indentation
914func generateSliceElementWriteInlineIndented(buf *bytes.Buffer, elemType types.Type, elemAccess string, indent string) error {
915 // Handle basic types - write the actual value without type info (type already written above)
916 if basic, ok := elemType.Underlying().(*types.Basic); ok {
917 switch basic.Kind() {
918 case types.Bool:
919 fmt.Fprintf(buf, "%sbuf.WriteBool(%s)\n", indent, elemAccess)
920 case types.Int8:
921 fmt.Fprintf(buf, "%sbuf.WriteInt8(%s)\n", indent, elemAccess)
922 case types.Int16:
923 fmt.Fprintf(buf, "%sbuf.WriteInt16(%s)\n", indent, elemAccess)
924 case types.Int32:
925 fmt.Fprintf(buf, "%sbuf.WriteVarint32(%s)\n", indent, elemAccess)
926 case types.Int, types.Int64:
927 fmt.Fprintf(buf, "%sbuf.WriteVarint64(%s)\n", indent, elemAccess)
928 case types.Uint8:
929 fmt.Fprintf(buf, "%sbuf.WriteByte_(%s)\n", indent, elemAccess)
930 case types.Uint16:
931 fmt.Fprintf(buf, "%sbuf.WriteInt16(int16(%s))\n", indent, elemAccess)
932 case types.Uint32:
933 fmt.Fprintf(buf, "%sbuf.WriteInt32(int32(%s))\n", indent, elemAccess)
934 case types.Uint, types.Uint64:
935 fmt.Fprintf(buf, "%sbuf.WriteInt64(int64(%s))\n", indent, elemAccess)
936 case types.Float32:
937 fmt.Fprintf(buf, "%sbuf.WriteFloat32(%s)\n", indent, elemAccess)
938 case types.Float64:
939 fmt.Fprintf(buf, "%sbuf.WriteFloat64(%s)\n", indent, elemAccess)
940 case types.String:
941 fmt.Fprintf(buf, "%sctx.WriteString(%s)\n", indent, elemAccess)
942 default:
943 return fmt.Errorf("unsupported basic type for element write: %s", basic.String())
944 }
945 return nil
946 }
947
948 // Handle interface types (including 'any' which is an alias for interface{})
949 unwrappedElem := types.Unalias(elemType)
950 if iface, ok := unwrappedElem.(*types.Interface); ok {
951 if iface.Empty() {
952 // For any elements, use WriteValue for dynamic type handling
953 fmt.Fprintf(buf, "%sctx.WriteValue(reflect.ValueOf(%s), fory.RefModeTracking, true)\n", indent, elemAccess)
954 return nil
955 }
956 }
957
958 return fmt.Errorf("unsupported element type for write: %s", elemType.String())
959}

Callers 2

generateSliceWriteInlineFunction · 0.85

Calls 3

KindMethod · 0.80
StringMethod · 0.45
EmptyMethod · 0.45

Tested by

no test coverage detected