From 74f56c1e7014748771dfa3d74a8263c2788add20 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 22 Aug 2020 17:06:22 +0800 Subject: [PATCH] Public API stabilization --- backend/mirai-console/build.gradle.kts | 1 + .../mamoe/mirai/console/data/PluginData.kt | 30 +++++-------------- .../mamoe/mirai/console/data/SerialName.kt | 22 ++++++++++++++ .../console/internal/data/PublishedApis.kt | 11 +++++++ .../internal/data/valueFromKTypeImpl.kt | 1 - 5 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/SerialName.kt create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PublishedApis.kt diff --git a/backend/mirai-console/build.gradle.kts b/backend/mirai-console/build.gradle.kts index ce210caa3..d53796664 100644 --- a/backend/mirai-console/build.gradle.kts +++ b/backend/mirai-console/build.gradle.kts @@ -52,6 +52,7 @@ kotlin { useExperimentalAnnotation("kotlin.experimental.ExperimentalTypeInference") useExperimentalAnnotation("kotlin.contracts.ExperimentalContracts") useExperimentalAnnotation("kotlinx.serialization.ExperimentalSerializationApi") + useExperimentalAnnotation("net.mamoe.mirai.console.data.ExperimentalPluginConfig") } } } 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 73e26021e..0ed42e12c 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 @@ -8,6 +8,7 @@ */ @file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "EXPOSED_SUPER_CLASS", "NOTHING_TO_INLINE") +@file:JvmName("PluginDataKt") package net.mamoe.mirai.console.data @@ -24,28 +25,6 @@ import kotlin.reflect.KClass import kotlin.reflect.KProperty import kotlin.reflect.KType - -/** - * 序列化之后的名称. - * - * 例: - * ``` - * @SerialName("accounts") - * object AccountPluginData : PluginData by ... { - * @SerialName("info") - * val map: Map by value("a" to "b") - * } - * ``` - * - * 将被保存为配置 (YAML 作为示例): - * ```yaml - * accounts: - * info: - * a: b - * ``` - */ -public typealias SerialName = kotlinx.serialization.SerialName - /** * 一个插件内部的, 对用户隐藏的数据对象. 可包含对多个 [Value] 的值变更的跟踪. * @@ -210,7 +189,12 @@ public inline fun PluginData.value(default: T): SerializerAwareValue */ @LowPriorityInOverloadResolution public inline fun PluginData.value(): SerializerAwareValue = - value(T::class.run { objectInstance ?: createInstanceSmart() } as T) + valueImpl(typeOf0(), T::class) + +@Suppress("UNCHECKED_CAST") +@PublishedApi +internal fun PluginData.valueImpl(type: KType, classifier: KClass<*>): SerializerAwareValue = + valueFromKType(type, classifier.run { objectInstance ?: createInstanceSmart() } as T) /** * 通过一个特定的 [KType] 创建 [Value], 并设置初始值. diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/SerialName.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/SerialName.kt new file mode 100644 index 000000000..9930f9ef4 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/SerialName.kt @@ -0,0 +1,22 @@ +package net.mamoe.mirai.console.data + +/** + * 序列化之后的名称. + * + * 例: + * ``` + * @SerialName("accounts") + * object AccountPluginData : PluginData by ... { + * @SerialName("info") + * val map: Map by value("a" to "b") + * } + * ``` + * + * 将被保存为配置 (YAML 作为示例): + * ```yaml + * accounts: + * info: + * a: b + * ``` + */ +public typealias SerialName = kotlinx.serialization.SerialName diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PublishedApis.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PublishedApis.kt new file mode 100644 index 000000000..0867aaae7 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/data/PublishedApis.kt @@ -0,0 +1,11 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.internal.data + 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 4bfb9ed43..2f6a7c5c6 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 @@ -28,7 +28,6 @@ private val primitiveCollectionsImplemented by lazy { @OptIn(ExperimentalStdlibApi::class) internal inline fun typeOf0(): KType = kotlin.reflect.typeOf() -@PublishedApi @Suppress("UnsafeCall", "SMARTCAST_IMPOSSIBLE", "UNCHECKED_CAST") internal fun PluginData.valueFromKTypeImpl(type: KType): SerializerAwareValue<*> { val classifier = type.classifier