Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Sep 9, 2024
1 parent 4f192e6 commit 22183d3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ val providerClass = KtorfitClass("ClassProvider", "de.jensklingenberg.ktorfit.in
val typeDataClass = KtorfitClass("TypeData", "de.jensklingenberg.ktorfit.converter", "_typeData")
val extDataClass = KtorfitClass("HttpRequestBuilder.() -> Unit", "", "_ext")
val formParameters = KtorfitClass("", "", "__formParameters")
val formData = KtorfitClass("", "", "__formData")
val converterHelper = KtorfitClass("KtorfitConverterHelper", "de.jensklingenberg.ktorfit.internal", "_helper")
val internalApi = ClassName("de.jensklingenberg.ktorfit.internal", "InternalKtorfitApi")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.devtools.ksp.symbol.KSType
import de.jensklingenberg.ktorfit.model.ParameterData
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Part
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.PartMap
import de.jensklingenberg.ktorfit.model.formData
import de.jensklingenberg.ktorfit.utils.surroundIfNotEmpty

fun getPartsCode(
Expand Down Expand Up @@ -41,7 +42,7 @@ fun getPartsCode(
}

return (partText + partMapStrings).surroundIfNotEmpty(
"val __formData = formData {\n",
"}\nsetBody(MultiPartFormDataContent(__formData))\n",
"val ${formData.objectName} = formData {\n",
"}\nsetBody(MultiPartFormDataContent(${formData.objectName}))\n",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ private fun getQueryMapText(params: List<ParameterData>) =
params
.filter { it.hasAnnotation<QueryMap>() }
.joinToString(separator = "") { parameterData ->
val queryMap = parameterData.findAnnotationOrNull<QueryMap>()!!
val queryMap =
parameterData.findAnnotationOrNull<QueryMap>()
?: throw IllegalStateException("QueryMap annotation not found")
val encoded = queryMap.encoded
val data = parameterData.name
"%s?.forEach { entry -> entry.value?.let{ %s(entry.key, \"\${entry.value}\") } }\n".format(
Expand All @@ -44,7 +46,9 @@ private fun getQueryNameText(
) = params
.filter { it.hasAnnotation<QueryName>() }
.joinToString(separator = "") { parameterData ->
val queryName = parameterData.annotations.filterIsInstance<QueryName>().first()
val queryName =
parameterData.annotations.filterIsInstance<QueryName>().firstOrNull()
?: throw IllegalStateException("QueryName annotation not found")
val encoded = queryName.encoded
val name = parameterData.name

Expand Down

0 comments on commit 22183d3

Please sign in to comment.