@@ -46,16 +46,6 @@ internal class ExpressionsVisitorK2(
46
46
return parent.wrapAsList()
47
47
}
48
48
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
-
59
49
// Evaluation Order 1
60
50
override fun visitSimpleFunction (simpleFunction : FirSimpleFunction , parent : KtorElement ? ): List <KtorElement > {
61
51
@@ -233,73 +223,72 @@ internal class ExpressionsVisitorK2(
233
223
val expName = resolvedExp?.name?.asString() ? : " "
234
224
val tagsFromAnnotation = functionCall.findTags(session)
235
225
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()
248
228
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
+ }
257
237
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
259
245
}
246
+
247
+ else -> null
260
248
}
249
+ }
261
250
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
+ )
276
265
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
+ }
286
275
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
295
283
}
296
284
}
297
-
298
- else -> null
299
285
}
300
- } else {
301
- null
286
+
287
+ else -> null
302
288
}
289
+ } else {
290
+ null
291
+ }
303
292
304
293
functionCall.findLambda()?.accept(this , resultElement ? : parent) ? : run {
305
294
val declaration = functionCall.calleeReference.toResolvedFunctionSymbol()?.fir
@@ -462,30 +451,31 @@ internal class ExpressionsVisitorK2(
462
451
463
452
private fun FirFunctionCall.isInPackage (fqName : FqName ): Boolean =
464
453
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
- )
473
454
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
+ }
475
464
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 ()
479
468
480
- return docsAnnotation.extractDescription(session)
481
- }
469
+ return docsAnnotation.extractDescription(session)
470
+ }
482
471
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
+ }
490
480
}
491
481
}
0 commit comments