From 9affdc3181b68b04acfc55900d1545c9ac577903 Mon Sep 17 00:00:00 2001 From: Arnaud Giuliani Date: Tue, 13 Dec 2022 11:11:40 +0100 Subject: [PATCH] Fix #1463 to allow several bindings of the same type --- .../kotlin/org/koin/core/module/Module.kt | 5 +-- .../kotlin/org/koin/dsl/DefinitionBinding.kt | 4 +- .../org/koin/dsl/AdditionalTypeBindingTest.kt | 6 +-- .../kotlin/org/koin/dsl/ConstructorDSLTest.kt | 41 +++++++++++++++++++ .../koin/dsl/ModuleDeclarationRulesTest.kt | 15 +++---- 5 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 core/koin-core/src/commonTest/kotlin/org/koin/dsl/ConstructorDSLTest.kt diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt index ffd619193..ff7afcc47 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/core/module/Module.kt @@ -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 } diff --git a/core/koin-core/src/commonMain/kotlin/org/koin/dsl/DefinitionBinding.kt b/core/koin-core/src/commonMain/kotlin/org/koin/dsl/DefinitionBinding.kt index cf2e00f96..688d872fe 100644 --- a/core/koin-core/src/commonMain/kotlin/org/koin/dsl/DefinitionBinding.kt +++ b/core/koin-core/src/commonMain/kotlin/org/koin/dsl/DefinitionBinding.kt @@ -37,7 +37,7 @@ import kotlin.reflect.KClass infix fun KoinDefinition.bind(clazz: KClass): KoinDefinition { 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 } @@ -62,7 +62,7 @@ infix fun KoinDefinition<*>.binds(classes: Array>): 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 } diff --git a/core/koin-core/src/commonTest/kotlin/org/koin/dsl/AdditionalTypeBindingTest.kt b/core/koin-core/src/commonTest/kotlin/org/koin/dsl/AdditionalTypeBindingTest.kt index 957dcdbc8..d20afe54c 100644 --- a/core/koin-core/src/commonTest/kotlin/org/koin/dsl/AdditionalTypeBindingTest.kt +++ b/core/koin-core/src/commonTest/kotlin/org/koin/dsl/AdditionalTypeBindingTest.kt @@ -156,7 +156,6 @@ class AdditionalTypeBindingTest { // } @Test - @Ignore fun `additional type conflict`() { val koin = koinApplication { printLogger(Level.DEBUG) @@ -167,8 +166,8 @@ class AdditionalTypeBindingTest { }) }.koin - assertTrue(koin.getAll().size == 2) - assertTrue(koin.get() is Simple.Component1) + assertTrue(koin.getAll().size == 1) + assertTrue(koin.get() is Simple.Component2) } @Test @@ -188,7 +187,6 @@ class AdditionalTypeBindingTest { } @Test - @Ignore fun `additional types`() { val koin = koinApplication { printLogger(Level.DEBUG) diff --git a/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ConstructorDSLTest.kt b/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ConstructorDSLTest.kt new file mode 100644 index 000000000..f78f16cb7 --- /dev/null +++ b/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ConstructorDSLTest.kt @@ -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(::ClassA) + }) + }.koin + assertNotNull(koin.getOrNull()) + assertNull(koin.getOrNull()) + } + + @Test + fun test_allow_extra_binds(){ + + val koin = koinApplication { + printLogger(Level.DEBUG) + modules(module { + singleOf(::ClassA) { bind()} + singleOf(::ClassA2) { bind()} + }) + }.koin + + assertNotNull(koin.getOrNull() is ClassA2) + assertNotNull(koin.getOrNull()) + assertNotNull(koin.getOrNull()) + } +} \ No newline at end of file diff --git a/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleDeclarationRulesTest.kt b/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleDeclarationRulesTest.kt index 6ed3fa559..bd60b5115 100644 --- a/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleDeclarationRulesTest.kt +++ b/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleDeclarationRulesTest.kt @@ -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() } + }) } }