-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support reserved kotlin keywords (#83)
* unit test * implement the fix * implement the fix * rename unit test
- Loading branch information
1 parent
086877e
commit d173cef
Showing
10 changed files
with
189 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2024 Expedia, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Sanitizes a name of a field or type if it is a reserved keyword in Kotlin. | ||
*/ | ||
export function sanitizeName(name: string) { | ||
return RESERVED_KEYWORDS.includes(name) ? `\`${name}\`` : name; | ||
} | ||
|
||
/** | ||
* https://kotlinlang.org/docs/keyword-reference.html#hard-keywords | ||
*/ | ||
const RESERVED_KEYWORDS = [ | ||
"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", | ||
] as const; |
5 changes: 5 additions & 0 deletions
5
test/unit/should_handle_reserved_kotlin_keywords/codegen.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { GraphQLKotlinCodegenConfig } from "../../../src/plugin"; | ||
|
||
export default { | ||
namingConvention: "keep", | ||
} satisfies GraphQLKotlinCodegenConfig; |
58 changes: 58 additions & 0 deletions
58
test/unit/should_handle_reserved_kotlin_keywords/expected.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.kotlin.generated | ||
|
||
import com.expediagroup.graphql.generator.annotations.* | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
data class TypeWithReservedKotlinKeywords( | ||
val `as`: String? = null, | ||
val `break`: String? = null, | ||
val `is`: String? = null | ||
) | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
open class TypeWithReservedKotlinKeywordsAndFieldArgs( | ||
val `typeof`: String? = null, | ||
private val `throw`: String? = null | ||
) { | ||
open fun `throw`(`else`: String? = null, dataFetchingEnvironment: graphql.schema.DataFetchingEnvironment): String? = `throw` | ||
} | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
data class `true`( | ||
val field: String? = null | ||
) | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.INPUT_OBJECT]) | ||
data class InputWithReservedKotlinKeywords( | ||
val `continue`: String? = null, | ||
val `class`: String? = null, | ||
val `do`: String? = null | ||
) | ||
|
||
enum class EnumWithReservedKotlinKeywords { | ||
`fun`, | ||
`package`, | ||
`val`; | ||
|
||
companion object { | ||
fun findByName(name: String, ignoreCase: Boolean = false): EnumWithReservedKotlinKeywords? = values().find { it.name.equals(name, ignoreCase = ignoreCase) } | ||
} | ||
} | ||
|
||
interface InterfaceWithReservedKotlinKeywords { | ||
val `null`: String? | ||
val `return`: String? | ||
val `object`: String? | ||
} | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
data class TypeForUnion1( | ||
val field: String? = null | ||
) : `this` | ||
|
||
@GraphQLValidObjectLocations(locations = [GraphQLValidObjectLocations.Locations.OBJECT]) | ||
data class TypeForUnion2( | ||
val field: String? = null | ||
) : `this` | ||
|
||
interface `this` |
41 changes: 41 additions & 0 deletions
41
test/unit/should_handle_reserved_kotlin_keywords/schema.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
type TypeWithReservedKotlinKeywords { | ||
as: String | ||
break: String | ||
is: String | ||
} | ||
|
||
type TypeWithReservedKotlinKeywordsAndFieldArgs { | ||
typeof: String | ||
throw(else: String): String | ||
} | ||
|
||
type true { | ||
field: String | ||
} | ||
|
||
input InputWithReservedKotlinKeywords { | ||
continue: String | ||
class: String | ||
do: String | ||
} | ||
|
||
enum EnumWithReservedKotlinKeywords { | ||
fun | ||
package | ||
val | ||
} | ||
|
||
interface InterfaceWithReservedKotlinKeywords { | ||
null: String | ||
return: String | ||
object: String | ||
} | ||
|
||
type TypeForUnion1 { | ||
field: String | ||
} | ||
type TypeForUnion2 { | ||
field: String | ||
} | ||
|
||
union this = TypeForUnion1 | TypeForUnion2 |