Skip to content

Commit

Permalink
1.4.0 - RC2 - Fix compile safety
Browse files Browse the repository at this point in the history
revert

clean up

c
  • Loading branch information
arnaudgiuliani committed Jul 19, 2024
1 parent 30357ce commit 850ef35
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 37 deletions.
6 changes: 3 additions & 3 deletions examples/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# /!\ Koin in gradle.properties /!\

# Core
kotlin = "1.9.23"
kotlin = "1.9.24"
koin = "3.5.6"
koinAnnotations = "1.4.0-Alpha1"
ksp = "1.9.23-1.0.20"
koinAnnotations = "1.4.0-RC2"
ksp = "1.9.24-1.0.20"
junit = "4.13.2"
# Android
agp = "7.4.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.koin.core.annotation.Factory
import org.koin.core.annotation.Property
import org.koin.core.annotation.PropertyValue
import org.koin.core.annotation.Single
import org.koin.example.newmodule.ComponentWithProps.Companion.DEFAULT_ID

public interface ComponentInterface {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class TestModule {
koin.get<ComponentWithDefaultValues>()
koin.get<MyOtherComponent3F>()

//TODO Handle default prop
koin.get<ComponentWithProps>().let {
assertTrue { it.id == DEFAULT_ID }
}
Expand Down
2 changes: 1 addition & 1 deletion projects/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ org.gradle.parallel=true
#Kotlin
kotlin.code.style=official
#Koin
koinAnnotationsVersion=1.4.0-RC1
koinAnnotationsVersion=1.4.0-RC2
#Android
android.useAndroidX=true
androidMinSDK=14
Expand Down
4 changes: 2 additions & 2 deletions projects/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# /!\ Koin in gradle.properties /!\

# Core
kotlin = "1.9.23"
kotlin = "1.9.24"
koin = "3.5.6"
ksp = "1.9.23-1.0.20"
ksp = "1.9.24-1.0.20"
publish = "2.0.0"
dokka = "1.9.10"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.koin.compiler.scanner.KoinMetaDataScanner
import org.koin.compiler.verify.KoinConfigVerification

class BuilderProcessor(
codeGenerator: CodeGenerator,
private val codeGenerator: CodeGenerator,
private val logger: KSPLogger,
private val options: Map<String, String>
) : SymbolProcessor {
Expand All @@ -34,8 +34,7 @@ class BuilderProcessor(
private val koinConfigVerification = KoinConfigVerification(codeGenerator, logger)

override fun process(resolver: Resolver): List<KSAnnotated> {
logger.logging("Scanning symbols ...")

logger.logging("Scan symbols ...")

val invalidSymbols = koinMetaDataScanner.scanSymbols(resolver)
if (invalidSymbols.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private fun generateConstructor(constructorParameters: List<KoinMetaData.Definit

is KoinMetaData.DefinitionParameter.ParameterInject -> if (!isNullable) "params.get()" else "params.getOrNull()"
is KoinMetaData.DefinitionParameter.Property -> {
val defaultValue = if (ctorParam.defaultField != null) ",${ctorParam.defaultField}" else ""
val defaultValue = ctorParam.defaultValue?.let { ",${it.field}" } ?: ""
if (!isNullable) "getProperty(\"${ctorParam.value}\"$defaultValue)" else "getPropertyOrNull(\"${ctorParam.value}\",$defaultValue)"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ sealed class KoinMetaData {
override val name: String?,
val value: String? = null,
val isNullable: Boolean = false,
var defaultField: String? = null,
var defaultValue: PropertyValue? = null,
override val hasDefault: Boolean
) : DefinitionParameter(isNullable)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,22 @@ class KoinMetaDataScanner(
private val componentMetadataScanner = ClassComponentScanner(logger)
private val functionMetadataScanner = FunctionComponentScanner(logger)

private var validModuleSymbols = listOf<KSAnnotated>()
private var validDefinitionSymbols = listOf<KSAnnotated>()
private var defaultProperties = listOf<KSAnnotated>()
private var validModuleSymbols = mutableListOf<KSAnnotated>()
private var validDefinitionSymbols = mutableListOf<KSAnnotated>()
private var defaultProperties = mutableListOf<KSAnnotated>()
private var externalDefinitions = listOf<KSDeclaration>()

private val definitionAnnotationName = Definition::class.simpleName

@OptIn(KspExperimental::class)
fun scanSymbols(resolver: Resolver): List<KSAnnotated> {
val moduleSymbols = resolver.getSymbolsWithAnnotation(Module::class.qualifiedName!!).toList()
val definitionSymbols = DEFINITION_ANNOTATION_LIST_TYPES.flatMap { annotation -> resolver.getSymbolsWithAnnotation(annotation.qualifiedName!!) }
val definitionSymbols = DEFINITION_ANNOTATION_LIST_TYPES.flatMap { annotation ->
resolver.getSymbolsWithAnnotation(annotation.qualifiedName!!)
}

validModuleSymbols = moduleSymbols.filter { it.validate() }
validDefinitionSymbols = definitionSymbols.filter { it.validate() }
validModuleSymbols.addAll(moduleSymbols.filter { it.validate() })
validDefinitionSymbols.addAll(definitionSymbols.filter { it.validate() })

val invalidModuleSymbols = moduleSymbols.filter { !it.validate() }
val invalidDefinitionSymbols = definitionSymbols.filter { !it.validate() }
Expand All @@ -62,9 +64,8 @@ class KoinMetaDataScanner(
return invalidSymbols
}

defaultProperties = resolver.getSymbolsWithAnnotation(PropertyValue::class.qualifiedName!!).toList().filter { it.validate() }

logger.logging("All symbols are valid")
val propertyValueSymbols = resolver.getSymbolsWithAnnotation(PropertyValue::class.qualifiedName!!).toList()
defaultProperties.addAll(propertyValueSymbols.filter { it.validate() })

externalDefinitions = resolver.getDeclarationsFromPackage("org.koin.ksp.generated")
.filter { it.annotations.any { it.shortName.asString() == definitionAnnotationName } }
Expand Down Expand Up @@ -96,11 +97,7 @@ class KoinMetaDataScanner(
.let { a ->
val id = a.arguments.first().value?.toString()
val field = (a.parent as? KSDeclaration)
id?.let {
field?.qualifiedName?.asString()?.let {
KoinMetaData.PropertyValue(id = id , it)
}
}
id?.let { field?.qualifiedName?.asString()?.let { KoinMetaData.PropertyValue(id = id , it) } }
}
}

Expand All @@ -109,11 +106,13 @@ class KoinMetaDataScanner(
.flatMap { it.parameters }
.filterIsInstance<KoinMetaData.DefinitionParameter.Property>()

//attribute default values
propertyValues.forEach { pv -> allProperties
.filter { it.value == pv.id }
.forEach { it.defaultField = pv.field }
}
//associate default values
propertyValues
.forEach { propertyValue ->
allProperties.filter { it.value == propertyValue.id }.forEach {
it.defaultValue = propertyValue
}
}
}

private fun scanExternalDefinitions(index: List<KoinMetaData.Module>) {
Expand Down Expand Up @@ -198,7 +197,7 @@ class KoinMetaDataScanner(
val alreadyExists = foundModule.definitions.contains(definition)
if (!alreadyExists) {
if (foundModule == defaultModule) {
logger.warn("No module found for '$definitionPackage.${definition.label}'. Definition is added to 'defaultModule'")
logger.info("No module found for '$definitionPackage.${definition.label}'. Definition is added to 'defaultModule'")
}
foundModule.definitions.add(definition)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class KoinConfigVerification(val codeGenerator: CodeGenerator, val logger: KSPLo
moduleList: List<KoinMetaData.Module>,
resolver: Resolver
) {
val noGenFile = codeGenerator.generatedFile.isEmpty()
val alreadyDeclared = arrayListOf<String>()
val isAlreadyGenerated = codeGenerator.generatedFile.isEmpty()
val alreadyDeclaredTags = arrayListOf<String>()

moduleList
.flatMap { it.definitions }
.forEach { def ->
if (noGenFile) {
if (isAlreadyGenerated) {
def.parameters
.filterIsInstance<KoinMetaData.DefinitionParameter.Dependency>()
.forEach { param ->
Expand All @@ -52,7 +52,7 @@ class KoinConfigVerification(val codeGenerator: CodeGenerator, val logger: KSPLo
//TODO Check Cycle
}
} else {
writeDefinitionTag(def, alreadyDeclared)
writeDefinitionTag(def, alreadyDeclaredTags)
}
}
}
Expand Down

0 comments on commit 850ef35

Please sign in to comment.