Skip to content

feat(compiler): add union support to fory compiler and runtime#3195

Merged
chaokunyang merged 21 commits intoapache:mainfrom
chaokunyang:add_union_support_to_idl
Jan 23, 2026
Merged

feat(compiler): add union support to fory compiler and runtime#3195
chaokunyang merged 21 commits intoapache:mainfrom
chaokunyang:add_union_support_to_idl

Conversation

@chaokunyang
Copy link
Copy Markdown
Collaborator

@chaokunyang chaokunyang commented Jan 23, 2026

Why?

Fory needs Union type support in its IDL compiler to enable cross-language serialization of sum types (tagged unions). This allows users to define Union types in FDL/Protobuf/FlatBuffers schemas and have the compiler generate code for all supported languages (Java, C++, Python, Go, Rust).

What does this PR do?

This PR adds comprehensive Union type support across the entire Fory stack:

Compiler & IDL:

  • Added Union AST node to the IR with support for FDL, Protobuf (oneof), and FlatBuffers (union)
  • Implemented Union code generation for all languages: Java, C++, Python, Go, and Rust
  • Added validation and semantic checks for Union types
  • Updated parsers to recognize Union/oneof/union syntax in FDL, Proto, and FBS formats

Runtime Support:

  • Java: Implemented generic UnionSerializer for generated Union classes, supporting Union2-Union6 typed variants
  • C++: Added union_serializer.h with template-based serialization for unions, integrating with existing std::variant support
  • Python: Created union.py module with UnionSerializer for dynamic union type handling
  • Go: Implemented union.go with union type registration and serialization
  • Rust: Enhanced enum derive macro to generate Union-compatible serialization with proper TypeId normalization

Protocol & Xlang:

  • Aligned cross-language Union serialization format: index + ref_flag + type_id + data
  • Updated xlang specification to document Union type behavior
  • Added named union support with meta-sharing for efficient type encoding
  • Implemented skip logic for Union types in all languages

Testing:

  • Added integration tests in idl_tests for C++, Go, Java, Python roundtrip validation
  • Added xlang tests to verify cross-language Union compatibility

Related issues

Closes #3182 #3191 #3170
#1017
#3099

Does this PR introduce any user-facing change?

Yes. Users can now define Union types in Fory IDL files and use Protobuf oneof or FlatBuffers union syntax which will be compiled to Fory Union types.

  • Does this PR introduce any public API change? Yes - Adds Union types to the type system
  • Does this PR introduce any binary protocol compatibility change? No - Union uses existing TypeId (31) from xlang spec

Benchmark

No performance regression expected. Union serialization uses the same optimized paths as existing reference serialization with additional index metadata.

@chaokunyang chaokunyang mentioned this pull request Jan 23, 2026
16 tasks
@chaokunyang chaokunyang force-pushed the add_union_support_to_idl branch from ff228ec to 6e79841 Compare January 23, 2026 10:21
@chaokunyang chaokunyang changed the title [Xlang][Compiler] Add union support to fory compiler feat(compiler): add union support to fory compiler Jan 23, 2026
@chaokunyang chaokunyang changed the title feat(compiler): add union support to fory compiler feat(compiler): add union support to fory compiler and runtime Jan 23, 2026
@chaokunyang chaokunyang force-pushed the add_union_support_to_idl branch from 4c97df4 to 66fd0fa Compare January 23, 2026 14:39
@chaokunyang chaokunyang force-pushed the add_union_support_to_idl branch from 66fd0fa to be6c032 Compare January 23, 2026 14:45
@chaokunyang chaokunyang merged commit c200874 into apache:main Jan 23, 2026
61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Xlang][Compiler] Union support

2 participants