All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.de.jensklingenberg.ktorfit.reqBuilderExtension.PartsCodeGeneration.kt Maven / Gradle / Ivy

The newest version!
package de.jensklingenberg.ktorfit.reqBuilderExtension

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(
    params: List,
    listType: KSType,
    arrayType: KSType,
): String {
    val partText =
        params.filter { it.hasAnnotation() }.joinToString("") { parameterData ->
            val part =
                parameterData.annotations
                    .filterIsInstance()
                    .firstOrNull() ?: throw IllegalStateException("Part annotation not found")
            val name = parameterData.name
            val partValue = part.value

            val starProj = parameterData.type.parameterType?.starProjection()
            val isList = starProj?.isAssignableFrom(listType) ?: false
            val isArray = starProj?.isAssignableFrom(arrayType) ?: false

            when {
                isList || isArray -> {
                    "$name?.filterNotNull()?.forEach { append(\"$partValue\", \"\${it}\") }\n"
                }

                else -> {
                    "$name?.let{ append(\"$partValue\", \"\${it}\") }\n"
                }
            }
        }

    val partMapStrings =
        params.filter { it.hasAnnotation() }.joinToString("") { parameterData ->
            "${parameterData.name}?.forEach { entry -> entry.value?.let{ append(entry.key, \"\${entry.value}\") } }\n"
        }

    return (partText + partMapStrings).surroundIfNotEmpty(
        "val ${formData.objectName} = formData {\n",
        "}\nsetBody(MultiPartFormDataContent(${formData.objectName}))\n",
    )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy