diff --git a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt index 7ad275465..54ddc3179 100644 --- a/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt +++ b/backend/codegen/src/main/kotlin/net/mamoe/mirai/console/codegen/ValueSettingCodegen.kt @@ -29,7 +29,7 @@ internal object ValueSettingCodegen { appendKCode( """ /** - * Represents a non-null [$ktType] value. + * 表示一个不可空 [$ktType] [Value]. */ public interface ${ktType}Value : PrimitiveValue<$ktType> """ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt index 47073be6e..f780b7752 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Setting.kt @@ -180,6 +180,14 @@ public inline fun Setting.value(default: T): SerializerAwareValue /** * 通过具体化类型创建一个 [SerializerAwareValue]. + * @see valueFromKType 查看更多实现信息 + */ +@LowPriorityInOverloadResolution +public inline fun Setting.value(): SerializerAwareValue = + value(T::class.run { objectInstance ?: createInstanceSmart() } as T) + +/** + * 通过一个特定的 [KType] 创建 [Value], 并设置初始值. * * 对于 [List], [Map], [Set] 等标准库类型, 这个函数会尝试构造 [LinkedHashMap] 等相关类型. * 而对于自定义数据类型, 本函数只会反射获取 [objectInstance][KClass.objectInstance] 或使用无参构造器构造实例. @@ -190,13 +198,6 @@ public inline fun Setting.value(default: T): SerializerAwareValue * - 标准库数据类型 ([Map.Entry], [Pair], [Triple]) * - 和使用 [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) 的 [Serializable] 标记的 */ -@LowPriorityInOverloadResolution -public inline fun Setting.value(): SerializerAwareValue = - value(T::class.run { objectInstance ?: createInstanceSmart() } as T) - -/** - * Creates a [Value] with specified [KType], and set default value. - */ @Suppress("UNCHECKED_CAST") @ConsoleExperimentalAPI public fun Setting.valueFromKType(type: KType, default: T): SerializerAwareValue = diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt index 8120af880..b37bcd6f2 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt @@ -20,21 +20,21 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import kotlin.reflect.KProperty /** - * Represents a observable, immutable value wrapping. + * 表示一个可被观测的, 不可变的值包装. * - * The value can be modified by delegation just like Kotlin's `var`, however it can also be done by the user, e.g. changing using the UI frontend. + * [Value.value] 可以像 Kotlin 的 `var` 一样被修改, 然而它也可能被用户修改, 如通过 UI 前端. * - * Some frequently used types are specially treated with performance enhancement by codegen. + * 一些常用的基础类型实现由代码生成创建特性的优化. * - * @see PrimitiveValue - * @see CompositeValue + * @see PrimitiveValue 基础数据类型实现 + * @see CompositeValue 复合数据类型实现 */ public interface Value { public var value: T } /** - * Typically returned by [Setting.value] functions. + * 可被序列化的 [Value]. */ public class SerializableValue( private val delegate: Value, @@ -66,59 +66,74 @@ public class SerializableValue( * @see Setting.value 创建一个这样的 [SerializerAwareValue] */ public interface SerializerAwareValue : Value { + /** + * 用于更新 [value] 的序列化器. 在反序列化时不会创建新的 [T] 对象实例. + * + * - 序列化: `val text: String = Yaml.default.encodeToString(serializer, Unit)` + * - 反序列化 (本质上是更新 [value], 不创建新的 [T] 实例): `Yaml.default.decodeFromString(serializer, text)` + */ public val serializer: KSerializer public companion object { + /** + * 使用 [指定格式格式][format] 序列化一个 [SerializerAwareValue] + */ @JvmStatic - @ConsoleExperimentalAPI("will be changed due to reconstruction of kotlinx.serialization") public fun SerializerAwareValue.serialize(format: StringFormat): String { return format.encodeToString(this.serializer, Unit) } + /** + * 使用 [指定格式格式][format] 序列化一个 [SerializerAwareValue] + */ @JvmStatic - @ConsoleExperimentalAPI("will be changed due to reconstruction of kotlinx.serialization") public fun SerializerAwareValue.serialize(format: BinaryFormat): ByteArray { return format.encodeToByteArray(this.serializer, Unit) } + /** + * 使用 [指定格式格式][format] 反序列化 (更新) 一个 [SerializerAwareValue] + */ @JvmStatic - @ConsoleExperimentalAPI("will be changed due to reconstruction of kotlinx.serialization") - public fun SerializerAwareValue.deserialize(format: StringFormat, value: String) { - format.decodeFromString(this.serializer, value) + public fun SerializerAwareValue.deserialize(format: StringFormat, string: String) { + format.decodeFromString(this.serializer, string) } + /** + * 使用 [指定格式格式][format] 反序列化 (更新) 一个 [SerializerAwareValue] + */ @JvmStatic - @ConsoleExperimentalAPI("will be changed due to reconstruction of kotlinx.serialization") - public fun SerializerAwareValue.deserialize(format: BinaryFormat, value: ByteArray) { - format.decodeFromByteArray(this.serializer, value) + public fun SerializerAwareValue.deserialize(format: BinaryFormat, bytes: ByteArray) { + format.decodeFromByteArray(this.serializer, bytes) } } } +/** + * 用于支持属性委托 + */ @JvmSynthetic public inline operator fun Value.getValue(mySetting: Any?, property: KProperty<*>): T = value +/** + * 用于支持属性委托 + */ @JvmSynthetic public inline operator fun Value.setValue(mySetting: Any?, property: KProperty<*>, value: T) { this.value = value } /** - * The serializer for a specific kind of [Value]. - */ -public typealias ValueSerializer = KSerializer> - -/** - * Represents a observable *primitive* value wrapping. + * 基础数据类型 [Value] * - * 9 types that are considered *primitive*: - * - Integers: [Byte], [Short], [Int], [Long] - * - Floating: [Float], [Double] + * 9 个被认为是 *基础类型* 的类型: + * - 整数: [Byte], [Short], [Int], [Long] + * - 浮点: [Float], [Double] * - [Boolean] * - [Char], [String] * - * Note: The values are actually *boxed* because of the generic type T. - * *Primitive* indicates only it is one of the 9 types mentioned above. + * 注意: 目前这些类型都会被装箱, 由于泛型 T. 在将来可能会有优化处理. + * *Primitive* 仅表示一个类型是上面 9 种类型之一. */ public interface PrimitiveValue : Value @@ -126,71 +141,77 @@ public interface PrimitiveValue : Value //// region PrimitiveValues CODEGEN //// /** - * Represents a non-null [Byte] value. + * 表示一个不可空 [Byte] [Value]. */ public interface ByteValue : PrimitiveValue /** - * Represents a non-null [Short] value. + * 表示一个不可空 [Short] [Value]. */ public interface ShortValue : PrimitiveValue /** - * Represents a non-null [Int] value. + * 表示一个不可空 [Int] [Value]. */ public interface IntValue : PrimitiveValue /** - * Represents a non-null [Long] value. + * 表示一个不可空 [Long] [Value]. */ public interface LongValue : PrimitiveValue /** - * Represents a non-null [Float] value. + * 表示一个不可空 [Float] [Value]. */ public interface FloatValue : PrimitiveValue /** - * Represents a non-null [Double] value. + * 表示一个不可空 [Double] [Value]. */ public interface DoubleValue : PrimitiveValue /** - * Represents a non-null [Char] value. + * 表示一个不可空 [Char] [Value]. */ public interface CharValue : PrimitiveValue /** - * Represents a non-null [Boolean] value. + * 表示一个不可空 [Boolean] [Value]. */ public interface BooleanValue : PrimitiveValue /** - * Represents a non-null [String] value. + * 表示一个不可空 [String] [Value]. */ public interface StringValue : PrimitiveValue //// endregion PrimitiveValues CODEGEN //// +/** + * 复合数据类型实现 + */ @ConsoleExperimentalAPI public interface CompositeValue : Value /** - * Superclass of [CompositeListValue], [PrimitiveListValue]. + * @see [CompositeListValue] + * @see [PrimitiveListValue] */ public interface ListValue : CompositeValue> /** - * Elements can by anything, wrapped as [Value]. - * @param E is not primitive types. + * 复合数据类型的 [List] + * + * @param E 不是基础数据类型 */ public interface CompositeListValue : ListValue /** - * Elements can only be primitives, not wrapped. - * @param E is not primitive types. + * 针对基础类型优化的 [List] + * + * @param E 是基础类型 */ public interface PrimitiveListValue : ListValue @@ -205,19 +226,20 @@ public interface PrimitiveLongListValue : PrimitiveListValue /** - * Superclass of [CompositeSetValue], [PrimitiveSetValue]. + * @see [CompositeSetValue] + * @see [PrimitiveSetValue] */ public interface SetValue : CompositeValue> /** - * Elements can by anything, wrapped as [Value]. - * @param E is not primitive types. + * 复合数据类型 [Set] + * @param E 是基础数据类型 */ public interface CompositeSetValue : SetValue /** - * Elements can only be primitives, not wrapped. - * @param E is not primitive types. + * 基础数据类型 [Set] + * @param E 是基础数据类型 */ public interface PrimitiveSetValue : SetValue @@ -232,7 +254,8 @@ public interface PrimitiveLongSetValue : PrimitiveSetValue /** - * Superclass of [CompositeMapValue], [PrimitiveMapValue]. + * @see [CompositeMapValue] + * @see [PrimitiveMapValue] */ public interface MapValue : CompositeValue>