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

View File

@ -25,7 +25,7 @@ public object BuiltInCommandCallResolver : CommandCallResolver {
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(

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.CommandSender
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.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.descriptor.NoValueArgumentMappingException
import net.mamoe.mirai.console.command.parse.CommandCall
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 kotlin.LazyThreadSafetyMode.PUBLICATION
@ -65,10 +68,12 @@ public class ResolvedCommandCallImpl(
override val callee: Command,
override val calleeSignature: CommandSignatureVariant,
override val rawValueArguments: List<CommandValueArgument>,
private val context: CommandArgumentContext,
) : ResolvedCommandCall {
override val resolvedValueArguments: List<Any?> by lazy(PUBLICATION) {
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
}
}

View File

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