Skip to content

Commit

Permalink
Merge pull request #900 from simple-robot/support-config-base-seriali…
Browse files Browse the repository at this point in the history
…zeModule

ApplicationBuilder中新增可配置项 `serializersModule` 以允许配置一个自定义的序列化模块'基底'
  • Loading branch information
ForteScarlet authored Aug 5, 2024
2 parents fe003d6 + 34cfe9e commit a8796a3
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 15 deletions.
7 changes: 7 additions & 0 deletions simbot-api/api/simbot-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ public final class love/forte/simbot/ability/StandardDeleteOption$StandardAnalys
public abstract class love/forte/simbot/application/AbstractApplicationBuilder : love/forte/simbot/application/ApplicationBuilder {
public fun <init> ()V
public fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
public fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public fun setCoroutineContext (Lkotlin/coroutines/CoroutineContext;)V
public fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)V
}

public abstract class love/forte/simbot/application/AbstractApplicationEventRegistrar : love/forte/simbot/application/ApplicationEventRegistrar {
Expand Down Expand Up @@ -189,11 +191,14 @@ public abstract interface class love/forte/simbot/application/Application : kotl

public abstract interface class love/forte/simbot/application/ApplicationBuilder {
public abstract fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
public abstract fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
public abstract fun setCoroutineContext (Lkotlin/coroutines/CoroutineContext;)V
public abstract fun setSerializersModule (Lkotlinx/serialization/modules/SerializersModule;)V
}

public abstract interface class love/forte/simbot/application/ApplicationConfiguration {
public abstract fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
public abstract fun getSerializersModule ()Lkotlinx/serialization/modules/SerializersModule;
}

public abstract interface class love/forte/simbot/application/ApplicationEventHandler {
Expand Down Expand Up @@ -847,6 +852,8 @@ public abstract interface class love/forte/simbot/component/ComponentInstaller {

public final class love/forte/simbot/component/ComponentUtil {
public static final fun toComponents (Ljava/util/Collection;)Llove/forte/simbot/component/Components;
public static final fun toComponents (Ljava/util/Collection;Lkotlinx/serialization/modules/SerializersModule;)Llove/forte/simbot/component/Components;
public static synthetic fun toComponents$default (Ljava/util/Collection;Lkotlinx/serialization/modules/SerializersModule;ILjava/lang/Object;)Llove/forte/simbot/component/Components;
}

public abstract interface class love/forte/simbot/component/Components : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Project https://github.com/simple-robot/simpler-robot
* Email [email protected]
*
* This file is part of the Simple Robot Library.
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -24,7 +24,11 @@
package love.forte.simbot.application

import kotlinx.coroutines.Job
import kotlinx.serialization.modules.EmptySerializersModule
import kotlinx.serialization.modules.SerializersModule
import love.forte.simbot.common.coroutines.linkTo
import love.forte.simbot.component.Components
import love.forte.simbot.plugin.Plugins
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

Expand All @@ -51,6 +55,13 @@ public interface ApplicationConfiguration {
*/
public val coroutineContext: CoroutineContext

/**
* [ApplicationBuilder.serializersModule] 所配置的**基础**序列化模块信息。
* 不包含 [Components] 中的所有其他组件合并后的结果。
*
* @since 4.5.0
*/
public val serializersModule: SerializersModule
}

/**
Expand All @@ -67,6 +78,20 @@ public interface ApplicationBuilder {
* @see ApplicationConfiguration.coroutineContext
*/
public var coroutineContext: CoroutineContext

/**
* 一个用于 [Components.serializersModule] 的基础序列化模块,
* [Components] 中所有组件的 [SerializersModule] 聚合完成后,
* 会以此 [serializersModule] 为基准构建 [Components.serializersModule]。
*
* ```kotlin
* val finalModule =
* parentSerializersModule overwriteWith allComponentsSerializersModule
* ```
*
* @since 4.5.0
*/
public var serializersModule: SerializersModule
}

/**
Expand All @@ -75,4 +100,5 @@ public interface ApplicationBuilder {
*/
public abstract class AbstractApplicationBuilder : ApplicationBuilder {
override var coroutineContext: CoroutineContext = EmptyCoroutineContext
override var serializersModule: SerializersModule = EmptySerializersModule()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

package love.forte.simbot.component

import kotlinx.serialization.modules.EmptySerializersModule
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.overwriteWith
import love.forte.simbot.common.collection.toImmutable
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads

/**
* 用于表示一组 [Component] 。
Expand Down Expand Up @@ -63,17 +66,24 @@ public inline fun <reified C : Component> Components.get(): C =
/**
* 将一个 [Component] 的集合转化为 [Components]。
*/
public fun Collection<Component>.toComponents(): Components = CollectionComponents(toImmutable())
@JvmOverloads
public fun Collection<Component>.toComponents(
parentSerializersModule: SerializersModule = EmptySerializersModule()
): Components =
CollectionComponents(toImmutable(), parentSerializersModule)

/**
* @see Components
*/
private class CollectionComponents(private val collections: Collection<Component>) :
Components,
private class CollectionComponents(
private val collections: Collection<Component>,
parentSerializersModule: SerializersModule
) : Components,
Collection<Component> by collections {
override val serializersModule: SerializersModule = SerializersModule {
collections.forEach { include(it.serializersModule) }
}
override val serializersModule: SerializersModule =
parentSerializersModule overwriteWith SerializersModule {
collections.forEach { include(it.serializersModule) }
}

override fun toString(): String = "Components(values=$collections)"
override fun equals(other: Any?): Boolean {
Expand Down
3 changes: 2 additions & 1 deletion simbot-api/src/commonTest/kotlin/ComponentTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Project https://github.com/simple-robot/simpler-robot
* Email [email protected]
*
* This file is part of the Simple Robot Library.
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -65,6 +65,7 @@ class ComponentTests {
private val testContext = object : ComponentConfigureContext {
override val applicationConfiguration: ApplicationConfiguration = object : ApplicationConfiguration {
override val coroutineContext: CoroutineContext = EmptyCoroutineContext
override val serializersModule: SerializersModule = EmptySerializersModule()
}

override val applicationEventRegistrar: ApplicationEventRegistrar = object : ApplicationEventRegistrar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
requires transitive simbot.core;
requires static java.annotation;
requires kotlinx.coroutines.core;
requires kotlinx.serialization.core;

exports love.forte.simbot.spring.common.application;
exports love.forte.simbot.spring.common;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.serialization.modules.SerializersModule
import love.forte.simbot.annotations.InternalSimbotAPI
import love.forte.simbot.application.*
import love.forte.simbot.core.event.SimpleEventDispatcherConfiguration
Expand Down Expand Up @@ -73,7 +74,8 @@ public open class SpringApplicationBuilder : AbstractApplicationBuilder() {

return SpringApplicationConfigurationImpl(
context.minusKey(Job) + job,
applicationConfigurationProperties
serializersModule,
applicationConfigurationProperties,
)
}

Expand Down Expand Up @@ -119,5 +121,6 @@ public interface SpringEventDispatcherConfiguration : SimpleEventDispatcherConfi
@InternalSimbotAPI
public open class SpringApplicationConfigurationImpl(
override val coroutineContext: CoroutineContext,
override val serializersModule: SerializersModule,
override val applicationConfigurationProperties: SpringApplicationConfigurationProperties,
) : SpringApplicationConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requires kotlin.stdlib;
requires kotlin.reflect;
requires kotlinx.coroutines.core;
requires kotlinx.serialization.core;
requires kotlinx.serialization.json;
// spring
requires spring.core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public object Spring : SpringApplicationFactory {

override val applicationEventRegistrar: ApplicationEventRegistrar
get() = registrar
}).toComponents()
}).toComponents(
parentSerializersModule = configuration.serializersModule
)

// plugins
val pluginCollections = springConfigurer.pluginFactoriesConfigurator.createAll(object : PluginConfigureContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
requires kotlin.stdlib;
requires kotlin.reflect;
requires kotlinx.coroutines.core;
requires kotlinx.serialization.core;
requires kotlinx.serialization.json;
// spring
requires spring.core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public object Spring : SpringApplicationFactory {

override val applicationEventRegistrar: ApplicationEventRegistrar
get() = registrar
}).toComponents()
}).toComponents(
parentSerializersModule = configuration.serializersModule
)

// plugins
val pluginCollections = springConfigurer.pluginFactoriesConfigurator.createAll(object : PluginConfigureContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package love.forte.simbot.core.application
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.serialization.modules.SerializersModule
import love.forte.simbot.ability.OnCompletion
import love.forte.simbot.annotations.ExperimentalSimbotAPI
import love.forte.simbot.application.*
Expand Down Expand Up @@ -183,7 +184,9 @@ public object Simple :

override val applicationEventRegistrar: ApplicationEventRegistrar
get() = registrar
}).toComponents()
}).toComponents(
parentSerializersModule = configuration.serializersModule
)

// plugins
val pluginCollections = simpleConfigurer.pluginFactoriesConfigurator.createAll(object : PluginConfigureContext {
Expand Down Expand Up @@ -266,12 +269,17 @@ public class SimpleApplicationBuilder : AbstractApplicationBuilder() {
val job = SupervisorJob(context[Job])

// 至少有个 Job
return SimpleApplicationConfigurationImpl(context.minusKey(Job) + job)
return SimpleApplicationConfigurationImpl(
coroutineContext = context.minusKey(Job) + job,
serializersModule = serializersModule
)
}
}

private class SimpleApplicationConfigurationImpl(override val coroutineContext: CoroutineContext) :
SimpleApplicationConfiguration
private class SimpleApplicationConfigurationImpl(
override val coroutineContext: CoroutineContext,
override val serializersModule: SerializersModule
) : SimpleApplicationConfiguration

private class SimpleApplicationLauncherImpl(
private val applicationCreator: () -> SimpleApplicationImpl
Expand Down
Loading

0 comments on commit a8796a3

Please sign in to comment.