Skip to content

Commit

Permalink
koinConfiguration function to help declare, extend and reuse Koin con…
Browse files Browse the repository at this point in the history
…figuration
  • Loading branch information
arnaudgiuliani committed Nov 15, 2024
1 parent 8ed33be commit 4247d8f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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<ComponentB>())
}

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<ComponentA>())
stopKoin()

init {
modules(
module {
singleOf(::ComponentB)
}
)
}
assertNotNull(KoinPlatform.getKoin().getOrNull<ComponentB>())
stopKoin()
}

// @Test
// fun `allow declare a print logger level`() {
// startKoin {
Expand Down

0 comments on commit 4247d8f

Please sign in to comment.