streem / pbandk

Kotlin Code Generator and Runtime for Protocol Buffers
MIT License
271 stars 37 forks source link

Fix compiler error when a protobuf type is named `Sequence` #241

Closed garyp closed 2 years ago

garyp commented 2 years ago

Fixes #237

garyp commented 2 years ago

How come only Sequence needed special treatment though?

We already have special support for other common Kotlin types here:

https://github.com/streem/pbandk/blob/f1c87adc69cb4a79171bb4f85de2282c4d43243a/protoc-gen-pbandk/lib/src/commonMain/kotlin/pbandk/gen/Namer.kt#L21

Basically, if you have a protobuf type named something like Long, pbandk will name the generated Kotlin class Long_ instead. I considered just adding Sequence to that list, so that it also gets transformed into Sequence_. But I think trying to add every single class from the Kotlin standard library to the disallowed names list is a losing battle. Plus as Kotlin adds new classes to the stdlib, it has the potential to break pbandk-generated classes that were previously working.

So instead I think it's better to ensure that the generated code is always using fully-qualified class names to avoid generated class names from clashing with the Kotlin stdlib. I didn't want to break existing code with this PR, but in a future change I'd like to remove the existing entries from disallowedTypeNames and instead use fully-qualified class names like I'm doing for Sequence in this PR. That's a low priority for now, but I didn't want to dig a deeper hole by adding Sequence to disallowedTypeNames.