Skip to content

Make enums as sealed classes generate sealed interfaces #5921

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

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions libraries/apollo-annotations/api/apollo-annotations.api
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ public abstract interface annotation class com/apollographql/apollo3/annotations
public abstract interface annotation class com/apollographql/apollo3/annotations/ApolloRequiresOptIn : java/lang/annotation/Annotation {
}

public abstract interface annotation class com/apollographql/apollo3/annotations/ApolloUnknownEnum : java/lang/annotation/Annotation {
}

public abstract interface annotation class com/apollographql/apollo3/annotations/GraphQLAdapter : java/lang/annotation/Annotation {
public abstract fun forScalar ()Ljava/lang/String;
}
Expand Down
3 changes: 0 additions & 3 deletions libraries/apollo-annotations/api/apollo-annotations.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ open annotation class com.apollographql.apollo3.annotations/ApolloInternal : kot
open annotation class com.apollographql.apollo3.annotations/ApolloRequiresOptIn : kotlin/Annotation { // com.apollographql.apollo3.annotations/ApolloRequiresOptIn|null[0]
constructor <init>() // com.apollographql.apollo3.annotations/ApolloRequiresOptIn.<init>|<init>(){}[0]
}
open annotation class com.apollographql.apollo3.annotations/ApolloUnknownEnum : kotlin/Annotation { // com.apollographql.apollo3.annotations/ApolloUnknownEnum|null[0]
constructor <init>() // com.apollographql.apollo3.annotations/ApolloUnknownEnum.<init>|<init>(){}[0]
}
open annotation class com.apollographql.apollo3.annotations/GraphQLAdapter : kotlin/Annotation { // com.apollographql.apollo3.annotations/GraphQLAdapter|null[0]
constructor <init>(kotlin/String) // com.apollographql.apollo3.annotations/GraphQLAdapter.<init>|<init>(kotlin.String){}[0]
final val forScalar // com.apollographql.apollo3.annotations/GraphQLAdapter.forScalar|{}forScalar[0]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ internal object KotlinSymbols {

val ApolloAdaptableWith = ClassName(ClassNames.apolloAnnotationsPackageName, "ApolloAdaptableWith")
val ApolloExperimental = ClassName(ClassNames.apolloAnnotationsPackageName, "ApolloExperimental")
val ApolloUnknownEnum = ClassName(ClassNames.apolloAnnotationsPackageName, "ApolloUnknownEnum")

val JsExport = ClassName("kotlin.js", "JsExport")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ internal class EnumAsEnumBuilder(
"Note: unknown values of [rawValue] will return [UNKNOWN__]. You may want to update your schema instead of calling this function directly.\n",
selfClassName
)
.addAnnotation(KotlinSymbols.ApolloUnknownEnum)
.addParameter("rawValue", String::
class)
.returns(selfClassName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ internal class EnumAsSealedBuilder(
return CgFile(
packageName = packageName,
fileName = simpleName,
typeSpecs = listOf(enum.toSealedClassTypeSpec())
typeSpecs = listOf(enum.toSealedClassTypeSpec(), enum.unknownClassTypeSpec())
)
}

private fun IrEnum.toSealedClassTypeSpec(): TypeSpec {
return TypeSpec.classBuilder(simpleName)
return TypeSpec.interfaceBuilder(simpleName)
.maybeAddDescription(description)
// XXX: can an enum be made deprecated (and not only its values) ?
.addModifiers(KModifier.SEALED)
.primaryConstructor(primaryConstructorWithOverriddenParamSpec)
.addProperty(rawValuePropertySpec)
.addType(companionTypeSpec())
.addTypes(values.map { value ->
Expand All @@ -82,19 +81,30 @@ internal class EnumAsSealedBuilder(
.maybeAddDeprecation(deprecationReason)
.maybeAddDescription(description)
.maybeAddRequiresOptIn(context.resolver, optInFeature)
.superclass(superClass)
.addSuperclassConstructorParameter("rawValue·=·%S", name)
.addSuperinterface(superClass)
.addProperty(
PropertySpec.builder("rawValue", KotlinSymbols.String)
.addModifiers(KModifier.OVERRIDE)
.initializer("%S", name)
.build()
)
.build()
}

private fun IrEnum.unknownValueTypeSpec(): TypeSpec {
return TypeSpec.classBuilder("UNKNOWN__")
.addKdoc("An enum value that wasn't known at compile time.\n" +
"Note: the `UNKNOWN__` class represents GraphQL enums that are not present in the schema and whose `rawValue` cannot be checked at build time. You may want to update your schema instead of instantiating it directly."
)
return TypeSpec.interfaceBuilder("UNKNOWN__")
.addKdoc("An enum value that wasn't known at compile time.")
.addSuperinterface(selfClassName)
.addProperty(unknownValueRawValuePropertySpec)
.build()
}

private fun IrEnum.unknownClassTypeSpec(): TypeSpec {
return TypeSpec.classBuilder("UNKNOWN__${simpleName}")
.addSuperinterface(unknownValueInterfaceName())
.primaryConstructor(unknownValuePrimaryConstructorSpec)
.superclass(selfClassName)
.addSuperclassConstructorParameter("rawValue·=·rawValue")
.addProperty(unknownValueRawValuePropertySpecWithInitializer)
.addModifiers(KModifier.PRIVATE)
.addFunction(
FunSpec.builder("equals")
.addModifiers(KModifier.OVERRIDE)
Expand Down Expand Up @@ -128,7 +138,6 @@ internal class EnumAsSealedBuilder(
"Note: unknown values of [rawValue] will return [UNKNOWN__]. You may want to update your schema instead of calling this function directly.\n",
selfClassName
)
.addAnnotation(KotlinSymbols.ApolloUnknownEnum)
.addSuppressions(enum.values.any { it.deprecationReason != null })
.maybeAddOptIn(context.resolver, enum.values)
.addParameter("rawValue", KotlinSymbols.String)
Expand All @@ -139,7 +148,7 @@ internal class EnumAsSealedBuilder(
.map { CodeBlock.of("%S·->·%T", it.name, it.valueClassName()) }
.joinToCode(separator = "\n", suffix = "\n")
)
.addCode("else -> @OptIn(%T::class) %T(rawValue)\n", KotlinSymbols.ApolloUnknownEnum, unknownValueClassName())
.addCode("else -> %T(rawValue)\n", unknownValueClassName())
.endControlFlow()
.build()
}
Expand Down Expand Up @@ -170,24 +179,31 @@ internal class EnumAsSealedBuilder(
return ClassName(selfClassName.packageName, selfClassName.simpleName, targetName.escapeKotlinReservedWordInSealedClass())
}

private fun unknownValueClassName(): ClassName {
private fun unknownValueInterfaceName(): ClassName {
return ClassName(selfClassName.packageName, selfClassName.simpleName, "UNKNOWN__")
}

private fun unknownValueClassName(): ClassName {
return ClassName(selfClassName.packageName, "UNKNOWN__${selfClassName.simpleName}")
}

private val unknownValuePrimaryConstructorSpec =
FunSpec.constructorBuilder()
.addAnnotation(KotlinSymbols.ApolloUnknownEnum)
.addParameter("rawValue", KotlinSymbols.String)
.build()

private val primaryConstructorWithOverriddenParamSpec =
FunSpec.constructorBuilder()
.addParameter("rawValue", KotlinSymbols.String)
private val unknownValueRawValuePropertySpec =
PropertySpec.builder("rawValue", KotlinSymbols.String)
.addModifiers(KModifier.OVERRIDE)
.build()

private val rawValuePropertySpec =
private val unknownValueRawValuePropertySpecWithInitializer =
PropertySpec.builder("rawValue", KotlinSymbols.String)
.addModifiers(KModifier.OVERRIDE)
.initializer("rawValue")
.build()

private val rawValuePropertySpec =
PropertySpec.builder("rawValue", KotlinSymbols.String)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.apollographql.apollo3.compiler.codegen.kotlin.CgFile
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFileBuilder
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSchemaContext
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
import com.apollographql.apollo3.compiler.codegen.kotlin.helpers.requiresOptInAnnotation
import com.apollographql.apollo3.compiler.codegen.responseAdapter
import com.apollographql.apollo3.compiler.codegen.typeAdapterPackageName
import com.apollographql.apollo3.compiler.ir.IrEnum
Expand Down Expand Up @@ -43,7 +42,6 @@ internal class EnumResponseAdapterBuilder(
private fun IrEnum.typeSpec(): TypeSpec {
val adaptedTypeName = context.resolver.resolveSchemaType(enum.name)
val fromResponseFunSpec = FunSpec.builder(Identifier.fromJson)
.addAnnotation(requiresOptInAnnotation(KotlinSymbols.ApolloUnknownEnum))
.addModifiers(KModifier.OVERRIDE)
.addParameter(Identifier.reader, KotlinSymbols.JsonReader)
.addParameter(Identifier.customScalarAdapters, KotlinSymbols.CustomScalarAdapters)
Expand Down

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.

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.

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.

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
Loading