From 45e5b6c48c63bc47580ad0dd796fa34e37c171fb Mon Sep 17 00:00:00 2001 From: Jens Klingenberg Date: Fri, 9 Sep 2022 21:56:37 +0200 Subject: [PATCH] docs(KSP): improve method documentation --- .../src/main/kotlin/KtorfitProcessor.kt | 3 ++ .../model/annotations/FunctionAnnotation.kt | 4 +-- .../model/annotations/HttpMethodAnnotation.kt | 6 ++++ .../ktorfit/parser/ClassParser.kt | 28 ++++++++----------- 4 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/HttpMethodAnnotation.kt diff --git a/ktorfit-ksp/src/main/kotlin/KtorfitProcessor.kt b/ktorfit-ksp/src/main/kotlin/KtorfitProcessor.kt index 04b6ed2d5..8552373d7 100644 --- a/ktorfit-ksp/src/main/kotlin/KtorfitProcessor.kt +++ b/ktorfit-ksp/src/main/kotlin/KtorfitProcessor.kt @@ -57,6 +57,9 @@ public class KtorfitProcessor(private val env: SymbolProcessorEnvironment) : Sym return emptyList() } + /** + * Returns a list of all [KSFunctionDeclaration] which are annotated with a Http Method Annotation + */ private fun getAnnotatedFunctions(): List { val getAnnotated = resolver.getSymbolsWithAnnotation(GET::class.java.name).toList() val postAnnotated = resolver.getSymbolsWithAnnotation(POST::class.java.name).toList() 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 ebec59580..322ecf1cd 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 @@ -4,14 +4,12 @@ enum class HttpMethod(val keyword: String) { GET("GET"), POST("POST"), PUT("PUT"), DELETE("DELETE"), HEAD("HEAD"), PATCH("PATCH"), CUSTOM("") } -class CustomHttp(override val path: String, override val httpMethod: HttpMethod, val hasBody: Boolean = false) : - HttpMethodAnnotation(path, httpMethod) + /** * Annotation at a function */ open class FunctionAnnotation -open class HttpMethodAnnotation(open val path: String, open val httpMethod: HttpMethod) : FunctionAnnotation() class Headers(val path: List) : FunctionAnnotation() class FormUrlEncoded : FunctionAnnotation() diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/HttpMethodAnnotation.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/HttpMethodAnnotation.kt new file mode 100644 index 000000000..9dddc8776 --- /dev/null +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/annotations/HttpMethodAnnotation.kt @@ -0,0 +1,6 @@ +package de.jensklingenberg.ktorfit.model.annotations + +open class HttpMethodAnnotation(open val path: String, open val httpMethod: HttpMethod) : FunctionAnnotation() + +class CustomHttp(override val path: String, override val httpMethod: HttpMethod, val hasBody: Boolean = false) : + HttpMethodAnnotation(path, httpMethod) \ No newline at end of file diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ClassParser.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ClassParser.kt index 8df6130cf..0ad4e7e1e 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ClassParser.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ClassParser.kt @@ -19,6 +19,8 @@ import java.io.File /** + * Gets the imports of a class by reading the imports from the file + * which contains the class * TODO: Find better way to get imports */ private fun getImports(ksClassDeclaration: KSClassDeclaration): List { @@ -26,27 +28,21 @@ private fun getImports(ksClassDeclaration: KSClassDeclaration): List { File(ksClassDeclaration.containingFile!!.filePath) .readLines() .filter { it.trimStart().startsWith("import") } - .toMutableList() + .toMutableSet() - importList.addIfAbsent(ktorfitClass.packageName + "." + ktorfitClass.name) - importList.addIfAbsent(clientClass.packageName + "." + clientClass.name) - importList.addIfAbsent(requestDataClass.packageName + "." + requestDataClass.name) - importList.addIfAbsent("de.jensklingenberg.ktorfit.internal.QueryData") - importList.addIfAbsent("de.jensklingenberg.ktorfit.internal.QueryType") - importList.addIfAbsent("de.jensklingenberg.ktorfit.internal.HeaderData") - importList.addIfAbsent("de.jensklingenberg.ktorfit.internal.FieldData") - importList.addIfAbsent("de.jensklingenberg.ktorfit.internal.FieldType") - importList.addIfAbsent(pathDataClass.packageName+"."+ pathDataClass.name) + importList.add(ktorfitClass.packageName + "." + ktorfitClass.name) + importList.add(clientClass.packageName + "." + clientClass.name) + importList.add(requestDataClass.packageName + "." + requestDataClass.name) + importList.add("de.jensklingenberg.ktorfit.internal.QueryData") + importList.add("de.jensklingenberg.ktorfit.internal.QueryType") + importList.add("de.jensklingenberg.ktorfit.internal.HeaderData") + importList.add("de.jensklingenberg.ktorfit.internal.FieldData") + importList.add("de.jensklingenberg.ktorfit.internal.FieldType") + importList.add(pathDataClass.packageName+"."+ pathDataClass.name) return importList.map { it.removePrefix("import ") } } -private fun MutableList.addIfAbsent(text: String) { - if (this.none { it.contains(text) }) { - this.add(text) - } -} - fun toClassData(ksClassDeclaration: KSClassDeclaration, logger: KSPLogger): ClassData { val functionDataList: List =