Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Aug 26, 2024
1 parent 8309551 commit eba6a4a
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package de.jensklingenberg.ktorfit
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.KSNode

class KtorfitLogger(private val kspLogger: KSPLogger, private val loggingType: Int) : KSPLogger by kspLogger {
class KtorfitLogger(
private val kspLogger: KSPLogger,
private val loggingType: Int
) : KSPLogger by kspLogger {
override fun error(
message: String,
symbol: KSNode?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.jensklingenberg.ktorfit

class KtorfitOptions(options: Map<String, String>) {
class KtorfitOptions(
options: Map<String, String>
) {
/**
* 0: Turn off all Ktorfit related error checking
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import de.jensklingenberg.ktorfit.http.PUT
import de.jensklingenberg.ktorfit.model.toClassData

class KtorfitProcessorProvider : SymbolProcessorProvider {
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor {
return KtorfitProcessor(environment, KtorfitOptions(environment.options))
}
override fun create(environment: SymbolProcessorEnvironment): SymbolProcessor =
KtorfitProcessor(environment, KtorfitOptions(environment.options))
}

class KtorfitProcessor(private val env: SymbolProcessorEnvironment, private val ktorfitOptions: KtorfitOptions) :
SymbolProcessor {
class KtorfitProcessor(
private val env: SymbolProcessorEnvironment,
private val ktorfitOptions: KtorfitOptions
) : SymbolProcessor {
private var invoked = false

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ fun KSFunctionDeclaration.toFunctionData(logger: KSPLogger): FunctionData {
val functionName = funcDeclaration.simpleName.asString()
val functionParameters = funcDeclaration.parameters.map { it.createParameterData(logger) }

val resolvedReturnType = funcDeclaration.returnType?.resolve() ?: throw IllegalStateException("Return type not found")
val resolvedReturnType =
funcDeclaration.returnType?.resolve() ?: throw IllegalStateException("Return type not found")

val returnType =
ReturnTypeData(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.jensklingenberg.ktorfit.model.annotations

enum class HttpMethod(val keyword: String) {
enum class HttpMethod(
val keyword: String
) {
GET("GET"),
POST("POST"),
PUT("PUT"),
Expand All @@ -15,7 +17,9 @@ enum class HttpMethod(val keyword: String) {
*/
open class FunctionAnnotation

class Headers(val value: List<String>) : FunctionAnnotation()
class Headers(
val value: List<String>
) : FunctionAnnotation()

class FormUrlEncoded : FunctionAnnotation()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ fun getAttributeCode(parameterDataList: List<ParameterData>): String =
parameterDataList
.filter { it.hasAnnotation<ParameterAnnotation.Tag>() }
.joinToString("\n") {
val tag = it.findAnnotationOrNull<ParameterAnnotation.Tag>() ?: throw IllegalStateException("Tag annotation not found")
val tag =
it.findAnnotationOrNull<ParameterAnnotation.Tag>()
?: throw IllegalStateException("Tag annotation not found")
if (it.type.parameterType.isMarkedNullable) {
"${it.name}?.let{ attributes.put(io.ktor.util.AttributeKey(\"${tag.value}\"), it) }"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package de.jensklingenberg.ktorfit.reqBuilderExtension
import de.jensklingenberg.ktorfit.model.ParameterData
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation.Body

fun getBodyDataText(params: List<ParameterData>): String {
return params.firstOrNull { it.hasAnnotation<Body>() }?.name?.let { "setBody($it)" }.orEmpty()
}
fun getBodyDataText(params: List<ParameterData>): String =
params
.firstOrNull {
it.hasAnnotation<Body>()
}?.name
?.let { "setBody($it)" }
.orEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package de.jensklingenberg.ktorfit.reqBuilderExtension
import de.jensklingenberg.ktorfit.model.ParameterData
import de.jensklingenberg.ktorfit.model.annotations.ParameterAnnotation

fun getCustomRequestBuilderText(parameterDataList: List<ParameterData>): String {
return parameterDataList.find { it.hasAnnotation<ParameterAnnotation.RequestBuilder>() }?.let {
it.name + "(this)"
}.orEmpty()
}
fun getCustomRequestBuilderText(parameterDataList: List<ParameterData>): String =
parameterDataList
.find { it.hasAnnotation<ParameterAnnotation.RequestBuilder>() }
?.let {
it.name + "(this)"
}.orEmpty()
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ fun getUrlCode(
): String {
var urlPath =
methodAnnotation.path.ifEmpty {
params.firstOrNull { it.hasAnnotation<Url>() }?.let {
"\${${it.name}}"
}.orEmpty()
params
.firstOrNull { it.hasAnnotation<Url>() }
?.let {
"\${${it.name}}"
}.orEmpty()
}

val baseUrl =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ fun KSValueParameter.getRequestTypeAnnotation(): RequestType? {
val filteredAnnotations =
this.annotations.filter {
it.shortName.getShortName() == requestTypeClazz.simpleName &&
it.annotationType.resolve().declaration.qualifiedName?.asString() == requestTypeClazz.qualifiedName
it.annotationType
.resolve()
.declaration.qualifiedName
?.asString() == requestTypeClazz.qualifiedName
}
return filteredAnnotations.mapNotNull {
it.arguments.map { arg ->
RequestType((arg.value as KSType))
return filteredAnnotations
.mapNotNull {
it.arguments
.map { arg ->
RequestType((arg.value as KSType))
}.firstOrNull()
}.firstOrNull()
}.firstOrNull()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,18 @@ fun KSType?.resolveTypeName(): String {
return this.toString().removePrefix("[typealias ").removeSuffix("]")
}

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

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

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

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

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

fun FileSpec.Builder.addImports(imports: List<String>): FileSpec.Builder {
imports.forEach {
Expand All @@ -47,10 +37,6 @@ fun FileSpec.Builder.addImports(imports: List<String>): FileSpec.Builder {
return this
}

inline fun <reified T> List<*>.anyInstance(): Boolean {
return this.filterIsInstance<T>().isNotEmpty()
}
inline fun <reified T> List<*>.anyInstance(): Boolean = this.filterIsInstance<T>().isNotEmpty()

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

0 comments on commit eba6a4a

Please sign in to comment.