Support custom data classes in PluginData

This commit is contained in:
Him188 2020-08-27 23:18:36 +08:00
parent 16242c9a93
commit 73c28c6cbf
6 changed files with 41 additions and 13 deletions

View File

@ -362,5 +362,3 @@ internal fun <T> PluginData.valueImpl(type: KType, classifier: KClass<*>): Seria
@ConsoleExperimentalAPI
public fun <T> PluginData.valueFromKType(type: KType, default: T): SerializerAwareValue<T> =
(valueFromKTypeImpl(type) as SerializerAwareValue<Any?>).apply { this.value = default } as SerializerAwareValue<T>
// TODO: 2020/6/24 Introduce class TypeToken for compound types for Java.

View File

@ -189,6 +189,7 @@ public interface ListValue<E> : CompositeValue<List<E>>
*
* @param E 不是基础数据类型
*/
@ConsoleExperimentalAPI
public interface CompositeListValue<E> : ListValue<E>
/**
@ -196,12 +197,16 @@ public interface CompositeListValue<E> : ListValue<E>
*
* @param E 是基础类型
*/
@ConsoleExperimentalAPI
public interface PrimitiveListValue<E> : ListValue<E>
//// region PrimitiveListValue CODEGEN ////
@ConsoleExperimentalAPI
public interface PrimitiveIntListValue : PrimitiveListValue<Int>
@ConsoleExperimentalAPI
public interface PrimitiveLongListValue : PrimitiveListValue<Long>
// TODO + codegen
@ -212,24 +217,30 @@ public interface PrimitiveLongListValue : PrimitiveListValue<Long>
* @see [CompositeSetValue]
* @see [PrimitiveSetValue]
*/
@ConsoleExperimentalAPI
public interface SetValue<E> : CompositeValue<Set<E>>
/**
* 复合数据类型 [Set]
* @param E 是基础数据类型
*/
@ConsoleExperimentalAPI
public interface CompositeSetValue<E> : SetValue<E>
/**
* 基础数据类型 [Set]
* @param E 是基础数据类型
*/
@ConsoleExperimentalAPI
public interface PrimitiveSetValue<E> : SetValue<E>
//// region PrimitiveSetValue CODEGEN ////
@ConsoleExperimentalAPI
public interface PrimitiveIntSetValue : PrimitiveSetValue<Int>
@ConsoleExperimentalAPI
public interface PrimitiveLongSetValue : PrimitiveSetValue<Long>
// TODO + codegen
@ -252,17 +263,15 @@ public interface PrimitiveMapValue<K, V> : MapValue<K, V>
//// region PrimitiveMapValue CODEGEN ////
@ConsoleExperimentalAPI
public interface PrimitiveIntIntMapValue : PrimitiveMapValue<Int, Int>
@ConsoleExperimentalAPI
public interface PrimitiveIntLongMapValue : PrimitiveMapValue<Int, Long>
// TODO + codegen
//// endregion PrimitiveSetValue CODEGEN ////
@ConsoleExperimentalAPI
public interface ReferenceValue<T> : Value<T>

View File

@ -14,8 +14,8 @@ import java.time.Instant
internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants)
@JvmStatic
val buildDate: Instant = Instant.ofEpochSecond(0)
val buildDate: Instant = Instant.ofEpochSecond(1598539421)
@JvmStatic
val version: Semver = Semver("0", Semver.SemverType.LOOSE)
val version: Semver = Semver("1.0-M3-1", Semver.SemverType.LOOSE)
}

View File

@ -11,6 +11,7 @@ package net.mamoe.mirai.console.internal.data
import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.data.ReferenceValue
import net.mamoe.mirai.console.data.SerializerAwareValue
import kotlin.reflect.KClass
@ -180,3 +181,19 @@ internal fun PluginData.stringValueImpl(): SerializerAwareValue<String> {
}
//// endregion PluginData_value_PrimitivesImpl CODEGEN ////
internal class LazyReferenceValueImpl<T> : ReferenceValue<T>, AbstractValueImpl<T>() {
private var initialied: Boolean = false
private var valueField: T? = null
@Suppress("UNCHECKED_CAST")
override var value: T
get() {
check(initialied) { "Internal error: LazyReferenceValueImpl.valueField isn't initialized" }
return valueField as T
}
set(value) {
initialied = true
valueField = value
}
}

View File

@ -25,7 +25,8 @@ internal abstract class AbstractValueImpl<T> : Value<T> {
}
}
internal fun <T> Value<T>.setValueBySerializer(value: T) = (this as AbstractValueImpl<T>).setValueBySerializer(value)
internal fun <T> Value<T>.setValueBySerializer(value: T) =
(this.castOrInternalError<AbstractValueImpl<T>>()).setValueBySerializer(value)
//// region PrimitiveValuesImpl CODEGEN ////

View File

@ -96,7 +96,10 @@ internal fun PluginData.valueFromKTypeImpl(type: KType): SerializerAwareValue<*>
.serializableValueWith(serializerMirai(type) as KSerializer<Set<Any?>>)
}
}
else -> error("Custom composite value is not supported yet (${classifier.qualifiedName})")
else -> {
val serializer = serializerMirai(type)
return LazyReferenceValueImpl<Any?>().serializableValueWith(serializer)
}
}
}