value delegating

This commit is contained in:
Him188 2020-06-21 22:48:34 +08:00
parent ecd41bc4e0
commit 2c09f1d82d
4 changed files with 24 additions and 9 deletions

View File

@ -23,7 +23,15 @@ import kotlin.reflect.typeOf
// TODO: 2020/6/21 move to JvmPlugin to inherit SettingStorage and CoroutineScope for saving // 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> Value<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): Value<T> {
@Suppress("UNCHECKED_CAST")
valueNodes.add(Node(property as KProperty<T>, value as Value<T>, TODO()))
// TODO: 2020/6/21 track on
return this
}
}
/** /**
* Internal implementation for [Setting] including: * Internal implementation for [Setting] including:
@ -32,13 +40,13 @@ abstract class Setting : SettingImpl()
*/ */
// TODO move to internal package. // TODO move to internal package.
internal abstract class SettingImpl { internal abstract class SettingImpl {
private class Node<T>( internal class Node<T>(
val property: KProperty<T>, val property: KProperty<T>,
val value: Value<T>, val value: Value<T>,
val serializer: ValueSerializer<T> val serializer: ValueSerializer<T>
) )
private val valueNodes: List<Node<*>> = kotlin.run { internal val valueNodes: MutableList<Node<*>> = kotlin.run {
TODO("reflection") TODO("reflection")
} }

View File

@ -7,12 +7,13 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * 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 package net.mamoe.mirai.console.setting
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
import kotlin.reflect.KProperty
/** /**
* Represents a observable, immutable value wrapping. * Represents a observable, immutable value wrapping.
@ -28,6 +29,11 @@ interface Value<T> {
var value: T var value: T
} }
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
}
/** /**
* The serializer for a specific kind of [Value]. * The serializer for a specific kind of [Value].
*/ */

View File

@ -10,11 +10,6 @@
package net.mamoe.mirai.console.setting.internal package net.mamoe.mirai.console.setting.internal
import kotlinx.serialization.* 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.KProperty
import kotlin.reflect.full.findAnnotation import kotlin.reflect.full.findAnnotation

View File

@ -13,6 +13,12 @@ import org.junit.jupiter.api.Test
internal class SettingTest { 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 @Test
fun testPrimitive() { fun testPrimitive() {