io.github.freya022.botcommands.internal.parameters.ParameterResolverAdapters.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.internal.parameters
import io.github.freya022.botcommands.api.core.service.annotations.Resolver
import io.github.freya022.botcommands.api.core.utils.simpleNestedName
import io.github.freya022.botcommands.api.parameters.ClassParameterResolver
import io.github.freya022.botcommands.api.parameters.ParameterResolverFactory
import io.github.freya022.botcommands.api.parameters.ResolverRequest
import io.github.freya022.botcommands.api.parameters.TypedParameterResolver
private class ClassParameterResolverFactoryAdapter>(
private val resolver: T,
override val priority: Int,
): ParameterResolverFactory(resolver::class) {
override val supportedTypesStr: List = listOf(resolver.jvmErasure.simpleNestedName)
override fun isResolvable(request: ResolverRequest): Boolean = resolver.jvmErasure == request.parameter.erasure
override fun get(request: ResolverRequest): T = resolver
override fun toString(): String = "ClassParameterResolverFactoryAdapter(resolver=$resolver)"
}
internal fun > T.toResolverFactory(annotation: Resolver): ParameterResolverFactory {
return ClassParameterResolverFactoryAdapter(this, annotation.priority)
}
private class TypedParameterResolverFactoryAdapter>(
private val resolver: T,
override val priority: Int,
): ParameterResolverFactory(resolver::class) {
override val supportedTypesStr: List = listOf(resolver.type.simpleNestedName)
override fun isResolvable(request: ResolverRequest): Boolean = resolver.type == request.parameter.type
override fun get(request: ResolverRequest): T = resolver
override fun toString(): String = "TypedParameterResolverFactoryAdapter(resolver=$resolver)"
}
internal fun > T.toResolverFactory(annotation: Resolver): ParameterResolverFactory {
return TypedParameterResolverFactoryAdapter(this, annotation.priority)
}