Skip to content
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

Update enum naming and validation #245

Merged

Conversation

fluxxion82
Copy link
Contributor

Enum namings could end up being named as an invalid kotlin identifier, so updated the enum name generation to avoid invalid names.

Fixes #226

name = name.uppercase()
var name = splitWordsToSnakeCase(preferred).let { snakeCase ->
snakeCase.substringAfter(typePrefix).takeUnless {
"^\\d|\\W".toRegex().matches(it)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't sure about adding the \W for non-word characters. i wasn't able to test it since if I tried to add a weird character in the protobuf the project wouldn't compile, but figured I'd leave it for good measure just in case.

@fluxxion82
Copy link
Contributor Author

@garyp hey gary, how does this look?

@fluxxion82
Copy link
Contributor Author

@garyp is this project still alive?

Copy link
Collaborator

@garyp garyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @fluxxion82. Thank you so much for working on this bug fix. Sorry for letting this PR go unreviewed for so long! As you probably noticed, I hadn't had much time to devote to pbandk in the last 1.5 years. I'm now getting the project back into a maintained state.

Please let me know if you're still interested in working on this PR and plan to followup on my comments below. I completely understand if you have moved on to other things since you first submitted this PR. So if I don't hear back from you, I'll try to finish the PR myself and get it merged.

private val kotlinKeywords = setOf(
"as", "break", "class", "continue", "do", "else", "false", "for", "fun", "if", "in",
"interface", "is", "null", "object", "package", "return", "super", "this", "throw",
"true", "try", "typealias", "typeof", "val", "var", "when", "while"
)
private val disallowedValueTypeNames = disallowedTypeNames + kotlinKeywords + setOf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Why do we need to exclude kotlin keywords from the allowed value type names?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I wasn't sure if this is needed, I removed it from this PR. If you have an example that requires this, let's add it as a unit test and reintroduce this change.

@garyp garyp added bug Something isn't working component-codegen labels Apr 2, 2024
@fluxxion82
Copy link
Contributor Author

@garyp you're alive! welcome back :) I did switch things up but I'd still be interested in seeing this pr through. I'll need to find some time to get back in this and remember what I was doing but I'll try to push an update in the not too distant future.

@garyp garyp added this to the 0.16.0 milestone Aug 29, 2024
@garyp garyp force-pushed the salbury/update_disallowed_enum_names branch from d79497b to 96c447e Compare August 29, 2024 11:31
Enum namings could end up being named as an invalid kotlin identifier, so updated the enum name generation to avoid invalid names.
@garyp garyp force-pushed the salbury/update_disallowed_enum_names branch from 96c447e to 77ea8c8 Compare August 29, 2024 12:33
@garyp
Copy link
Collaborator

garyp commented Aug 29, 2024

@fluxxion82 I just ran into this bug myself because the newest version of descriptor.proto includes an Edition.EDITION_2023 enum value that was being turned into an invalid Kotlin identifier. Since this was blocking me now, I went ahead and finished this PR myself. Thanks for the initial version! It saved me a bunch of time. I'll leave some comments about the changes I made to your PR. Please reply if you have any questions and I can address them in a follow-up PR.

@garyp
Copy link
Collaborator

garyp commented Aug 29, 2024

Your original PR had this change (I can't comment on it anymore since I removed it):

-                    disallowedValueTypeNames.contains(name) ||
+                    disallowedValueTypeNames.any { it.equals(name, true) } ||

I found that this was being too aggressive in renaming identifiers in the existing generated code even though they weren't causing any conflicts (e.g. an enum value named STRING was being turned into STRING_ because of the case-insensitive matching you added). So I changed the code back to its original behavior of doing case-sensitive matching.

@garyp garyp merged commit fce4d46 into streem:master Aug 29, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working component-codegen
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid generated names from enum
2 participants