Use RawCommandArgument for CommandValueArgument

This commit is contained in:
Him188 2020-10-09 10:13:07 +08:00
parent d6adb3c9ea
commit 4ddee69531
2 changed files with 16 additions and 7 deletions

View File

@ -9,6 +9,8 @@
package net.mamoe.mirai.console.command.descriptor
import net.mamoe.mirai.console.command.parse.RawCommandArgument
import net.mamoe.mirai.message.data.MessageContent
import kotlin.reflect.KType
import kotlin.reflect.typeOf
@ -16,23 +18,23 @@ import kotlin.reflect.typeOf
public interface TypeVariant<out OutType> {
public val outType: KType
public fun mapValue(valueParameter: String): OutType
public fun mapValue(valueParameter: MessageContent): OutType
public companion object {
@OptIn(ExperimentalStdlibApi::class)
@JvmSynthetic
public inline operator fun <reified OutType> invoke(crossinline block: (valueParameter: String) -> OutType): TypeVariant<OutType> {
public inline operator fun <reified OutType> invoke(crossinline block: (valueParameter: RawCommandArgument) -> OutType): TypeVariant<OutType> {
return object : TypeVariant<OutType> {
override val outType: KType = typeOf<OutType>()
override fun mapValue(valueParameter: String): OutType = block(valueParameter)
override fun mapValue(valueParameter: MessageContent): OutType = block(valueParameter)
}
}
}
}
@ExperimentalCommandDescriptors
public object StringTypeVariant : TypeVariant<String> {
public object StringTypeVariant : TypeVariant<RawCommandArgument> {
@OptIn(ExperimentalStdlibApi::class)
override val outType: KType = typeOf<String>()
override fun mapValue(valueParameter: String): String = valueParameter
override fun mapValue(valueParameter: RawCommandArgument): RawCommandArgument = valueParameter
}

View File

@ -13,18 +13,25 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.descriptor.NoValueArgumentMappingException
import net.mamoe.mirai.console.command.descriptor.StringTypeVariant
import net.mamoe.mirai.console.command.descriptor.TypeVariant
import net.mamoe.mirai.message.data.MessageContent
import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.typeOf
/**
* For developing use, to be inlined in the future.
*/
public typealias RawCommandArgument = MessageContent
@ExperimentalCommandDescriptors
public interface CommandValueArgument {
public val value: String
public val value: RawCommandArgument
public val typeVariants: List<TypeVariant<*>>
}
@ExperimentalCommandDescriptors
public data class InvariantCommandValueArgument(
public override val value: String,
public override val value: RawCommandArgument,
) : CommandValueArgument {
override val typeVariants: List<TypeVariant<*>> = listOf(StringTypeVariant)
}