Skip to content

Commit 602d05c

Browse files
committed
clean up expressions visitor
1 parent 4337e04 commit 602d05c

File tree

3 files changed

+87
-88
lines changed

3 files changed

+87
-88
lines changed

create-plugin/src/main/kotlin/io/github/tabilzad/ktor/k2/ExpressionsVisitorK2.kt

+77-87
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ internal class ExpressionsVisitorK2(
4646
return parent.wrapAsList()
4747
}
4848

49-
@OptIn(PrivateForInline::class)
50-
private fun FirStatement.findTags(session: FirSession): Set<String>? {
51-
val annotation = findAnnotation(ClassIds.KTOR_TAGS_ANNOTATION, session) ?: return null
52-
val resolved = FirExpressionEvaluator.evaluateAnnotationArguments(annotation, session)
53-
return resolved?.entries?.find { it.key.asString() == "tags" }?.value?.result?.accept(
54-
StringArrayLiteralVisitor(),
55-
emptyList()
56-
)?.toSet()
57-
}
58-
5949
// Evaluation Order 1
6050
override fun visitSimpleFunction(simpleFunction: FirSimpleFunction, parent: KtorElement?): List<KtorElement> {
6151

@@ -233,73 +223,72 @@ internal class ExpressionsVisitorK2(
233223
val expName = resolvedExp?.name?.asString() ?: ""
234224
val tagsFromAnnotation = functionCall.findTags(session)
235225

236-
val resultElement: KtorElement? =
237-
if (functionCall.isARouteDefinition() || ExpType.METHOD.labels.contains(expName)) {
238-
val pathValue = functionCall.resolvePath()
239-
240-
when {
241-
ExpType.ROUTE.labels.contains(expName) -> {
242-
when (parent) {
243-
null -> {
244-
pathValue?.let {
245-
DocRoute(it, tags = tagsFromAnnotation)
246-
} ?: DocRoute(expName, tags = tagsFromAnnotation)
247-
}
226+
val resultElement = if (functionCall.isARouteDefinition() || ExpType.METHOD.labels.contains(expName)) {
227+
val pathValue = functionCall.resolvePath()
248228

249-
is DocRoute -> {
250-
val newElement = DocRoute(
251-
pathValue.toString(),
252-
tags = parent.tags merge tagsFromAnnotation
253-
)
254-
parent.children.add(newElement)
255-
newElement
256-
}
229+
when {
230+
ExpType.ROUTE.labels.contains(expName) -> {
231+
when (parent) {
232+
null -> {
233+
pathValue?.let {
234+
DocRoute(it, tags = tagsFromAnnotation)
235+
} ?: DocRoute(expName, tags = tagsFromAnnotation)
236+
}
257237

258-
else -> null
238+
is DocRoute -> {
239+
val newElement = DocRoute(
240+
pathValue.toString(),
241+
tags = parent.tags merge tagsFromAnnotation
242+
)
243+
parent.children.add(newElement)
244+
newElement
259245
}
246+
247+
else -> null
260248
}
249+
}
261250

262-
ExpType.METHOD.labels.contains(expName) -> {
263-
val descr = functionCall.findDocsDescription(session)
264-
val responses = functionCall.findRespondsAnnotation(session)?.resolveToOpenSpecFormat()
265-
val params = functionCall.typeArguments
266-
267-
val endpoint = EndPoint(
268-
path = null,
269-
method = expName,
270-
description = descr.description,
271-
summary = descr.summary,
272-
operationId = descr.operationId,
273-
tags = descr.tags merge tagsFromAnnotation,
274-
responses = responses
275-
)
251+
ExpType.METHOD.labels.contains(expName) -> {
252+
val descr = functionCall.findDocsDescription(session)
253+
val responses = functionCall.findRespondsAnnotation(session)?.resolveToOpenSpecFormat()
254+
val params = functionCall.typeArguments
255+
256+
val endpoint = EndPoint(
257+
path = null,
258+
method = expName,
259+
description = descr.description,
260+
summary = descr.summary,
261+
operationId = descr.operationId,
262+
tags = descr.tags merge tagsFromAnnotation,
263+
responses = responses
264+
)
276265

277-
val resource = functionCall.findResource(endpoint)
278-
val type = params.firstOrNull()?.toConeTypeProjection()?.type
279-
val newElement = resource ?: endpoint.copy(path = pathValue, body = type?.toEndpointBody())
280-
when (parent) {
281-
null -> newElement
282-
is DocRoute -> {
283-
parent.children.add(newElement)
284-
newElement
285-
}
266+
val resource = functionCall.findResource(endpoint)
267+
val type = params.firstOrNull()?.toConeTypeProjection()?.type
268+
val newElement = resource ?: endpoint.copy(path = pathValue, body = type?.toEndpointBody())
269+
when (parent) {
270+
null -> newElement
271+
is DocRoute -> {
272+
parent.children.add(newElement)
273+
newElement
274+
}
286275

287-
else -> {
288-
log?.report(
289-
CompilerMessageSeverity.WARNING,
290-
"Endpoints can't have Endpoint as routes",
291-
functionCall.getLocation()
292-
)
293-
null
294-
}
276+
else -> {
277+
log?.report(
278+
CompilerMessageSeverity.WARNING,
279+
"Endpoints can't have Endpoint as routes",
280+
functionCall.getLocation()
281+
)
282+
null
295283
}
296284
}
297-
298-
else -> null
299285
}
300-
} else {
301-
null
286+
287+
else -> null
302288
}
289+
} else {
290+
null
291+
}
303292

304293
functionCall.findLambda()?.accept(this, resultElement ?: parent) ?: run {
305294
val declaration = functionCall.calleeReference.toResolvedFunctionSymbol()?.fir
@@ -462,30 +451,31 @@ internal class ExpressionsVisitorK2(
462451

463452
private fun FirFunctionCall.isInPackage(fqName: FqName): Boolean =
464453
toResolvedCallableSymbol()?.callableId?.packageName == fqName
465-
}
466-
467-
internal data class KtorK2ResponseBag(
468-
val descr: String,
469-
val status: String,
470-
val type: ConeKotlinType?,
471-
val isCollection: Boolean = false
472-
)
473454

474-
private fun KtorElement?.wrapAsList() = this?.let { listOf(this) } ?: emptyList()
455+
@OptIn(PrivateForInline::class)
456+
private fun FirStatement.findTags(session: FirSession): Set<String>? {
457+
val annotation = findAnnotation(ClassIds.KTOR_TAGS_ANNOTATION, session) ?: return null
458+
val resolved = FirExpressionEvaluator.evaluateAnnotationArguments(annotation, session)
459+
return resolved?.entries?.find { it.key.asString() == "tags" }?.value?.result?.accept(
460+
StringArrayLiteralVisitor(),
461+
emptyList()
462+
)?.toSet()
463+
}
475464

476-
private fun FirFunctionCall.findDocsDescription(session: FirSession): KtorDescriptionBag {
477-
val docsAnnotation = findAnnotationNamed(KtorDescription::class.simpleName!!)
478-
?: return KtorDescriptionBag()
465+
private fun FirFunctionCall.findDocsDescription(session: FirSession): KtorDescriptionBag {
466+
val docsAnnotation = findAnnotationNamed(KtorDescription::class.simpleName!!)
467+
?: return KtorDescriptionBag()
479468

480-
return docsAnnotation.extractDescription(session)
481-
}
469+
return docsAnnotation.extractDescription(session)
470+
}
482471

483-
@OptIn(PrivateForInline::class)
484-
private fun FirFunctionCall.findRespondsAnnotation(session: FirSession): List<KtorK2ResponseBag>? {
485-
val annotation = findAnnotationNamed(KtorResponds::class.simpleName!!)
486-
return annotation?.let {
487-
val resolved = FirExpressionEvaluator.evaluateAnnotationArguments(annotation, session)
488-
val mapping = resolved?.entries?.find { it.key.asString() == "mapping" }?.value?.result
489-
mapping?.accept(RespondsAnnotationVisitor(session), null)
472+
@OptIn(PrivateForInline::class)
473+
private fun FirFunctionCall.findRespondsAnnotation(session: FirSession): List<KtorK2ResponseBag>? {
474+
val annotation = findAnnotationNamed(KtorResponds::class.simpleName!!)
475+
return annotation?.let {
476+
val resolved = FirExpressionEvaluator.evaluateAnnotationArguments(annotation, session)
477+
val mapping = resolved?.entries?.find { it.key.asString() == "mapping" }?.value?.result
478+
mapping?.accept(RespondsAnnotationVisitor(session), null)
479+
}
490480
}
491481
}

create-plugin/src/main/kotlin/io/github/tabilzad/ktor/k2/K2Utils.kt

+2
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,5 @@ fun FirPropertyAccessExpression.isEnum(session: FirSession): Boolean = this.disp
218218
?.toResolvedCallableSymbol(session)
219219
?.resolvedReturnType
220220
?.toRegularClassSymbol(session)?.isEnumClass == true
221+
222+
fun <T> T?.wrapAsList() = this?.let { listOf(this) } ?: emptyList()

create-plugin/src/main/kotlin/io/github/tabilzad/ktor/k2/visitors/RespondsAnnotationVisitor.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package io.github.tabilzad.ktor.k2.visitors
22

3-
import io.github.tabilzad.ktor.k2.KtorK2ResponseBag
43
import org.jetbrains.kotlin.fir.FirElement
54
import org.jetbrains.kotlin.fir.FirSession
65
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
76
import org.jetbrains.kotlin.fir.expressions.*
7+
import org.jetbrains.kotlin.fir.types.ConeKotlinType
88
import org.jetbrains.kotlin.fir.types.resolvedType
99
import org.jetbrains.kotlin.fir.types.type
1010
import org.jetbrains.kotlin.fir.visitors.FirDefaultVisitor
1111

12+
internal data class KtorK2ResponseBag(
13+
val descr: String,
14+
val status: String,
15+
val type: ConeKotlinType?,
16+
val isCollection: Boolean = false
17+
)
18+
1219
internal class RespondsAnnotationVisitor(private val session: FirSession) : FirDefaultVisitor<List<KtorK2ResponseBag>, KtorK2ResponseBag?>() {
1320

1421
// if we don't override a particular visit, it will come here by default

0 commit comments

Comments
 (0)