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

Do not use 'header' as a enum value name as it breaks the Kotlin compiler #4086

Merged
merged 4 commits into from May 9, 2022

Conversation

martinbonnin
Copy link
Contributor

@martinbonnin martinbonnin commented May 7, 2022

@netlify
Copy link

netlify bot commented May 7, 2022

Deploy Preview for apollo-android-docs canceled.

Name Link
🔨 Latest commit a6ce589
🔍 Latest deploy log https://app.netlify.com/sites/apollo-android-docs/deploys/627950467264a20008a6711c

@@ -13,7 +13,8 @@ private val JAVA_RESERVED_WORDS = arrayOf(

// Reference:
// https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position
private val KOTLIN_RESERVED_ENUM_VALUE_NAMES = arrayOf("name", "ordinal")
// "header" and "impl" are added to this list because of https://youtrack.jetbrains.com/issue/KT-52315
Copy link
Contributor

Choose a reason for hiding this comment

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

😱

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR to handle this at the KotlinPoet level: square/kotlinpoet#1248

@martinbonnin
Copy link
Contributor Author

Updated this PR with a better fix that escapes with backticks. If this gets fixed in the Kotlin compiler, no change will be needed on the caller side

fun String.escapeKotlinReservedEnumValueNames() : String {
return when {
// https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants:~:text=properties%20for%20obtaining%20its%20name%20and%20position
this in arrayOf("name", "ordinal") -> "${this}_"
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you remember why we don't use backticks for these two as well? (we wouldn't want to change this now as it would be breaking, but I'm curious if we simply didn't think of it or a good reason).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's different. One is an actual language limitation (name, ordinal). I think because there's a nameclash somewhere. The other is most likely a compiler bug (header, impl).

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if you want to

"(?:name|ordinal)_*".toRegex().matches(this)

to prevent collisions if a schema defines both name and name_.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Excellent point, much better 👍

@martinbonnin martinbonnin merged commit 9dab1f3 into main May 9, 2022
@martinbonnin martinbonnin deleted the workaround-kt-52315 branch May 9, 2022 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants