Skip to content

Commit

Permalink
docs(KSP): improve method documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Sep 9, 2022
1 parent f204f48 commit 45e5b6c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
3 changes: 3 additions & 0 deletions ktorfit-ksp/src/main/kotlin/KtorfitProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<KSFunctionDeclaration> {
val getAnnotated = resolver.getSymbolsWithAnnotation(GET::class.java.name).toList()
val postAnnotated = resolver.getSymbolsWithAnnotation(POST::class.java.name).toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) : FunctionAnnotation()
class FormUrlEncoded : FunctionAnnotation()
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,30 @@ 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<String> {
val importList =
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<String>.addIfAbsent(text: String) {
if (this.none { it.contains(text) }) {
this.add(text)
}
}

fun toClassData(ksClassDeclaration: KSClassDeclaration, logger: KSPLogger): ClassData {

val functionDataList: List<FunctionData> =
Expand Down

0 comments on commit 45e5b6c

Please sign in to comment.