Open
Description
I would like to support preserving unknown fields for ProtoBuf, here is an issue for discussion beforehand.
Motivation
Basically it is required in protobuf spec, so it needs to be implemented to make kotlinx.serialization
a valid protobuf parser.
Basic design
A new helper class will be introduced
interface UnknownFieldsHolder: Map<ProtoId, ProtoField>
Users can add to class property according to their needs:
@Serializable
data class ForwardCompatibleData(
@ProtoNumber(1) val name: String,
// No @ProtoNumber needed
// Maybe introducing a new annotation?
val unknownFields: UnknownFieldsHolder,
)
Behaviours
- The ProtoBuf format should keeps all undeclare fields in the
UnknownFieldsHolder
when deserializing; - All data in the
UnknownFieldsHolder
should be written back to wire as is during serializing; - If new fields are added in
oneof
field, the new value will be stored inUnknownFieldsHolder
, leaving the @ProtoOneOf property to be null; - Other formats should not deseralize or serialize this property.
Further more
Enumeration
Enums in protobuf can also have unknown values
in old version of code, but it is much more easier to store it in sealed interface / objects styles:
sealed interface IPhoneType {
val value: Int
}
data object WorkPhone: IPhoneType {
override val value: Int
get() = 1
}
data object HomePhone: IPhoneType {
override val value: Int
get() = 2
}
data class UnknownPhoneType(override val value: Int): IPhoneType
Other formats
Do other formats need similar functionality?