userzhy opened a new pull request, #3064:
URL: https://github.com/apache/fory/pull/3064

   ## 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:
   - Serialize union types to communicate with other languages
   - Use tagged union patterns common in serialization scenarios
   - Leverage the full xlang serialization capabilities
   
   ## What does this PR do?
   
   This PR implements complete Union type support for Go Fory by:
   
   1. **Added TypeId constants** to `go/fory/types.go`:
      - `UNION = 38` - for tagged union types
      - `NONE = 39` - for empty/unit values
   
   2. **Implemented Union struct and unionSerializer** in `go/fory/union.go`:
      - `Union` struct that holds an `interface{}` value
      - Serializes Union types by writing variant index + value
      - Supports both internal Go mode and xlang mode (with type info)
      - Properly dispatches to alternative type serializers
   
   3. **Added RegisterUnionType API** to Fory:
      - Allows users to register alternative types for a union
      - Example: `f.RegisterUnionType(reflect.TypeOf(int32(0)), 
reflect.TypeOf(""))`
   
   4. **Added comprehensive tests** in `go/fory/union_test.go`:
      - Basic types (int32, string, float64)
      - Multiple alternative types
      - Null/nil values
      - Pointer types
      - Bytes type
      - Reference tracking mode
      - Error cases (invalid alternatives, empty registration)
   
   The implementation follows the same binary protocol as C++/Python/Rust 
variant serialization:
   1. Write variant index (varuint32)
   2. In xlang mode, write type info for the active alternative
   3. Write the value using the alternative's serializer
   
   ## Related issues
   
   Fixes #3031
   Related to #3027 (tracking issue for xlang union type system)
   
   ## Does this PR introduce any user-facing change?
   
   - [x] Does this PR introduce any public API change?
     - **Yes**: Adds `Union` struct and `RegisterUnionType` method. Users can 
now use Union types in their code and they will be automatically 
serialized/deserialized.
     - This is backward compatible - existing code continues to work.
   
   - [x] Does this PR introduce any binary protocol compatibility change?
     - **No**: The implementation uses the existing TypeId.UNION (38) and 
TypeId.NONE (39) defined in the specification.
     - The binary protocol follows the same format as C++/Python/Rust 
implementations.
     - Existing serialized data is not affected.
   
   ## Benchmark
   
   This PR does not have a performance impact on existing functionality:
   - Union type handling only activates when Union types are actually used
   - The implementation uses efficient type matching via reflection
   - Variant index is encoded using varuint32 (compact encoding)
   - No changes to existing serializers or hot paths
   
   For Union types specifically, the overhead is minimal:
   - One varuint32 write/read for the variant index
   - One type dispatch (same as normal polymorphic serialization)
   - No additional allocations beyond the Union struct itself


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to