generateSliceElementRead generates code to read a single slice element
(buf *bytes.Buffer, elemType types.Type, elemAccess string)
| 391 | |
| 392 | // generateSliceElementRead generates code to read a single slice element |
| 393 | func generateSliceElementRead(buf *bytes.Buffer, elemType types.Type, elemAccess string) error { |
| 394 | // Handle basic types |
| 395 | if basic, ok := elemType.Underlying().(*types.Basic); ok { |
| 396 | switch basic.Kind() { |
| 397 | case types.Bool: |
| 398 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadBool()\n", elemAccess) |
| 399 | case types.Int8: |
| 400 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadInt8()\n", elemAccess) |
| 401 | case types.Int16: |
| 402 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadInt16()\n", elemAccess) |
| 403 | case types.Int32: |
| 404 | fmt.Fprintf(buf, "\t\t\t\tif flag := buf.ReadInt8(); flag != -1 {\n") |
| 405 | fmt.Fprintf(buf, "\t\t\t\t\treturn fmt.Errorf(\"expected NotNullValueFlag for slice element, got %%d\", flag)\n") |
| 406 | fmt.Fprintf(buf, "\t\t\t\t}\n") |
| 407 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadVarint32()\n", elemAccess) |
| 408 | case types.Int, types.Int64: |
| 409 | fmt.Fprintf(buf, "\t\t\t\tif flag := buf.ReadInt8(); flag != -1 {\n") |
| 410 | fmt.Fprintf(buf, "\t\t\t\t\treturn fmt.Errorf(\"expected NotNullValueFlag for slice element, got %%d\", flag)\n") |
| 411 | fmt.Fprintf(buf, "\t\t\t\t}\n") |
| 412 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadVarint64()\n", elemAccess) |
| 413 | case types.Uint8: |
| 414 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadByte_()\n", elemAccess) |
| 415 | case types.Uint16: |
| 416 | fmt.Fprintf(buf, "\t\t\t\t%s = uint16(buf.ReadInt16())\n", elemAccess) |
| 417 | case types.Uint32: |
| 418 | fmt.Fprintf(buf, "\t\t\t\t%s = uint32(buf.ReadInt32())\n", elemAccess) |
| 419 | case types.Uint, types.Uint64: |
| 420 | fmt.Fprintf(buf, "\t\t\t\t%s = uint64(buf.ReadInt64())\n", elemAccess) |
| 421 | case types.Float32: |
| 422 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadFloat32()\n", elemAccess) |
| 423 | case types.Float64: |
| 424 | fmt.Fprintf(buf, "\t\t\t\t%s = buf.ReadFloat64()\n", elemAccess) |
| 425 | case types.String: |
| 426 | fmt.Fprintf(buf, "\t\t\t\tif flag := buf.ReadInt8(); flag != 0 {\n") |
| 427 | fmt.Fprintf(buf, "\t\t\t\t\treturn fmt.Errorf(\"expected RefValueFlag for string element, got %%d\", flag)\n") |
| 428 | fmt.Fprintf(buf, "\t\t\t\t}\n") |
| 429 | fmt.Fprintf(buf, "\t\t\t\t%s = ctx.ReadString()\n", elemAccess) |
| 430 | default: |
| 431 | fmt.Fprintf(buf, "\t\t\t\t// TODO: unsupported basic type %s\n", basic.String()) |
| 432 | } |
| 433 | return nil |
| 434 | } |
| 435 | |
| 436 | // Handle named types |
| 437 | if named, ok := elemType.(*types.Named); ok { |
| 438 | typeStr := named.String() |
| 439 | switch typeStr { |
| 440 | case "time.Time": |
| 441 | fmt.Fprintf(buf, "\t\t\t\tseconds := buf.ReadInt64()\n") |
| 442 | fmt.Fprintf(buf, "\t\t\t\tnanos := buf.ReadUint32()\n") |
| 443 | fmt.Fprintf(buf, "\t\t\t\t%s = fory.CreateTimeFromUnixSecondsAndNanos(seconds, nanos)\n", elemAccess) |
| 444 | return nil |
| 445 | case "github.com/apache/fory/go/fory.Date": |
| 446 | fmt.Fprintf(buf, "\t\t\t\tif ctx.TypeResolver().IsXlang() {\n") |
| 447 | fmt.Fprintf(buf, "\t\t\t\t\tdays := buf.ReadVarint64(err)\n") |
| 448 | fmt.Fprintf(buf, "\t\t\t\t\tif ctx.HasError() {\n") |
| 449 | fmt.Fprintf(buf, "\t\t\t\t\t\treturn ctx.TakeError()\n") |
| 450 | fmt.Fprintf(buf, "\t\t\t\t\t}\n") |