Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

koinConfiguration function #2054

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading