feat(go): implement union type for xlang serialization #3064
+860
−0
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: