This commit is contained in:
Him188 2020-06-24 04:31:30 +08:00
parent 1186083f09
commit 0dd274fc4e
5 changed files with 19 additions and 9 deletions

View File

@ -17,7 +17,6 @@ import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
import kotlin.reflect.KType import kotlin.reflect.KType
// TODO: 2020/6/21 move to JvmPlugin to inherit SettingStorage and CoroutineScope for saving
// Shows public APIs such as deciding when to auto-save. // Shows public APIs such as deciding when to auto-save.
abstract class Setting : SettingImpl() { abstract class Setting : SettingImpl() {
operator fun <T> SerializerAwareValue<T>.provideDelegate( operator fun <T> SerializerAwareValue<T>.provideDelegate(

View File

@ -117,6 +117,8 @@ internal fun <K, V> Setting.createCompositeMapValueImpl(
} }
} }
// TODO: 2020/6/24 在一个 Value 被删除后停止追踪其更新.
internal abstract class CompositeMapValueImpl<K, V>( internal abstract class CompositeMapValueImpl<K, V>(
kToValue: (K) -> Value<K>, // should override onChanged kToValue: (K) -> Value<K>, // should override onChanged
vToValue: (V) -> Value<V> // should override onChanged vToValue: (V) -> Value<V> // should override onChanged

View File

@ -147,7 +147,14 @@ internal inline fun <R, T> T.cast(): R = this as R
* Copied from kotlinx.serialization, modifications are marked with "/* mamoe modify */" * Copied from kotlinx.serialization, modifications are marked with "/* mamoe modify */"
* Copyright 2017-2020 JetBrains s.r.o. * Copyright 2017-2020 JetBrains s.r.o.
*/ */
@Suppress("UNCHECKED_CAST", "NO_REFLECTION_IN_CLASS_PATH", "UNSUPPORTED", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") @Suppress(
"UNCHECKED_CAST",
"NO_REFLECTION_IN_CLASS_PATH",
"UNSUPPORTED",
"INVISIBLE_MEMBER",
"INVISIBLE_REFERENCE",
"IMPLICIT_CAST_TO_ANY"
)
@OptIn(ImplicitReflectionSerializer::class) @OptIn(ImplicitReflectionSerializer::class)
internal fun serializerMirai(type: KType): KSerializer<Any?> { internal fun serializerMirai(type: KType): KSerializer<Any?> {
fun serializerByKTypeImpl(type: KType): KSerializer<Any> { fun serializerByKTypeImpl(type: KType): KSerializer<Any> {

View File

@ -14,6 +14,7 @@ package net.mamoe.mirai.console.setting.internal
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.builtins.MapSerializer import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.serializer import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.setting.Setting
import net.mamoe.mirai.console.setting.Value import net.mamoe.mirai.console.setting.Value
import net.mamoe.yamlkt.YamlNullableDynamicSerializer import net.mamoe.yamlkt.YamlNullableDynamicSerializer
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@ -26,7 +27,6 @@ internal val KProperty<*>.serialName: String get() = this.findAnnotation<SerialN
* - Reflection on Kotlin properties and Java fields * - Reflection on Kotlin properties and Java fields
* - Auto-saving * - Auto-saving
*/ */
// TODO move to internal package.
internal abstract class SettingImpl { internal abstract class SettingImpl {
internal fun findNodeInstance(name: String): Node<*>? = valueNodes.firstOrNull { it.serialName == name } internal fun findNodeInstance(name: String): Node<*>? = valueNodes.firstOrNull { it.serialName == name }
@ -110,9 +110,7 @@ internal abstract class SettingImpl {
/** /**
* flatten * flatten
*/ */
internal fun onValueChanged(value: Value<*>) { abstract fun onValueChanged(value: Value<*>)
// TODO: 2020/6/22
}
companion object { companion object {
private val settingUpdaterSerializerTypeArguments = arrayOf(String.serializer(), YamlNullableDynamicSerializer) private val settingUpdaterSerializerTypeArguments = arrayOf(String.serializer(), YamlNullableDynamicSerializer)

View File

@ -20,8 +20,12 @@ internal class SettingTest {
class MySetting : Setting() { class MySetting : Setting() {
var int by value(1) var int by value(1)
val map by value(mapOf("" to "")) val map by value<MutableMap<String, String>>()
val map2 by value(mutableMapOf("" to mutableMapOf("" to ""))) val map2 by value<MutableMap<String, MutableMap<String, String>>>()
override fun onValueChanged(value: Value<*>) {
}
} }
@OptIn(UnstableDefault::class) @OptIn(UnstableDefault::class)
@ -65,7 +69,7 @@ internal class SettingTest {
val refBefore = setting.map val refBefore = setting.map
fun reference() = refBefore fun reference() = refBefore
assertEquals(mapOf(), delegation()) // delegation assertEquals(mutableMapOf(), delegation()) // delegation
json.parse( json.parse(
setting.updaterSerializer, """ setting.updaterSerializer, """