From b3880093bffefe8a95731e0a83231ef9be9a6b09 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 24 Oct 2020 11:41:32 +0800 Subject: [PATCH] Fix StringConstant params --- .../internal/command/CommandReflector.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandReflector.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandReflector.kt index 2dc504b85..ebba4b153 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandReflector.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CommandReflector.kt @@ -36,8 +36,11 @@ internal object CompositeCommandSubCommandAnnotationResolver : override fun hasAnnotation(ownerCommand: Command, function: KFunction<*>) = function.hasAnnotation() - override fun getSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array = - function.findAnnotation()!!.value + override fun getSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array { + val annotated = function.findAnnotation()!!.value + return if (annotated.isEmpty()) arrayOf(function.name) + else annotated + } override fun getAnnotatedName(ownerCommand: Command, parameter: KParameter): String? = parameter.findAnnotation()?.value @@ -190,7 +193,7 @@ internal class CommandReflector( .map { function -> val functionNameAsValueParameter = - annotationResolver.getSubCommandNames(command, function).map { createStringConstantParameter(it) } + annotationResolver.getSubCommandNames(command, function).mapIndexed { index, s -> createStringConstantParameter(index, s) } val functionValueParameters = function.valueParameters.associateBy { it.toUserDefinedCommandParameter() } @@ -202,7 +205,10 @@ internal class CommandReflector( ) { call -> val args = LinkedHashMap() - call.resolvedValueArguments.forEach { (commandParameter, value) -> + for ((commandParameter, value) in call.resolvedValueArguments) { + if (commandParameter is AbstractCommandValueParameter.StringConstant) { + continue + } val functionParameter = functionValueParameters[commandParameter] ?: error("Could not find a corresponding function parameter '${commandParameter.name}'") args[functionParameter] = value @@ -224,8 +230,8 @@ internal class CommandReflector( return CommandReceiverParameter(this.type.isMarkedNullable, this.type) } - private fun createStringConstantParameter(expectingValue: String): AbstractCommandValueParameter.StringConstant { - return AbstractCommandValueParameter.StringConstant(null, expectingValue) + private fun createStringConstantParameter(index: Int, expectingValue: String): AbstractCommandValueParameter.StringConstant { + return AbstractCommandValueParameter.StringConstant("#$index", expectingValue) } private fun KParameter.toUserDefinedCommandParameter(): AbstractCommandValueParameter.UserDefinedType<*> {