Introduce SerializableValue, SerializerAwareValue

This commit is contained in:
Him188 2020-06-22 13:50:34 +08:00
parent 68c0142765
commit 14d698651a
4 changed files with 39 additions and 14 deletions

View File

@ -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 ////

View File

@ -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

View File

@ -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 ////

View File

@ -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(