feat(go): implement union type for xlang serialization#3064
Open
userzhy wants to merge 2 commits intoapache:mainfrom
Open
feat(go): implement union type for xlang serialization#3064userzhy wants to merge 2 commits intoapache:mainfrom
userzhy wants to merge 2 commits intoapache:mainfrom
Conversation
chaokunyang
reviewed
Dec 20, 2025
Add support for tagged union types in Go Fory, enabling cross-language serialization compatibility with other languages like C++, Java, Python, and Rust that already support union/variant types. Changes: - Add UNION (38) and NONE (39) type constants to types.go - Implement Union struct and unionSerializer in union.go - Add RegisterUnionType API to Fory for registering union alternatives - Add comprehensive tests in union_test.go The implementation follows the same binary protocol as other languages: 1. Write variant index (varuint32) 2. In xlang mode, write type info for the active alternative 3. Write the value data using the alternative's serializer Fixes apache#3031
Address review feedback from chaokunyang. Since Go 1.23+ is used,
replace interface{}-based Union with generic Union2, Union3, Union4 types.
New API:
- Union2[T1, T2], Union3[T1, T2, T3], Union4[T1, T2, T3, T4] generic types
- NewUnion2A/B, NewUnion3A/B/C, NewUnion4A/B/C/D constructors
- Match() methods for pattern matching (type-safe callbacks)
- Index(), IsFirst(), IsSecond() accessor methods
- First(), Second() value extraction methods (panics on wrong access)
- RegisterUnion2Type, RegisterUnion3Type, RegisterUnion4Type functions
Benefits:
- Compile-time type safety
- Explicit variant tracking
- Pattern matching support
- No runtime type assertions needed
d7d3c70 to
950e1da
Compare
Contributor
Author
|
@chaokunyang Fixed, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
Go Fory lacks support for Union types, which prevents cross-language serialization compatibility with other languages like C++, Java, Python, and Rust that already support union/variant types. This limitation was reported in issue #3031.
Without Union type support, Go users cannot:
What does this PR do?
This PR implements complete Union type support for Go Fory by:
Added TypeId constants to
go/fory/types.go:UNION = 38- for tagged union typesNONE = 39- for empty/unit valuesImplemented Union struct and unionSerializer in
go/fory/union.go:Unionstruct that holds aninterface{}valueAdded RegisterUnionType API to Fory:
f.RegisterUnionType(reflect.TypeOf(int32(0)), reflect.TypeOf(""))Added comprehensive tests in
go/fory/union_test.go:The implementation follows the same binary protocol as C++/Python/Rust variant serialization:
Related issues
Fixes #3031
Related to #3027 (tracking issue for xlang union type system)
Does this PR introduce any user-facing change?
Does this PR introduce any public API change?
Unionstruct andRegisterUnionTypemethod. Users can now use Union types in their code and they will be automatically serialized/deserialized.Does this PR introduce any binary protocol compatibility change?
Benchmark
This PR does not have a performance impact on existing functionality:
For Union types specifically, the overhead is minimal: