From 33f1b5735dcfaeced3d54a0f1908606be2f0fa8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=ED=9D=AC=EB=A7=9D?= <105429536+esperar@users.noreply.github.com> Date: Mon, 25 Sep 2023 19:03:04 +0900 Subject: [PATCH] Update `FlowResponseConverterFunctionProvider` Type Extension functions toClass() (#5201) ### Type of PR Hello! While reviewing the Kotlin part of Armeria, I noticed that in the `FlowResponseConverterFunctionProvider` class, there's an extension function for `Type` used in the `createResponseConverterFunction()` function of `DelegatingResponseConverterFunctionProvider`. The `toClass()` function returns a type of `Class<*>?`.
In the past, when working with Kotlin, I found it beneficial to define extension functions that return nullable values with names following the `toXXXOrNull()` pattern. This made the code more understandable for me and my teammates, especially during maintenance.
Since the extension function is defined just below `createResponseConverterFunction()`, it may lead to questions like "Why use `toClass()?.let` here?" Adding `OrNull` to the function name can make the code more readable.
> I understand that naming conventions can vary among teams, and it may not be a critical issue. However, if you find the suggestion helpful, I'd appreciate it if you could consider applying the change. --- ```kt private fun Type.toClassOrNull(): Class<*>? = when (this) { is ParameterizedType -> this.rawType as Class<*> is Class<*> -> this else -> null } --- .../common/kotlin/FlowResponseConverterFunctionProvider.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kotlin/src/main/kotlin/com/linecorp/armeria/internal/common/kotlin/FlowResponseConverterFunctionProvider.kt b/kotlin/src/main/kotlin/com/linecorp/armeria/internal/common/kotlin/FlowResponseConverterFunctionProvider.kt index aaf2d8cd984..7ccbfa10659 100644 --- a/kotlin/src/main/kotlin/com/linecorp/armeria/internal/common/kotlin/FlowResponseConverterFunctionProvider.kt +++ b/kotlin/src/main/kotlin/com/linecorp/armeria/internal/common/kotlin/FlowResponseConverterFunctionProvider.kt @@ -32,7 +32,7 @@ class FlowResponseConverterFunctionProvider : DelegatingResponseConverterFunctio responseConverter: ResponseConverterFunction ): ResponseConverterFunction? = returnType - .toClass() + .toClassOrNull() ?.let { if (Flow::class.java.isAssignableFrom(it)) { FlowResponseConverterFunction(responseConverter) @@ -41,7 +41,7 @@ class FlowResponseConverterFunctionProvider : DelegatingResponseConverterFunctio } } - private fun Type.toClass(): Class<*>? = + private fun Type.toClassOrNull(): Class<*>? = when (this) { is ParameterizedType -> this.rawType as Class<*> is Class<*> -> this