-
Notifications
You must be signed in to change notification settings - Fork 612
Add accessors for oneof's generated data classes #3594
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -338,6 +338,16 @@ class KotlinGenerator private constructor( | |
| } | ||
| } | ||
|
|
||
| fun generateSealedOneOfAccessors(type: Type): List<PropertySpec> { | ||
| if (oneofMode != OneofMode.SEALED_CLASS) return listOf() | ||
|
|
||
| return when (type) { | ||
| is MessageType -> type.sealedOneOfAccessors() | ||
| is EnclosingType -> type.nestedTypes.flatMap(::generateSealedOneOfAccessors) | ||
| else -> listOf() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generates all [TypeSpec]s for the given [Service]. | ||
| * | ||
|
|
@@ -3063,6 +3073,53 @@ class KotlinGenerator private constructor( | |
| else -> boxedOneOfClassName(oneOf.name) | ||
| } | ||
|
|
||
| /** | ||
| * Generates top-level extension properties that read values from sealed oneof instances. | ||
| * | ||
| * Example: | ||
| * ``` | ||
| * public val PaymentMethodChoice.Method.card_id: String? | ||
| * get() = (this as? PaymentMethodChoice.Method.CardId)?.value | ||
| * | ||
| * public val PaymentMethodChoice.Method.bank_account: BankAccount? | ||
| * get() = (this as? PaymentMethodChoice.Method.BankAccount)?.value | ||
| * ``` | ||
| */ | ||
| private fun MessageType.sealedOneOfAccessors(): List<PropertySpec> { | ||
| val accessors = mutableListOf<PropertySpec>() | ||
| val className = typeName as ClassName | ||
| val nameAllocator = nameAllocator(this) | ||
|
|
||
| for (oneOf in sealedOneOfs()) { | ||
| val sealedClassName = className.nestedClass(nameAllocator[boxedOneOfClassName(oneOf.name)]) | ||
| val subclassNameAllocator = sealedSubclassNameAllocator(oneOf) | ||
| val accessorNameAllocator = NameAllocator(preallocateKeywords = !escapeKotlinKeywords) | ||
|
|
||
| for (field in oneOf.fields) { | ||
| val fieldName = if (field.name == field.type!!.simpleName || hasEponymousType(schema, field)) { | ||
| accessorNameAllocator.newName(legacyQualifiedFieldName(field), field) | ||
| } else { | ||
| accessorNameAllocator.newName(field.name, field) | ||
| } | ||
| val subclassName = sealedClassName.nestedClass(subclassNameAllocator[field]) | ||
| accessors += PropertySpec.builder(fieldName, field.type!!.typeName.copy(nullable = true)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the |
||
| .receiver(sealedClassName) | ||
| .getter( | ||
| FunSpec.getterBuilder() | ||
| .addStatement("return (this as? %T)?.value", subclassName) | ||
| .build(), | ||
| ) | ||
| .build() | ||
| } | ||
| } | ||
|
|
||
| for (nestedType in nestedTypes) { | ||
| accessors += generateSealedOneOfAccessors(nestedType) | ||
| } | ||
|
|
||
| return accessors | ||
| } | ||
|
|
||
| /** | ||
| * Generates a sealed class for a oneof. | ||
| * | ||
|
|
@@ -3314,18 +3371,18 @@ class KotlinGenerator private constructor( | |
|
|
||
| private fun MessageType.flatOneOfs(): List<OneOf> = when (oneofMode) { | ||
| OneofMode.FLAT -> oneOfs.filter { it.fields.size < boxOneOfsMinSize } | ||
| else -> emptyList() | ||
| else -> listOf() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, I'm just curious to understand why switching from |
||
| } | ||
|
|
||
| private fun MessageType.boxOneOfs(): List<OneOf> = when (oneofMode) { | ||
| OneofMode.FLAT -> oneOfs.filter { it.fields.size >= boxOneOfsMinSize } | ||
| OneofMode.BOXED -> oneOfs | ||
| OneofMode.SEALED_CLASS -> emptyList() | ||
| OneofMode.SEALED_CLASS -> listOf() | ||
| } | ||
|
|
||
| private fun MessageType.sealedOneOfs(): List<OneOf> = when (oneofMode) { | ||
| OneofMode.SEALED_CLASS -> oneOfs | ||
| else -> emptyList() | ||
| else -> listOf() | ||
| } | ||
|
|
||
| private fun sealedSubclassNameAllocator(oneOf: OneOf): NameAllocator = sealedSubclassNameAllocatorStore.getOrPut(oneOf) { | ||
|
|
@@ -3545,7 +3602,7 @@ class KotlinGenerator private constructor( | |
| AnnotationTarget.FIELD, | ||
| ) | ||
| METHOD_OPTIONS -> listOf(AnnotationTarget.FUNCTION) | ||
| else -> emptyList() | ||
| else -> listOf() | ||
| } | ||
|
|
||
| internal fun String.sanitizeKdoc(): String = this | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, would prefer to see this last case listed out for exhaustivity and future-proofing.