Skip to content

Commit

Permalink
Merge pull request #1993 from GrzegorzBobryk/maintenance/replace-list…
Browse files Browse the repository at this point in the history
…of-with-emptylist

[Maintenance] replace `listOf()` with `emptyList()`
  • Loading branch information
arnaudgiuliani authored Nov 15, 2024
2 parents 8ed33be + 5094647 commit 1fd7c85
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import kotlin.reflect.KClass
* @param extraTypes - allow to declare extra type, to be bound above the existing definitions
* @throws MissingKoinDefinitionException
*/
fun Module.verify(extraTypes: List<KClass<*>> = listOf(), injections: List<ParameterTypeInjection>? = null) {
org.koin.test.verify.Verify.verify(this,extraTypes + androidTypes, injections)
fun Module.verify(extraTypes: List<KClass<*>> = emptyList(), injections: List<ParameterTypeInjection>? = null) {
org.koin.test.verify.Verify.verify(this, extraTypes + androidTypes, injections)
}

/**
Expand All @@ -36,8 +36,8 @@ fun Module.verify(extraTypes: List<KClass<*>> = listOf(), injections: List<Param
* @param extraTypes - allow to declare extra type, to be bound above the existing definitions
* @throws MissingKoinDefinitionException
*/
fun Module.androidVerify(extraTypes: List<KClass<*>> = listOf(), injections: List<ParameterTypeInjection>? = null) {
org.koin.test.verify.Verify.verify(this,extraTypes + androidTypes, injections)
fun Module.androidVerify(extraTypes: List<KClass<*>> = emptyList(), injections: List<ParameterTypeInjection>? = null) {
org.koin.test.verify.Verify.verify(this, extraTypes + androidTypes, injections)
}

object AndroidVerify {
Expand All @@ -49,4 +49,4 @@ object AndroidVerify {
SavedStateHandle::class,
WorkerParameters::class
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BeanDefinition<T>(
var qualifier: Qualifier? = null,
val definition: Definition<T>,
val kind: Kind,
var secondaryTypes: List<KClass<*>> = listOf(),
var secondaryTypes: List<KClass<*>> = emptyList(),
) {
var callbacks: Callbacks<T> = Callbacks()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:
private val extraKeys: List<String> = (extraTypes + Verify.whiteList).map { it.getFullName() }
internal val definitionIndex: List<IndexKey> = allModules.flatMap { it.mappings.keys.toList() }
private val verifiedFactories: HashMap<InstanceFactory<*>, List<KClass<*>>> = hashMapOf()
private val parameterInjectionIndex : Map<String, List<String>> = injections?.associate { inj -> inj.targetType.getFullName() to inj.injectedTypes.map { it.getFullName() }.toList() } ?: emptyMap()
private val parameterInjectionIndex: Map<String, List<String>> = injections?.associate { inj -> inj.targetType.getFullName() to inj.injectedTypes.map { it.getFullName() }.toList() }.orEmpty()

fun verify() {
factories.forEach { factory ->
Expand All @@ -55,7 +55,7 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:
): List<KClass<*>> {
val beanDefinition = factory.beanDefinition
println("\n|-> definition $beanDefinition")
if (beanDefinition.qualifier != null){
if (beanDefinition.qualifier != null) {
println("| qualifier: ${beanDefinition.qualifier}")
}

Expand All @@ -71,26 +71,26 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:
constructor,
functionType,
index
)
}
)
}
val verificationByStatus = verifications.groupBy { it.status }
verificationByStatus[VerificationStatus.MISSING]?.let { list ->
val first = list.first()
val errorMessage = "Missing definition for '$first' in definition '$beanDefinition'."
val generateParameterInjection = "- Fix your Koin configuration -\n${generateFixDefinition(first)}\n${generateInjectionCode(beanDefinition,first)}"
System.err.println("* ----- > $errorMessage\n$generateParameterInjection")
throw MissingKoinDefinitionException(errorMessage)
val first = list.first()
val errorMessage = "Missing definition for '$first' in definition '$beanDefinition'."
val generateParameterInjection = "- Fix your Koin configuration -\n${generateFixDefinition(first)}\n${generateInjectionCode(beanDefinition, first)}"
System.err.println("* ----- > $errorMessage\n$generateParameterInjection")
throw MissingKoinDefinitionException(errorMessage)
}
verificationByStatus[VerificationStatus.CIRCULAR]?.let { list ->
val errorMessage = "Circular injection between ${list.first()} and '${functionType.qualifiedName}'.\nFix your Koin configuration!"
System.err.println("* ----- > $errorMessage")
throw CircularInjectionException(errorMessage)
val errorMessage = "Circular injection between ${list.first()} and '${functionType.qualifiedName}'.\nFix your Koin configuration!"
System.err.println("* ----- > $errorMessage")
throw CircularInjectionException(errorMessage)
}

return verificationByStatus[VerificationStatus.OK]?.map {
println("|- dependency '${it.name}' - ${it.type.qualifiedName} found!")
it.type
} ?: emptyList()
}.orEmpty()
}

private fun generateFixDefinition(first: VerifiedParameter): String {
Expand Down Expand Up @@ -120,7 +120,7 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:
): List<VerifiedParameter> {
val constructorParameters = constructorFunction.parameters

if (constructorParameters.isEmpty()){
if (constructorParameters.isEmpty()) {
println("| no dependency to check")
} else {
println("| ${constructorParameters.size} dependencies to check")
Expand All @@ -133,11 +133,11 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:

val hasDefinition = isClassInDefinitionIndex(index, ctorParamFullClassName)
val isParameterInjected = isClassInInjectionIndex(functionType, ctorParamFullClassName)
if (isParameterInjected){
if (isParameterInjected) {
println("| dependency '$ctorParamLabel' is injected")
}
val isWhiteList = ctorParamFullClassName in extraKeys
if (isWhiteList){
if (isWhiteList) {
println("| dependency '$ctorParamLabel' is whitelisted")
}
val isDefinitionDeclared = hasDefinition || isParameterInjected || isWhiteList
Expand All @@ -148,16 +148,16 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:

//TODO refactor to attach type / case of error
when {
!isDefinitionDeclared -> VerifiedParameter(ctorParamLabel,ctorParamClass,VerificationStatus.MISSING)
isCircular -> VerifiedParameter(ctorParamLabel,ctorParamClass,VerificationStatus.CIRCULAR)
else -> VerifiedParameter(ctorParamLabel,ctorParamClass,VerificationStatus.OK)
!isDefinitionDeclared -> VerifiedParameter(ctorParamLabel, ctorParamClass, VerificationStatus.MISSING)
isCircular -> VerifiedParameter(ctorParamLabel, ctorParamClass, VerificationStatus.CIRCULAR)
else -> VerifiedParameter(ctorParamLabel, ctorParamClass, VerificationStatus.OK)
}
}
}

private fun isClassInInjectionIndex(
classOrigin: KClass<*>,
ctorParamFullClassName: String
ctorParamFullClassName: String,
): Boolean {
return parameterInjectionIndex[classOrigin.getFullName()]?.let { ctorParamFullClassName in it } ?: false
}
Expand All @@ -166,10 +166,10 @@ class Verification(val module: Module, extraTypes: List<KClass<*>>, injections:
index.any { k -> k.contains(ctorParamFullClassName) }
}

data class VerifiedParameter(val name : String, val type : KClass<*>, val status: VerificationStatus){
data class VerifiedParameter(val name: String, val type: KClass<*>, val status: VerificationStatus) {
override fun toString(): String = "[field:'$name' - type:'${type.qualifiedName}']"
}

enum class VerificationStatus {
OK, MISSING, CIRCULAR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import kotlin.time.measureTime
* @param injections - Experimental - defines parameters injection types to allow (normally used in parametersOf)
* @throws MissingKoinDefinitionException
*/
fun Module.verify(extraTypes: List<KClass<*>> = listOf(), injections: List<ParameterTypeInjection>? = emptyList()) = Verify.verify(this, extraTypes,injections)
fun Module.verify(extraTypes: List<KClass<*>> = emptyList(), injections: List<ParameterTypeInjection>? = emptyList()) = Verify.verify(this, extraTypes, injections)

/**
* Verify a list of Modules
*
* @see Module.verify
*/
fun List<Module>.verifyAll(extraTypes: List<KClass<*>> = listOf(), injections: List<ParameterTypeInjection>? = emptyList()) {
forEach { module -> module.verify(extraTypes,injections) }
fun List<Module>.verifyAll(extraTypes: List<KClass<*>> = emptyList(), injections: List<ParameterTypeInjection>? = emptyList()) {
forEach { module -> module.verify(extraTypes, injections) }
}

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ object Verify {
* @param extraTypes allow to declare extra type, to be bound above the existing definitions
* @throws MissingKoinDefinitionException
*/
fun verify(module: Module, extraTypes: List<KClass<*>> = listOf(), injections: List<ParameterTypeInjection>? = null) {
fun verify(module: Module, extraTypes: List<KClass<*>> = emptyList(), injections: List<ParameterTypeInjection>? = null) {

val verification = Verification(module, extraTypes, injections)
println("Verifying module '$module' ...")
Expand Down

0 comments on commit 1fd7c85

Please sign in to comment.