Skip to content

[Xlang] [Compiler] Flatbuffer Schema IDL support #3171

@chaokunyang

Description

@chaokunyang

Feature Request

Add FlatBuffers IDL support to Fory compiler, enabling users to define schemas using FlatBuffers .fbs files and generate serialization code through Fory's compilation pipeline.

Is your feature request related to a problem? Please describe

Currently, Fory compiler only supports its native IDL format. Users who have existing FlatBuffers schemas or prefer FlatBuffers IDL syntax cannot leverage Fory's high-performance serialization without manually converting their schemas. This creates friction for teams migrating from FlatBuffers to Fory or those who want to use FlatBuffers IDL as their schema definition language.

Describe the solution you'd like

Implement FlatBuffers IDL parser and translator in Fory compiler with the following specifications:

1. FlatBuffers IDL Parser

  • Parse .fbs files and translate to Fory IR (Intermediate Representation)

2. Scalar Type Mapping

FlatBuffers Type Fory Type
byte int8
ubyte uint8
short / int16 int16
ushort / uint16 uint16
int / int32 varint32
uint / uint32 var_uint32
long / int64 varint64
ulong / uint64 var_uint64
float / float32 float32
double / float64 float64

3. Complex Type Mapping

FlatBuffers Construct Fory Mapping
struct Fory struct with evolving=false
table Fory struct with evolving=true
enum Fory enum
[T] (array/vector) Fory ListType
attribute Fory option
field attributes (e.g., deprecated, priority: 1) Fory field options
union Fory union

4. Auto growing field id

When translate flatbuffer table/struct schema to fory, we should allocate an autogrowing field id to each field, since flatbuffer use this order for schema compatibility.

flatbuffer:

table Foo {
  x: float;
  y: float;
}

fory:

message {
  float32 x = 0;
  float32 y = 1;
}

5. Implementation Tasks

  • Implement FlatBuffers IDL lexer and parser
  • Implement IR translator from FlatBuffers AST to Fory IR
  • Add error handling for unsupported features (union types)
  • Rename integration_tests/idl_tests/proto to integration_tests/idl_tests/idl and fix all tests
  • Create comprehensive .fbs test file at integration_tests/idl_tests/idl covering all supported FlatBuffers IDL features except union
  • Add documentation docs/compiler/flatbuffers-idl.md detailing:
    • Differences between FlatBuffers IDL and Fory IDL
    • Usage guide for FlatBuffers IDL with Fory compiler
    • Generated code differences and examples

Describe alternatives you've considered

  1. Manual schema conversion: Users could manually convert FlatBuffers schemas to Fory IDL, but this is error-prone and time-consuming for large schemas.

  2. External conversion tool: A standalone tool to convert .fbs to .fory files, but this adds extra steps and doesn't integrate well with the compilation pipeline.

  3. Protocol Buffers only: Continue supporting only Protobuf IDL, but this limits adoption from the FlatBuffers community.

Additional context

Example FlatBuffers IDL input:

namespace MyGame.Sample;

enum Color : byte { Red = 0, Green, Blue }

struct Vec3 {
  x: float;
  y: float;
  z: float;
}

table Monster {
  pos: Vec3;
  mana: short = 150;
  hp: short = 100;
  name: string;
  friendly: bool = false (deprecated, priority: 1);
  inventory: [ubyte];
  color: Color = Blue;
}

root_type Monster;

Expected behavior:

  • struct Vec3 maps to Fory struct with evolving=false (fixed layout)
  • table Monster maps to Fory struct with evolving=true (schema evolution supported)
  • Field attributes like deprecated and priority: 1 map to Fory field options
  • Default values are preserved in generated code

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions