Skip to content

Commit

Permalink
Nothing to see here
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Jun 19, 2024
1 parent 4f52d38 commit 1c65f85
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 171 deletions.
1 change: 0 additions & 1 deletion .github/FUNDING.yml

This file was deleted.

27 changes: 0 additions & 27 deletions .github/workflows/code_quality.yml

This file was deleted.

20 changes: 13 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ repositories {
mavenCentral()
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
compilerOptions {
jvmTarget.set(JVM_21)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
apiVersion.set(KotlinVersion.KOTLIN_2_0)
freeCompilerArgs.add("-Xcontext-receivers")
}
kotlin {
jvmToolchain(21)
}

tasks
.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>()
.configureEach {
compilerOptions {
jvmTarget.set(JVM_21)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
apiVersion.set(KotlinVersion.KOTLIN_2_0)
freeCompilerArgs.add("-Xcontext-receivers")
}
}

tasks.test {
useJUnitPlatform()
}
Expand Down
9 changes: 0 additions & 9 deletions qodana.yaml

This file was deleted.

120 changes: 14 additions & 106 deletions src/main/kotlin/io/heapy/komok/infra/di/binder_impl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ data class BindingWithModule<T>(
internal val binding: Binding<T>,
internal val module: Module,
) : Binding<T> {

override val key: GenericKey<T>
get() = binding.key
}
Expand All @@ -98,11 +97,6 @@ data class ProviderBinding<T>(
@PublishedApi internal val provider: KFunction<T>,
) : Binding<T>

data class MultiBinding<T>(
override val key: GenericKey<Set<T>>,
internal val bindings: List<Binding<out T>>,
) : Binding<Set<T>>

internal class KomokContext(
private val definitions: Map<Key, BindingWithModule<*>>,
) : Context {
Expand Down Expand Up @@ -187,62 +181,27 @@ private fun processBindings(
module.bindings.forEach { binding ->
val processedBinding = bindings[binding.key]

when (binding) {
is MultiBinding<*> -> {
when (val processed = processedBinding?.binding) {
null -> bindings[binding.key] = BindingWithModule(
module = module,
binding = MultiBinding(
key = binding.key as GenericKey<Set<*>>,
bindings = listOf(
BindingWithModule(
module = module,
binding = binding.bindings.single(),
),
),
),
)

is MultiBinding<*> -> bindings[binding.key] = BindingWithModule(
module = module,
binding = MultiBinding(
key = processed.key as GenericKey<Set<*>>,
bindings = processed.bindings + BindingWithModule(
module = module,
binding = binding.bindings.single(),
),
),
)

else -> throw ContextException("Same type [$binding] provided as a singleton and as a Set.")
}
}

else -> {
if (processedBinding == null) {
bindings[binding.key] = BindingWithModule(
module = module,
binding = binding,
)
} else {
val processed = processedBinding.module.source
val current = module.source
if (processedBinding == null) {
bindings[binding.key] = BindingWithModule(
module = module,
binding = binding,
)
} else {
val processed = processedBinding.module.source
val current = module.source

if (current == processed) {
throw ContextException("Binding [${processedBinding.key}] duplicated in module [$current].")
} else {
throw ContextException(
"Binding [${processedBinding.key}] already present in module " + "[$processed]. Current module: [$current]",
)
}
}
if (current == processed) {
throw ContextException("Binding [${processedBinding.key}] duplicated in module [$current].")
} else {
throw ContextException(
"Binding [${processedBinding.key}] already present in module " + "[$processed]. Current module: [$current]",
)
}
}
}
}

interface ExecutionContext {

val definitions: Map<Key, BindingWithModule<*>>
val stack: MutableList<Key>
val instances: MutableMap<Key, Any?>
Expand Down Expand Up @@ -308,17 +267,6 @@ internal suspend fun ExecutionContext.createType(
actualBinding.provider.callBy(params)
}
}

is MultiBinding<*> -> {
actualBinding.bindings
.map { multiBinding ->
createType(
key = multiBinding.key,
saveInstance = false,
)
}
.toSet()
}
}
stack.remove(key)

Expand Down Expand Up @@ -351,7 +299,6 @@ fun printCircularDependencyGraph(
is ZeroArgProviderBinding -> "provider [${binding.provider::class}]"
is ProviderBinding -> "provider [${binding.provider::class}]"
is BindingWithModule -> throw ContextException("Binding with Module shouldn't be a part of cycle")
is MultiBinding<*> -> "multibinding"
}
append(desc)
}
Expand All @@ -369,7 +316,6 @@ fun printCircularDependencyGraph(
is ZeroArgProviderBinding -> "provider [${binding.provider::class}]"
is ProviderBinding -> "provider [${binding.provider::class}]"
is BindingWithModule -> throw ContextException("Binding with Module shouldn't be a part of cycle")
is MultiBinding<*> -> "multibinding"
}
append(desc)
}
Expand Down Expand Up @@ -418,41 +364,3 @@ inline fun <reified I> Binder.provide(
contribute(customizer(it))
}
}

@ModuleDSL
inline fun <reified I : Any> Binder.provideSet(
noinline provider: suspend () -> I,
crossinline customizer: Binding<I>.() -> Binding<I> = { this },
) {
MultiBinding(
key = genericKey<Set<I>>(),
bindings = listOf(
customizer(
ZeroArgProviderBinding(
key = genericKey<I>(),
provider = provider,
),
),
),
).also {
contribute(it)
}
}

@ModuleDSL
inline fun <reified I : Any> Binder.provideSet(
provider: KFunction<I>,
crossinline customizer: Binding<I>.() -> Binding<I> = { this },
) {
MultiBinding(
key = genericKey<Set<I>>(),
bindings = listOf(
customizer(
ProviderBinding(
key = genericKey<I>(),
provider = provider,
),
),
),
).also { contribute(it) }
}
21 changes: 0 additions & 21 deletions src/test/kotlin/io/heapy/komok/infra/di/BinderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertSame
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

Expand Down Expand Up @@ -285,23 +284,3 @@ class IsProviderUtilityTest {
assertFalse(genericKey<String>().isProvider())
}
}

@Disabled("Not implemented")
class MultiBindingTest {
private val module1 by module {
provideSet({ 1 })
provideSet({ 2 })
provideSet({ 3 })
provideSet({ 4 })
provideSet({ 5 })
}

@Test
fun `collect all values`() =
runTest {
createContextAndGet(
genericKey<Set<Int>>(),
module1,
)
}
}

0 comments on commit 1c65f85

Please sign in to comment.