Skip to content

Commit 1c4a2fd

Browse files
Tailcard parameter support
1 parent dcdf9c6 commit 1c4a2fd

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

ksp-processor/jvm/src/dev/programadorthi/routing/ksp/RoutingProcessor.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dev.programadorthi.routing.ksp
22

33
import com.google.devtools.ksp.KspExperimental
44
import com.google.devtools.ksp.getAnnotationsByType
5+
import com.google.devtools.ksp.getClassDeclarationByName
56
import com.google.devtools.ksp.getVisibility
67
import com.google.devtools.ksp.processing.CodeGenerator
78
import com.google.devtools.ksp.processing.Dependencies
@@ -86,12 +87,32 @@ private class RoutingProcessor(
8687
.firstOrNull()
8788
?.value
8889
?: paramName
90+
val paramType = param.type.resolve()
91+
if (routeAnnotation.path.contains("{$customName...}")) {
92+
val listDeclaration = checkNotNull(resolver.getClassDeclarationByName<List<*>>()) {
93+
"Class declaration not found to List<String>?"
94+
}
95+
check(paramType.declaration == listDeclaration) {
96+
"Tailcard parameter must be a List<String>?"
97+
}
98+
val genericArgument = checkNotNull(param.type.element?.typeArguments?.firstOrNull()?.type?.resolve()) {
99+
"No <String> type found at tailcard parameter"
100+
}
101+
check(genericArgument == resolver.builtIns.stringType) {
102+
"Tailcard list items type must be non nullable String"
103+
}
104+
check(paramType.isMarkedNullable) {
105+
"Tailcard list must be nullable as List<String>?"
106+
}
107+
parameters += """$paramName = %M.parameters.getAll("$customName")"""
108+
continue
109+
}
110+
89111
val isOptional = routeAnnotation.path.contains("{$customName?}")
90112
val isRequired = routeAnnotation.path.contains("{$customName}")
91113
check(isOptional || isRequired) {
92114
"'$qualifiedName' has parameter '$paramName' that is not declared as path parameter {$customName}"
93115
}
94-
val paramType = param.type.resolve()
95116
val parsed = """$paramName = %M.parameters["$customName"]"""
96117
parameters += when {
97118
isOptional -> optionalParse(paramType, resolver, parsed)

samples/ksp-sample/src/main/kotlin/Main.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ suspend fun main() {
2323
delay(500)
2424
router.call(uri = "/optional/ABC")
2525
delay(500)
26+
router.call(uri = "/tailcard/p1")
27+
delay(500)
28+
router.call(uri = "/tailcard/p1/p2/p3/p4")
29+
delay(500)
2630
}

samples/ksp-sample/src/main/kotlin/dev/programadorthi/routing/sample/Routes.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ fun optional(id: Char?) {
2828
println(">>>> Optional ID: $id")
2929
}
3030

31+
@Route(path = "/tailcard/{param...}")
32+
fun tailcard(param: List<String>?) {
33+
println(">>>> Tailcard params: $param")
34+
}
35+
3136
class Routes {
3237
//@Route("/path")
3338
fun run() {

0 commit comments

Comments
 (0)