start 0.18.2 and make json of Startup customizable

This commit is contained in:
InsanusMokrassar 2023-05-09 01:38:04 +06:00
parent 3e366ea73b
commit 9fb62e1e25
4 changed files with 69 additions and 2 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 0.18.2
* `Startup`:
* Now internal `Json` is fully customizable
## 0.18.1
* `Common`:

View File

@ -2,6 +2,7 @@ plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "application"
id "com.google.devtools.ksp"
}
apply from: "$mppJvmJsLinuxMingwProjectPresetPath"
@ -11,6 +12,7 @@ kotlin {
commonMain {
dependencies {
api internalProject("micro_utils.startup.plugin")
api internalProject("micro_utils.koin")
}
}
commonTest {
@ -29,3 +31,10 @@ java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
dependencies {
add("kspCommonMainMetadata", project(":micro_utils.koin.generator"))
}
ksp {
}

View File

@ -0,0 +1,38 @@
// THIS CODE HAVE BEEN GENERATED AUTOMATICALLY
// TO REGENERATE IT JUST DELETE FILE
// ORIGINAL FILE: StartLauncherPlugin.kt
package dev.inmo.micro_utils.startup.launcher
import kotlin.Boolean
import kotlinx.serialization.json.Json
import org.koin.core.Koin
import org.koin.core.definition.Definition
import org.koin.core.definition.KoinDefinition
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import org.koin.core.scope.Scope
/**
* @return Definition by key "baseJsonProvider"
*/
public val Scope.baseJsonProvider: Json?
get() = getOrNull(named("baseJsonProvider"))
/**
* @return Definition by key "baseJsonProvider"
*/
public val Koin.baseJsonProvider: Json?
get() = getOrNull(named("baseJsonProvider"))
/**
* Will register [definition] with [org.koin.core.module.Module.single] and key "baseJsonProvider"
*/
public fun Module.baseJsonProviderSingle(createdAtStart: Boolean = false, definition: Definition<Json>):
KoinDefinition<Json> = single(named("baseJsonProvider"), createdAtStart = createdAtStart, definition
= definition)
/**
* Will register [definition] with [org.koin.core.module.Module.factory] and key "baseJsonProvider"
*/
public fun Module.baseJsonProviderFactory(definition: Definition<Json>): KoinDefinition<Json> =
factory(named("baseJsonProvider"), definition = definition)

View File

@ -1,9 +1,11 @@
@file:GenerateKoinDefinition("baseJsonProvider", Json::class)
package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i
import dev.inmo.kslog.common.taggedLogger
import dev.inmo.kslog.common.w
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.koin.annotations.GenerateKoinDefinition
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.setupDI
import dev.inmo.micro_utils.startup.launcher.StartLauncherPlugin.startPlugin
import dev.inmo.micro_utils.startup.plugin.StartPlugin
@ -13,9 +15,10 @@ import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.serialization.SerialFormat
import kotlinx.serialization.StringFormat
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.modules.SerializersModule
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.context.startKoin
@ -35,7 +38,19 @@ object StartLauncherPlugin : StartPlugin {
single { rawJsonObject }
single { config }
single { CoroutineScope(Dispatchers.Default) }
single { defaultJson } binds arrayOf(StringFormat::class, SerialFormat::class)
single {
val serializersModules = getAll<SerializersModule>().distinct()
val baseJson = baseJsonProvider ?: defaultJson
if (serializersModules.isEmpty()) {
baseJson
} else {
Json(baseJson) {
SerializersModule {
serializersModules.forEach { include(it) }
}
}
}
} binds arrayOf(StringFormat::class, SerialFormat::class)
includes(
config.plugins.mapNotNull {