glureau
07/28/2023, 3:18 PMglureau
07/28/2023, 3:39 PMkevin.cianfarini
07/28/2023, 4:16 PMmessage SampleMessage {
oneof test_oneof {
string name = 4;
SubMessage sub_message = 9;
}
}
Could not be directly deserialized to String | Submessage
, you’d have to create a wrapper.
sealed interface TestOneOf {
class Name(val name: String) : TestOneOf
class SubMessage(val subMessage: ActualSubMessage) : TestOneOf
}
I suspect since this isn’t a perfect mapping the serialization team doesn’t want to support itGrégory Lureau
07/30/2023, 6:31 PMsandwwraith
07/31/2023, 1:17 PMthis Protobuf feature does not naturally map into Kotlin typesystem
means that oneOf in protobuf is often used with strings and other primitives, which are not sealed classes in Kotlin. Also, polymorphism on sealed classes usually requires writing a type token into object (as it is done in Json) or into an array, while oneOf can be just a string. Moreover, oneOf is embedded directly into the message, while sealed class introduces sub-message of its own.
I agree that inline wrappers in sealed interface can provide more or less accurate representation, but it requires this "a lot of hand-work and checking":
• handling AbstractPolymorphicSerializer
with a custom logic instead of default one
• embedding properly the result of such polymorphic serialization
• carefully checking that all indices in oneOf hierarchy make sense inside the outer message
And that's only a serialization. For deserialization, there also should be a reverse mapping from index to a sealed interface inheritor.