Skip to content

Commit

Permalink
Remove reading of imports from .kt file (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso authored Oct 29, 2023
1 parent 0cf0ca3 commit c4b0a57
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ package de.jensklingenberg.ktorfit.model
import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.symbol.*
import com.squareup.kotlinpoet.*
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSFile
import com.google.devtools.ksp.symbol.KSPropertyDeclaration
import com.google.devtools.ksp.symbol.KSTypeReference
import com.squareup.kotlinpoet.ANY
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.ksp.toKModifier
import com.squareup.kotlinpoet.ksp.toTypeName
import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.PROPERTIES_NOT_SUPPORTED
import de.jensklingenberg.ktorfit.model.annotations.FormUrlEncoded
import de.jensklingenberg.ktorfit.model.annotations.Multipart
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.*
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Field
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Part
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.RequestType
import de.jensklingenberg.ktorfit.utils.addImports
import de.jensklingenberg.ktorfit.utils.getFileImports

/**
* @param name of the interface that contains annotations
Expand Down Expand Up @@ -141,13 +155,13 @@ private fun getCreateExtensionFunctionSpec(
*/
fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
val ksClassDeclaration = this
val imports = ksClassDeclaration.getFileImports().toMutableList().apply {
add("io.ktor.util.reflect.*")
add("io.ktor.client.request.*")
add("io.ktor.http.*")
add(ktorfitClass.packageName + "." + ktorfitClass.name)
add("de.jensklingenberg.ktorfit.internal.*")
}
val imports = mutableListOf(
"io.ktor.util.reflect.*",
"io.ktor.client.request.*",
"io.ktor.http.*",
ktorfitClass.packageName + "." + ktorfitClass.name,
"de.jensklingenberg.ktorfit.internal.*"
)

val packageName = ksClassDeclaration.packageName.asString()
val className = ksClassDeclaration.simpleName.asString()
Expand All @@ -163,7 +177,9 @@ fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
it.annotations.any { it is FormUrlEncoded || it is Multipart } ||
it.parameterDataList.any { param -> param.hasAnnotation<Field>() || param.hasAnnotation<Part>() }
}) {
imports.add("io.ktor.client.request.forms.*")
imports.add("io.ktor.client.request.forms.FormDataContent")
imports.add("io.ktor.client.request.forms.MultiPartFormDataContent")
imports.add("io.ktor.client.request.forms.formData")
}

if (functionDataList.any { it.parameterDataList.any { param -> param.hasAnnotation<RequestType>() } }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ data class FunctionData(
it.add(KModifier.SUSPEND)
}
})
.returns(TypeVariableName(returnTypeName))
.returns(returnType.parameterType!!.toTypeName())
.addParameters(this.parameterDataList.map {
ParameterSpec(it.name, TypeVariableName(it.type.name))
ParameterSpec(it.name, it.type.parameterType!!.toTypeName())
})
.addRequestConverterText(this.parameterDataList)
.addStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fun KSValueParameter.createParameterData(logger: KSPLogger): ParameterData {

val parameterAnnotations = ksValueParameter.getParamAnnotationList(logger)

val parameterName = ksValueParameter.name?.asString() ?: ""
val parameterName = ksValueParameter.name?.asString().orEmpty()
val parameterType = ksValueParameter.type.resolve()
val hasRequestBuilderAnno = parameterAnnotations.anyInstance<RequestBuilder>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.ksp.toClassName
import de.jensklingenberg.ktorfit.model.ParameterData
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.RequestType
import de.jensklingenberg.ktorfit.model.ktorfitClass

fun FunSpec.Builder.addRequestConverterText(parameterDataList: List<ParameterData>) = apply {
if (parameterDataList.any { it.hasAnnotation<RequestType>() }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ fun String.surroundIfNotEmpty(prefix: String = "", postFix: String = ""): String
return this.prefixIfNotEmpty(prefix).postfixIfNotEmpty(postFix)
}

/**
* Gets the imports of a class by reading the imports from the file
* which contains the class
* TODO: Find better way to get imports
*/
fun KSClassDeclaration.getFileImports(): List<String> {
return File(this.containingFile!!.filePath)
.readLines()
.filter { it.trimStart().startsWith("import") && !it.startsWith("import de.jensklingenberg.ktorfit.http.") }
.toMutableSet()
.map { it.removePrefix("import ") }
}


fun String.removeWhiteSpaces(): String {
return this.replace("\\s".toRegex(), "")
}
Expand Down

0 comments on commit c4b0a57

Please sign in to comment.