generateFieldReadTyped generates field reading code for the typed method
(buf *bytes.Buffer, field *FieldInfo)
| 87 | |
| 88 | // generateFieldReadTyped generates field reading code for the typed method |
| 89 | func generateFieldReadTyped(buf *bytes.Buffer, field *FieldInfo) error { |
| 90 | fmt.Fprintf(buf, "\t// Field: %s (%s)\n", field.GoName, field.Type.String()) |
| 91 | |
| 92 | fieldAccess := fmt.Sprintf("v.%s", field.GoName) |
| 93 | if field.IsOptional { |
| 94 | return generateOptionReadTyped(buf, field, fieldAccess) |
| 95 | } |
| 96 | |
| 97 | // Handle special named types first |
| 98 | // According to new spec, time types are "other internal types" and use ReadValue |
| 99 | if named, ok := field.Type.(*types.Named); ok { |
| 100 | typeStr := named.String() |
| 101 | switch typeStr { |
| 102 | case "time.Time", "github.com/apache/fory/go/fory.Date": |
| 103 | // These types are "other internal types" in the new spec |
| 104 | // They use: | null flag | value data | format |
| 105 | fmt.Fprintf(buf, "\tctx.ReadValue(reflect.ValueOf(&%s).Elem(), fory.RefModeTracking, true)\n", fieldAccess) |
| 106 | return nil |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Handle pointer types |
| 111 | if _, ok := field.Type.(*types.Pointer); ok { |
| 112 | // For pointer types, use ReadValue |
| 113 | fmt.Fprintf(buf, "\tctx.ReadValue(reflect.ValueOf(&%s).Elem(), fory.RefModeTracking, true)\n", fieldAccess) |
| 114 | return nil |
| 115 | } |
| 116 | |
| 117 | // Handle basic types |
| 118 | // Note: primitive serializers read values directly without NotNullValueFlag check |
| 119 | // Use error-aware methods for deferred error checking |
| 120 | if basic, ok := field.Type.Underlying().(*types.Basic); ok { |
| 121 | switch basic.Kind() { |
| 122 | case types.Bool: |
| 123 | fmt.Fprintf(buf, "\t%s = buf.ReadBool(err)\n", fieldAccess) |
| 124 | case types.Int8: |
| 125 | fmt.Fprintf(buf, "\t%s = buf.ReadInt8(err)\n", fieldAccess) |
| 126 | case types.Int16: |
| 127 | fmt.Fprintf(buf, "\t%s = buf.ReadInt16(err)\n", fieldAccess) |
| 128 | case types.Int32: |
| 129 | fmt.Fprintf(buf, "\t%s = buf.ReadVarint32(err)\n", fieldAccess) |
| 130 | case types.Int, types.Int64: |
| 131 | fmt.Fprintf(buf, "\t%s = buf.ReadVarint64(err)\n", fieldAccess) |
| 132 | case types.Uint8: |
| 133 | fmt.Fprintf(buf, "\t%s = buf.ReadByte(err)\n", fieldAccess) |
| 134 | case types.Uint16: |
| 135 | fmt.Fprintf(buf, "\t%s = uint16(buf.ReadInt16(err))\n", fieldAccess) |
| 136 | case types.Uint32: |
| 137 | fmt.Fprintf(buf, "\t%s = uint32(buf.ReadInt32(err))\n", fieldAccess) |
| 138 | case types.Uint, types.Uint64: |
| 139 | fmt.Fprintf(buf, "\t%s = uint64(buf.ReadInt64(err))\n", fieldAccess) |
| 140 | case types.Float32: |
| 141 | fmt.Fprintf(buf, "\t%s = buf.ReadFloat32(err)\n", fieldAccess) |
| 142 | case types.Float64: |
| 143 | fmt.Fprintf(buf, "\t%s = buf.ReadFloat64(err)\n", fieldAccess) |
| 144 | case types.String: |
| 145 | // In xlang mode, nullable=false is the default for struct fields. |
| 146 | // With nullable=false, RefMode = RefModeNone, so no ref flag is written/read. |
no test coverage detected