/k2pb

KSP generator to convert Kotlin @Serializable to ProtoBuf files

Primary LanguageKotlin

K2PB (Kotlin to ProtoBuf)

(WIP)

Using KSP to generate protobuf from @Serializable kotlin classes.

Comparison with KotlinX Serialization ProtoBufSchemaGenerator

This tool is another approach to KotlinX solution, some notable differences:

  • Serialization works at run time, K2PB works at compile time.
  • At run time, Serialization COULD know the serializerModule and @Contextual custom serializer, K2PB don't know them but allow you to pass arguments to replace a class by another class
  • Ktx Serialization has some limitations right now:
    • custom serializer / @Contextual are not supported (the generator should ask for a ProtoBuf instance to know those)
    • value class are not supported (creating useless message, not matching the encoded format)
    • nested classes are not supported
  • At compile time, K2PB has access to documentation and more code information to be able to copy those in generated protobuf files, generating better/richer files

(Eventually I hope to be able to handle upgrades from existing protobuf files, that's another reason to get this out of KotlinXSerialization/run-time limitations.)

Status

This is a WORK-IN-PROGRESS, consider it an experimental tool, and don't hesitate to create issues.

Current known limitations / things I'd like to achieve:

  • proto2 is not supported
  • Support reserved fields (dedicated annotation? Real usage?)
  • Support upgrading proto files (checking that the newly generated files are compatible with the current ones)

Testing

Tests are located in the 2 samples modules. It's asserting that protobuf files are stable.

It's also using protoc on the generated protobuf files, allowing us to test that a message encoded via KotlinX Serialization will be decoded successfully by the code generated by the protobuf files.

Proto2/3

Proto2 is not supported at the moment (please open an issue/PR if you need it). KotlinX Serialization support the Proto3 format IF all List<> are annotated with @ProtoPacked.