From eba6a4a38ce5cc889dc73e07c9d93c97bb7892f1 Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Mon, 26 Aug 2024 22:54:00 +0200 Subject: [PATCH] Reformat --- .../jensklingenberg/ktorfit/KtorfitLogger.kt | 5 +++- .../jensklingenberg/ktorfit/KtorfitOptions.kt | 4 ++- .../ktorfit/KtorfitProcessor.kt | 11 ++++---- .../ktorfit/model/FunctionData.kt | 3 +- .../model/annotations/FunctionAnnotation.kt | 8 ++++-- .../AttributeCodeGenerator.kt | 4 ++- .../reqBuilderExtension/BodyCodeGenerator.kt | 10 +++++-- .../CustomRequestBuilderCodeGeneration.kt | 11 ++++---- .../reqBuilderExtension/UrlCodeGeneration.kt | 8 ++++-- .../ktorfit/utils/KSValueParameterExt.kt | 15 ++++++---- .../de/jensklingenberg/ktorfit/utils/Utils.kt | 28 +++++-------------- 11 files changed, 59 insertions(+), 48 deletions(-) diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitLogger.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitLogger.kt index 7c2e8ae0e..9ebaa8446 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitLogger.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitLogger.kt @@ -3,7 +3,10 @@ package de.jensklingenberg.ktorfit import com.google.devtools.ksp.processing.KSPLogger import com.google.devtools.ksp.symbol.KSNode -class KtorfitLogger(private val kspLogger: KSPLogger, private val loggingType: Int) : KSPLogger by kspLogger { +class KtorfitLogger( + private val kspLogger: KSPLogger, + private val loggingType: Int +) : KSPLogger by kspLogger { override fun error( message: String, symbol: KSNode?, diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitOptions.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitOptions.kt index 6fde82809..3b7ccc224 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitOptions.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitOptions.kt @@ -1,6 +1,8 @@ package de.jensklingenberg.ktorfit -class KtorfitOptions(options: Map) { +class KtorfitOptions( + options: Map +) { /** * 0: Turn off all Ktorfit related error checking * diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitProcessor.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitProcessor.kt index 39723b9bb..d33fd9d0d 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitProcessor.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/KtorfitProcessor.kt @@ -19,13 +19,14 @@ import de.jensklingenberg.ktorfit.http.PUT import de.jensklingenberg.ktorfit.model.toClassData class KtorfitProcessorProvider : SymbolProcessorProvider { - override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor { - return KtorfitProcessor(environment, KtorfitOptions(environment.options)) - } + override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor = + KtorfitProcessor(environment, KtorfitOptions(environment.options)) } -class KtorfitProcessor(private val env: SymbolProcessorEnvironment, private val ktorfitOptions: KtorfitOptions) : - SymbolProcessor { +class KtorfitProcessor( + private val env: SymbolProcessorEnvironment, + private val ktorfitOptions: KtorfitOptions +) : SymbolProcessor { private var invoked = false companion object { diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/FunctionData.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/FunctionData.kt index 575dcc4db..9578f0b25 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/FunctionData.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/FunctionData.kt @@ -53,7 +53,8 @@ fun KSFunctionDeclaration.toFunctionData(logger: KSPLogger): FunctionData { val functionName = funcDeclaration.simpleName.asString() val functionParameters = funcDeclaration.parameters.map { it.createParameterData(logger) } - val resolvedReturnType = funcDeclaration.returnType?.resolve() ?: throw IllegalStateException("Return type not found") + val resolvedReturnType = + funcDeclaration.returnType?.resolve() ?: throw IllegalStateException("Return type not found") val returnType = ReturnTypeData( diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/FunctionAnnotation.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/FunctionAnnotation.kt index 32e35b7d7..bf5bbe509 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/FunctionAnnotation.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/FunctionAnnotation.kt @@ -1,6 +1,8 @@ package de.jensklingenberg.ktorfit.model.annotations -enum class HttpMethod(val keyword: String) { +enum class HttpMethod( + val keyword: String +) { GET("GET"), POST("POST"), PUT("PUT"), @@ -15,7 +17,9 @@ enum class HttpMethod(val keyword: String) { */ open class FunctionAnnotation -class Headers(val value: List) : FunctionAnnotation() +class Headers( + val value: List +) : FunctionAnnotation() class FormUrlEncoded : FunctionAnnotation() diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/AttributeCodeGenerator.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/AttributeCodeGenerator.kt index c019e00e2..8688358ee 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/AttributeCodeGenerator.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/AttributeCodeGenerator.kt @@ -7,7 +7,9 @@ fun getAttributeCode(parameterDataList: List): String = parameterDataList .filter { it.hasAnnotation() } .joinToString("\n") { - val tag = it.findAnnotationOrNull() ?: throw IllegalStateException("Tag annotation not found") + val tag = + it.findAnnotationOrNull() + ?: throw IllegalStateException("Tag annotation not found") if (it.type.parameterType.isMarkedNullable) { "${it.name}?.let{ attributes.put(io.ktor.util.AttributeKey(\"${tag.value}\"), it) }" } else { diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/BodyCodeGenerator.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/BodyCodeGenerator.kt index 8893a1e85..1679caed0 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/BodyCodeGenerator.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/BodyCodeGenerator.kt @@ -3,6 +3,10 @@ package de.jensklingenberg.ktorfit.reqBuilderExtension import de.jensklingenberg.ktorfit.model.ParameterData import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Body -fun getBodyDataText(params: List): String { - return params.firstOrNull { it.hasAnnotation() }?.name?.let { "setBody($it)" }.orEmpty() -} +fun getBodyDataText(params: List): String = + params + .firstOrNull { + it.hasAnnotation() + }?.name + ?.let { "setBody($it)" } + .orEmpty() diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/CustomRequestBuilderCodeGeneration.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/CustomRequestBuilderCodeGeneration.kt index e78634679..cac2d3534 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/CustomRequestBuilderCodeGeneration.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/CustomRequestBuilderCodeGeneration.kt @@ -3,8 +3,9 @@ package de.jensklingenberg.ktorfit.reqBuilderExtension import de.jensklingenberg.ktorfit.model.ParameterData import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation -fun getCustomRequestBuilderText(parameterDataList: List): String { - return parameterDataList.find { it.hasAnnotation() }?.let { - it.name + "(this)" - }.orEmpty() -} +fun getCustomRequestBuilderText(parameterDataList: List): String = + parameterDataList + .find { it.hasAnnotation() } + ?.let { + it.name + "(this)" + }.orEmpty() diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/UrlCodeGeneration.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/UrlCodeGeneration.kt index 424283e5e..e154730af 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/UrlCodeGeneration.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/reqBuilderExtension/UrlCodeGeneration.kt @@ -13,9 +13,11 @@ fun getUrlCode( ): String { var urlPath = methodAnnotation.path.ifEmpty { - params.firstOrNull { it.hasAnnotation() }?.let { - "\${${it.name}}" - }.orEmpty() + params + .firstOrNull { it.hasAnnotation() } + ?.let { + "\${${it.name}}" + }.orEmpty() } val baseUrl = diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt index 9d713c7ce..b34d62c49 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/KSValueParameterExt.kt @@ -128,11 +128,16 @@ fun KSValueParameter.getRequestTypeAnnotation(): RequestType? { val filteredAnnotations = this.annotations.filter { it.shortName.getShortName() == requestTypeClazz.simpleName && - it.annotationType.resolve().declaration.qualifiedName?.asString() == requestTypeClazz.qualifiedName + it.annotationType + .resolve() + .declaration.qualifiedName + ?.asString() == requestTypeClazz.qualifiedName } - return filteredAnnotations.mapNotNull { - it.arguments.map { arg -> - RequestType((arg.value as KSType)) + return filteredAnnotations + .mapNotNull { + it.arguments + .map { arg -> + RequestType((arg.value as KSType)) + }.firstOrNull() }.firstOrNull() - }.firstOrNull() } diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt index aae786868..2d1ae72ee 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/utils/Utils.kt @@ -10,28 +10,18 @@ fun KSType?.resolveTypeName(): String { return this.toString().removePrefix("[typealias ").removeSuffix("]") } -inline fun FunctionData.findAnnotationOrNull(): T? { - return this.annotations.firstOrNull { it is T } as? T -} +inline fun FunctionData.findAnnotationOrNull(): T? = this.annotations.firstOrNull { it is T } as? T -fun String.prefixIfNotEmpty(s: String): String { - return (s + this).takeIf { this.isNotEmpty() } ?: this -} +fun String.prefixIfNotEmpty(s: String): String = (s + this).takeIf { this.isNotEmpty() } ?: this -fun String.postfixIfNotEmpty(s: String): String { - return (this + s).takeIf { this.isNotEmpty() } ?: this -} +fun String.postfixIfNotEmpty(s: String): String = (this + s).takeIf { this.isNotEmpty() } ?: this fun String.surroundIfNotEmpty( prefix: String = "", postFix: String = "", -): String { - return this.prefixIfNotEmpty(prefix).postfixIfNotEmpty(postFix) -} +): String = this.prefixIfNotEmpty(prefix).postfixIfNotEmpty(postFix) -fun String.removeWhiteSpaces(): String { - return this.replace("\\s".toRegex(), "") -} +fun String.removeWhiteSpaces(): String = this.replace("\\s".toRegex(), "") fun FileSpec.Builder.addImports(imports: List): FileSpec.Builder { imports.forEach { @@ -47,10 +37,6 @@ fun FileSpec.Builder.addImports(imports: List): FileSpec.Builder { return this } -inline fun List<*>.anyInstance(): Boolean { - return this.filterIsInstance().isNotEmpty() -} +inline fun List<*>.anyInstance(): Boolean = this.filterIsInstance().isNotEmpty() -fun KSName?.safeString(): String { - return this?.asString() ?: "" -} +fun KSName?.safeString(): String = this?.asString() ?: ""