rocketraman
02/03/2025, 3:40 PMSomeResult
type with Success
and Failure
subtypes. Obviously these subtypes have different fields.
I see that there is OneOf support [1] but the docs give a simplistic example with the same single field type, across both subclass types, which wouldn't work for most use cases including the SomeResult
type mentioned. When I try to serialize this, I get the error:
Implementation of oneOf type Foo should contain only 1 element, but get 4
(Foo
here is one of the two subtypes). I don't believe Protobuf limits OneOf fields in this way, but I'm not super-familiar with Protobufs in general, so I could be wrong. Can someone shed some light here?
[1] https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/formats.md#oneof-field-experimentalephemient
02/03/2025, 5:01 PMmessage Data {
required string name = 1;
oneof phone {
HomePhone home_phone = 2;
WorkPhone work_phone = 3;
}
}
message HomePhone { ... }
message WorkPhone { ... }
in protobuf, then I think you'll need
@Serializable
data class Data(
@ProtoNumber(1) val name: String,
@ProtoOneOf val phone: IPhoneType?,
)
@Serializable
sealed interface IPhoneType
@Serializable
class HomePhoneType(
@ProtoNumber(2) val homePhone: HomePhone,
): IPhoneType
@Serializable
class WorkPhoneType(
@ProtoNumber(3) val workPhone: WorkPhone,
): IPhoneType
@Serializable
@Serializable
class HomePhone(...)
@Serializable
class WorkPhone(...)
in kxsrocketraman
02/03/2025, 5:20 PMProtoNumber
for the type, but couldn't an annotation on the subclass type be used for that value instead?rocketraman
02/03/2025, 5:21 PMsyntax = "proto2";
// serial name 'Data'
message Data {
required string name = 1;
oneof phone {
HomePhone homePhone = 2;
WorkPhone workPhone = 3;
}
}
// serial name 'HomePhone'
message HomePhone {
required string foo = 1;
}
// serial name 'WorkPhone'
message WorkPhone {
required string bar = 1;
}
rocketraman
02/03/2025, 5:40 PM