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

com.casadetasha.kexp.sproute.processor.values.KotlinNames.kt Maven / Gradle / Ivy

Go to download

KAPT processor to manage routing boilerplate for KTOR projects. Use in conjunction with the sproutes library.

There is a newer version: 2.1.2-beta-1
Show newest version
package com.casadetasha.kexp.sproute.processor.values

import com.casadetasha.kexp.sproute.processor.ktx.asCanonicalName
import com.casadetasha.kexp.sproute.processor.ktx.toMemberName
import com.casadetasha.kexp.sproute.processor.values.KotlinNames.MethodNames.applicationCallGetter
import com.casadetasha.kexp.sproute.processor.values.KotlinNames.MethodNames.applicationGetter
import com.squareup.kotlinpoet.MemberName
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.pipeline.*
import kotlinx.metadata.KmValueParameter
import kotlin.reflect.KClass

internal object KotlinNames {

    val VALID_EXTENSION_CLASSES =
        listOf(
            ApplicationCall::class.toMemberName(),
            Route::class.toMemberName()
        )

    private object KtorMethodNames {
        // I'm not sure how to get the name of an overloaded generic method via reflection
        const val RESPOND = "respond"
        const val ROUTE: String = "route"
        val ROUTING: String = Application::routing.name
        val AUTH: String = Route::authenticate.name
        val CALL: String = PipelineContext<*, ApplicationCall>::call.name
        val APPLICATION = PipelineContext<*, ApplicationCall>::application.name
        val APPLY: String = KClass<*>::apply.name
    }

    object GeneratedMethodNames {
        const val SPROUTE_CONFIGURATION: String = "configureSproutes"
    }

    object KtorPackageNames {
        const val APPLICATION = "io.ktor.server.application"
        const val AUTH = "io.ktor.server.auth"
        const val RESPONSE = "io.ktor.server.response"
        const val ROUTING = "io.ktor.server.routing"
        const val KOTLIN = "kotlin"
    }

    object MethodNames {
        val applicationGetter = MemberName(KtorPackageNames.APPLICATION, KtorMethodNames.APPLICATION)
        val applicationCallGetter = MemberName(KtorPackageNames.APPLICATION, KtorMethodNames.CALL)
        val authenticationScopeMethod = MemberName(KtorPackageNames.AUTH, KtorMethodNames.AUTH)
        val routingMethod = MemberName(KtorPackageNames.ROUTING, KtorMethodNames.ROUTING)
        val routeMethod = MemberName(KtorPackageNames.ROUTING, KtorMethodNames.ROUTE)
        val callRespondMethod = MemberName(KtorPackageNames.RESPONSE, KtorMethodNames.RESPOND)
        val applyMethod = MemberName(KtorPackageNames.KOTLIN, KtorMethodNames.APPLY)
    }

    fun List.toRequestParamMemberNames(): List {
        return map { it.asCanonicalName() }
            .apply {
                val containsInvalidParameter = any { it !in validParameterTypes }
                if (containsInvalidParameter) throw IllegalArgumentException(
                    "Only the following parameters can be used for Route Classes and Request methods:" +
                            " ${validParameterTypes.joinToString()}. Attempted to send parameters: " +
                            " ${[email protected]()}"
                )
            }
            .map { validParamMemberMap[it]!! }
    }

    private val validParamMemberMap = mapOf(
        Application::class.asCanonicalName() to applicationGetter,
        ApplicationCall::class.asCanonicalName() to applicationCallGetter
    )
    private val validParameterTypes = validParamMemberMap.keys
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy