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()
private val PACKAGE = """
package net.mamoe.mirai.console.setting
package net.mamoe.mirai.console.setting.internal
""".trimIndent()
private val IMPORTS = """
import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import net.mamoe.mirai.console.setting.*
""".trimIndent()
fun genAllValueImpl(): String = buildString {
@ -56,20 +57,20 @@ fun genAllValueImpl(): String = buildString {
// PRIMITIVE
for (number in NUMBERS + OTHER_PRIMITIVES) {
appendln(genValueImpl(number, number, "$number.serializer()", false))
appendln(genPrimitiveValueImpl(number, number, "$number.serializer()", false))
appendln()
}
// PRIMITIVE ARRAYS
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()
}
// TYPED ARRAYS
for (number in NUMBERS + OTHER_PRIMITIVES) {
appendln(
genValueImpl(
genPrimitiveValueImpl(
"Array<${number}>",
"Typed${number}Array",
"ArraySerializer(${number}.serializer())",
@ -83,7 +84,8 @@ fun genAllValueImpl(): String = buildString {
for (collectionName in listOf("List", "Set")) {
for (number in NUMBERS + OTHER_PRIMITIVES) {
appendln(
genValueImpl(
genCollectionValueImpl(
collectionName,
"${collectionName}<${number}>",
"${number}${collectionName}",
"${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 {
return object : ${miraiValueName}Value() {
@ -206,3 +213,38 @@ fun genValueImpl(kotlinTypeName: String, miraiValueName: String, serializer: Str
}
""".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()
for (number in (NUMBERS + OTHER_PRIMITIVES).filterNot { it == "String" }) {
val template = """
appendln(
"""
abstract class ${number}ArrayValue internal constructor() : PrimitiveArrayValue<${number}Array>(), Iterable<${number}> {
override fun iterator(): Iterator<${number}> = this.value.iterator()
}
"""
appendln(template)
)
appendln()
}
appendln()
@ -180,11 +181,11 @@ sealed class Value<T : Any> : ReadWriteProperty<Setting, T> {
appendln()
for (number in (NUMBERS + OTHER_PRIMITIVES)) {
val template = """
appendln(
"""
abstract class Typed${number}ArrayValue internal constructor() : TypedPrimitiveArrayValue<${number}>()
"""
appendln(template)
)
}
appendln()
@ -194,9 +195,7 @@ sealed class Value<T : Any> : ReadWriteProperty<Setting, T> {
appendln(
"""
sealed class ${collectionName}Value<E> : Value<${collectionName}<E>>(), Iterable<E>{
override fun iterator() = this.value.iterator()
}
sealed class ${collectionName}Value<E> : Value<${collectionName}<E>>(), ${collectionName}<E>
"""
)

View File

@ -14,7 +14,7 @@ import kotlin.properties.ReadWriteProperty
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
* !!! 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> {
override fun iterator(): Iterator<Int> = this.value.iterator()
}
abstract class ShortArrayValue internal constructor() : PrimitiveArrayValue<ShortArray>(), Iterable<Short> {
override fun iterator(): Iterator<Short> = this.value.iterator()
}
abstract class ByteArrayValue internal constructor() : PrimitiveArrayValue<ByteArray>(), Iterable<Byte> {
override fun iterator(): Iterator<Byte> = this.value.iterator()
}
abstract class LongArrayValue internal constructor() : PrimitiveArrayValue<LongArray>(), Iterable<Long> {
override fun iterator(): Iterator<Long> = this.value.iterator()
}
abstract class FloatArrayValue internal constructor() : PrimitiveArrayValue<FloatArray>(), Iterable<Float> {
override fun iterator(): Iterator<Float> = this.value.iterator()
}
abstract class DoubleArrayValue internal constructor() : PrimitiveArrayValue<DoubleArray>(), Iterable<Double> {
override fun iterator(): Iterator<Double> = this.value.iterator()
}
abstract class BooleanArrayValue internal constructor() : PrimitiveArrayValue<BooleanArray>(), Iterable<Boolean> {
override fun iterator(): Iterator<Boolean> = this.value.iterator()
}
abstract class CharArrayValue internal constructor() : PrimitiveArrayValue<CharArray>(), Iterable<Char> {
override fun iterator(): Iterator<Char> = this.value.iterator()
}
sealed class TypedPrimitiveArrayValue<E> : ArrayValue<Array<E>>(), Iterable<E> {
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 TypedStringArrayValue internal constructor() : TypedPrimitiveArrayValue<String>()
sealed class ListValue<E> : Value<List<E>>(), Iterable<E> {
override fun iterator() = this.value.iterator()
}
sealed class ListValue<E> : Value<List<E>>(), List<E>
abstract class IntListValue internal constructor() : ListValue<Int>()
abstract class ShortListValue internal constructor() : ListValue<Short>()
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>
sealed class SetValue<E> : Value<Set<E>>(), Iterable<E> {
override fun iterator() = this.value.iterator()
}
sealed class SetValue<E> : Value<Set<E>>(), Set<E>
abstract class IntSetValue internal constructor() : SetValue<Int>()
abstract class ShortSetValue internal constructor() : SetValue<Short>()
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 {
return object : IntListValue() {
private var internalValue: List<Int> = default
var internalValue: List<Int> = default
return object : IntListValue(), List<Int> by dynamicList({ internalValue }) {
override var value: List<Int>
get() = internalValue
set(new) {
@ -429,8 +429,8 @@ internal fun Setting.valueImpl(default: List<Int>): IntListValue {
}
internal fun Setting.valueImpl(default: List<Short>): ShortListValue {
return object : ShortListValue() {
private var internalValue: List<Short> = default
var internalValue: List<Short> = default
return object : ShortListValue(), List<Short> by dynamicList({ internalValue }) {
override var value: List<Short>
get() = internalValue
set(new) {
@ -444,8 +444,8 @@ internal fun Setting.valueImpl(default: List<Short>): ShortListValue {
}
internal fun Setting.valueImpl(default: List<Byte>): ByteListValue {
return object : ByteListValue() {
private var internalValue: List<Byte> = default
var internalValue: List<Byte> = default
return object : ByteListValue(), List<Byte> by dynamicList({ internalValue }) {
override var value: List<Byte>
get() = internalValue
set(new) {
@ -459,8 +459,8 @@ internal fun Setting.valueImpl(default: List<Byte>): ByteListValue {
}
internal fun Setting.valueImpl(default: List<Long>): LongListValue {
return object : LongListValue() {
private var internalValue: List<Long> = default
var internalValue: List<Long> = default
return object : LongListValue(), List<Long> by dynamicList({ internalValue }) {
override var value: List<Long>
get() = internalValue
set(new) {
@ -474,8 +474,8 @@ internal fun Setting.valueImpl(default: List<Long>): LongListValue {
}
internal fun Setting.valueImpl(default: List<Float>): FloatListValue {
return object : FloatListValue() {
private var internalValue: List<Float> = default
var internalValue: List<Float> = default
return object : FloatListValue(), List<Float> by dynamicList({ internalValue }) {
override var value: List<Float>
get() = internalValue
set(new) {
@ -489,8 +489,8 @@ internal fun Setting.valueImpl(default: List<Float>): FloatListValue {
}
internal fun Setting.valueImpl(default: List<Double>): DoubleListValue {
return object : DoubleListValue() {
private var internalValue: List<Double> = default
var internalValue: List<Double> = default
return object : DoubleListValue(), List<Double> by dynamicList({ internalValue }) {
override var value: List<Double>
get() = internalValue
set(new) {
@ -504,8 +504,8 @@ internal fun Setting.valueImpl(default: List<Double>): DoubleListValue {
}
internal fun Setting.valueImpl(default: List<Boolean>): BooleanListValue {
return object : BooleanListValue() {
private var internalValue: List<Boolean> = default
var internalValue: List<Boolean> = default
return object : BooleanListValue(), List<Boolean> by dynamicList({ internalValue }) {
override var value: List<Boolean>
get() = internalValue
set(new) {
@ -519,8 +519,8 @@ internal fun Setting.valueImpl(default: List<Boolean>): BooleanListValue {
}
internal fun Setting.valueImpl(default: List<Char>): CharListValue {
return object : CharListValue() {
private var internalValue: List<Char> = default
var internalValue: List<Char> = default
return object : CharListValue(), List<Char> by dynamicList({ internalValue }) {
override var value: List<Char>
get() = internalValue
set(new) {
@ -534,8 +534,8 @@ internal fun Setting.valueImpl(default: List<Char>): CharListValue {
}
internal fun Setting.valueImpl(default: List<String>): StringListValue {
return object : StringListValue() {
private var internalValue: List<String> = default
var internalValue: List<String> = default
return object : StringListValue(), List<String> by dynamicList({ internalValue }) {
override var value: List<String>
get() = internalValue
set(new) {
@ -549,8 +549,8 @@ internal fun Setting.valueImpl(default: List<String>): StringListValue {
}
internal fun Setting.valueImpl(default: Set<Int>): IntSetValue {
return object : IntSetValue() {
private var internalValue: Set<Int> = default
var internalValue: Set<Int> = default
return object : IntSetValue(), Set<Int> by dynamicSet({ internalValue }) {
override var value: Set<Int>
get() = internalValue
set(new) {
@ -564,8 +564,8 @@ internal fun Setting.valueImpl(default: Set<Int>): IntSetValue {
}
internal fun Setting.valueImpl(default: Set<Short>): ShortSetValue {
return object : ShortSetValue() {
private var internalValue: Set<Short> = default
var internalValue: Set<Short> = default
return object : ShortSetValue(), Set<Short> by dynamicSet({ internalValue }) {
override var value: Set<Short>
get() = internalValue
set(new) {
@ -579,8 +579,8 @@ internal fun Setting.valueImpl(default: Set<Short>): ShortSetValue {
}
internal fun Setting.valueImpl(default: Set<Byte>): ByteSetValue {
return object : ByteSetValue() {
private var internalValue: Set<Byte> = default
var internalValue: Set<Byte> = default
return object : ByteSetValue(), Set<Byte> by dynamicSet({ internalValue }) {
override var value: Set<Byte>
get() = internalValue
set(new) {
@ -594,8 +594,8 @@ internal fun Setting.valueImpl(default: Set<Byte>): ByteSetValue {
}
internal fun Setting.valueImpl(default: Set<Long>): LongSetValue {
return object : LongSetValue() {
private var internalValue: Set<Long> = default
var internalValue: Set<Long> = default
return object : LongSetValue(), Set<Long> by dynamicSet({ internalValue }) {
override var value: Set<Long>
get() = internalValue
set(new) {
@ -609,8 +609,8 @@ internal fun Setting.valueImpl(default: Set<Long>): LongSetValue {
}
internal fun Setting.valueImpl(default: Set<Float>): FloatSetValue {
return object : FloatSetValue() {
private var internalValue: Set<Float> = default
var internalValue: Set<Float> = default
return object : FloatSetValue(), Set<Float> by dynamicSet({ internalValue }) {
override var value: Set<Float>
get() = internalValue
set(new) {
@ -624,8 +624,8 @@ internal fun Setting.valueImpl(default: Set<Float>): FloatSetValue {
}
internal fun Setting.valueImpl(default: Set<Double>): DoubleSetValue {
return object : DoubleSetValue() {
private var internalValue: Set<Double> = default
var internalValue: Set<Double> = default
return object : DoubleSetValue(), Set<Double> by dynamicSet({ internalValue }) {
override var value: Set<Double>
get() = internalValue
set(new) {
@ -639,8 +639,8 @@ internal fun Setting.valueImpl(default: Set<Double>): DoubleSetValue {
}
internal fun Setting.valueImpl(default: Set<Boolean>): BooleanSetValue {
return object : BooleanSetValue() {
private var internalValue: Set<Boolean> = default
var internalValue: Set<Boolean> = default
return object : BooleanSetValue(), Set<Boolean> by dynamicSet({ internalValue }) {
override var value: Set<Boolean>
get() = internalValue
set(new) {
@ -654,8 +654,8 @@ internal fun Setting.valueImpl(default: Set<Boolean>): BooleanSetValue {
}
internal fun Setting.valueImpl(default: Set<Char>): CharSetValue {
return object : CharSetValue() {
private var internalValue: Set<Char> = default
var internalValue: Set<Char> = default
return object : CharSetValue(), Set<Char> by dynamicSet({ internalValue }) {
override var value: Set<Char>
get() = internalValue
set(new) {
@ -669,8 +669,8 @@ internal fun Setting.valueImpl(default: Set<Char>): CharSetValue {
}
internal fun Setting.valueImpl(default: Set<String>): StringSetValue {
return object : StringSetValue() {
private var internalValue: Set<String> = default
var internalValue: Set<String> = default
return object : StringSetValue(), Set<String> by dynamicSet({ internalValue }) {
override var value: Set<String>
get() = internalValue
set(new) {
@ -1354,10 +1354,7 @@ internal fun <T : Setting> Setting.valueImpl(default: T): Value<T> {
}
override fun serialize(encoder: Encoder, value: T) {
internalValue.updaterSerializer.serialize(
encoder,
SettingSerializerMark
)
internalValue.updaterSerializer.serialize(encoder, SettingSerializerMark)
}
}
}