Skip to content

Commit

Permalink
docs(KSP): improve method documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Foso committed Sep 9, 2022
1 parent 45e5b6c commit 194854a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ private const val WILDCARDIMPORT = "WILDCARDIMPORT"
*/
fun generateImplClass(classDataList: List<ClassData>, 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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<HttpRequestBuilder, Unit>") {
logger.ktorfitError(
REQ_BUILDER_PARAMETER_TYPE_NEEDS_TO_BE_HTTP_REQUEST_BUILDER,
Expand All @@ -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<ParameterAnnotation> {

val pararamAnnos = mutableListOf<ParameterAnnotation>()
val paramAnnos = mutableListOf<ParameterAnnotation>()
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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}


Expand Down

0 comments on commit 194854a

Please sign in to comment.