-
Notifications
You must be signed in to change notification settings - Fork 623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Proto oneOf with new annotations and polymophic serializer #2538
Comments
Yes, that's one of the ways to do it. Although I'd say that |
Personally I would go with As an implementation note, it is a bit "clunky" to extract all polymorphic descendants from a |
It is true that oneOf cannot just work with current polymorphic system. This implementation is also to find a way to make them work together. Just change a bit according to @pdvrieze suggested. data class Person(
@ProtoNum(1) val name: String,
@ProtoOneOf(2, 3) val phone: IPhoneType,
)
sealed interface IPhoneType
@ProtoNum(2) data class MobilePhone(val value: String): IPhoneType
@ProtoNum(3) data class HomePhone(val value: String): IPhoneType The first thing is, when the serializer goes to That is why I think a As for the polymorphic handling, the |
And such new polymophic serializer is helpful if someone wants to hold proto enum data in kotlin sealed interface, rather than kotlin enum class. |
For implementation the format can recognise the polymorphic serializer and do its own thing. No need for another serializer that tells it how to. Alternatively you can have the format synthesise the data expected by the polymorphic serializer. The xml format does this for example. |
The way to implement it is as follows for the decoding
Encoding works in parallel, but the |
I have exactly the same use case. Coincidentally, I just started to take a look to see how hard it was to implement it after checking the protobuf schema generated for a polymorphic class. Good to know I'm not the only one. For context, this is the schema generated for my classes:
And this is what I was expecting:
|
Sounds interesting @xiaozhikang0916 |
🎉 great news |
I commented in the issue #67 but I think it is necessary to open a new issue for further discussion.
I am writing a code-gen plugin to build kotlin data class for our proto file. As for
oneOf
case, asealed interface
may be an ideal solution.Let's say I have a proto message like
My ideal data class will be like
So I need to tell the ProtoBuf Decoder that if it comes with ProtoNum 2 or 3, deserialize it as
IPhoneType
and assign to thephone
field.A custom serializer for the whole
Person
class can work, but it would be nice to have some additional annotation supports. Like:@ProtoOneOfFields
tells that this field may be assined by the following ProtoNums, and the@ProtoOneOfNum
on the concrete class tells which ProtoNum can be parsed to this type.Generally, this case could be a
flatten key
for common serializer ( not ProtoBuf only ).For example, encoding an instance of
Person
defined likeis especting content (show in json) like:
And then,
@ProtoOneOfFields
can be a subtype of@Flatten
, with more flexiable funcitons.The text was updated successfully, but these errors were encountered: