diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/generator/ClassGenerator.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/generator/ClassGenerator.kt index 45b5df4c5..a94d5e773 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/generator/ClassGenerator.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/generator/ClassGenerator.kt @@ -15,14 +15,14 @@ private const val WILDCARDIMPORT = "WILDCARDIMPORT" */ fun generateImplClass(classDataList: List, codeGenerator: CodeGenerator) { classDataList.forEach { classData -> - val file = getFileSpec(classData).toString().replace(WILDCARDIMPORT, "*") + val fileSource = getFileSpec(classData).toString().replace(WILDCARDIMPORT, "*") val packageName = classData.packageName val className = classData.name codeGenerator.createNewFile(Dependencies.ALL_FILES, packageName, "_${className}Impl", "kt").use { output -> OutputStreamWriter(output).use { writer -> - writer.write(file) + writer.write(fileSource) } } } diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/KtorfitError.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/KtorfitError.kt index c8760242a..a559e8d15 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/KtorfitError.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/model/KtorfitError.kt @@ -36,7 +36,9 @@ class KtorfitError { const val COULD_NOT_FIND_ANY_KTORFIT_ANNOTATIONS_IN_CLASS = "Could not find any Ktorfit annotations in class" fun MISSING_EITHER_KEYWORD_URL_OrURL_PARAMETER(keyword: String) = "Missing either @$keyword URL or @Url parameter" - + fun NO_KTORFIT_ANNOTATION_FOUND_AT(parameterName:String): String { + return "No Ktorfit Annotation found at $parameterName" + } fun MISSING_X_IN_RELATIVE_URL_PATH(keyword: String) = "Missing {${keyword}} in relative url path" const val JAVA_INTERFACES_ARE_NOT_SUPPORTED = "Java Interfaces are not supported" const val INTERNAL_INTERFACES_ARE_NOT_SUPPORTED = "internal Interfaces are not supported" diff --git a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ParameterParser.kt b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ParameterParser.kt index 14814a007..4f5f3a07a 100644 --- a/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ParameterParser.kt +++ b/ktorfit-ksp/src/main/kotlin/de/jensklingenberg/ktorfit/parser/ParameterParser.kt @@ -6,6 +6,7 @@ import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.FIELD_MAP_KEYS_MU import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.FIELD_MAP_PARAMETER_TYPE_MUST_BE_MAP import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.HEADER_MAP_KEYS_MUST_BE_OF_TYPE_STRING import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.HEADER_MAP_PARAMETER_TYPE_MUST_BE_MAP +import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.NO_KTORFIT_ANNOTATION_FOUND_AT import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.NULLABLE_PARAMETERS_ARE_NOT_SUPPORTED import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.PART_MAP_PARAMETER_TYPE_MUST_BE_MAP import de.jensklingenberg.ktorfit.model.KtorfitError.Companion.PART_PARAMETER_TYPE_MAY_NOT_BE_NULLABLE @@ -30,20 +31,22 @@ fun getParameterData(ksValueParameter: KSValueParameter, logger: KSPLogger): Par logger.ktorfitError(NULLABLE_PARAMETERS_ARE_NOT_SUPPORTED, ksValueParameter) } - val pararameterAnnotations = getParamAnnotationList(ksValueParameter, logger) + val parameterAnnotations = getParamAnnotationList(ksValueParameter, logger) val reqBuilderAnno = ksValueParameter.getRequestBuilderAnnotation() val parameterName = ksValueParameter.name?.asString() ?: "" val parameterType = ksValueParameter.type.resolve() val hasRequestBuilderAnno = reqBuilderAnno != null - if (pararameterAnnotations.isEmpty() && reqBuilderAnno == null) { + if (parameterAnnotations.isEmpty() && !hasRequestBuilderAnno) { logger.ktorfitError( - "No Ktorfit Annotation found at " + parameterName + " " + ksValueParameter.parent.toString(), + NO_KTORFIT_ANNOTATION_FOUND_AT(parameterName), ksValueParameter ) } + + if (hasRequestBuilderAnno && parameterType.resolveTypeName() != "[@kotlin.ExtensionFunctionType] Function1") { logger.ktorfitError( REQ_BUILDER_PARAMETER_TYPE_NEEDS_TO_BE_HTTP_REQUEST_BUILDER, @@ -63,27 +66,27 @@ fun getParameterData(ksValueParameter: KSValueParameter, logger: KSPLogger): Par ) } - return ParameterData(parameterName, type, pararameterAnnotations, hasRequestBuilderAnno) + return ParameterData(parameterName, type, parameterAnnotations, hasRequestBuilderAnno) } fun getParamAnnotationList(ksValueParameter: KSValueParameter, logger: KSPLogger): List { - val pararamAnnos = mutableListOf() + val paramAnnos = mutableListOf() ksValueParameter.getBodyAnnotation()?.let { - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getPathAnnotation()?.let { if (ksValueParameter.type.resolve().isMarkedNullable) { logger.ktorfitError(PATH_PARAMETER_TYPE_MAY_NOT_BE_NULLABLE, ksValueParameter.type) } - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getHeadersAnnotation()?.let { - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getHeaderMapAnnotation()?.let { @@ -95,15 +98,15 @@ fun getParamAnnotationList(ksValueParameter: KSValueParameter, logger: KSPLogger if (mapKey.type.toString() != "String" || mapKey.type?.resolve()?.isMarkedNullable == true) { logger.error(HEADER_MAP_KEYS_MUST_BE_OF_TYPE_STRING, ksValueParameter) } - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getQueryAnnotation()?.let { - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getQueryNameAnnotation()?.let { - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getQueryMapAnnotation()?.let { @@ -118,11 +121,11 @@ fun getParamAnnotationList(ksValueParameter: KSValueParameter, logger: KSPLogger if (ksValueParameter.type.resolve().isMarkedNullable) { logger.ktorfitError(QUERY_MAP_PARAMETER_TYPE_MAY_NOT_BE_NULLABLE, ksValueParameter.type) } - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getFieldAnnotation()?.let { - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getFieldMapAnnotation()?.let { @@ -134,27 +137,27 @@ fun getParamAnnotationList(ksValueParameter: KSValueParameter, logger: KSPLogger if (mapKey.type.toString() != "String" || mapKey.type?.resolve()?.isMarkedNullable == true) { logger.error(FIELD_MAP_KEYS_MUST_BE_OF_TYPE_STRING, ksValueParameter) } - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getPartAnnotation()?.let { if (ksValueParameter.type.resolve().isMarkedNullable) { logger.ktorfitError(PART_PARAMETER_TYPE_MAY_NOT_BE_NULLABLE, ksValueParameter.type) } - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getPartMapAnnotation()?.let { if (!ksValueParameter.type.toString().endsWith("Map")) { logger.ktorfitError(PART_MAP_PARAMETER_TYPE_MUST_BE_MAP, ksValueParameter) } - pararamAnnos.add(it) + paramAnnos.add(it) } ksValueParameter.getUrlAnnotation()?.let { - pararamAnnos.add(it) + paramAnnos.add(it) } - return pararamAnnos + return paramAnnos }