From 2c09f1d82da52c1ebc57f90aad5f000b1c9512f6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 21 Jun 2020 22:48:34 +0800 Subject: [PATCH] value delegating --- .../net/mamoe/mirai/console/setting/Setting.kt | 14 +++++++++++--- .../net/mamoe/mirai/console/setting/Value.kt | 8 +++++++- .../console/setting/internal/serializerUtil.kt | 5 ----- .../net/mamoe/mirai/console/setting/SettingTest.kt | 6 ++++++ 4 files changed, 24 insertions(+), 9 deletions(-) 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 923a4d890..cac3cace6 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 @@ -23,7 +23,15 @@ import kotlin.reflect.typeOf // TODO: 2020/6/21 move to JvmPlugin to inherit SettingStorage and CoroutineScope for saving // Shows public APIs such as deciding when to auto-save. -abstract class Setting : SettingImpl() +abstract class Setting : SettingImpl() { + + operator fun Value.provideDelegate(thisRef: Any?, property: KProperty<*>): Value { + @Suppress("UNCHECKED_CAST") + valueNodes.add(Node(property as KProperty, value as Value, TODO())) + // TODO: 2020/6/21 track on + return this + } +} /** * Internal implementation for [Setting] including: @@ -32,13 +40,13 @@ abstract class Setting : SettingImpl() */ // TODO move to internal package. internal abstract class SettingImpl { - private class Node( + internal class Node( val property: KProperty, val value: Value, val serializer: ValueSerializer ) - private val valueNodes: List> = kotlin.run { + internal val valueNodes: MutableList> = kotlin.run { TODO("reflection") } 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 3383e532e..270914b40 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 @@ -7,12 +7,13 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "unused") +@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "unused", "NOTHING_TO_INLINE") package net.mamoe.mirai.console.setting import kotlinx.serialization.KSerializer import net.mamoe.mirai.utils.MiraiExperimentalAPI +import kotlin.reflect.KProperty /** * Represents a observable, immutable value wrapping. @@ -28,6 +29,11 @@ interface Value { var value: T } +inline operator fun Value.getValue(mySetting: Any?, property: KProperty<*>): T = value +inline operator fun Value.setValue(mySetting: Any?, property: KProperty<*>, value: T) { + this.value = value +} + /** * The serializer for a specific kind of [Value]. */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/serializerUtil.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/serializerUtil.kt index b054b31b8..1abad6b95 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/serializerUtil.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/internal/serializerUtil.kt @@ -10,11 +10,6 @@ package net.mamoe.mirai.console.setting.internal import kotlinx.serialization.* -import kotlinx.serialization.builtins.ListSerializer -import kotlinx.serialization.builtins.serializer -import net.mamoe.mirai.utils.MiraiExperimentalAPI -import net.mamoe.yamlkt.Yaml -import net.mamoe.yamlkt.YamlConfiguration import kotlin.reflect.KProperty import kotlin.reflect.full.findAnnotation diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt index 370edeb1b..011486cc5 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/setting/SettingTest.kt @@ -13,6 +13,12 @@ import org.junit.jupiter.api.Test internal class SettingTest { + class MySetting : Setting() { + val int by value(1) + val map by valueReified(mapOf("" to "")) + val map2 by valueReified(mapOf("" to mapOf("" to mapOf("" to "")))) + } + @Test fun testPrimitive() {