Skip to content

Commit

Permalink
Komok 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
IRus committed Aug 11, 2024
1 parent 2b8e98d commit 8f52f7e
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 143 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ build/

# Project
src/main/resources/application.conf

# System
.DS_Store
10 changes: 10 additions & 0 deletions .run/Komok.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Komok" type="JetRunConfigurationType">
<option name="MAIN_CLASS_NAME" value="io.heapy.komok.Application" />
<module name="komok.main" />
<shortenClasspath name="NONE" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class BasicBinderTest {
}

private val module1 by module {
provide<Test1>(BasicBinderTest::Test1Impl)
provide<Test1>(::Test1Impl)
provide(::test2Provider)
}

private val module2 by module {}

private val module3 by module {
provide(BasicBinderTest::Test3)
provide(BasicBinderTest::TestRoot)
provide(::Test3)
provide(::TestRoot)
}

@Test
Expand Down Expand Up @@ -96,8 +96,8 @@ class SingletonBinderTest {
fun test() =
runTest {
val module by module {
provide(SingletonBinderTest::Test1)
provide(SingletonBinderTest::TestRoot)
provide(::Test1)
provide(::TestRoot)
}

val root = createContextAndGet(
Expand Down Expand Up @@ -125,7 +125,7 @@ class SingletonZeroArgBinderTest {
runTest {
val module by module {
provideInstance<Test1>({ Test1() })
provide(SingletonZeroArgBinderTest::TestRoot)
provide(::TestRoot)
}

val root = createContextAndGet(
Expand All @@ -148,7 +148,7 @@ class OptionalInjectionTest {
class Bar

private val module1 by module {
provide(OptionalInjectionTest::Foo)
provide(::Foo)
}

@Test
Expand Down Expand Up @@ -196,9 +196,9 @@ class CyclicDependencyTest {
class Baz(val foo: Foo)

private val cyclic by module {
provide(CyclicDependencyTest::Foo)
provide(CyclicDependencyTest::Bar)
provide(CyclicDependencyTest::Baz)
provide(::Foo)
provide(::Bar)
provide(::Baz)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class EntryPointReturningTest {
fun `test inline module`() =
runTest {
val result = komok<Application, String> {
provide(EntryPointReturningTest::Application)
provide(EntryPointReturningTest::Service1)
provide(EntryPointReturningTest::Service2)
provide(::Application)
provide(::Service1)
provide(::Service2)
}

assertEquals(
Expand All @@ -24,7 +24,7 @@ class EntryPointReturningTest {
fun `test module`() =
runTest {
val result = komok<Application, String> {
provide(EntryPointReturningTest::Application)
provide(::Application)
dependency(k1)
}

Expand Down Expand Up @@ -58,10 +58,10 @@ class EntryPointReturningTest {

private val k1 by module {
dependency(k2)
provide(EntryPointReturningTest::Service1)
provide(::Service1)
}

private val k2 by module {
provide(EntryPointReturningTest::Service2)
provide(::Service2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class EntryPointTest {
assertFalse(TestInlineModule.executed)

komok<TestInlineModule, Unit> {
provide(EntryPointTest::TestInlineModule)
provide(EntryPointTest::Service1)
provide(EntryPointTest::Service2)
provide(::TestInlineModule)
provide(::Service1)
provide(::Service2)
}

assertTrue(TestInlineModule.executed)
Expand All @@ -38,9 +38,9 @@ class EntryPointTest {
runTest {
val exception = assertThrows<ContextException> {
komok<EntryPoint<Unit>, Unit> {
provide(EntryPointTest::TestInlineModule)
provide(EntryPointTest::Service1)
provide(EntryPointTest::Service2)
provide(::TestInlineModule)
provide(::Service1)
provide(::Service2)
}
}

Expand All @@ -56,7 +56,7 @@ class EntryPointTest {
assertFalse(TestModule.executed)

komok<TestModule, Unit> {
provide(EntryPointTest::TestModule)
provide(::TestModule)
dependency(k1)
}

Expand Down Expand Up @@ -92,10 +92,10 @@ class EntryPointTest {

private val k1 by module {
dependency(k2)
provide(EntryPointTest::Service1)
provide(::Service1)
}

private val k2 by module {
provide(EntryPointTest::Service2)
provide(::Service2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.heapy.komok.tech.di.test2

import io.heapy.komok.tech.di.lib.Module

class A

@Module
open class ModuleA {
open val a by lazy {
A()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.heapy.komok.tech.di.test2

import io.heapy.komok.tech.di.lib.Module

class B(
private val a: A,
) {
fun getA() = a
}

@Module
open class ModuleB(
private val moduleA: ModuleA,
) {
open val b by lazy {
B(moduleA.a)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.heapy.komok.tech.di.test2

import io.heapy.komok.tech.di.lib.Module

class C(
private val a: A,
) {
fun getA() =
a
}

@Module
open class ModuleC(
private val moduleA: ModuleA,
) {
open val c by lazy {
C(moduleA.a)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.heapy.komok.tech.di.test2

import io.heapy.komok.tech.di.lib.Module

class D(
private val a: A,
private val b: B,
private val c: C,
) {
fun getAA() = a
fun getAB() = b.getA()
fun getAC() = c.getA()
}

@Module
open class ModuleD(
private val moduleA: ModuleA,
private val moduleB: ModuleB,
private val moduleC: ModuleC,
) {
open val d by lazy {
D(moduleA.a, moduleB.b, moduleC.c)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.heapy.komok.tech.di.test2

import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.Assertions.assertSame
import org.junit.jupiter.api.Test

class SingletonTest {
@Test
fun `test singleton`() {
val moduleD = createModuleD {}

assertAll(
{ assertSame(moduleD.d.getAA(), moduleD.d.getAB()) },
{ assertSame(moduleD.d.getAB(), moduleD.d.getAC()) },
{ assertSame(moduleD.d.getAA(), moduleD.d.getAC()) },
)
}

@Test
fun `test singleton flatten`() {
val flat = createFlattenModuleD {}

assertAll(
{ assertSame(flat.moduleD.d.getAA(), flat.moduleD.d.getAB()) },
{ assertSame(flat.moduleD.d.getAB(), flat.moduleD.d.getAC()) },
{ assertSame(flat.moduleD.d.getAA(), flat.moduleD.d.getAC()) },
{ assertSame(flat.moduleA.a, flat.moduleD.d.getAA()) },
)
}

@Test
fun `test singleton with override`() {
val a = A()
val moduleD = createModuleD {
moduleA {
a {
a
}
}
}

assertAll(
{ assertSame(moduleD.d.getAA(), moduleD.d.getAB()) },
{ assertSame(moduleD.d.getAB(), moduleD.d.getAC()) },
{ assertSame(moduleD.d.getAA(), moduleD.d.getAC()) },
)
}

@Test
fun `test singleton with override flatten`() {
val a = A()
val flat = createFlattenModuleD {
moduleA {
a {
a
}
}
}

assertAll(
{ assertSame(a, flat.moduleD.d.getAA()) },
{ assertSame(a, flat.moduleD.d.getAB()) },
{ assertSame(a, flat.moduleD.d.getAC()) },
{ assertSame(a, flat.moduleA.a) },
{ assertSame(a, flat.moduleB.b.getA()) },
{ assertSame(a, flat.moduleC.c.getA()) },
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,23 @@ private fun TypeSpec.Builder.addModuleBuildFunction(
val primaryConstructor = module.primaryConstructor
?: error("MBF: Primary constructor not found for $className")

val moduleBuilderFunction = try {
FunSpec
.builder("build")
.returns(
val moduleBuildProperty = try {
PropertySpec
.builder(
"module",
ClassName(
packageName,
className,
className.overrideClassName(),
),
PRIVATE,
)
.addCode(
.mutable(false)
.delegate(
buildCodeBlock {
add("lazy {\n")
indent()
add(
"return %T(\n",
"%T(\n",
ClassName(
packageName,
className.overrideClassName(),
Expand Down Expand Up @@ -148,6 +152,32 @@ private fun TypeSpec.Builder.addModuleBuildFunction(
}
unindent()
add(")\n")
unindent()
addStatement("}\n")
},
)
.build()
} catch (e: Exception) {
logger.error("Failed to generate module build property")
throw e
}

addProperty(moduleBuildProperty)

val moduleBuilderFunction = try {
FunSpec
.builder("build")
.returns(
ClassName(
packageName,
className,
),
)
.addCode(
buildCodeBlock {
addStatement(
"return module",
)
},
)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ fun String.flattenClassName(): String =
"${this}Flatten"

fun generateFlattenModuleClass(
graph: Map<KSClassDeclaration, List<KSClassDeclaration>>,
module: KSClassDeclaration,
moduleDependencies: List<KSClassDeclaration>,
sortedGraph: List<KSClassDeclaration>,
): TypeSpec {
val className = module.simpleName.asString()
val packageName = module.packageName.asString()

return TypeSpec
.classBuilder(className.flattenClassName())
.addFlattenModuleClassConstructor(moduleDependencies)
.addFlattenModuleClassConstructor(moduleDependencies + module)
.build()
}

Expand Down
Loading

0 comments on commit 8f52f7e

Please sign in to comment.