Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Sep 8, 2024
1 parent 36e9e04 commit da7339c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun generateImplClass(
) {
classDataList.forEach { classData ->
with(classData) {
val implClassSpec = classData.getImplClassSpec(resolver, ktorfitOptions, classDataList)
val implClassSpec = classData.getImplClassSpec(resolver, ktorfitOptions)

val fileSource =
createFileSpec(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.jensklingenberg.ktorfit.model

import com.google.devtools.ksp.getDeclaredFunctions
import com.google.devtools.ksp.getDeclaredProperties
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSAnnotation
Expand Down Expand Up @@ -69,7 +70,7 @@ fun KSClassDeclaration.toClassData(logger: KSPLogger): ClassData {
/** In KSP Any is a supertype of an interface */
it.toTypeName() == ANY
}
val properties = ksClassDeclaration.getAllProperties().toList()
val properties = ksClassDeclaration.getDeclaredProperties().toList()

return ClassData(
name = className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import de.jensklingenberg.ktorfit.poetspec.findTypeName
import de.jensklingenberg.ktorfit.utils.anyInstance
import de.jensklingenberg.ktorfit.utils.getFormUrlEncodedAnnotation
import de.jensklingenberg.ktorfit.utils.getHeaderAnnotation
import de.jensklingenberg.ktorfit.utils.getKsFile
import de.jensklingenberg.ktorfit.utils.getMultipartAnnotation
import de.jensklingenberg.ktorfit.utils.getStreamingAnnotation
import de.jensklingenberg.ktorfit.utils.isSuspend
Expand Down Expand Up @@ -75,7 +76,7 @@ fun KSFunctionDeclaration.toFunctionData(
ReturnTypeData(
name = resolvedReturnType.resolveTypeName(),
parameterType = resolvedReturnType,
typeName = findTypeName(resolvedReturnType, funcDeclaration.containingFile!!.filePath),
typeName = findTypeName(resolvedReturnType, funcDeclaration.getKsFile().filePath),
)

val functionAnnotationList = mutableListOf<FunctionAnnotation>()
Expand Down Expand Up @@ -187,18 +188,17 @@ fun KSFunctionDeclaration.toFunctionData(
}
}

functionParameters.filter { it.hasAnnotation<Path>() }.forEach {
val pathAnnotation = it.findAnnotationOrNull<Path>()
if (!firstHttpMethodAnnotation.path.contains("{${pathAnnotation?.value ?: ""}}")) {
logger.error(
KtorfitError.missingXInRelativeUrlPath(pathAnnotation?.value.orEmpty()),
funcDeclaration,
)
}
}

functionParameters.forEach { parameterData ->
parameterData.annotations.forEach {
if (it is Path) {
if (!firstHttpMethodAnnotation.path.contains("{${it.value}}")) {
logger.error(
KtorfitError.missingXInRelativeUrlPath(it.value),
funcDeclaration,
)
}
}

if (it is Header || it is HeaderMap) {
addImport("io.ktor.client.request.headers")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,13 @@ fun KSValueParameter.createParameterData(logger: KSPLogger): ParameterData {
)
}

val name = if (hasRequestBuilderAnno) "HttpRequestBuilder.()->Unit" else parameterType.resolveTypeName()
val type =
if (hasRequestBuilderAnno) {
ReturnTypeData(
"HttpRequestBuilder.()->Unit",
parameterType,
findTypeName(parameterType, ksValueParameter.containingFile!!.filePath)
)
} else {
ReturnTypeData(
parameterType.resolveTypeName(),
parameterType,
findTypeName(parameterType, ksValueParameter.containingFile!!.filePath)
)
}
ReturnTypeData(
name,
parameterType,
findTypeName(parameterType, ksValueParameter.containingFile!!.filePath)
)

return ParameterData(
parameterName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,25 @@ import de.jensklingenberg.ktorfit.model.toClassName
fun ClassData.getImplClassSpec(
resolver: Resolver,
ktorfitOptions: KtorfitOptions,
classDataList: List<ClassData>,
): TypeSpec {
val classData = this

val implClassName = classData.implName
val implClassProperties =
classData.properties.map { property ->
propertySpec(property)
}

val implClassSpec =
createImplClassTypeSpec(
implClassName,
classData.implName,
classData,
implClassProperties,
classData.functions.map {
it.toFunSpec(
resolver,
ktorfitOptions.setQualifiedType
)
},
classDataList
}
)

return implClassSpec
Expand All @@ -57,8 +54,7 @@ private fun createImplClassTypeSpec(
implClassName: String,
classData: ClassData,
implClassProperties: List<PropertySpec>,
funSpecs: List<FunSpec>,
classDataList: List<ClassData>
funSpecs: List<FunSpec>
): TypeSpec {
val optInAnnotations =
classData.annotations.filter { it.shortName.getShortName() == "OptIn" }.map { it.toAnnotationSpec() }
Expand Down Expand Up @@ -130,7 +126,6 @@ private fun propertySpec(property: KSPropertyDeclaration): PropertySpec {
* For every know class of [classDataList], there will
* be a generated implementation for each interface that we can use.
* @param superClasses List of qualifiedNames of interface that a Ktorfit interface extends
* @param classDataList List of all know Ktorfit interfaces for the current compilation
*/
private fun TypeSpec.Builder.addKtorfitSuperInterface(superClasses: List<KSTypeReference>): TypeSpec.Builder {
(superClasses).forEach { superClassReference ->
Expand Down

0 comments on commit da7339c

Please sign in to comment.