Skip to content

Commit

Permalink
Optimize imports (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso authored Sep 8, 2024
1 parent d6a6141 commit d859927
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ data class ClassData(
val name: String,
val packageName: String,
val functions: List<FunctionData>,
val imports: List<String>,
val imports: Set<String>,
val superClasses: List<KSTypeReference> = emptyList(),
val properties: List<KSPropertyDeclaration> = emptyList(),
val modifiers: List<KModifier> = emptyList(),
Expand All @@ -45,10 +45,9 @@ data class ClassData(
fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
val ksClassDeclaration = this
val imports =
mutableListOf(
mutableSetOf(
"io.ktor.util.reflect.typeInfo",
"io.ktor.client.request.HttpRequestBuilder",
"io.ktor.client.request.setBody",
"io.ktor.client.request.headers",
"io.ktor.client.request.parameter",
"io.ktor.http.URLBuilder",
Expand All @@ -69,6 +68,15 @@ fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
return@map funcDeclaration.toFunctionData(logger)
}

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

if (functionDataList.any { it ->
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 @@ -20,7 +20,7 @@ fun String.surroundIfNotEmpty(

fun String.removeWhiteSpaces(): String = this.replace("\\s".toRegex(), "")

fun FileSpec.Builder.addImports(imports: List<String>): FileSpec.Builder {
fun FileSpec.Builder.addImports(imports: Set<String>): FileSpec.Builder {
imports.forEach {
/**
* Wildcard imports are not allowed by KotlinPoet, as a workaround * is replaced with WILDCARDIMPORT, and it will be replaced again
Expand Down

0 comments on commit d859927

Please sign in to comment.