Skip to content

Commit

Permalink
Generate annotation cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann committed Aug 21, 2023
1 parent 54f8cc9 commit 2c772f7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions gateway/src/commonMain/kotlin/Intents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
""",
),
Entry(
"GuildMembers", shift = 1, additionalOptInMarkerAnnotations = [privilegedIntentAnnotation],
"GuildMembers", shift = 1, requiresOptInAnnotations = [privilegedIntentAnnotation],
kDoc = """
Enables the following events:
- [GuildMemberAdd]
Expand Down Expand Up @@ -87,7 +87,7 @@
""",
),
Entry(
"GuildPresences", shift = 8, additionalOptInMarkerAnnotations = [privilegedIntentAnnotation],
"GuildPresences", shift = 8, requiresOptInAnnotations = [privilegedIntentAnnotation],
kDoc = """
Enables the following events:
- [PresenceUpdate]
Expand Down Expand Up @@ -148,7 +148,7 @@
""",
),
Entry(
"MessageContent", shift = 15, additionalOptInMarkerAnnotations = [privilegedIntentAnnotation],
"MessageContent", shift = 15, requiresOptInAnnotations = [privilegedIntentAnnotation],
kDoc = "[MessageContent] is a unique [privileged intent][PrivilegedIntent] that isn't directly " +
"associated with any Gateway [event][Event]s. Instead, access to [MessageContent] permits your app " +
"to receive message content data across the APIs.\n\nFor example, the [content][dev.kord.common." +
Expand Down
6 changes: 3 additions & 3 deletions ksp-annotations/src/commonMain/kotlin/Generate.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.kord.ksp

import dev.kord.ksp.Generate.*
import dev.kord.ksp.Generate.EntityType.*
import dev.kord.ksp.Generate.Entry
import kotlin.annotation.AnnotationRetention.SOURCE
import kotlin.annotation.AnnotationTarget.FILE

Expand Down Expand Up @@ -46,7 +46,7 @@ annotation class Generate(
val kDoc: String = "",
/** [Deprecated] annotation to mark this entry as deprecated. */
val deprecated: Deprecated = Deprecated(""),
/** Additional annotations to add to this entry. */
val additionalOptInMarkerAnnotations: Array<String> = [],
/** [RequiresOptIn] annotations to add to this entry. */
val requiresOptInAnnotations: Array<String> = [],
)
}
12 changes: 6 additions & 6 deletions ksp-processors/src/main/kotlin/generation/GenerationEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSAnnotation
import dev.kord.ksp.AnnotationArguments.Companion.arguments
import dev.kord.ksp.Generate
import dev.kord.ksp.Generate.*
import dev.kord.ksp.Generate.EntityType
import dev.kord.ksp.Generate.EntityType.*
import dev.kord.ksp.Generate.Entry
import dev.kord.ksp.generation.GenerationEntity.BitFlags
import dev.kord.ksp.generation.GenerationEntity.KordEnum
import kotlin.reflect.KProperty1
Expand Down Expand Up @@ -45,7 +46,7 @@ internal sealed class GenerationEntity(
val kDoc: String?,
val value: Comparable<*>,
val deprecated: Deprecated?,
val additionalOptInMarkerAnnotations: List<String>,
val requiresOptInAnnotations: List<String>,
)
}

Expand All @@ -72,8 +73,7 @@ internal fun Generate.toGenerationEntityOrNull(logger: KSPLogger, annotation: KS
INT_FLAGS, BIT_SET_FLAGS -> true
}

val entries = entries zip args[Generate::entries]!!
val mappedEntries = entries.mapNotNull { (entry, annotation) ->
val mappedEntries = (entries zip args[Generate::entries]!!).mapNotNull { (entry, annotation) ->
entry.toGenerationEntityEntryOrNull(entityType, logger, annotation)
}

Expand Down Expand Up @@ -153,10 +153,10 @@ private fun Entry.toGenerationEntityEntryOrNull(
.run { Deprecated(message, replaceWith.run { ReplaceWith(expression, *imports) }, level) }
.takeUnless { args.isDefault(Entry::deprecated) },
// because of https://github.com/google/ksp/pull/1330#issuecomment-1616066129
if (args.isDefault(Entry::additionalOptInMarkerAnnotations)) {
if (args.isDefault(Entry::requiresOptInAnnotations)) {
emptyList()
} else {
additionalOptInMarkerAnnotations.toList()
requiresOptInAnnotations.toList()
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal fun TypeSpec.Builder.addEntityEntries() {
entry.kDoc?.let { addKdoc(it) }
@OptIn(DelicateKotlinPoetApi::class) // `AnnotationSpec.get` is ok for `Deprecated`
entry.deprecated?.let { addAnnotation(it) }
entry.additionalOptInMarkerAnnotations.forEach { annotation ->
entry.requiresOptInAnnotations.forEach { annotation ->
addAnnotation(ClassName.bestGuess(annotation))
}
addModifiers(PUBLIC)
Expand Down
2 changes: 1 addition & 1 deletion ksp-processors/src/main/kotlin/generation/shared/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal val GenerationEntity.Entry.nameWithSuppressedDeprecation
context(GenerationContext)
internal fun Annotatable.Builder<*>.addEntryOptIns() {
val optIns = entriesDistinctByValue
.flatMap { it.additionalOptInMarkerAnnotations }
.flatMap { it.requiresOptInAnnotations }
.distinct()
.map { name -> CodeBlock.of("%T::class", ClassName.bestGuess(name)) }
.joinToCode()
Expand Down

0 comments on commit 2c772f7

Please sign in to comment.