-
Notifications
You must be signed in to change notification settings - Fork 15.6k
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
Kotlin generator #5341
Kotlin generator #5341
Conversation
Right this just an empty generator that calls Java generation via super class.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
I signed it! |
CLAs look good, thanks! |
@oleksiyp What do you think about plain data classes instead of builders? Or provide both, use common interface, implement it with immutable data classes, and mutable classes with builders? |
Redoing all from scratch is hard. A lot of stuff there + some apps would be nice to maintain Java interop. It is one of Kotlin principles as well. Data classes would be nice to have of course as one more option, possibly usable in multiplatform code. |
So simply - not instead, but complementary |
I just wanted to voice my support for what IRus said, generating data classes feels a lot more Kotlin-esque |
Personal opinion: it would be super useful if the generated code was multiplatform, such that it can be used in common modules between different platforms. |
I agree that it adds a lot of value being multiplatform. Need to think how complex it is |
Are data classes not available in in multiplatform Kotlin? or what is the issue? |
@RichoDemus data classes are perfectly fine, but that's just a tip of an iceberg. Does not solve all the problems. Protobuf is quite complex and how to make it available for multiplatform use need to go through it's features and analyze how to bring them to Kotlin multiplatform |
Thanks for the clarification, I leave it up to the maintainers to figure out if they agree and which course they want to take |
I think these convenient extension functions are fine, but the real problem is, of course, full support of Kotlin language i.e. having it multiplatform. I will try to do the second iteration by copying all the java code generation code and converting it to Kotlin generation code while keeping it multiplatform. Not sure I understand how much effort is there and if I really going finish this. |
There is already a project out there that provides builders for protobuf messages in Kotlin, check https://github.com/marcoferrer/kroto-plus. I use this generator for proto objects and gRPC calls a lot in my project already and I think would make sense that if support for Kotlin is gonna become official in protobuf then maybe efforts could be combined to create a more complete implementation. About the point of generating Kotlin data classes maybe a simple implementation would be to just generate data classes with protobuf annotations for https://github.com/Kotlin/kotlinx.serialization. That way Kotlin multiplatform could be supported. |
One thing to note is that without the As for the argument for creating truly kotlin first classes, there are some things to point out. There are certain optimizations in java base proto classes that would need to be mimicked in the kotlin variants. ie memoized hashcode. Using the multiplatform serialization lib would be a good idea but I remember there being a discussion about it lacking support for proto3 syntax. That might have changed by now, or atleast might be feasible with the addition of inline classes. Although I see the need for Kotlin specific code. I think that would need to be carefully planned and would most likely hold back this PR. |
Yes, I know about "@DslMarker". I've spent on this implementation around 4-6 hours of today. I implemented this because I do it manually and I do not want to generate all new protobuf definitions from scratch by kroto-plus or alike. I just want few additional extension functions for convenient usage. So I automated what I am doing manually. Actually, probably it was not best idea to do it as a PR to original protoc. But it was damn interesting to code again C++ 😄 Regarding optimizations, yes I've seen this generation code and it is quite advanced. |
protoc
that is able to generate convenient Kotlin builders on top of generated Java classes.I submit this as minimal implementation for early review. So that it can be discussed and correct direction is chosen from the start.
Example proto file:
Example code using builders:
Example generated code: