Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Sep 8, 2024
1 parent 1795903 commit b6ac530
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ksp.toKModifier
import com.squareup.kotlinpoet.ksp.toTypeName
import de.jensklingenberg.ktorfit.model.annotations.FormUrlEncoded
import de.jensklingenberg.ktorfit.model.annotations.Headers
import de.jensklingenberg.ktorfit.model.annotations.Multipart
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Field
Expand Down Expand Up @@ -48,7 +49,6 @@ fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
mutableSetOf(
"io.ktor.util.reflect.typeInfo",
"io.ktor.client.request.HttpRequestBuilder",
"io.ktor.client.request.headers",
"io.ktor.client.request.parameter",
"io.ktor.http.URLBuilder",
"io.ktor.http.HttpMethod",
Expand All @@ -63,25 +63,17 @@ fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
checkClassForErrors(this, logger)

val functionDataList: List<FunctionData> =
ksClassDeclaration.getDeclaredFunctions().toList().map { funcDeclaration ->
return@map funcDeclaration.toFunctionData(logger)
}

functionDataList.forEach {
it.parameterDataList.forEach {
if (it.hasAnnotation<ParameterAnnotation.Body>()) {
imports.add("io.ktor.client.request.setBody")
}

if (it.findAnnotationOrNull<ParameterAnnotation.Path>()?.encoded == false) {
imports.add("io.ktor.http.encodeURLPath")
ksClassDeclaration
.getDeclaredFunctions()
.toList()
.map { funcDeclaration ->
return@map funcDeclaration.toFunctionData(logger, addImport = { imports.add(it) })
}

if (it.hasAnnotation<ParameterAnnotation.RequestType>()) {
imports.add("kotlin.reflect.cast")
}
functionDataList.forEach {
if (it.annotations.any { it is Headers || it is FormUrlEncoded }) {
imports.add("io.ktor.client.request.headers")
}

if (it.annotations.any { it is FormUrlEncoded || it is Multipart } ||
it.parameterDataList.any { param -> param.hasAnnotation<Field>() || param.hasAnnotation<ParameterAnnotation.Part>() }
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import de.jensklingenberg.ktorfit.model.annotations.FunctionAnnotation
import de.jensklingenberg.ktorfit.model.annotations.HttpMethod
import de.jensklingenberg.ktorfit.model.annotations.HttpMethodAnnotation
import de.jensklingenberg.ktorfit.model.annotations.Multipart
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Body
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Field
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.FieldMap
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Path
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Url
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.RequestBuilder
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.*
import de.jensklingenberg.ktorfit.poetspec.findTypeName
import de.jensklingenberg.ktorfit.utils.anyInstance
import de.jensklingenberg.ktorfit.utils.getFormUrlEncodedAnnotation
Expand Down Expand Up @@ -53,7 +48,10 @@ private fun getHttpMethodAnnotations(ksFunctionDeclaration: KSFunctionDeclaratio
return listOfNotNull(getAnno, postAnno, putAnno, deleteAnno, headAnno, optionsAnno, patchAnno, httpAnno)
}

fun KSFunctionDeclaration.toFunctionData(logger: KSPLogger): FunctionData {
fun KSFunctionDeclaration.toFunctionData(
logger: KSPLogger,
addImport: (String) -> Unit
): FunctionData {
val funcDeclaration = this
val functionName = funcDeclaration.simpleName.asString()
val functionParameters = funcDeclaration.parameters.map { it.createParameterData(logger) }
Expand All @@ -68,6 +66,30 @@ fun KSFunctionDeclaration.toFunctionData(logger: KSPLogger): FunctionData {
typeName = findTypeName(resolvedReturnType, funcDeclaration.containingFile!!.filePath),
)

functionParameters.forEach { parameterData ->
parameterData.annotations.forEach {
if (it is Header || it is HeaderMap) {
addImport("io.ktor.client.request.headers")
}

if (it is Tag) {
addImport("io.ktor.util.AttributeKey")
}

if (it is Body) {
addImport("io.ktor.client.request.setBody")
}

if (it is Path && !it.encoded) {
addImport("io.ktor.http.encodeURLPath")
}

if (it is RequestType) {
addImport("kotlin.reflect.cast")
}
}
}

val functionAnnotationList = mutableListOf<FunctionAnnotation>()

funcDeclaration.getMultipartAnnotation()?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fun getAttributeCode(parameterDataList: List<ParameterData>): String =
it.findAnnotationOrNull<ParameterAnnotation.Tag>()
?: throw IllegalStateException("Tag annotation not found")
if (it.type.parameterType.isMarkedNullable) {
"${it.name}?.let{ attributes.put(io.ktor.util.AttributeKey(\"${tag.value}\"), it) }"
"${it.name}?.let{ attributes.put(AttributeKey(\"${tag.value}\"), it) }"
} else {
"attributes.put(io.ktor.util.AttributeKey(\"${tag.value}\"), ${it.name})"
"attributes.put(AttributeKey(\"${tag.value}\"), ${it.name})"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ fun getFieldArgumentsText(
): String {
val fieldText =
params.filter { it.hasAnnotation<Field>() }.joinToString("") { parameterData ->
val field = parameterData.annotations.filterIsInstance<Field>().first()
val field =
parameterData.annotations.filterIsInstance<Field>().firstOrNull()
?: throw IllegalStateException("Field annotation not found")
val encoded = field.encoded
val paramName = parameterData.name
val fieldValue = field.value
Expand Down Expand Up @@ -56,7 +58,7 @@ fun getFieldArgumentsText(
val fieldMapStrings =
params.filter { it.hasAnnotation<FieldMap>() }.joinToString("") { parameterData ->

val fieldMap = parameterData.findAnnotationOrNull<FieldMap>()!!
val fieldMap = parameterData.findAnnotationOrNull<FieldMap>() ?: throw IllegalStateException("FieldMap annotation not found")
val encoded = fieldMap.encoded
val data = parameterData.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ interface TestService {
)

val expectedHeadersArgumentText =
"""attributes.put(io.ktor.util.AttributeKey("myTag1"), myTag1)
someParameter?.let{ attributes.put(io.ktor.util.AttributeKey("myTag2"), it) } """
"""attributes.put(AttributeKey("myTag1"), myTag1)
someParameter?.let{ attributes.put(AttributeKey("myTag2"), it) } """

val compilation = getCompilation(listOf(source))
val result = compilation.compile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ interface JsonPlaceHolderApi {

@GET("posts/{postId}")
suspend fun getPostById(
@Path postId: Int = 4
@Path postId: Int = 4,
@Tag value: String = "test"
): Post

@GET("posts/{postId}/comments")
Expand Down

0 comments on commit b6ac530

Please sign in to comment.