Support generic List and Set implementations

This commit is contained in:
Him188 2020-05-22 20:39:33 +08:00
parent f10d35db8f
commit f363a7d4b0
4 changed files with 104 additions and 64 deletions

View File

@ -41,12 +41,13 @@ private val DO_NOT_MODIFY = """
""".trimIndent() """.trimIndent()
private val PACKAGE = """ private val PACKAGE = """
package net.mamoe.mirai.console.setting package net.mamoe.mirai.console.setting.internal
""".trimIndent() """.trimIndent()
private val IMPORTS = """ private val IMPORTS = """
import kotlinx.serialization.* import kotlinx.serialization.*
import kotlinx.serialization.builtins.* import kotlinx.serialization.builtins.*
import net.mamoe.mirai.console.setting.*
""".trimIndent() """.trimIndent()
fun genAllValueImpl(): String = buildString { fun genAllValueImpl(): String = buildString {
@ -56,20 +57,20 @@ fun genAllValueImpl(): String = buildString {
// PRIMITIVE // PRIMITIVE
for (number in NUMBERS + OTHER_PRIMITIVES) { for (number in NUMBERS + OTHER_PRIMITIVES) {
appendln(genValueImpl(number, number, "$number.serializer()", false)) appendln(genPrimitiveValueImpl(number, number, "$number.serializer()", false))
appendln() appendln()
} }
// PRIMITIVE ARRAYS // PRIMITIVE ARRAYS
for (number in NUMBERS + OTHER_PRIMITIVES.filterNot { it == "String" }) { for (number in NUMBERS + OTHER_PRIMITIVES.filterNot { it == "String" }) {
appendln(genValueImpl("${number}Array", "${number}Array", "${number}ArraySerializer()", true)) appendln(genPrimitiveValueImpl("${number}Array", "${number}Array", "${number}ArraySerializer()", true))
appendln() appendln()
} }
// TYPED ARRAYS // TYPED ARRAYS
for (number in NUMBERS + OTHER_PRIMITIVES) { for (number in NUMBERS + OTHER_PRIMITIVES) {
appendln( appendln(
genValueImpl( genPrimitiveValueImpl(
"Array<${number}>", "Array<${number}>",
"Typed${number}Array", "Typed${number}Array",
"ArraySerializer(${number}.serializer())", "ArraySerializer(${number}.serializer())",
@ -83,7 +84,8 @@ fun genAllValueImpl(): String = buildString {
for (collectionName in listOf("List", "Set")) { for (collectionName in listOf("List", "Set")) {
for (number in NUMBERS + OTHER_PRIMITIVES) { for (number in NUMBERS + OTHER_PRIMITIVES) {
appendln( appendln(
genValueImpl( genCollectionValueImpl(
collectionName,
"${collectionName}<${number}>", "${collectionName}<${number}>",
"${number}${collectionName}", "${number}${collectionName}",
"${collectionName}Serializer(${number}.serializer())", "${collectionName}Serializer(${number}.serializer())",
@ -178,7 +180,12 @@ fun genAllValueImpl(): String = buildString {
) )
} }
fun genValueImpl(kotlinTypeName: String, miraiValueName: String, serializer: String, isArray: Boolean): String = fun genPrimitiveValueImpl(
kotlinTypeName: String,
miraiValueName: String,
serializer: String,
isArray: Boolean
): String =
""" """
internal fun Setting.valueImpl(default: ${kotlinTypeName}): ${miraiValueName}Value { internal fun Setting.valueImpl(default: ${kotlinTypeName}): ${miraiValueName}Value {
return object : ${miraiValueName}Value() { return object : ${miraiValueName}Value() {
@ -206,3 +213,38 @@ fun genValueImpl(kotlinTypeName: String, miraiValueName: String, serializer: Str
} }
""".trimIndent() + "\n" """.trimIndent() + "\n"
fun genCollectionValueImpl(
collectionName: String,
kotlinTypeName: String,
miraiValueName: String,
serializer: String,
isArray: Boolean
): String =
"""
internal fun Setting.valueImpl(default: ${kotlinTypeName}): ${miraiValueName}Value {
var internalValue: $kotlinTypeName = default
return object : ${miraiValueName}Value(), $kotlinTypeName by dynamic$collectionName({ internalValue }) {
override var value: $kotlinTypeName
get() = internalValue
set(new) {
${
if (isArray) """
if (!new.contentEquals(internalValue)) {
internalValue = new
onElementChanged(this)
}
""".trim()
else """
if (new != internalValue) {
internalValue = new
onElementChanged(this)
}
""".trim()
}
}
override val serializer = $serializer
}
}
""".trimIndent() + "\n"

View File

@ -157,13 +157,14 @@ sealed class Value<T : Any> : ReadWriteProperty<Setting, T> {
appendln() appendln()
for (number in (NUMBERS + OTHER_PRIMITIVES).filterNot { it == "String" }) { for (number in (NUMBERS + OTHER_PRIMITIVES).filterNot { it == "String" }) {
val template = """ appendln(
"""
abstract class ${number}ArrayValue internal constructor() : PrimitiveArrayValue<${number}Array>(), Iterable<${number}> { abstract class ${number}ArrayValue internal constructor() : PrimitiveArrayValue<${number}Array>(), Iterable<${number}> {
override fun iterator(): Iterator<${number}> = this.value.iterator() override fun iterator(): Iterator<${number}> = this.value.iterator()
} }
""" """
)
appendln(template) appendln()
} }
appendln() appendln()
@ -180,11 +181,11 @@ sealed class Value<T : Any> : ReadWriteProperty<Setting, T> {
appendln() appendln()
for (number in (NUMBERS + OTHER_PRIMITIVES)) { for (number in (NUMBERS + OTHER_PRIMITIVES)) {
val template = """ appendln(
"""
abstract class Typed${number}ArrayValue internal constructor() : TypedPrimitiveArrayValue<${number}>() abstract class Typed${number}ArrayValue internal constructor() : TypedPrimitiveArrayValue<${number}>()
""" """
)
appendln(template)
} }
appendln() appendln()
@ -194,9 +195,7 @@ sealed class Value<T : Any> : ReadWriteProperty<Setting, T> {
appendln( appendln(
""" """
sealed class ${collectionName}Value<E> : Value<${collectionName}<E>>(), Iterable<E>{ sealed class ${collectionName}Value<E> : Value<${collectionName}<E>>(), ${collectionName}<E>
override fun iterator() = this.value.iterator()
}
""" """
) )

View File

@ -14,7 +14,7 @@ import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
/** /**
* !!! These primitive types are auto-generated by backend/codegen/src/main/kotlin/net.mamoe.mirai.console.codegen.ValuesCodegen.kt * !!! This file is auto-generated by backend/codegen/src/main/kotlin/net.mamoe.mirai.console.codegen.ValuesCodegen.kt
* !!! for better performance * !!! for better performance
* !!! DO NOT MODIFY THIS FILE MANUALLY * !!! DO NOT MODIFY THIS FILE MANUALLY
*/ */
@ -62,28 +62,36 @@ sealed class PrimitiveArrayValue<T : Any> : ArrayValue<T>()
abstract class IntArrayValue internal constructor() : PrimitiveArrayValue<IntArray>(), Iterable<Int> { abstract class IntArrayValue internal constructor() : PrimitiveArrayValue<IntArray>(), Iterable<Int> {
override fun iterator(): Iterator<Int> = this.value.iterator() override fun iterator(): Iterator<Int> = this.value.iterator()
} }
abstract class ShortArrayValue internal constructor() : PrimitiveArrayValue<ShortArray>(), Iterable<Short> { abstract class ShortArrayValue internal constructor() : PrimitiveArrayValue<ShortArray>(), Iterable<Short> {
override fun iterator(): Iterator<Short> = this.value.iterator() override fun iterator(): Iterator<Short> = this.value.iterator()
} }
abstract class ByteArrayValue internal constructor() : PrimitiveArrayValue<ByteArray>(), Iterable<Byte> { abstract class ByteArrayValue internal constructor() : PrimitiveArrayValue<ByteArray>(), Iterable<Byte> {
override fun iterator(): Iterator<Byte> = this.value.iterator() override fun iterator(): Iterator<Byte> = this.value.iterator()
} }
abstract class LongArrayValue internal constructor() : PrimitiveArrayValue<LongArray>(), Iterable<Long> { abstract class LongArrayValue internal constructor() : PrimitiveArrayValue<LongArray>(), Iterable<Long> {
override fun iterator(): Iterator<Long> = this.value.iterator() override fun iterator(): Iterator<Long> = this.value.iterator()
} }
abstract class FloatArrayValue internal constructor() : PrimitiveArrayValue<FloatArray>(), Iterable<Float> { abstract class FloatArrayValue internal constructor() : PrimitiveArrayValue<FloatArray>(), Iterable<Float> {
override fun iterator(): Iterator<Float> = this.value.iterator() override fun iterator(): Iterator<Float> = this.value.iterator()
} }
abstract class DoubleArrayValue internal constructor() : PrimitiveArrayValue<DoubleArray>(), Iterable<Double> { abstract class DoubleArrayValue internal constructor() : PrimitiveArrayValue<DoubleArray>(), Iterable<Double> {
override fun iterator(): Iterator<Double> = this.value.iterator() override fun iterator(): Iterator<Double> = this.value.iterator()
} }
abstract class BooleanArrayValue internal constructor() : PrimitiveArrayValue<BooleanArray>(), Iterable<Boolean> { abstract class BooleanArrayValue internal constructor() : PrimitiveArrayValue<BooleanArray>(), Iterable<Boolean> {
override fun iterator(): Iterator<Boolean> = this.value.iterator() override fun iterator(): Iterator<Boolean> = this.value.iterator()
} }
abstract class CharArrayValue internal constructor() : PrimitiveArrayValue<CharArray>(), Iterable<Char> { abstract class CharArrayValue internal constructor() : PrimitiveArrayValue<CharArray>(), Iterable<Char> {
override fun iterator(): Iterator<Char> = this.value.iterator() override fun iterator(): Iterator<Char> = this.value.iterator()
} }
sealed class TypedPrimitiveArrayValue<E> : ArrayValue<Array<E>>(), Iterable<E> { sealed class TypedPrimitiveArrayValue<E> : ArrayValue<Array<E>>(), Iterable<E> {
override fun iterator() = this.value.iterator() override fun iterator() = this.value.iterator()
} }
@ -98,10 +106,7 @@ abstract class TypedBooleanArrayValue internal constructor() : TypedPrimitiveArr
abstract class TypedCharArrayValue internal constructor() : TypedPrimitiveArrayValue<Char>() abstract class TypedCharArrayValue internal constructor() : TypedPrimitiveArrayValue<Char>()
abstract class TypedStringArrayValue internal constructor() : TypedPrimitiveArrayValue<String>() abstract class TypedStringArrayValue internal constructor() : TypedPrimitiveArrayValue<String>()
sealed class ListValue<E> : Value<List<E>>(), Iterable<E> { sealed class ListValue<E> : Value<List<E>>(), List<E>
override fun iterator() = this.value.iterator()
}
abstract class IntListValue internal constructor() : ListValue<Int>() abstract class IntListValue internal constructor() : ListValue<Int>()
abstract class ShortListValue internal constructor() : ListValue<Short>() abstract class ShortListValue internal constructor() : ListValue<Short>()
abstract class ByteListValue internal constructor() : ListValue<Byte>() abstract class ByteListValue internal constructor() : ListValue<Byte>()
@ -114,10 +119,7 @@ abstract class StringListValue internal constructor() : ListValue<String>()
abstract class SettingListValue<T : Setting> internal constructor() : Value<List<T>>(), List<T> abstract class SettingListValue<T : Setting> internal constructor() : Value<List<T>>(), List<T>
sealed class SetValue<E> : Value<Set<E>>(), Iterable<E> { sealed class SetValue<E> : Value<Set<E>>(), Set<E>
override fun iterator() = this.value.iterator()
}
abstract class IntSetValue internal constructor() : SetValue<Int>() abstract class IntSetValue internal constructor() : SetValue<Int>()
abstract class ShortSetValue internal constructor() : SetValue<Short>() abstract class ShortSetValue internal constructor() : SetValue<Short>()
abstract class ByteSetValue internal constructor() : SetValue<Byte>() abstract class ByteSetValue internal constructor() : SetValue<Byte>()

View File

@ -414,8 +414,8 @@ internal fun Setting.valueImpl(default: Array<String>): TypedStringArrayValue {
} }
internal fun Setting.valueImpl(default: List<Int>): IntListValue { internal fun Setting.valueImpl(default: List<Int>): IntListValue {
return object : IntListValue() { var internalValue: List<Int> = default
private var internalValue: List<Int> = default return object : IntListValue(), List<Int> by dynamicList({ internalValue }) {
override var value: List<Int> override var value: List<Int>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -429,8 +429,8 @@ internal fun Setting.valueImpl(default: List<Int>): IntListValue {
} }
internal fun Setting.valueImpl(default: List<Short>): ShortListValue { internal fun Setting.valueImpl(default: List<Short>): ShortListValue {
return object : ShortListValue() { var internalValue: List<Short> = default
private var internalValue: List<Short> = default return object : ShortListValue(), List<Short> by dynamicList({ internalValue }) {
override var value: List<Short> override var value: List<Short>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -444,8 +444,8 @@ internal fun Setting.valueImpl(default: List<Short>): ShortListValue {
} }
internal fun Setting.valueImpl(default: List<Byte>): ByteListValue { internal fun Setting.valueImpl(default: List<Byte>): ByteListValue {
return object : ByteListValue() { var internalValue: List<Byte> = default
private var internalValue: List<Byte> = default return object : ByteListValue(), List<Byte> by dynamicList({ internalValue }) {
override var value: List<Byte> override var value: List<Byte>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -459,8 +459,8 @@ internal fun Setting.valueImpl(default: List<Byte>): ByteListValue {
} }
internal fun Setting.valueImpl(default: List<Long>): LongListValue { internal fun Setting.valueImpl(default: List<Long>): LongListValue {
return object : LongListValue() { var internalValue: List<Long> = default
private var internalValue: List<Long> = default return object : LongListValue(), List<Long> by dynamicList({ internalValue }) {
override var value: List<Long> override var value: List<Long>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -474,8 +474,8 @@ internal fun Setting.valueImpl(default: List<Long>): LongListValue {
} }
internal fun Setting.valueImpl(default: List<Float>): FloatListValue { internal fun Setting.valueImpl(default: List<Float>): FloatListValue {
return object : FloatListValue() { var internalValue: List<Float> = default
private var internalValue: List<Float> = default return object : FloatListValue(), List<Float> by dynamicList({ internalValue }) {
override var value: List<Float> override var value: List<Float>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -489,8 +489,8 @@ internal fun Setting.valueImpl(default: List<Float>): FloatListValue {
} }
internal fun Setting.valueImpl(default: List<Double>): DoubleListValue { internal fun Setting.valueImpl(default: List<Double>): DoubleListValue {
return object : DoubleListValue() { var internalValue: List<Double> = default
private var internalValue: List<Double> = default return object : DoubleListValue(), List<Double> by dynamicList({ internalValue }) {
override var value: List<Double> override var value: List<Double>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -504,8 +504,8 @@ internal fun Setting.valueImpl(default: List<Double>): DoubleListValue {
} }
internal fun Setting.valueImpl(default: List<Boolean>): BooleanListValue { internal fun Setting.valueImpl(default: List<Boolean>): BooleanListValue {
return object : BooleanListValue() { var internalValue: List<Boolean> = default
private var internalValue: List<Boolean> = default return object : BooleanListValue(), List<Boolean> by dynamicList({ internalValue }) {
override var value: List<Boolean> override var value: List<Boolean>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -519,8 +519,8 @@ internal fun Setting.valueImpl(default: List<Boolean>): BooleanListValue {
} }
internal fun Setting.valueImpl(default: List<Char>): CharListValue { internal fun Setting.valueImpl(default: List<Char>): CharListValue {
return object : CharListValue() { var internalValue: List<Char> = default
private var internalValue: List<Char> = default return object : CharListValue(), List<Char> by dynamicList({ internalValue }) {
override var value: List<Char> override var value: List<Char>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -534,8 +534,8 @@ internal fun Setting.valueImpl(default: List<Char>): CharListValue {
} }
internal fun Setting.valueImpl(default: List<String>): StringListValue { internal fun Setting.valueImpl(default: List<String>): StringListValue {
return object : StringListValue() { var internalValue: List<String> = default
private var internalValue: List<String> = default return object : StringListValue(), List<String> by dynamicList({ internalValue }) {
override var value: List<String> override var value: List<String>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -549,8 +549,8 @@ internal fun Setting.valueImpl(default: List<String>): StringListValue {
} }
internal fun Setting.valueImpl(default: Set<Int>): IntSetValue { internal fun Setting.valueImpl(default: Set<Int>): IntSetValue {
return object : IntSetValue() { var internalValue: Set<Int> = default
private var internalValue: Set<Int> = default return object : IntSetValue(), Set<Int> by dynamicSet({ internalValue }) {
override var value: Set<Int> override var value: Set<Int>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -564,8 +564,8 @@ internal fun Setting.valueImpl(default: Set<Int>): IntSetValue {
} }
internal fun Setting.valueImpl(default: Set<Short>): ShortSetValue { internal fun Setting.valueImpl(default: Set<Short>): ShortSetValue {
return object : ShortSetValue() { var internalValue: Set<Short> = default
private var internalValue: Set<Short> = default return object : ShortSetValue(), Set<Short> by dynamicSet({ internalValue }) {
override var value: Set<Short> override var value: Set<Short>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -579,8 +579,8 @@ internal fun Setting.valueImpl(default: Set<Short>): ShortSetValue {
} }
internal fun Setting.valueImpl(default: Set<Byte>): ByteSetValue { internal fun Setting.valueImpl(default: Set<Byte>): ByteSetValue {
return object : ByteSetValue() { var internalValue: Set<Byte> = default
private var internalValue: Set<Byte> = default return object : ByteSetValue(), Set<Byte> by dynamicSet({ internalValue }) {
override var value: Set<Byte> override var value: Set<Byte>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -594,8 +594,8 @@ internal fun Setting.valueImpl(default: Set<Byte>): ByteSetValue {
} }
internal fun Setting.valueImpl(default: Set<Long>): LongSetValue { internal fun Setting.valueImpl(default: Set<Long>): LongSetValue {
return object : LongSetValue() { var internalValue: Set<Long> = default
private var internalValue: Set<Long> = default return object : LongSetValue(), Set<Long> by dynamicSet({ internalValue }) {
override var value: Set<Long> override var value: Set<Long>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -609,8 +609,8 @@ internal fun Setting.valueImpl(default: Set<Long>): LongSetValue {
} }
internal fun Setting.valueImpl(default: Set<Float>): FloatSetValue { internal fun Setting.valueImpl(default: Set<Float>): FloatSetValue {
return object : FloatSetValue() { var internalValue: Set<Float> = default
private var internalValue: Set<Float> = default return object : FloatSetValue(), Set<Float> by dynamicSet({ internalValue }) {
override var value: Set<Float> override var value: Set<Float>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -624,8 +624,8 @@ internal fun Setting.valueImpl(default: Set<Float>): FloatSetValue {
} }
internal fun Setting.valueImpl(default: Set<Double>): DoubleSetValue { internal fun Setting.valueImpl(default: Set<Double>): DoubleSetValue {
return object : DoubleSetValue() { var internalValue: Set<Double> = default
private var internalValue: Set<Double> = default return object : DoubleSetValue(), Set<Double> by dynamicSet({ internalValue }) {
override var value: Set<Double> override var value: Set<Double>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -639,8 +639,8 @@ internal fun Setting.valueImpl(default: Set<Double>): DoubleSetValue {
} }
internal fun Setting.valueImpl(default: Set<Boolean>): BooleanSetValue { internal fun Setting.valueImpl(default: Set<Boolean>): BooleanSetValue {
return object : BooleanSetValue() { var internalValue: Set<Boolean> = default
private var internalValue: Set<Boolean> = default return object : BooleanSetValue(), Set<Boolean> by dynamicSet({ internalValue }) {
override var value: Set<Boolean> override var value: Set<Boolean>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -654,8 +654,8 @@ internal fun Setting.valueImpl(default: Set<Boolean>): BooleanSetValue {
} }
internal fun Setting.valueImpl(default: Set<Char>): CharSetValue { internal fun Setting.valueImpl(default: Set<Char>): CharSetValue {
return object : CharSetValue() { var internalValue: Set<Char> = default
private var internalValue: Set<Char> = default return object : CharSetValue(), Set<Char> by dynamicSet({ internalValue }) {
override var value: Set<Char> override var value: Set<Char>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -669,8 +669,8 @@ internal fun Setting.valueImpl(default: Set<Char>): CharSetValue {
} }
internal fun Setting.valueImpl(default: Set<String>): StringSetValue { internal fun Setting.valueImpl(default: Set<String>): StringSetValue {
return object : StringSetValue() { var internalValue: Set<String> = default
private var internalValue: Set<String> = default return object : StringSetValue(), Set<String> by dynamicSet({ internalValue }) {
override var value: Set<String> override var value: Set<String>
get() = internalValue get() = internalValue
set(new) { set(new) {
@ -1354,10 +1354,7 @@ internal fun <T : Setting> Setting.valueImpl(default: T): Value<T> {
} }
override fun serialize(encoder: Encoder, value: T) { override fun serialize(encoder: Encoder, value: T) {
internalValue.updaterSerializer.serialize( internalValue.updaterSerializer.serialize(encoder, SettingSerializerMark)
encoder,
SettingSerializerMark
)
} }
} }
} }