From 4247d8fd29040a6bbd7f914b3b6e49a7f887dc15 Mon Sep 17 00:00:00 2001 From: Arnaud Giuliani Date: Fri, 15 Nov 2024 14:59:28 +0100 Subject: [PATCH] koinConfiguration function to help declare, extend and reuse Koin configuration --- .../kotlin/org/koin/dsl/KoinApplication.kt | 1 + .../kotlin/org/koin/dsl/KoinConfiguration.kt | 41 +++++++++++ .../org/koin/dsl/KoinAppCreationTest.kt | 68 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinConfiguration.kt diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinApplication.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinApplication.kt index 3ac403b36..8a7d1c702 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinApplication.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinApplication.kt @@ -18,6 +18,7 @@ package org.koin.dsl import org.koin.core.KoinApplication import org.koin.core.module.KoinApplicationDslMarker +//TODO Koin 4.1 - KoinAppDeclaration migration type to KoinConfiguration typealias KoinAppDeclaration = KoinApplication.() -> Unit /** diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinConfiguration.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinConfiguration.kt new file mode 100644 index 000000000..7b19efe70 --- /dev/null +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/dsl/KoinConfiguration.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2017-Present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.koin.dsl + +import org.koin.core.KoinApplication +import org.koin.core.module.KoinApplicationDslMarker + +//TODO Koin 4.1 - KoinAppDeclaration migration type to KoinConfiguration + +/** + * function helper to save a Koin configuration + * + * @param configuration - Koin configuration lambda + * @author Arnaud Giuliani + */ +@KoinApplicationDslMarker +public fun koinConfiguration(configuration: KoinAppDeclaration): KoinAppDeclaration = configuration + +/** + * Includes other KoinConfiguration in the current KoinApplication + * + * @param configurations - Koin configurations + * @author Arnaud Giuliani + */ +public fun KoinApplication.includes(vararg configurations: KoinAppDeclaration?): KoinApplication { + configurations.forEach { it?.invoke(this@includes) } + return this +} \ No newline at end of file diff --git a/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/KoinAppCreationTest.kt b/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/KoinAppCreationTest.kt index de75189bc..5a5a3ea15 100644 --- a/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/KoinAppCreationTest.kt +++ b/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/KoinAppCreationTest.kt @@ -1,16 +1,21 @@ package org.koin.dsl +import org.koin.Simple.ComponentA +import org.koin.Simple.ComponentB import org.koin.core.annotation.KoinInternalApi import org.koin.core.context.startKoin import org.koin.core.context.stopKoin import org.koin.core.error.KoinApplicationAlreadyStartedException import org.koin.core.logger.Level +import org.koin.core.module.dsl.singleOf +import org.koin.mp.KoinPlatform import org.koin.mp.KoinPlatformTools import org.koin.test.assertDefinitionsCount import org.koin.test.assertHasNoStandaloneInstance import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertNotNull import kotlin.test.fail @OptIn(KoinInternalApi::class) @@ -79,6 +84,69 @@ class KoinAppCreationTest { KoinPlatformTools.defaultContext().get().logger.error("error") } + @Test + fun `allow declare a configuration`() { + val config = koinConfiguration { + printLogger(Level.DEBUG) + } + + koinApplication(config) + startKoin(config) + } + + @Test + fun `allow declare a configuration extension`() { + val config1 = koinConfiguration { + printLogger(Level.DEBUG) + modules(module { + singleOf(::ComponentA) + }) + } + + val config2 = koinConfiguration { + includes(config1) + + modules(module { + singleOf(::ComponentB) + }) + } + + val k = koinApplication(config2).koin + + assertEquals( + 2, + k.instanceRegistry.instances.size + ) + assertNotNull(k.getOrNull()) + } + + fun init(config : KoinAppDeclaration? = null){ + startKoin { + printLogger(Level.DEBUG) + includes(config) + modules(module { + singleOf(::ComponentA) + }) + } + } + + @Test + fun `allow declare a configuration extension - from function`() { + init() + assertNotNull(KoinPlatform.getKoin().getOrNull()) + stopKoin() + + init { + modules( + module { + singleOf(::ComponentB) + } + ) + } + assertNotNull(KoinPlatform.getKoin().getOrNull()) + stopKoin() + } + // @Test // fun `allow declare a print logger level`() { // startKoin {