All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.freya022.botcommands.internal.parameters.ParameterResolverAdapters.kt Maven / Gradle / Ivy

Go to download

A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.

There is a newer version: 3.0.0-alpha.18
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy