mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-25 15:40:28 +08:00
Introduce SerializableValue, SerializerAwareValue
This commit is contained in:
parent
68c0142765
commit
14d698651a
@ -11,10 +11,12 @@
|
||||
|
||||
package net.mamoe.mirai.console.setting
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import net.mamoe.mirai.console.setting.internal.cast
|
||||
import net.mamoe.mirai.console.setting.internal.valueFromKTypeImpl
|
||||
import net.mamoe.mirai.console.setting.internal.valueImpl
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalAPI
|
||||
import java.util.*
|
||||
import kotlin.internal.LowPriorityInOverloadResolution
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KType
|
||||
@ -25,10 +27,12 @@ import kotlin.reflect.typeOf
|
||||
// Shows public APIs such as deciding when to auto-save.
|
||||
abstract class Setting : SettingImpl() {
|
||||
|
||||
operator fun <T> Value<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): Value<T> {
|
||||
operator fun <T> SerializerAwareValue<T>.provideDelegate(
|
||||
thisRef: Any?,
|
||||
property: KProperty<*>
|
||||
): SerializerAwareValue<T> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
valueNodes.add(Node(property as KProperty<T>, value as Value<T>, TODO()))
|
||||
// TODO: 2020/6/21 track on
|
||||
valueNodes.add(Node(property as KProperty<T>, this, this.serializer))
|
||||
return this
|
||||
}
|
||||
}
|
||||
@ -43,18 +47,16 @@ internal abstract class SettingImpl {
|
||||
internal class Node<T>(
|
||||
val property: KProperty<T>,
|
||||
val value: Value<T>,
|
||||
val serializer: ValueSerializer<T>
|
||||
val updaterSerializer: KSerializer<Unit>
|
||||
)
|
||||
|
||||
internal val valueNodes: MutableList<Node<*>> = kotlin.run {
|
||||
TODO("reflection")
|
||||
}
|
||||
internal val valueNodes: MutableList<Node<*>> = Collections.synchronizedList(mutableListOf())
|
||||
|
||||
/**
|
||||
* flatten
|
||||
*/
|
||||
internal fun onValueChanged(value: Value<*>) {
|
||||
|
||||
// TODO: 2020/6/22
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +65,7 @@ internal abstract class SettingImpl {
|
||||
|
||||
// TODO: 2020/6/19 CODEGEN
|
||||
|
||||
fun Setting.value(default: Int): IntValue = valueImpl(default)
|
||||
fun Setting.value(default: Int): SerializableValue<Int> = valueImpl(default)
|
||||
|
||||
//// endregion Setting.value primitives CODEGEN ////
|
||||
|
||||
|
@ -29,6 +29,24 @@ interface Value<T> {
|
||||
var value: T
|
||||
}
|
||||
|
||||
/**
|
||||
* Typically returned by [Setting.value] functions.
|
||||
*/
|
||||
class SerializableValue<T>(
|
||||
delegate: Value<T>,
|
||||
/**
|
||||
* The serializer used to update and dump [delegate]
|
||||
*/
|
||||
override val serializer: KSerializer<Unit>
|
||||
) : Value<T> by delegate, SerializerAwareValue<T>
|
||||
|
||||
/**
|
||||
* @see SerializableValue
|
||||
*/
|
||||
interface SerializerAwareValue<T> : Value<T> {
|
||||
val serializer: KSerializer<Unit>
|
||||
}
|
||||
|
||||
inline operator fun <T> Value<T>.getValue(mySetting: Any?, property: KProperty<*>): T = value
|
||||
inline operator fun <T> Value<T>.setValue(mySetting: Any?, property: KProperty<*>, value: T) {
|
||||
this.value = value
|
||||
|
@ -9,7 +9,8 @@
|
||||
|
||||
package net.mamoe.mirai.console.setting.internal
|
||||
|
||||
import net.mamoe.mirai.console.setting.IntValue
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import net.mamoe.mirai.console.setting.SerializableValue
|
||||
import net.mamoe.mirai.console.setting.Setting
|
||||
|
||||
|
||||
@ -17,8 +18,14 @@ import net.mamoe.mirai.console.setting.Setting
|
||||
|
||||
// TODO: 2020/6/21 CODEGEN
|
||||
|
||||
internal fun Setting.valueImpl(default: Int): IntValue = object : IntValueImpl(default) {
|
||||
override fun onChanged() = this@valueImpl.onValueChanged(this)
|
||||
internal fun Setting.valueImpl(default: Int): SerializableValue<Int> {
|
||||
val instance = object : IntValueImpl(default) {
|
||||
override fun onChanged() = this@valueImpl.onValueChanged(this)
|
||||
}
|
||||
return SerializableValue(instance, Int.serializer().map(
|
||||
serializer = { instance.value },
|
||||
deserializer = { instance.value = it }
|
||||
))
|
||||
}
|
||||
|
||||
//// endregion Setting.value primitives impl CODEGEN ////
|
||||
|
@ -13,8 +13,6 @@ import kotlinx.serialization.*
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
|
||||
internal object SettingSerializerMark
|
||||
|
||||
internal val KProperty<*>.serialNameOrPropertyName: String get() = this.findAnnotation<SerialName>()?.value ?: this.name
|
||||
|
||||
internal inline fun <E> KSerializer<E>.bind(
|
||||
|
Loading…
Reference in New Issue
Block a user