Skip to content

Commit

Permalink
ApplicationCall parameters support
Browse files Browse the repository at this point in the history
  • Loading branch information
programadorthi committed Nov 14, 2024
1 parent a7d5969 commit 9394150
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import com.squareup.kotlinpoet.ksp.writeTo
import dev.programadorthi.routing.annotation.Body
import dev.programadorthi.routing.annotation.Path
import dev.programadorthi.routing.annotation.Route
import dev.programadorthi.routing.core.application.Application
import io.ktor.http.Parameters
import io.ktor.util.Attributes

public class RoutingProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
Expand All @@ -39,7 +42,6 @@ private class RoutingProcessor(
) : SymbolProcessor {
private var invoked = false

@OptIn(KspExperimental::class)
override fun process(resolver: Resolver): List<KSAnnotated> {
if (invoked) {
return emptyList()
Expand Down Expand Up @@ -135,7 +137,6 @@ private class RoutingProcessor(
.endControlFlow()
}

@OptIn(KspExperimental::class)
private fun KSFunctionDeclaration.generateHandleBody(
isRegexRoute: Boolean,
routeAnnotation: Route,
Expand All @@ -157,8 +158,11 @@ private class RoutingProcessor(
check(param.isVararg.not()) {
"Vararg is not supported as fun parameter"
}
var applied = param.tryApplyBody(hasZeroOrOneParameter, funcBuilder)
if (!isRegexRoute && !applied) {
var applied = param.tryApplyCallProperty(hasZeroOrOneParameter, resolver, funcBuilder)
if (!applied) {
applied = param.tryApplyBody(hasZeroOrOneParameter, funcBuilder)
}
if (!applied && !isRegexRoute) {
applied = param.tryApplyTailCard(
routePath = routeAnnotation.path,
resolver = resolver,
Expand Down Expand Up @@ -288,6 +292,26 @@ private class RoutingProcessor(
return true
}

private fun KSValueParameter.tryApplyCallProperty(
hasZeroOrOneParameter: Boolean,
resolver: Resolver,
builder: CodeBlock.Builder,
): Boolean {
val paramType = type.resolve()
val propertyName = when (paramType.declaration) {
resolver.getClassDeclarationByName<Application>() -> "application"
resolver.getClassDeclarationByName<Parameters>() -> "parameters"
resolver.getClassDeclarationByName<Attributes>() -> "attributes"
else -> return false
}
val paramName = name?.asString()
when {
hasZeroOrOneParameter -> builder.add(CALL_PROPERTY_TEMPLATE, paramName, call, propertyName, "")
else -> builder.addStatement(CALL_PROPERTY_TEMPLATE, paramName, call, propertyName, ",")
}
return true
}

private fun FunSpec.generateFile(ksFiles: Set<KSFile>) {
FileSpec
.builder(
Expand Down Expand Up @@ -337,6 +361,7 @@ private class RoutingProcessor(
private val receiveNullable =
MemberName("dev.programadorthi.routing.core.application", "receiveNullable")

private const val CALL_PROPERTY_TEMPLATE = """%L = %M.%L%L"""
private const val BODY_TEMPLATE = "%L = %M.%M()%L"
private const val FUN_INVOKE_END = ")"
private const val FUN_INVOKE_START = "%M("
Expand Down
2 changes: 2 additions & 0 deletions samples/ksp-sample/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ suspend fun main() {
delay(500)
router.call(uri = "/path/13579/partition")
delay(500)
router.call(uri = "/call/p01/p02")
delay(500)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import dev.programadorthi.routing.annotation.Body
import dev.programadorthi.routing.annotation.Path
import dev.programadorthi.routing.annotation.Route
import dev.programadorthi.routing.core.RouteMethod
import dev.programadorthi.routing.core.application.Application
import io.ktor.http.Parameters
import io.ktor.util.Attributes

data class User(
val id: Int,
Expand Down Expand Up @@ -70,6 +73,19 @@ fun multiParameters(part1: Int, part2: String) {
println(">>>> Parts: $part1 and $part2")
}

@Route("/call/{part1}/{part2}")
fun callParameters(
application: Application,
parameters: Parameters,
attributes: Attributes,
) {
println("""
>>>> application: $application
>>>> parameters: $parameters
>>>> attributes: $attributes
""".trimIndent())
}

class Routes {
//@Route("/path")
fun run() {
Expand Down

0 comments on commit 9394150

Please sign in to comment.