Support conversion with CommandArgumentContext

This commit is contained in:
Him188 2020-10-20 14:07:08 +08:00
parent 3e2a5c382e
commit 3d4f31759f
4 changed files with 18 additions and 10 deletions

View File

@ -7,6 +7,8 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:Suppress("unused")
package net.mamoe.mirai.console.command.parse package net.mamoe.mirai.console.command.parse
import net.mamoe.mirai.console.command.descriptor.* import net.mamoe.mirai.console.command.descriptor.*
@ -74,8 +76,8 @@ public fun <T> CommandValueArgument.mapToTypeOrNull(expectingType: KType): T? {
@OptIn(ExperimentalStdlibApi::class) @OptIn(ExperimentalStdlibApi::class)
val result = typeVariants val result = typeVariants
.filter { it.outType.isSubtypeOf(expectingType) } .filter { it.outType.isSubtypeOf(expectingType) }
.also { .ifEmpty {
if (it.isEmpty()) return null return null
} }
.reduce { acc, typeVariant -> .reduce { acc, typeVariant ->
if (acc.outType.isSubtypeOf(typeVariant.outType)) if (acc.outType.isSubtypeOf(typeVariant.outType))

View File

@ -25,7 +25,7 @@ public object BuiltInCommandCallResolver : CommandCallResolver {
val signature = resolveImpl(callee, valueArguments, context) ?: return null val signature = resolveImpl(callee, valueArguments, context) ?: return null
return ResolvedCommandCallImpl(call.caller, callee, signature, call.valueArguments) return ResolvedCommandCallImpl(call.caller, callee, signature, call.valueArguments, context ?: EmptyCommandArgumentContext)
} }
private data class ResolveData( private data class ResolveData(

View File

@ -12,11 +12,14 @@ package net.mamoe.mirai.console.command.resolve
import net.mamoe.mirai.console.command.Command import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.command.CommandSender import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CompositeCommand import net.mamoe.mirai.console.command.CompositeCommand
import net.mamoe.mirai.console.command.descriptor.CommandArgumentContext
import net.mamoe.mirai.console.command.descriptor.CommandSignatureVariant import net.mamoe.mirai.console.command.descriptor.CommandSignatureVariant
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.descriptor.NoValueArgumentMappingException
import net.mamoe.mirai.console.command.parse.CommandCall import net.mamoe.mirai.console.command.parse.CommandCall
import net.mamoe.mirai.console.command.parse.CommandValueArgument import net.mamoe.mirai.console.command.parse.CommandValueArgument
import net.mamoe.mirai.console.command.parse.mapToType import net.mamoe.mirai.console.command.parse.mapToTypeOrNull
import net.mamoe.mirai.console.internal.data.classifierAsKClass
import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.LazyThreadSafetyMode.PUBLICATION import kotlin.LazyThreadSafetyMode.PUBLICATION
@ -65,10 +68,12 @@ public class ResolvedCommandCallImpl(
override val callee: Command, override val callee: Command,
override val calleeSignature: CommandSignatureVariant, override val calleeSignature: CommandSignatureVariant,
override val rawValueArguments: List<CommandValueArgument>, override val rawValueArguments: List<CommandValueArgument>,
private val context: CommandArgumentContext,
) : ResolvedCommandCall { ) : ResolvedCommandCall {
override val resolvedValueArguments: List<Any?> by lazy(PUBLICATION) { override val resolvedValueArguments: List<Any?> by lazy(PUBLICATION) {
calleeSignature.valueParameters.zip(rawValueArguments).map { (parameter, argument) -> calleeSignature.valueParameters.zip(rawValueArguments).map { (parameter, argument) ->
argument.mapToType(parameter.type) argument.mapToTypeOrNull(parameter.type) ?: context[parameter.type.classifierAsKClass()]?.parse(argument.value, caller)
?: throw NoValueArgumentMappingException(argument, parameter.type)
// TODO: 2020/10/17 consider vararg and optional // TODO: 2020/10/17 consider vararg and optional
} }
} }

View File

@ -136,12 +136,13 @@ internal class TestCommand {
@Test @Test
fun `executing command by string command`() = runBlocking { fun `executing command by string command`() = runBlocking {
TestCompositeCommand.register() TestCompositeCommand.withRegistration {
val result = withTesting<Int> { val result = withTesting<Int> {
assertSuccess(sender.executeCommand("/testComposite mute 1")) assertSuccess(sender.executeCommand("/testComposite mute 1"))
} }
assertEquals(1, result) assertEquals(1, result)
}
} }
@Test @Test