Skip to content

Commit

Permalink
FIx generation for Serializable types with uncommon names
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Mar 19, 2023
1 parent 739f91e commit a938f74
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"dev.kord.gateway.Identify","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Identify$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.IdentifyProperties","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.IdentifyProperties$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.GuildMembersChunkData","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.GuildMembersChunkData$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordPresence","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordPresence$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Resume","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Resume$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.RequestGuildMembers","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.RequestGuildMembers$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.UpdateVoiceStatus","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.UpdateVoiceStatus$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.UpdateStatus","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.UpdateStatus$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Hello","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Hello$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.ReadyData","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.ReadyData$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Heartbeat","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Heartbeat$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Resumed","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Resumed$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.InvalidSession","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.InvalidSession$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordAutoModerationActionExecution","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordAutoModerationActionExecution$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordDeletedInvite","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordDeletedInvite$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordCreatedInvite","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordCreatedInvite$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordRemovedEmoji","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordRemovedEmoji$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordRemovedReactionEmoji","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordRemovedReactionEmoji$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.GuildScheduledEventUserMetadata","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.GuildScheduledEventUserMetadata$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordThreadListSync","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordThreadListSync$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordThreadMembersUpdate","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordThreadMembersUpdate$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Intents","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Intents$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.OpCode","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.OpCode$Companion","methods":[{"name":"serializer","parameterTypes":[]}]}]
[{"name":"dev.kord.gateway.Identify","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Identify$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.IdentifyProperties","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.IdentifyProperties$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.GuildMembersChunkData","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.GuildMembersChunkData$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordPresence","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordPresence$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Resume","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Resume$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.RequestGuildMembers","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.RequestGuildMembers$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.UpdateVoiceStatus","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.UpdateVoiceStatus$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.UpdateStatus","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.UpdateStatus$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Hello","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Hello$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.ReadyData","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.ReadyData$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Heartbeat","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Heartbeat$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Resumed","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Resumed$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.InvalidSession","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.InvalidSession$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordAutoModerationActionExecution","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordAutoModerationActionExecution$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordDeletedInvite","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordDeletedInvite$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordCreatedInvite","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordCreatedInvite$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordRemovedEmoji","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordRemovedEmoji$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordRemovedReactionEmoji","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordRemovedReactionEmoji$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.GuildScheduledEventUserMetadata","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.GuildScheduledEventUserMetadata$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordThreadListSync","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordThreadListSync$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.DiscordThreadMembersUpdate","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.DiscordThreadMembersUpdate$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.Intents","fields":[{"name":"Companion"}]},{"name":"dev.kord.gateway.Intents$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.gateway.OpCode","fields":[{"name":"OpCodeSerializer"}]},{"name":"dev.kord.gateway.OpCode$OpCodeSerializer","methods":[{"name":"serializer","parameterTypes":[]}]}]
4 changes: 3 additions & 1 deletion graal-example/src/main/kotlin/dev/kord/core/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import dev.kord.gateway.Intent
import dev.kord.gateway.PrivilegedIntent

suspend fun main(args: Array<String>) {
val kord = Kord(args.firstOrNull() ?: error("token required"))
val kord = Kord(args.firstOrNull() ?: error("token required")) {
enableShutdownHook = true
}

kord.on<MessageCreateEvent> {
if (message.author?.isBot == true) return@on
Expand Down
11 changes: 11 additions & 0 deletions ksp-processors/src/main/kotlin/KSPUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.kord.ksp

import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.KSAnnotation
import com.google.devtools.ksp.symbol.KSClassDeclaration
import kotlin.reflect.KProperty1

internal inline fun <reified A : Annotation> Resolver.getSymbolsWithAnnotation(inDepth: Boolean = false) =
Expand All @@ -18,3 +19,13 @@ internal class AnnotationArguments private constructor(private val map: Map<Stri
get() = AnnotationArguments(arguments.associate { it.name!!.getShortName() to it.value!! })
}
}

internal val KSClassDeclaration.binaryName: String
get() {
val parent = parentDeclaration
return if (parent is KSClassDeclaration) {
"${parent.binaryName}\$${simpleName.asString()}"
} else {
qualifiedName!!.asString()
}
}
33 changes: 26 additions & 7 deletions ksp-processors/src/main/kotlin/graal/KordGraalProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package dev.kord.ksp.graal
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import com.google.devtools.ksp.symbol.KSClassDeclaration
import dev.kord.ksp.binaryName
import dev.kord.ksp.getSymbolsWithAnnotation
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable

class KordGraalProcessorProvider : SymbolProcessorProvider {
Expand Down Expand Up @@ -51,19 +53,36 @@ private class KordGraalProcessor(private val codeGenerator: CodeGenerator, priva
}

private fun processClass(clazz: KSClassDeclaration): List<ReflectConfigEntry> {
val qualifiedName = clazz.qualifiedName?.asString() ?: run {
logger.warn("Invalid Serializable element received", clazz)
return emptyList()
}
val name = clazz.findCompanionObjectName()

val qualifiedName = clazz.binaryName
val companionField = ReflectConfigEntry(
name = qualifiedName,
fields = listOf(ReflectConfigEntry.Field("Companion"))
fields = listOf(ReflectConfigEntry.Field(name))
)
val companionObject = ReflectConfigEntry(
name = "$qualifiedName\$Companion",
methods = listOf(ReflectConfigEntry.Method("serializer"))
name = "$qualifiedName\$$name",
methods = listOf(
ReflectConfigEntry.Method(
"serializer",
generateTypeParameters(clazz.typeParameters.count()).also {
logger.info("Class ${clazz.qualifiedName?.asString()} has ${it.size} type parameters")
}
)
)
)

return listOf(companionField, companionObject)
}

private fun KSClassDeclaration.findCompanionObjectName(): String {
val companionObject = declarations.firstOrNull {
(it as? KSClassDeclaration)?.isCompanionObject == true
}

return companionObject?.qualifiedName?.getShortName() ?: "Companion"
}

private fun generateTypeParameters(n: Int): List<String> = List(n) { KSerializer::class.qualifiedName!! }

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"dev.kord.voice.SpeakingFlags","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.SpeakingFlags$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Identify","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Identify$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Heartbeat","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Heartbeat$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SendSpeaking","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SendSpeaking$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SelectProtocol","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SelectProtocol$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SelectProtocol.Data","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SelectProtocol.Data$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Resume","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Resume$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Ready","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Ready$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Hello","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Hello$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.HeartbeatAck","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.HeartbeatAck$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SessionDescription","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SessionDescription$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Speaking","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Speaking$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Resumed","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Resumed$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.EncryptionMode","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.EncryptionMode$Companion","methods":[{"name":"serializer","parameterTypes":[]}]}]
[{"name":"dev.kord.voice.SpeakingFlags","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.SpeakingFlags$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Identify","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Identify$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Heartbeat","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Heartbeat$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SendSpeaking","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SendSpeaking$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SelectProtocol","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SelectProtocol$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SelectProtocol$Data","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SelectProtocol$Data$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Resume","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Resume$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Ready","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Ready$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Hello","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Hello$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.HeartbeatAck","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.HeartbeatAck$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.SessionDescription","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.SessionDescription$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Speaking","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Speaking$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.gateway.Resumed","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.gateway.Resumed$Companion","methods":[{"name":"serializer","parameterTypes":[]}]},{"name":"dev.kord.voice.EncryptionMode","fields":[{"name":"Companion"}]},{"name":"dev.kord.voice.EncryptionMode$Companion","methods":[{"name":"serializer","parameterTypes":[]}]}]

0 comments on commit a938f74

Please sign in to comment.