Skip to content

Commit

Permalink
fix: custom qualifier annotations not generating qualifier at injecti…
Browse files Browse the repository at this point in the history
…on point
  • Loading branch information
skyecodes authored and arnaudgiuliani committed Jan 27, 2025
1 parent b686049 commit 8c01fd6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class Cat : Animal

public class Bunny(public val color: String) : Animal

@Single
public class Farm(@WhiteBunny public val whiteBunny: Bunny, @BlackBunny public val blackBunny: Bunny)

@Named
public annotation class WhiteBunny

Expand All @@ -22,7 +25,6 @@ public annotation class BlackBunny
@Module
@ComponentScan
public class AnimalModule {

@Factory
public fun animal(cat: Cat, dog: Dog): Animal = if (randomBoolean()) cat else dog

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class TestModule {
}

assertEquals("White", koin.get<Bunny>(qualifier<WhiteBunny>()).color)

val farm = koin.get<Farm>()
assertEquals("White", farm.whiteBunny.color)
assertEquals("Black", farm.blackBunny.color)
}

private fun randomGetAnimal(koin: Koin): Animal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,11 @@ fun List<KSValueArgument>.getQualifier(): KoinMetaData.Qualifier {
?: error("Qualifier annotation needs parameters: either type value or name")
}

private val qualifierAnnotations = listOf("Named", "Qualifier")
private val qualifierAnnotations = listOf(Named::class.simpleName, Qualifier::class.simpleName)
fun KSAnnotated.getQualifier(): String? {
val qualifierAnnotation = annotations.firstOrNull { a ->
val annotationName = a.shortName.asString()
if (annotationName in qualifierAnnotations) true
else (a.annotationType.resolve().declaration as KSClassDeclaration).annotations.any { a2 ->
a2.shortName.asString() in qualifierAnnotations
}
annotationName in qualifierAnnotations || a.annotationType.resolve().isCustomQualifierAnnotation()
}
return qualifierAnnotation?.let {
when(it.shortName.asString()){
Expand Down Expand Up @@ -127,16 +124,25 @@ private fun getParameter(param: KSValueParameter): KoinMetaData.DefinitionParame
}
//TODO type value for ScopeId
else -> {
val kind = when {
isList -> KoinMetaData.DependencyKind.List
isLazy -> KoinMetaData.DependencyKind.Lazy
else -> KoinMetaData.DependencyKind.Single
val annotationType = firstAnnotation?.annotationType?.resolve()
if (annotationType != null && annotationType.isCustomQualifierAnnotation()) {
KoinMetaData.DefinitionParameter.Dependency(name = paramName, qualifier = annotationType.declaration.qualifiedName?.asString(), isNullable = isNullable, hasDefault = hasDefault, type = resolvedType, alreadyProvided = hasProvidedAnnotation(param))
} else {
val kind = when {
isList -> KoinMetaData.DependencyKind.List
isLazy -> KoinMetaData.DependencyKind.Lazy
else -> KoinMetaData.DependencyKind.Single
}
KoinMetaData.DefinitionParameter.Dependency(name = paramName, hasDefault = hasDefault, kind = kind, isNullable = isNullable, type = resolvedType, alreadyProvided = hasProvidedAnnotation(param))
}
KoinMetaData.DefinitionParameter.Dependency(name = paramName, hasDefault = hasDefault, kind = kind, isNullable = isNullable, type = resolvedType, alreadyProvided = hasProvidedAnnotation(param))
}
}
}

private fun KSType.isCustomQualifierAnnotation(): Boolean {
return (declaration as KSClassDeclaration).annotations.any { it.shortName.asString() in qualifierAnnotations }
}

internal fun List<KSValueArgument>.getValueArgument(): String? {
return firstOrNull { a -> a.name?.asString() == "value" }?.value as? String?
}
Expand Down

0 comments on commit 8c01fd6

Please sign in to comment.