diff --git a/CHANGELOG.md b/CHANGELOG.md index 5965bb0e95d..2fa1754ab7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.18.2 + +* `Startup`: + * Now internal `Json` is fully customizable + ## 0.18.1 * `Common`: diff --git a/startup/launcher/build.gradle b/startup/launcher/build.gradle index 38fe1179d7a..ac47e384790 100644 --- a/startup/launcher/build.gradle +++ b/startup/launcher/build.gradle @@ -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 { +} diff --git a/startup/launcher/src/commonMain/kotlin/GeneratedDefinitionsStartLauncherPlugin.kt b/startup/launcher/src/commonMain/kotlin/GeneratedDefinitionsStartLauncherPlugin.kt new file mode 100644 index 00000000000..52d5b7e3dae --- /dev/null +++ b/startup/launcher/src/commonMain/kotlin/GeneratedDefinitionsStartLauncherPlugin.kt @@ -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): + KoinDefinition = 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): KoinDefinition = + factory(named("baseJsonProvider"), definition = definition) diff --git a/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt b/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt index e9e14facee2..ccae5011089 100644 --- a/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt +++ b/startup/launcher/src/commonMain/kotlin/StartLauncherPlugin.kt @@ -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().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 {