From 73c28c6cbf2ac5578ed8418b91362cf1a3d4d5db Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 27 Aug 2020 23:18:36 +0800 Subject: [PATCH] Support custom data classes in PluginData --- .../mamoe/mirai/console/data/PluginData.kt | 2 -- .../net/mamoe/mirai/console/data/Value.kt | 23 +++++++++++++------ .../internal/MiraiConsoleBuildConstants.kt | 4 ++-- .../internal/data/_PluginData.value.kt | 17 ++++++++++++++ .../data/_PrimitiveValueDeclarations.kt | 3 ++- .../internal/data/valueFromKTypeImpl.kt | 5 +++- 6 files changed, 41 insertions(+), 13 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt index d15adccfd..26ec263bf 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/PluginData.kt @@ -362,5 +362,3 @@ internal fun PluginData.valueImpl(type: KType, classifier: KClass<*>): Seria @ConsoleExperimentalAPI public fun PluginData.valueFromKType(type: KType, default: T): SerializerAwareValue = (valueFromKTypeImpl(type) as SerializerAwareValue).apply { this.value = default } as SerializerAwareValue - -// TODO: 2020/6/24 Introduce class TypeToken for compound types for Java. \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt index 2aa802f1e..e6ae1a52e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/Value.kt @@ -189,6 +189,7 @@ public interface ListValue : CompositeValue> * * @param E 不是基础数据类型 */ +@ConsoleExperimentalAPI public interface CompositeListValue : ListValue /** @@ -196,12 +197,16 @@ public interface CompositeListValue : ListValue * * @param E 是基础类型 */ +@ConsoleExperimentalAPI public interface PrimitiveListValue : ListValue //// region PrimitiveListValue CODEGEN //// +@ConsoleExperimentalAPI public interface PrimitiveIntListValue : PrimitiveListValue + +@ConsoleExperimentalAPI public interface PrimitiveLongListValue : PrimitiveListValue // TODO + codegen @@ -212,24 +217,30 @@ public interface PrimitiveLongListValue : PrimitiveListValue * @see [CompositeSetValue] * @see [PrimitiveSetValue] */ +@ConsoleExperimentalAPI public interface SetValue : CompositeValue> /** * 复合数据类型 [Set] * @param E 是基础数据类型 */ +@ConsoleExperimentalAPI public interface CompositeSetValue : SetValue /** * 基础数据类型 [Set] * @param E 是基础数据类型 */ +@ConsoleExperimentalAPI public interface PrimitiveSetValue : SetValue //// region PrimitiveSetValue CODEGEN //// +@ConsoleExperimentalAPI public interface PrimitiveIntSetValue : PrimitiveSetValue + +@ConsoleExperimentalAPI public interface PrimitiveLongSetValue : PrimitiveSetValue // TODO + codegen @@ -252,17 +263,15 @@ public interface PrimitiveMapValue : MapValue //// region PrimitiveMapValue CODEGEN //// +@ConsoleExperimentalAPI public interface PrimitiveIntIntMapValue : PrimitiveMapValue + +@ConsoleExperimentalAPI public interface PrimitiveIntLongMapValue : PrimitiveMapValue // TODO + codegen //// endregion PrimitiveSetValue CODEGEN //// - - - - - - - +@ConsoleExperimentalAPI +public interface ReferenceValue : Value \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt index 5d4692d3f..b1a779868 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleBuildConstants.kt @@ -14,8 +14,8 @@ import java.time.Instant internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants) @JvmStatic - val buildDate: Instant = Instant.ofEpochSecond(0) + val buildDate: Instant = Instant.ofEpochSecond(1598539421) @JvmStatic - val version: Semver = Semver("0", Semver.SemverType.LOOSE) + val version: Semver = Semver("1.0-M3-1", Semver.SemverType.LOOSE) } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt index 505805e55..8dad18e32 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PluginData.value.kt @@ -11,6 +11,7 @@ package net.mamoe.mirai.console.internal.data import kotlinx.serialization.builtins.serializer import net.mamoe.mirai.console.data.PluginData +import net.mamoe.mirai.console.data.ReferenceValue import net.mamoe.mirai.console.data.SerializerAwareValue import kotlin.reflect.KClass @@ -180,3 +181,19 @@ internal fun PluginData.stringValueImpl(): SerializerAwareValue { } //// endregion PluginData_value_PrimitivesImpl CODEGEN //// + +internal class LazyReferenceValueImpl : ReferenceValue, AbstractValueImpl() { + private var initialied: Boolean = false + private var valueField: T? = null + + @Suppress("UNCHECKED_CAST") + override var value: T + get() { + check(initialied) { "Internal error: LazyReferenceValueImpl.valueField isn't initialized" } + return valueField as T + } + set(value) { + initialied = true + valueField = value + } +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PrimitiveValueDeclarations.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PrimitiveValueDeclarations.kt index a45938375..44c11ee9f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PrimitiveValueDeclarations.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/_PrimitiveValueDeclarations.kt @@ -25,7 +25,8 @@ internal abstract class AbstractValueImpl : Value { } } -internal fun Value.setValueBySerializer(value: T) = (this as AbstractValueImpl).setValueBySerializer(value) +internal fun Value.setValueBySerializer(value: T) = + (this.castOrInternalError>()).setValueBySerializer(value) //// region PrimitiveValuesImpl CODEGEN //// diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt index 72f5bd28d..c0673e76c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/valueFromKTypeImpl.kt @@ -96,7 +96,10 @@ internal fun PluginData.valueFromKTypeImpl(type: KType): SerializerAwareValue<*> .serializableValueWith(serializerMirai(type) as KSerializer>) } } - else -> error("Custom composite value is not supported yet (${classifier.qualifiedName})") + else -> { + val serializer = serializerMirai(type) + return LazyReferenceValueImpl().serializableValueWith(serializer) + } } }