Introduce default type variants

This commit is contained in:
Him188 2020-10-20 13:54:05 +08:00
parent 58af1b3354
commit 3e2a5c382e
4 changed files with 30 additions and 9 deletions

View File

@ -211,6 +211,7 @@ public suspend fun Command.execute(
): CommandExecuteResult { ): CommandExecuteResult {
// TODO: 2020/10/18 net.mamoe.mirai.console.command.CommandManager.execute // TODO: 2020/10/18 net.mamoe.mirai.console.command.CommandManager.execute
val chain = buildMessageChain { val chain = buildMessageChain {
append(CommandManager.commandPrefix)
append(this@execute.primaryName) append(this@execute.primaryName)
append(' ') append(' ')
append(arguments) append(arguments)

View File

@ -12,7 +12,10 @@ package net.mamoe.mirai.console.command.descriptor
import net.mamoe.mirai.console.command.parse.CommandCall import net.mamoe.mirai.console.command.parse.CommandCall
import net.mamoe.mirai.console.command.parse.CommandCallParser import net.mamoe.mirai.console.command.parse.CommandCallParser
import net.mamoe.mirai.console.command.parse.RawCommandArgument import net.mamoe.mirai.console.command.parse.RawCommandArgument
import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageContent import net.mamoe.mirai.message.data.MessageContent
import net.mamoe.mirai.message.data.asMessageChain
import net.mamoe.mirai.message.data.content
import kotlin.reflect.KType import kotlin.reflect.KType
import kotlin.reflect.typeOf import kotlin.reflect.typeOf
@ -45,6 +48,20 @@ public interface TypeVariant<out OutType> {
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors
public object MessageContentTypeVariant : TypeVariant<MessageContent> { public object MessageContentTypeVariant : TypeVariant<MessageContent> {
@OptIn(ExperimentalStdlibApi::class) @OptIn(ExperimentalStdlibApi::class)
override val outType: KType = typeOf<String>() override val outType: KType = typeOf<MessageContent>()
override fun mapValue(valueParameter: MessageContent): MessageContent = valueParameter override fun mapValue(valueParameter: MessageContent): MessageContent = valueParameter
} }
@ExperimentalCommandDescriptors
public object MessageChainTypeVariant : TypeVariant<MessageChain> {
@OptIn(ExperimentalStdlibApi::class)
override val outType: KType = typeOf<MessageChain>()
override fun mapValue(valueParameter: MessageContent): MessageChain = valueParameter.asMessageChain()
}
@ExperimentalCommandDescriptors
public object ContentStringTypeVariant : TypeVariant<String> {
@OptIn(ExperimentalStdlibApi::class)
override val outType: KType = typeOf<String>()
override fun mapValue(valueParameter: MessageContent): String = valueParameter.content
}

View File

@ -9,10 +9,8 @@
package net.mamoe.mirai.console.command.parse package net.mamoe.mirai.console.command.parse
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.command.descriptor.*
import net.mamoe.mirai.console.command.descriptor.MessageContentTypeVariant import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.console.command.descriptor.NoValueArgumentMappingException
import net.mamoe.mirai.console.command.descriptor.TypeVariant
import net.mamoe.mirai.message.data.MessageContent import net.mamoe.mirai.message.data.MessageContent
import kotlin.reflect.KType import kotlin.reflect.KType
import kotlin.reflect.full.isSubtypeOf import kotlin.reflect.full.isSubtypeOf
@ -31,7 +29,7 @@ public typealias RawCommandArgument = MessageContent
public interface CommandArgument public interface CommandArgument
/** /**
* @see InvariantCommandValueArgument * @see DefaultCommandValueArgument
*/ */
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors
public interface CommandValueArgument : CommandArgument { public interface CommandValueArgument : CommandArgument {
@ -43,13 +41,18 @@ public interface CommandValueArgument : CommandArgument {
/** /**
* The [CommandValueArgument] that doesn't vary in type (remaining [MessageContent]). * The [CommandValueArgument] that doesn't vary in type (remaining [MessageContent]).
*/ */
@ConsoleExperimentalApi
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors
public data class InvariantCommandValueArgument( public data class DefaultCommandValueArgument(
public override val value: RawCommandArgument, public override val value: RawCommandArgument,
) : CommandValueArgument { ) : CommandValueArgument {
@OptIn(ExperimentalStdlibApi::class) @OptIn(ExperimentalStdlibApi::class)
override val type: KType = typeOf<MessageContent>() override val type: KType = typeOf<MessageContent>()
override val typeVariants: List<TypeVariant<*>> = listOf(MessageContentTypeVariant) override val typeVariants: List<TypeVariant<*>> = listOf(
MessageContentTypeVariant,
MessageChainTypeVariant,
ContentStringTypeVariant,
)
} }
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors

View File

@ -18,7 +18,7 @@ public object SpaceSeparatedCommandCallParser : CommandCallParser {
return CommandCallImpl( return CommandCallImpl(
caller = caller, caller = caller,
calleeName = flatten.first().content, calleeName = flatten.first().content,
valueArguments = flatten.drop(1).map(::InvariantCommandValueArgument) valueArguments = flatten.drop(1).map(::DefaultCommandValueArgument)
) )
} }