mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-24 08:40:46 +00:00
Koin generator
It is Kotlin Symbol Processing generator for Koin module in MicroUtils.
What may do this generator
Let's imagine you want to have shortcuts in koin, to get something easily:
val koin: Koin// some initialization
val someUrl = koin.serverUrl
So, in that case you may mark containing file with next annotation (in the beginning of file):
@file:GenerateKoinDefinition("serverUrl", String::class, nullable = false)
If file is called like Sample.kt, will be generated file GeneratedDefinitionsSample.kt with next content:
public val Scope.serverUrl: String
get() = get(named("serverUrl"))
public val Koin.serverUrl: String
get() = get(named("serverUrl"))
public fun Module.serverUrlSingle(createdAtStart: Boolean = false,
definition: Definition<String>): KoinDefinition<String> =
single(named("serverUrl"), createdAtStart = createdAtStart, definition = definition)
public fun Module.serverUrlFactory(definition: Definition<String>):
KoinDefinition<String> = factory(named("serverUrl"), definition = definition)
Besides, you may use the generics:
@file:GenerateKoinDefinition("sampleInfo", Sample::class, G1::class, G2::class, nullable = false)
Will generate:
public val Scope.sampleInfo: Sample<G1, G2>
get() = get(named("sampleInfo"))
public val Koin.sampleInfo: Sample<G1, G2>
get() = get(named("sampleInfo"))
public fun Module.sampleInfoSingle(createdAtStart: Boolean = false,
definition: Definition<Sample<G1, G2>>): KoinDefinition<Sample<G1, G2>> =
single(named("sampleInfo"), createdAtStart = createdAtStart, definition = definition)
public fun Module.sampleInfoFactory(definition: Definition<Sample<G1, G2>>):
KoinDefinition<Sample<G1, G2>> = factory(named("sampleInfo"), definition = definition)
In case you wish not to generate single:
@file:GenerateKoinDefinition("sampleInfo", Sample::class, G1::class, G2::class, nullable = false, generateSingle = false)
And you will take next code:
public val Scope.sampleInfo: Sample<G1, G2>
get() = get(named("sampleInfo"))
public val Koin.sampleInfo: Sample<G1, G2>
get() = get(named("sampleInfo"))
public fun Module.sampleInfoFactory(definition: Definition<Sample<G1, G2>>):
KoinDefinition<Sample<G1, G2>> = factory(named("sampleInfo"), definition = definition)
How to add generator
Note: $ksp_version in the samples above is equal to supported ksp version presented in /gradle/libs.versions.toml of project
Note: $microutils_version in the version of MicroUtils library in your project
- Add
classpathinbuild.gradle(classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:$ksp_version") - Add plugin to the plugins list of your module:
id "com.google.devtools.ksp" - In
dependenciesblock add to the required target/compile the dependencydev.inmo:micro_utils.koin.generator:$microutils_version:dependencies { add("kspCommonMainMetadata", "dev.inmo:micro_utils.koin.generator:$microutils_version") // will work in commonMain of your multiplatform module add("kspJvm", "dev.inmo:micro_utils.koin.generator:$microutils_version") // will work in main of your JVM module } ksp { // this generator do not require any arguments and we should left `ksp` empty }