Hi, I'm an author of a code generation library <kr...
# random
m
Hi, I'm an author of a code generation library krotoDC, Currently, I have an issue when the generated code has matching fields with Kotlin keywords - these fields have to be escaped to keep the generated code from breaking. My question is that is there a native method that checks whether a string literal is a reserved keyword or not? If not, having this would greatly help the development of code generation libraries akin to mine. ex)
Copy code
fun String.isReservedKeyword()
If there's a channel that better fits this question, please let me know 🙇
Otherwise I would have to add my own implementation and maintain a list of Kotlin keywords to match this, which is sad panda
k
Sorry, I don't know the answer, but I'm intrigued by your project. What benefit does it give besides the official Kotlin support that Google has already provided? (https://developers.googleblog.com/2021/11/announcing-kotlin-support-for-protocol.html)
m
@Klitos Kyriacou Hi, I used to write(and still do for some) applications using the official Kotlin support, and there were a few things that were inconvenient • null compatibility -> you can't directly assign Kotlin nullable values to kotlin fields. you have to check for null and "not set" it every time. • visibility of protobuf schema -> even if you use the DSLs, the resulting class is still the Java protobuf class, which is very hard to check what fields exist in that proto message And it's also nice that you get to use dataclasses just like your other Dtos
👍 1
e
you could just always
quote
identifiers
m
@Adam S That seems like what I was looking for. Thanks a lot 🍪
e
kotlinpoet maintains its own list of keywords
m
you could just always
quote
identifiers
That was my first reaction to this issue, but I would also have to suppress the warnings coming from unneeded escapes so I was thinking about it
e
Gradle has a dependency on the Kotlin compiler anyways but if you don't yet and just want to know if something is a keyword, I think it's excessive to include it just for that
1
m
I guess both ways(compiler & Kotlinpoet maintained keywords) are good options with each having a drawback of having to adding a dependency or having the possibility of the maintained list loosing its coupling with recent Kotlin syntax
e
you're going to have to do some work to keep to with language/compiler changes either way
1
🙇 1
m
I managed to figure it out using Kotlinpoet's auto-quoting feature for name templates. Thanks for the support 😆 (pull request link just in case anyone's interested)