chaokunyang commented on code in PR #3062: URL: https://github.com/apache/fory/pull/3062#discussion_r2638545722
########## java/fory-core/src/main/java/org/apache/fory/serializer/UnionSerializer.java: ########## @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fory.serializer; + +import java.util.function.BiFunction; +import org.apache.fory.Fory; +import org.apache.fory.memory.MemoryBuffer; +import org.apache.fory.type.union.Union; +import org.apache.fory.type.union.Union2; +import org.apache.fory.type.union.Union3; +import org.apache.fory.type.union.Union4; +import org.apache.fory.type.union.Union5; +import org.apache.fory.type.union.Union6; + +/** + * Serializer for {@link Union} and its subclasses ({@link Union2}, {@link Union3}, {@link Union4}, + * {@link Union5}, {@link Union6}). + * + * <p>The serialization format is: + * + * <ul> + * <li>Union type tag (byte): 0=Union, 1=Union2, 2=Union3, 3=Union4, 4=Union5, 5=Union6 + * <li>Variant index (varuint32): identifies which alternative type is active + * <li>Value data: the serialized value of the active alternative + * </ul> + * + * <p>This allows cross-language interoperability with union types in other languages like C++'s + * std::variant, Rust's enum, or Python's typing.Union. + */ +public class UnionSerializer extends Serializer<Union> { + /** Array of factories for creating Union instances by type tag. */ + @SuppressWarnings("unchecked") + private static final BiFunction<Integer, Object, Union>[] FACTORIES = + new BiFunction[] { + (BiFunction<Integer, Object, Union>) Union::new, + (BiFunction<Integer, Object, Union>) (index, value) -> Union2.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union3.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union4.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union5.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union6.of(index, value) + }; + + private final int typeTag; + private final BiFunction<Integer, Object, Union> factory; + + @SuppressWarnings("unchecked") + public UnionSerializer(Fory fory, Class<? extends Union> cls) { + super(fory, (Class<Union>) cls); + this.typeTag = getTypeTag(cls); + this.factory = FACTORIES[typeTag]; + } + + private static int getTypeTag(Class<? extends Union> cls) { + if (cls == Union.class) { + return 0; + } else if (cls == Union2.class) { + return 1; + } else if (cls == Union3.class) { + return 2; + } else if (cls == Union4.class) { + return 3; + } else if (cls == Union5.class) { + return 4; + } else if (cls == Union6.class) { + return 5; + } else { + // Default to base Union for unknown subclasses + return 0; + } + } + + @Override + public void xwrite(MemoryBuffer buffer, Union union) { + // Write type tag to identify the Union subclass + buffer.writeByte(getTypeTag(union.getClass())); Review Comment: `Union` type should always write `UNION` as type info. For deserialization, we should use declared class into to determine which serialized should be used for deserialization. For example: ```java class Foo { Union2<String, Long> u1; } ``` When deserializing `u1`, we know we need to create `Union2` , so when creating serializer for `Union2`, we create serializer with `Union2` passed. and in `xread`, we will create `Union2` directly ########## java/fory-core/src/main/java/org/apache/fory/serializer/UnionSerializer.java: ########## @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fory.serializer; + +import java.util.function.BiFunction; +import org.apache.fory.Fory; +import org.apache.fory.memory.MemoryBuffer; +import org.apache.fory.type.union.Union; +import org.apache.fory.type.union.Union2; +import org.apache.fory.type.union.Union3; +import org.apache.fory.type.union.Union4; +import org.apache.fory.type.union.Union5; +import org.apache.fory.type.union.Union6; + +/** + * Serializer for {@link Union} and its subclasses ({@link Union2}, {@link Union3}, {@link Union4}, + * {@link Union5}, {@link Union6}). + * + * <p>The serialization format is: + * + * <ul> + * <li>Union type tag (byte): 0=Union, 1=Union2, 2=Union3, 3=Union4, 4=Union5, 5=Union6 + * <li>Variant index (varuint32): identifies which alternative type is active + * <li>Value data: the serialized value of the active alternative + * </ul> + * + * <p>This allows cross-language interoperability with union types in other languages like C++'s + * std::variant, Rust's enum, or Python's typing.Union. + */ +public class UnionSerializer extends Serializer<Union> { + /** Array of factories for creating Union instances by type tag. */ + @SuppressWarnings("unchecked") + private static final BiFunction<Integer, Object, Union>[] FACTORIES = + new BiFunction[] { + (BiFunction<Integer, Object, Union>) Union::new, + (BiFunction<Integer, Object, Union>) (index, value) -> Union2.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union3.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union4.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union5.of(index, value), + (BiFunction<Integer, Object, Union>) (index, value) -> Union6.of(index, value) + }; + + private final int typeTag; + private final BiFunction<Integer, Object, Union> factory; + + @SuppressWarnings("unchecked") + public UnionSerializer(Fory fory, Class<? extends Union> cls) { + super(fory, (Class<Union>) cls); + this.typeTag = getTypeTag(cls); + this.factory = FACTORIES[typeTag]; + } + + private static int getTypeTag(Class<? extends Union> cls) { + if (cls == Union.class) { + return 0; + } else if (cls == Union2.class) { + return 1; + } else if (cls == Union3.class) { + return 2; + } else if (cls == Union4.class) { + return 3; + } else if (cls == Union5.class) { + return 4; + } else if (cls == Union6.class) { + return 5; + } else { + // Default to base Union for unknown subclasses + return 0; + } + } + + @Override + public void xwrite(MemoryBuffer buffer, Union union) { + // Write type tag to identify the Union subclass + buffer.writeByte(getTypeTag(union.getClass())); Review Comment: `Union` type should always write `UNION` as type info. In java, that's handled outside. For `xread`, we never write any `type info`, we only write object value For deserialization, we should use declared class into to determine which serialized should be used for deserialization. For example: ```java class Foo { Union2<String, Long> u1; } ``` When deserializing `u1`, we know we need to create `Union2` , so when creating serializer for `Union2`, we create serializer with `Union2` passed. and in `xread`, we will create `Union2` directly -- 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]
