Skip to content

Commit

Permalink
Add support for Enum arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Sep 20, 2023
1 parent 046c082 commit b865c3f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.kord.codegen.generator.annotator

import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.squareup.kotlinpoet.*
import dev.kord.codegen.ksp.annotations.AnnotationArguments
import dev.kord.codegen.ksp.annotations.AnnotationArguments.Companion.arguments
import dev.kord.codegen.ksp.getAnnotationByType
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1

val KSClassDeclaration.receiversByTarget: List<ClassName>
get() {
val targetAnnotation = getAnnotationByType<Target>()
val targetsRaw = targetAnnotation.arguments<Target>()
val targets = targetsRaw[Target::allowedTargets]!!

return buildList {
if (AnnotationTarget.FILE in targets) {
add(FileSpec.Builder::class)
}
if (AnnotationTarget.FUNCTION in targets || AnnotationTarget.PROPERTY_GETTER in targets || AnnotationTarget.PROPERTY_SETTER in targets) {
add(FunSpec.Builder::class)
}
if (AnnotationTarget.VALUE_PARAMETER in targets) {
add(ParameterSpec.Builder::class)
}
if (AnnotationTarget.CLASS in targets) {
add(TypeSpec.Builder::class)
}
if (AnnotationTarget.TYPEALIAS in targets) {
add(TypeAliasSpec.Builder::class)
}
if (AnnotationTarget.PROPERTY in targets) {
add(PropertySpec.Builder::class)
}
}.map(KClass<*>::asClassName)
}
20 changes: 20 additions & 0 deletions ksp/src/main/kotlin/annotations/AnnotationArguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public class AnnotationArguments<A : Annotation> private constructor(
public operator fun get(parameter: KProperty1<A, KClass<*>>): KSType? =
parameter.value as KSType?

@JvmName("getEnum")
public inline operator fun <reified T : Enum<T>> get(parameter: KProperty1<A, T>): T? =
(parameter.value as KSType?)?.let {
enumValueOf<T>(it.declaration.simpleName.asString())
}

/**
* Returns the value of [parameter] as a [List] of [Numbers][Number] or `null` if it is the default value
* (see [NonNullAnnotationArguments] for more information).
Expand Down Expand Up @@ -105,6 +111,13 @@ public class AnnotationArguments<A : Annotation> private constructor(
public operator fun get(parameter: KProperty1<A, Array<out Boolean>>): List<Boolean>? =
@Suppress("UNCHECKED_CAST") (parameter.value as List<Boolean>?)


@JvmName("getEnumArray")
public inline operator fun <reified T : Enum<T>> get(parameter: KProperty1<A, Array<out T>>): List<T>? =
@Suppress("UNCHECKED_CAST") (parameter.value as List<KSType>?)?.map {
enumValueOf<T>(it.declaration.simpleName.asString())
}

/**
* Not null accessors for [AnnotationArguments].
*
Expand Down Expand Up @@ -157,6 +170,9 @@ public class AnnotationArguments<A : Annotation> private constructor(
public operator fun get(parameter: KProperty1<A, KClass<*>>): KSType =
delegate[parameter]!!

@JvmName("getEnum")
public inline operator fun <reified T : Enum<T>> get(parameter: KProperty1<A, T>): T = delegate[parameter]!!

/**
* Returns the value of [parameter] as a [List] [Numbers][Number].
*/
Expand Down Expand Up @@ -184,6 +200,10 @@ public class AnnotationArguments<A : Annotation> private constructor(
public operator fun get(parameter: KProperty1<A, Array<out Boolean>>): List<Boolean> =
delegate[parameter]!!

@JvmName("getEnumArray")
public inline operator fun <reified T : Enum<T>> get(parameter: KProperty1<A, Array<out T>>): List<T>? =
delegate[parameter]!!

public companion object {
/**
* Not null accessors for this [AnnotationArguments]. See documentation of [NonNullAnnotationArguments]
Expand Down

0 comments on commit b865c3f

Please sign in to comment.