Skip to content

Commit

Permalink
Fix #1463 to allow several bindings of the same type
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Dec 13, 2022
1 parent 32a10db commit 9affdc3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
Expand Up @@ -134,10 +134,7 @@ class Module(
}

@PublishedApi
internal fun saveMapping(mapping: IndexKey, factory: InstanceFactory<*>, allowOverride: Boolean = false) {
if (!allowOverride && mappings.contains(mapping)) {
overrideError(factory, mapping)
}
internal fun saveMapping(mapping: IndexKey, factory: InstanceFactory<*>) {
mappings[mapping] = factory
}

Expand Down
Expand Up @@ -37,7 +37,7 @@ import kotlin.reflect.KClass
infix fun <S : Any> KoinDefinition<out S>.bind(clazz: KClass<S>): KoinDefinition<out S> {
second.beanDefinition.secondaryTypes = second.beanDefinition.secondaryTypes + clazz
val mapping = indexKey(clazz,second.beanDefinition.qualifier,second.beanDefinition.scopeQualifier)
first.saveMapping(mapping,second,allowOverride = true)
first.saveMapping(mapping,second)
return this
}

Expand All @@ -62,7 +62,7 @@ infix fun KoinDefinition<*>.binds(classes: Array<KClass<*>>): KoinDefinition<*>
second.beanDefinition.secondaryTypes += classes
classes.forEach { clazz ->
val mapping = indexKey(clazz,second.beanDefinition.qualifier,second.beanDefinition.scopeQualifier)
first.saveMapping(mapping,second,allowOverride = true)
first.saveMapping(mapping,second)
}
return this
}
Expand Down
Expand Up @@ -156,7 +156,6 @@ class AdditionalTypeBindingTest {
// }

@Test
@Ignore
fun `additional type conflict`() {
val koin = koinApplication {
printLogger(Level.DEBUG)
Expand All @@ -167,8 +166,8 @@ class AdditionalTypeBindingTest {
})
}.koin

assertTrue(koin.getAll<Simple.ComponentInterface1>().size == 2)
assertTrue(koin.get<Simple.ComponentInterface1>() is Simple.Component1)
assertTrue(koin.getAll<Simple.ComponentInterface1>().size == 1)
assertTrue(koin.get<Simple.ComponentInterface1>() is Simple.Component2)
}

@Test
Expand All @@ -188,7 +187,6 @@ class AdditionalTypeBindingTest {
}

@Test
@Ignore
fun `additional types`() {
val koin = koinApplication {
printLogger(Level.DEBUG)
Expand Down
@@ -0,0 +1,41 @@
package org.koin.dsl

import org.koin.Simple
import org.koin.core.logger.Level
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.singleOf
import org.koin.core.qualifier.named
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

class ConstructorDSLTest {

@Test
fun test_reified_type_cosntructor(){
val koin = koinApplication {
modules(module {
singleOf<IClassA>(::ClassA)
})
}.koin
assertNotNull(koin.getOrNull<IClassA>())
assertNull(koin.getOrNull<ClassA>())
}

@Test
fun test_allow_extra_binds(){

val koin = koinApplication {
printLogger(Level.DEBUG)
modules(module {
singleOf(::ClassA) { bind<IClassA>()}
singleOf(::ClassA2) { bind<IClassA>()}
})
}.koin

assertNotNull(koin.getOrNull<IClassA>() is ClassA2)
assertNotNull(koin.getOrNull<ClassA>())
assertNotNull(koin.getOrNull<ClassA2>())
}
}
Expand Up @@ -13,16 +13,11 @@ class ModuleDeclarationRulesTest {

@Test
fun `don't allow redeclaration`() {
try {
koinApplication {
modules(module {
single { Simple.ComponentA() }
single { Simple.ComponentA() }
})
}
fail("should not redeclare")
} catch (e: DefinitionOverrideException) {
e.printStackTrace()
koinApplication {
modules(module {
single { Simple.ComponentA() }
single { Simple.ComponentA() }
})
}
}

Expand Down

0 comments on commit 9affdc3

Please sign in to comment.