From 5701598993e11ec7af9143c3cc6828fd0d774445 Mon Sep 17 00:00:00 2001 From: iliyangermanov Date: Wed, 11 Dec 2024 19:07:33 +0200 Subject: [PATCH] Cleanup the Ivy DI code --- .github/dependabot.yml | 2 +- gradle.properties | 14 +- learn-dsl/build.gradle.kts | 2 +- .../commonMain/kotlin/ivy/di/DiContainer.kt | 241 ------- .../kotlin/ivy/di/autowire/Autowire.kt | 362 ---------- .../ivy/di/autowire/AutowireSingleton.kt | 362 ---------- .../commonMain/kotlin/ivy/di/autowire/new.kt | 628 ------------------ .../kotlin/ivy/di/DiContainerTest.kt | 151 ----- .../src/commonTest/kotlin/ivy/di/LazyTest.kt | 102 --- .../ivy/di/testsupport/FakeAbstraction.kt | 4 - .../ivy/di/testsupport/FakeDataSource.kt | 3 - .../kotlin/ivy/di/testsupport/FakeImplOne.kt | 3 - .../kotlin/ivy/di/testsupport/FakeImplTwo.kt | 4 - .../ivy/di/testsupport/FakeModuleDep.kt | 3 - .../ivy/di/testsupport/FakeRepository.kt | 3 - .../ivy/di/testsupport/FakeStateHolder.kt | 5 - .../ivy/di/testsupport/FakeViewModel.kt | 8 - .../kotlin/ivy/di/testsupport/HttpClient.kt | 3 - .../ivy/di/testsupport/di/FakeDiModule.kt | 11 - 19 files changed, 9 insertions(+), 1902 deletions(-) delete mode 100644 learn-dsl/src/commonMain/kotlin/ivy/di/DiContainer.kt delete mode 100644 learn-dsl/src/commonMain/kotlin/ivy/di/autowire/Autowire.kt delete mode 100644 learn-dsl/src/commonMain/kotlin/ivy/di/autowire/AutowireSingleton.kt delete mode 100644 learn-dsl/src/commonMain/kotlin/ivy/di/autowire/new.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/DiContainerTest.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/LazyTest.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeAbstraction.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeDataSource.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplOne.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplTwo.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeModuleDep.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeRepository.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeStateHolder.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeViewModel.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/HttpClient.kt delete mode 100644 learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/di/FakeDiModule.kt diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 4215fb2..edb8f4a 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -14,7 +14,7 @@ updates: - maven-central open-pull-requests-limit: 10 groups: - ivy-di: + ivy-learn-dsl: patterns: - "*" diff --git a/gradle.properties b/gradle.properties index e5de1e5..6997206 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,20 +12,20 @@ android.useAndroidX=true android.nonTransitiveRClass=true # Pomfile definitions GROUP=com.ivy-apps -POM_ARTIFACT_ID=di -VERSION_NAME=0.0.5 +POM_ARTIFACT_ID=learn-content-dsl +VERSION_NAME=0.0.1 SONATYPE_HOST=CENTRAL_PORTAL RELEASE_SIGNING_ENABLED=true -POM_DESCRIPTION=A simple DI container for Kotlin Multiplatform apps. +POM_DESCRIPTION=Ivy Learn DSL for creating interactive lessons. POM_INCEPTION_YEAR=2024 -POM_URL=https://github.com/Ivy-Apps/di/ +POM_URL=https://github.com/Ivy-Apps/learn-content-dsl/ POM_LICENSE_NAME=The Apache Software License, Version 2.0 POM_LICENSE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt POM_LICENSE_DIST=repo POM_DEVELOPER_ID=ivy-apps POM_DEVELOPER_NAME=Ivy Apps POM_DEVELOPER_URL=https://github.com/Ivy-Apps/ -POM_SCM_URL=https://github.com/Ivy-Apps/di/ -POM_SCM_CONNECTION=scm:git:git://github.com/Ivy-Apps/di.git -POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/Ivy-Apps/di.git +POM_SCM_URL=https://github.com/Ivy-Apps/learn-content-dsl/ +POM_SCM_CONNECTION=scm:git:git://github.com/Ivy-Apps/learn-content-dsl.git +POM_SCM_DEV_CONNECTION=scm:git:ssh://git@github.com/Ivy-Apps/learn-content-dsl.git SONATYPE_AUTOMATIC_RELEASE=true diff --git a/learn-dsl/build.gradle.kts b/learn-dsl/build.gradle.kts index ee3b08d..f20b6e8 100644 --- a/learn-dsl/build.gradle.kts +++ b/learn-dsl/build.gradle.kts @@ -43,7 +43,7 @@ kotlin { } android { - namespace = "com.ivy_apps.di" + namespace = "com.ivy_apps.learn.dsl" compileSdk = libs.versions.android.compileSdk.get().toInt() defaultConfig { minSdk = libs.versions.android.minSdk.get().toInt() diff --git a/learn-dsl/src/commonMain/kotlin/ivy/di/DiContainer.kt b/learn-dsl/src/commonMain/kotlin/ivy/di/DiContainer.kt deleted file mode 100644 index 63afd4a..0000000 --- a/learn-dsl/src/commonMain/kotlin/ivy/di/DiContainer.kt +++ /dev/null @@ -1,241 +0,0 @@ - -package ivy.di - -import kotlin.jvm.JvmInline -import kotlin.reflect.KClass - -typealias Factory = () -> T -/** - * A qualifier used to distinguish between multiple dependencies of the same type. - * It must support equality checks: **hashCode** and **equals**. - */ -typealias Qualifier = Any - -val AppScope = Di.newScope("app") -val FeatureScope = Di.newScope("feature") - -object Di { - private val scopes = mutableSetOf() - - val factories = mutableMapOf>() - val singletons = mutableSetOf>() - val singletonInstances = mutableMapOf() - val multibindFactories = mutableMapOf>>() - - /** - * Initializes a set of modules by calling their [Module.init] functions. - */ - fun init(modules: Set) { - modules.forEach(::init) - } - - /** - * Initializes a module by calling its [Module.init] function. - */ - fun init(module: Module) = module.init() - - /** - * Scope used to register dependencies for the entire lifetime of the application. - */ - fun appScope(block: Scope.() -> Unit) = AppScope.block() - - /** - * Scope used to register dependencies for a feature. - */ - fun featureScope(block: Scope.() -> Unit) = FeatureScope.block() - - /** - * Registers a DI new scope. - * @param name Unique identifier for the scope. - * @return The newly created [Scope]. - */ - fun newScope(name: String): Scope = Scope(name).also(scopes::add) - - /** - * Utility function for registering dependencies in a specific scope. - */ - fun scope(scope: Scope, block: Scope.() -> Unit) = scope.block() - - /** - * Registers a factory for a dependency [T]. - * A new instance of [T] will be created only after the first call to [get]. - * Subsequent calls to [get] will create a new instance. - * @param named An optional qualifier to distinguish between multiple dependencies of the same type. - */ - inline fun Scope.register( - named: Any? = null, - noinline factory: () -> T, - ) { - factories[DependencyKey(this, T::class, named)] = factory - } - - inline fun Scope.registerIntoSet( - named: Any, - noinline factory: () -> T, - ) { - TODO() -// multibindingFactories[MultibindingDependencyKey(this, T::class, named)] = factory - } - - /** - * Registers a singleton factory for a dependency [T]. - * A single instance of [T] will be created only after the first call to [get]. - * Subsequent calls to [get] will return the same instance. - * @param named An optional qualifier to distinguish between multiple dependencies of the same type. - */ - inline fun Scope.singleton( - named: Any? = null, - noinline factory: () -> T, - ) { - val classKey = T::class - factories[DependencyKey(this, classKey, named)] = factory - singletons.add(classKey) - } - - /** - * Binds an interface (or a base class) [Base] to an implementation [Impl]. - */ - inline fun Scope.bind( - named: Any? = null, - ) { - register { get(named = named) } - } - - @Throws(DependencyInjectionError::class) - inline fun getSet(named: Any? = null): Set { - factoryOrThrow(T::class, named) // ensure that factory exists - return lazy { get(named) } - } - - /** - * The same as [get] but returns a [Lazy] instance. - * @param named An optional qualifier to distinguish between multiple dependencies of the same type. - * @throws DependencyInjectionError if no factory for [T] with qualifier [named] is registered. - */ - @Throws(DependencyInjectionError::class) - inline fun getLazy(named: Any? = null): Lazy { - factoryOrThrow(T::class, named) // ensure that factory exists - return lazy { get(named) } - } - - /** - * Returns an instance of a dependency [T]. - * Each call to [get] will return a new instance using your registered factory. - * If [T] is a [singleton], the same instance will be returned on subsequent calls. - * @param named An optional qualifier to distinguish between multiple dependencies of the same type. - * @throws DependencyInjectionError if no factory for [T] with qualifier [named] is registered. - */ - @Throws(DependencyInjectionError::class) - inline fun get(named: Any? = null): T { - val classKey = T::class - val (scope, factory) = factoryOrThrow(classKey, named) - val depKey = DependencyKey(scope, classKey, named) - return if (classKey in singletons) { - if (depKey in singletonInstances) { - // single instance already created - singletonInstances[depKey] as T - } else { - // create a new instance for the singleton - val instance = (factory() as T).also { - singletonInstances[depKey] = it - } - instance - } - } else { - // create a new instance - factory() as T - } - } - - /** - * Returns a factory for a dependency identified by [classKey] and [named]. - * @throws DependencyInjectionError if no factory is registered. - */ - @Throws(DependencyInjectionError::class) - fun factoryOrThrow( - classKey: KClass<*>, - named: Any?, - ): Pair> = scopes - .firstNotNullOfOrNull { scope -> - scopedFactoryOrNull(scope, classKey, named) - } ?: throw DependencyInjectionError(diErrorMsg(classKey, named)) - - private fun diErrorMsg(classKey: KClass<*>, named: Any?): String = buildString { - append("No factory") - if (named != null) { - append(" with qualifier named \"$named\"") - } - append(" found: ") - append('[') - append(classKey.toString()) - append(']') - val dependencyId = buildString { - append(classKey.toString()) - if (named != null) { - append("(named=\"$named\")") - } - } - append("\nDid you forget to register '$dependencyId' in Ivy DI?") - } - - private fun scopedFactoryOrNull( - scope: Scope, - classKey: KClass<*>, - named: Any?, - ): Pair Any>? = factories[DependencyKey(scope, classKey, named)] - ?.let { factory -> - scope to factory - } - - /** - * Clears all instances in the given [scope]. - */ - fun clear(scope: Scope) { - singletonInstances.keys.forEach { instanceKey -> - if (instanceKey.scope == scope) { - singletonInstances.remove(instanceKey) - } - } - } - - /** - * Resets the DI container by clearing all instances, singletons and factories. - * Note: [scopes] aren't clear for performance reasons. - */ - fun reset() { - singletonInstances.clear() - factories.clear() - singletons.clear() - } - - /** - * A key used to identify a dependency in the DI container. - * @param scope The scope in which the dependency is registered. - * @param klass The type of the dependency. Note: Generic types are not supported (Lazy == Lazy true). - * @param qualifier An optional qualifier to distinguish between multiple dependencies of the same type. - * _The qualifier must support hashCode and equals._ - */ - data class DependencyKey( - val scope: Scope, - val klass: KClass<*>, - val qualifier: Qualifier?, - ) - - /** - * A DI scope. Scopes are used to group dependencies and manage their lifecycle. - */ - @JvmInline - value class Scope internal constructor(val value: String) - - interface Module { - /** - * Register your DI dependencies in this function. - */ - fun init() - } -} - -/** - * An exception thrown when a factory for a dependency is not found in the DI container. - */ -class DependencyInjectionError(msg: String) : IllegalStateException(msg) \ No newline at end of file diff --git a/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/Autowire.kt b/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/Autowire.kt deleted file mode 100644 index df9a88e..0000000 --- a/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/Autowire.kt +++ /dev/null @@ -1,362 +0,0 @@ -package ivy.di.autowire - -import ivy.di.Di.Scope -import ivy.di.Di.register -import kotlin.jvm.JvmName - -/** - * Automatically registers a factory for a dependency [R] with the given constructor. - * [ivy.di.Di.get] will be called for each constructor parameter of [R]. - * - * Make sure to **import [R] before** calling `autoWire(::R)`, - * otherwise your IDE might not recognize the function. - * - * @param constructor A function reference to the constructor of the dependency like `::R`. - * [R] must be imported before calling this function. - * - * ``` - * class Repository(val a: A, val b: B, val c: C) - * - * Di.appScope { - * autoWire(::Repository) - * // equivalent to: - * // register { Repository(Di.get(), Di.get(), Di.get()) } - * } - * ``` - */ -inline fun Scope.autoWire( - crossinline constructor: () -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire1") -inline fun Scope.autoWire( - crossinline constructor: (T1) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire2") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire3") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire4") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire5") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire6") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire7") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire8") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire9") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire10") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire11") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire12") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire13") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire14") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire15") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire16") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire17") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire18") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire19") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire20") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire21") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire22") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire23") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire24") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire25") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire26") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire27") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire28") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire29") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} - -/** - * See [autoWire]. - */ -@JvmName("autoWire30") -inline fun Scope.autoWire( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30) -> R, - named: Any? = null, -) { - register(named = named) { new(constructor) } -} \ No newline at end of file diff --git a/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/AutowireSingleton.kt b/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/AutowireSingleton.kt deleted file mode 100644 index 28a82d9..0000000 --- a/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/AutowireSingleton.kt +++ /dev/null @@ -1,362 +0,0 @@ -package ivy.di.autowire - -import ivy.di.Di.Scope -import ivy.di.Di.singleton -import kotlin.jvm.JvmName - -/** - * Automatically registers a singleton factory for a dependency [R] with the given constructor. - * [ivy.di.Di.get] will be called for each constructor parameter of [R]. - * - * Make sure to **import [R] before** calling `autoWireSingleton(::R)`, - * otherwise your IDE might not recognize the function. - * - * @param constructor A function reference to the constructor of the dependency like `::R`. - * [R] must be imported before calling this function. - * - * ``` - * class Repository(val a: A, val b: B, val c: C) - * - * Di.appScope { - * autoWireSingleton(::Repository) - * // equivalent to: - * // singleton { Repository(Di.get(), Di.get(), Di.get()) } - * } - * ``` - */ -inline fun Scope.autoWireSingleton( - crossinline constructor: () -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire1") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire2") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire3") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire4") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire5") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire6") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire7") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire8") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire9") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire10") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire11") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire12") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire13") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire14") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire15") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire16") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire17") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire18") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire19") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire20") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire21") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire22") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire23") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire24") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire25") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire26") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire27") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire28") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire29") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} - -/** - * See [autoWireSingleton]. - */ -@JvmName("autoWire30") -inline fun Scope.autoWireSingleton( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30) -> R, - named: Any? = null, -) { - singleton(named = named) { new(constructor) } -} \ No newline at end of file diff --git a/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/new.kt b/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/new.kt deleted file mode 100644 index 1b4a21e..0000000 --- a/learn-dsl/src/commonMain/kotlin/ivy/di/autowire/new.kt +++ /dev/null @@ -1,628 +0,0 @@ -package ivy.di.autowire - -import ivy.di.Di -import kotlin.jvm.JvmName - -/** - * Inspired by [Koin](https://github.com/InsertKoinIO/koin). - */ -inline fun new( - crossinline constructor: () -> R, -): R = constructor() - -/** - * @see new - */ -@JvmName("new1") -inline fun new( - crossinline constructor: (T1) -> R, -): R = constructor(Di.get()) - -/** - * @see new - */ -@JvmName("new2") -inline fun new( - crossinline constructor: (T1, T2) -> R, -): R = constructor(Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new3") -inline fun new( - crossinline constructor: (T1, T2, T3) -> R, -): R = constructor(Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new4") -inline fun new( - crossinline constructor: (T1, T2, T3, T4) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new5") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new6") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new7") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new8") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new9") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new10") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) -> R, -): R = constructor(Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get()) - -/** - * @see new - */ -@JvmName("new11") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new12") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new13") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new14") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new15") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new16") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new17") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new18") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new19") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new20") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new21") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new22") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new23") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new24") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new25") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new26") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new27") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new28") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28) -> R, -): R = constructor( - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get(), - Di.get() -) - -/** - * @see new - */ -@JvmName("new29") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29) -> R, -): R = constructor( - Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), - Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), - Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get() -) - -/** - * @see new - */ -@JvmName("new30") -inline fun new( - crossinline constructor: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30) -> R, -): R = constructor( - Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), - Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), - Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get(), Di.get() -) \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/DiContainerTest.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/DiContainerTest.kt deleted file mode 100644 index 0aaac3d..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/DiContainerTest.kt +++ /dev/null @@ -1,151 +0,0 @@ -package ivy.di - -import io.kotest.assertions.throwables.shouldThrow -import io.kotest.matchers.nulls.shouldNotBeNull -import io.kotest.matchers.shouldBe -import ivy.di.Di.register -import ivy.di.Di.singleton -import ivy.di.testsupport.* -import ivy.di.testsupport.di.FakeDiModule -import kotlin.test.BeforeTest -import kotlin.test.Test - -class DiContainerTest { - - @BeforeTest - fun setup() { - Di.reset() - } - - @Test - fun createsAnInstance_in_appScope() { - // Given - Di.appScope { register { FakeStateHolder() } } - Di.get().number = 42 - - // When - val stateHolder = Di.get() - - // Then - stateHolder.number shouldBe 0 - } - - @Test - fun creates_a_singleton_in_appScope() { - // Given - Di.appScope { singleton { FakeStateHolder() } } - Di.get().number = 42 - - // When - val stateHolder = Di.get() - - // Then - stateHolder.number shouldBe 42 - } - - @Test - fun moreComplex_DI_graph_1() { - // Given - Di.appScope { - singleton { FakeStateHolder() } - singleton { HttpClient() } - register { FakeDataSource(Di.get()) } - register { FakeRepository(Di.get()) } - register { FakeViewModel(Di.get(), Di.get()) } - } - - // When - val viewModel: FakeViewModel = Di.get() - - // Then - viewModel.shouldNotBeNull() - } - - @Test - fun throws_for_not_registered_classes() { - // When - val thrownException = shouldThrow { - Di.get() - } - - // Then - thrownException.message.shouldNotBeNull() - } - - @Test - fun binds_interface() { - // Given - Di.appScope { - register { FakeImplOne() } - } - - // When - val instance = Di.get() - - // Then - instance.shouldNotBeNull() - } - - @Test - fun createsAnInstance_in_featureScope() { - // Given - Di.featureScope { - register { FakeStateHolder() } - } - Di.get().number = 42 - - // When - val stateHolder = Di.get() - - // Then - stateHolder.number shouldBe 0 - } - - @Test - fun creates_a_singleton_in_featureScope() { - // Given - Di.featureScope { - singleton { FakeStateHolder() } - } - Di.get().number = 42 - - // When - val stateHolder = Di.get() - - // Then - stateHolder.number shouldBe 42 - - // When the scope is reset - Di.clear(FeatureScope) - - // Then after the reset - Di.get().number shouldBe 0 - } - - @Test - fun moduleRegistration() { - // Given - Di.init(setOf(FakeDiModule)) - - // When - val instance = Di.get() - - // Then - instance.shouldNotBeNull() - } - - @Test - fun create_DI_scope() { - // Given - val customScope = Di.newScope("new") - - // When - Di.scope(customScope) { - register { FakeStateHolder() } - } - val instance = Di.get() - - // Then - instance.shouldNotBeNull() - } -} \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/LazyTest.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/LazyTest.kt deleted file mode 100644 index d0ebc47..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/LazyTest.kt +++ /dev/null @@ -1,102 +0,0 @@ -package ivy.di - -import io.kotest.assertions.throwables.shouldThrow -import io.kotest.matchers.shouldBe -import ivy.di.Di.register -import ivy.di.Di.singleton -import kotlin.test.BeforeTest -import kotlin.test.Test - -private var initialized = false - -class Counter(var x: Int) { - init { - initialized = true - } -} - -class CountManager(private val counter: Lazy) { - fun increment() { - counter.value.x++ - } -} - -class LazyTest { - - @BeforeTest - fun setup() { - Di.reset() - initialized = false - } - - @Test - fun lazyInitialization() { - // Given - Di.appScope { - register { Counter(x = 42) } - } - - // When - val lazyCounter = Di.getLazy() - - // Initially the lazy counter isn't initialized - initialized shouldBe false - - // When the lazy value is accessed - val counter = lazyCounter.value - - // Then - initialized shouldBe true - counter.x shouldBe 42 - } - - @Test - fun lazySingletonInitialization() { - // Given - Di.appScope { - singleton { Counter(x = 0) } - } - - // When - val lazyCounter = Di.getLazy() - - // Initially the lazy counter isn't initialized - initialized shouldBe false - - // When the lazy value is accessed and modified - lazyCounter.value.x shouldBe 0 - lazyCounter.value.x = 42 - - // Then - initialized shouldBe true - Di.getLazy().value.x shouldBe 42 - } - - @Test - fun missingFactory() { - // When-Then - shouldThrow { - Di.getLazy() - } - } - - @Test - fun realWorldScenario() { - // Given - Di.appScope { - singleton { Counter(x = 0) } - register { CountManager(Di.getLazy()) } - } - val manager = Di.get() - - // Initially the lazy counter isn't initialized - initialized shouldBe false - - // When - manager.increment() - - // Then - initialized shouldBe true - Di.get().x shouldBe 1 - } -} \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeAbstraction.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeAbstraction.kt deleted file mode 100644 index 3f612af..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeAbstraction.kt +++ /dev/null @@ -1,4 +0,0 @@ -package ivy.di.testsupport - -interface FakeAbstraction - diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeDataSource.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeDataSource.kt deleted file mode 100644 index 05ea184..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeDataSource.kt +++ /dev/null @@ -1,3 +0,0 @@ -package ivy.di.testsupport - -class FakeDataSource(@Suppress("unused") val httpClient: HttpClient) \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplOne.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplOne.kt deleted file mode 100644 index 1a9c51f..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplOne.kt +++ /dev/null @@ -1,3 +0,0 @@ -package ivy.di.testsupport - -class FakeImplOne : FakeAbstraction \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplTwo.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplTwo.kt deleted file mode 100644 index eee0538..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeImplTwo.kt +++ /dev/null @@ -1,4 +0,0 @@ -package ivy.di.testsupport - -@Suppress("unused") -class FakeImplTwo : FakeAbstraction \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeModuleDep.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeModuleDep.kt deleted file mode 100644 index cb0b938..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeModuleDep.kt +++ /dev/null @@ -1,3 +0,0 @@ -package ivy.di.testsupport - -class FakeModuleDep \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeRepository.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeRepository.kt deleted file mode 100644 index f5bfbab..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeRepository.kt +++ /dev/null @@ -1,3 +0,0 @@ -package ivy.di.testsupport - -class FakeRepository(@Suppress("unused") val dataSource: FakeDataSource) \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeStateHolder.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeStateHolder.kt deleted file mode 100644 index ecef1e0..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeStateHolder.kt +++ /dev/null @@ -1,5 +0,0 @@ -package ivy.di.testsupport - -class FakeStateHolder { - var number = 0 -} \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeViewModel.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeViewModel.kt deleted file mode 100644 index 9e91c5b..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/FakeViewModel.kt +++ /dev/null @@ -1,8 +0,0 @@ -package ivy.di.testsupport - -class FakeViewModel( - @Suppress("unused") - val repository: FakeRepository, - @Suppress("unused") - val stateHolder: FakeStateHolder -) \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/HttpClient.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/HttpClient.kt deleted file mode 100644 index 2a070e2..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/HttpClient.kt +++ /dev/null @@ -1,3 +0,0 @@ -package ivy.di.testsupport - -class HttpClient \ No newline at end of file diff --git a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/di/FakeDiModule.kt b/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/di/FakeDiModule.kt deleted file mode 100644 index 2aadd77..0000000 --- a/learn-dsl/src/commonTest/kotlin/ivy/di/testsupport/di/FakeDiModule.kt +++ /dev/null @@ -1,11 +0,0 @@ -package ivy.di.testsupport.di - -import ivy.di.Di -import ivy.di.Di.register -import ivy.di.testsupport.FakeModuleDep - -object FakeDiModule : Di.Module { - override fun init() = Di.appScope { - register { FakeModuleDep() } - } -} \ No newline at end of file