Skip to content

Commit

Permalink
1.2.3 - Add type verification graph
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Aug 31, 2023
1 parent 804401f commit e1d574f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
*/
package org.koin.compiler

import appendText
import com.google.devtools.ksp.processing.*
import com.google.devtools.ksp.symbol.KSAnnotated
import org.koin.compiler.generator.KoinGenerator
import org.koin.compiler.generator.getFile
import org.koin.compiler.metadata.KoinMetaData
import org.koin.compiler.scanner.KoinMetaDataScanner

Expand Down Expand Up @@ -48,6 +50,41 @@ class BuilderProcessor(
logger.logging("Generate code ...")
koinCodeGenerator.generateModules(moduleList, defaultModule)

//TODO Use argument here to activate this part
val genSize = koinCodeGenerator.codeGenerator.generatedFile.size

val ignored = listOf("kotlin.Lazy", "kotlin.Any")
(moduleList + defaultModule).map { module ->
module.definitions.forEach { def ->
val label = def.label
val cn = label.first().toString().toUpperCase() + label.takeLast(label.length - 1)
val file = koinCodeGenerator.codeGenerator.getFile(fileName = cn)
file.appendText("package org.koin.ksp.generated")
def.bindings.forEach { d ->
val cn = d.simpleName.asString()
file.appendText("\nclass KoinDef$cn")
}
if (genSize == 0) {
def.parameters.forEach { param ->
if (param is KoinMetaData.ConstructorParameter.Dependency) {
val p = param.type.declaration.qualifiedName?.asString()
// logger.warn("$label look at dependency => $p")
if (p !in ignored && p != null) {
val d =
resolver.getKSNameFromString("org.koin.ksp.generated.KoinDef" + param.type.declaration.simpleName.asString())
// logger.warn("ksn => ${d.asString()}")
val dc = resolver.getClassDeclarationByName(d)
if (dc != null) {
// logger.warn("ks => $it")
} else {
logger.error("$label need dependency type '$p', but is not found. Check your Koin configuration to add the right definition or type binding.")
}
}
}
}
}
}
}
return emptyList()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.koin.compiler.metadata

import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.Visibility
import java.util.*

Expand Down Expand Up @@ -123,7 +124,7 @@ sealed class KoinMetaData {
}

sealed class ConstructorParameter(val nullable: Boolean = false) {
data class Dependency(val value: String? = null, val isNullable: Boolean = false, val kind : DependencyKind = DependencyKind.Single) :
data class Dependency(val value: String? = null, val isNullable: Boolean = false, val type : KSType, val kind : DependencyKind = DependencyKind.Single) :
ConstructorParameter(isNullable)

data class ParameterInject(val isNullable: Boolean = false) : ConstructorParameter(isNullable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.koin.compiler.scanner

import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.symbol.*
import org.koin.compiler.generator.KoinGenerator
import org.koin.compiler.metadata.*

class ClassComponentScanner(
Expand Down Expand Up @@ -104,14 +105,16 @@ class ClassComponentScanner(
allBindings: List<KSDeclaration>,
isCreatedAtStart : Boolean? = null,
scope: KoinMetaData.Scope? = null,
) = KoinMetaData.Definition.ClassDefinition(
packageName = packageName,
qualifier = qualifier,
isCreatedAtStart = isCreatedAtStart,
className = className,
constructorParameters = ctorParams ?: emptyList(),
bindings = allBindings,
keyword = keyword,
scope = scope
)
): KoinMetaData.Definition.ClassDefinition {
return KoinMetaData.Definition.ClassDefinition(
packageName = packageName,
qualifier = qualifier,
isCreatedAtStart = isCreatedAtStart,
className = className,
constructorParameters = ctorParams ?: emptyList(),
bindings = allBindings,
keyword = keyword,
scope = scope
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.koin.compiler.scanner
import com.google.devtools.ksp.symbol.KSAnnotation
import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSFunctionDeclaration
import org.koin.compiler.generator.KoinGenerator.Companion.LOGGER
import org.koin.compiler.metadata.*

/**
Expand All @@ -38,7 +39,9 @@ abstract class FunctionScanner(
ksFunctionDeclaration: KSFunctionDeclaration,
annotations: Map<String, KSAnnotation> = emptyMap()
): KoinMetaData.Definition.FunctionDefinition? {
val allBindings = declaredBindings(annotation)?.let { if (!it.hasDefaultUnitValue()) it else emptyList() } ?: emptyList()
val foundBindings: List<KSDeclaration> = declaredBindings(annotation)?.let { if (!it.hasDefaultUnitValue()) it else emptyList() } ?: emptyList()
val returnedType: KSDeclaration? = ksFunctionDeclaration.returnType?.resolve()?.declaration
val allBindings: List<KSDeclaration> = returnedType?.let { foundBindings + it } ?: foundBindings
val functionParameters = ksFunctionDeclaration.parameters.getConstructorParameters()

return when (annotationName) {
Expand Down Expand Up @@ -97,15 +100,17 @@ abstract class FunctionScanner(
allBindings: List<KSDeclaration>,
isCreatedAtStart : Boolean? = null,
scope: KoinMetaData.Scope? = null,
) = KoinMetaData.Definition.FunctionDefinition(
packageName = packageName,
qualifier = qualifier,
isCreatedAtStart = isCreatedAtStart,
functionName = functionName,
parameters = parameters ?: emptyList(),
bindings = allBindings,
keyword = keyword,
scope = scope
).apply { isClassFunction = isModuleFunction }
): KoinMetaData.Definition.FunctionDefinition {
return KoinMetaData.Definition.FunctionDefinition(
packageName = packageName,
qualifier = qualifier,
isCreatedAtStart = isCreatedAtStart,
functionName = functionName,
parameters = parameters ?: emptyList(),
bindings = allBindings,
keyword = keyword,
scope = scope
).apply { isClassFunction = isModuleFunction }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.koin.compiler.scanner

import com.google.devtools.ksp.symbol.*
import org.koin.compiler.generator.KoinGenerator.Companion.LOGGER
import org.koin.compiler.metadata.KoinMetaData
import org.koin.compiler.metadata.isScopeAnnotation
import org.koin.compiler.metadata.isValidAnnotation
Expand Down Expand Up @@ -73,21 +74,22 @@ private fun getConstructorParameter(param: KSValueParameter): KoinMetaData.Const
val firstAnnotation = param.annotations.firstOrNull()
val annotationName = firstAnnotation?.shortName?.asString()
val annotationValue = firstAnnotation?.arguments?.getValueArgument()
val resolvedType = param.type.resolve()
val resolvedType: KSType = param.type.resolve()
val isNullable = resolvedType.isMarkedNullable
val resolvedTypeString = resolvedType.toString()

//TODO check to see better way to detect this
val isList = resolvedTypeString.startsWith("List<")
val isLazy = resolvedTypeString.startsWith("Lazy<")

return when (annotationName) {
"${InjectedParam::class.simpleName}" -> KoinMetaData.ConstructorParameter.ParameterInject(isNullable)
"${Property::class.simpleName}" -> KoinMetaData.ConstructorParameter.Property(annotationValue, isNullable)
"${Named::class.simpleName}" -> KoinMetaData.ConstructorParameter.Dependency(annotationValue, isNullable)
"${Named::class.simpleName}" -> KoinMetaData.ConstructorParameter.Dependency(annotationValue, isNullable, type = resolvedType)
else -> {
if (isList) KoinMetaData.ConstructorParameter.Dependency(kind = KoinMetaData.DependencyKind.List)
else if (isLazy) KoinMetaData.ConstructorParameter.Dependency(isNullable = isNullable, kind = KoinMetaData.DependencyKind.Lazy)
else KoinMetaData.ConstructorParameter.Dependency(isNullable = isNullable)
if (isList) KoinMetaData.ConstructorParameter.Dependency(kind = KoinMetaData.DependencyKind.List, type = resolvedType)
else if (isLazy) KoinMetaData.ConstructorParameter.Dependency(isNullable = isNullable, kind = KoinMetaData.DependencyKind.Lazy, type = resolvedType)
else KoinMetaData.ConstructorParameter.Dependency(isNullable = isNullable, type = resolvedType)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sandbox/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kotlin.code.style=official
kotlinVersion=1.8.21
koinVersion=3.4.1
koinAndroidVersion=3.4.1
koinKspVersion=1.2.2
koinKspVersion=1.2.3
kspVersion=1.8.21-1.0.11
androidGradleVersion=4.3.1
appcompatVersion=1.6.1
Expand Down

0 comments on commit e1d574f

Please sign in to comment.