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

main.de.jensklingenberg.ktorfit.utils.Utils.kt Maven / Gradle / Ivy

There is a newer version: 2.2.0-1.0.28
Show newest version
package de.jensklingenberg.ktorfit.utils

import com.google.devtools.ksp.symbol.KSName
import com.google.devtools.ksp.symbol.KSType
import com.squareup.kotlinpoet.FileSpec
import de.jensklingenberg.ktorfit.model.FunctionData

fun KSType?.resolveTypeName(): String {
    // TODO: Find better way to handle type alias Types
    return this.toString().removePrefix("[typealias ").removeSuffix("]")
}

inline fun  FunctionData.findAnnotationOrNull(): T? = this.annotations.firstOrNull { it is T } as? T

fun String.prefixIfNotEmpty(s: String): String = (s + this).takeIf { this.isNotEmpty() } ?: this

fun String.postfixIfNotEmpty(s: String): String = (this + s).takeIf { this.isNotEmpty() } ?: this

fun String.surroundIfNotEmpty(
    prefix: String = "",
    postFix: String = "",
): String = this.prefixIfNotEmpty(prefix).postfixIfNotEmpty(postFix)

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

fun FileSpec.Builder.addImports(imports: List): FileSpec.Builder {
    imports.forEach {
        /**
         * Wildcard imports are not allowed by KotlinPoet, as a workaround * is replaced with WILDCARDIMPORT, and it will be replaced again
         * after Kotlin Poet generated the source code
         */
        val packageName = it.substringBeforeLast(".")
        val className = it.substringAfterLast(".")

        this.addImport(packageName, className)
    }
    return this
}

inline fun  List<*>.anyInstance(): Boolean = this.filterIsInstance().isNotEmpty()

fun KSName?.safeString(): String = this?.asString() ?: ""




© 2015 - 2024 Weber Informatics LLC | Privacy Policy