Skip to content

Commit

Permalink
escape 'companion' in Kotlin (#4630)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin authored Jan 12, 2023
1 parent d173f5e commit b92d141
Show file tree
Hide file tree
Showing 27 changed files with 771 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal class FragmentBuilder(
superClassName = if (it.id == fragment.dataModelGroup.baseModelId) KotlinSymbols.FragmentData else null,
path = listOf(packageName, simpleName),
hasSubclassesInSamePackage = true,
adaptableWith = null
adaptableWith = null,
reservedNames = setOf("Companion")
)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal class OperationBuilder(
path = listOf(packageName, simpleName),
hasSubclassesInSamePackage = true,
adaptableWith = if (it.id == operation.dataModelGroup.baseModelId) it.id else null,
reservedNames = setOf("Companion")
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ internal class ModelBuilder(
private val superClassName: ClassName?,
private val path: List<String>,
private val hasSubclassesInSamePackage: Boolean,
private val adaptableWith: String?
private val adaptableWith: String?,
private val reservedNames: Set<String> = emptySet()
) {
private val nestedBuilders = model.modelGroups.flatMap {
it.models.map {
Expand All @@ -43,11 +44,12 @@ internal class ModelBuilder(
)
}
}
private val simpleName = if (reservedNames.contains(model.modelName)) "${model.modelName}_" else model.modelName

fun prepare() {
context.resolver.registerModel(
model.id,
ClassName.from(path + model.modelName)
ClassName.from(path + simpleName)
)
nestedBuilders.forEach { it.prepare() }
}
Expand All @@ -74,14 +76,14 @@ internal class ModelBuilder(
} + listOfNotNull(superClassName)

val typeSpecBuilder = if (isInterface) {
TypeSpec.interfaceBuilder(modelName)
TypeSpec.interfaceBuilder(simpleName)
// All interfaces can be sealed except if implementations exist in different packages (not allowed in Kotlin)
.applyIf(context.isTargetLanguageVersionAtLeast(KOTLIN_1_5) && hasSubclassesInSamePackage) {
addModifiers(KModifier.SEALED)
}
.addProperties(properties)
} else {
TypeSpec.classBuilder(modelName)
TypeSpec.classBuilder(simpleName)
.makeDataClassFromProperties(properties)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query TestQuery {
companion {
foo
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b92d141

Please sign in to comment.