From f10632e7542de4f32181251be5b1b7f892f770cc Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 28 Oct 2020 13:30:44 +0800 Subject: [PATCH] Separate command descriptor files --- ...mmandDescriptor.kt => CommandParameter.kt} | 79 +--------------- .../command/descriptor/CommandSignature.kt | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+), 78 deletions(-) rename backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/{CommandDescriptor.kt => CommandParameter.kt} (75%) create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandSignature.kt diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandDescriptor.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandParameter.kt similarity index 75% rename from backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandDescriptor.kt rename to backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandParameter.kt index 562cc9b40..eae2a09b4 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandDescriptor.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandParameter.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020 Mamoe Technologies and contributors. + * Copyright 2019-2020 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -14,93 +14,16 @@ import net.mamoe.mirai.console.command.descriptor.AbstractCommandValueParameter. import net.mamoe.mirai.console.command.descriptor.AbstractCommandValueParameter.UserDefinedType.Companion.createRequired import net.mamoe.mirai.console.command.descriptor.ArgumentAcceptance.Companion.isAcceptable import net.mamoe.mirai.console.command.parse.CommandValueArgument -import net.mamoe.mirai.console.command.resolve.ResolvedCommandCall import net.mamoe.mirai.console.internal.data.classifierAsKClass import net.mamoe.mirai.console.internal.data.classifierAsKClassOrNull import net.mamoe.mirai.console.internal.data.typeOf0 import net.mamoe.mirai.console.util.ConsoleExperimentalApi import kotlin.reflect.KClass -import kotlin.reflect.KFunction import kotlin.reflect.KType import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.isSubtypeOf import kotlin.reflect.typeOf -/** - * 指令签名. 表示指令定义的需要的参数. - * - * @see AbstractCommandSignature - */ -@ExperimentalCommandDescriptors -public interface CommandSignature { - /** - * 接收者参数, 为 [CommandSender] 子类 - */ - @ConsoleExperimentalApi - public val receiverParameter: CommandReceiverParameter? - - /** - * 形式 值参数. - */ - public val valueParameters: List> - - /** - * 调用这个指令. - */ - public suspend fun call(resolvedCommandCall: ResolvedCommandCall) -} - -/** - * 来自 [KFunction] 反射得到的 [CommandSignature] - * - * @see CommandSignatureFromKFunctionImpl - */ -@ConsoleExperimentalApi -@ExperimentalCommandDescriptors -public interface CommandSignatureFromKFunction : CommandSignature { - public val originFunction: KFunction<*> -} - -/** - * @see CommandSignatureImpl - * @see CommandSignatureFromKFunctionImpl - */ -@ExperimentalCommandDescriptors -public abstract class AbstractCommandSignature : CommandSignature { - override fun toString(): String { - val receiverParameter = receiverParameter - return if (receiverParameter == null) { - "CommandSignatureVariant(${valueParameters.joinToString()})" - } else { - "CommandSignatureVariant($receiverParameter, ${valueParameters.joinToString()})" - } - } -} - -@ExperimentalCommandDescriptors -public open class CommandSignatureImpl( - override val receiverParameter: CommandReceiverParameter?, - override val valueParameters: List>, - private val onCall: suspend CommandSignatureImpl.(resolvedCommandCall: ResolvedCommandCall) -> Unit, -) : CommandSignature, AbstractCommandSignature() { - override suspend fun call(resolvedCommandCall: ResolvedCommandCall) { - return onCall(resolvedCommandCall) - } -} - -@ConsoleExperimentalApi -@ExperimentalCommandDescriptors -public open class CommandSignatureFromKFunctionImpl( - override val receiverParameter: CommandReceiverParameter?, - override val valueParameters: List>, - override val originFunction: KFunction<*>, - private val onCall: suspend CommandSignatureFromKFunctionImpl.(resolvedCommandCall: ResolvedCommandCall) -> Unit, -) : CommandSignatureFromKFunction, AbstractCommandSignature() { - override suspend fun call(resolvedCommandCall: ResolvedCommandCall) { - return onCall(resolvedCommandCall) - } -} - /** * Inherited instances must be [CommandValueParameter] or [CommandReceiverParameter] diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandSignature.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandSignature.kt new file mode 100644 index 000000000..8dba2deaf --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandSignature.kt @@ -0,0 +1,90 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.command.descriptor + +import net.mamoe.mirai.console.command.CommandSender +import net.mamoe.mirai.console.command.resolve.ResolvedCommandCall +import net.mamoe.mirai.console.util.ConsoleExperimentalApi +import kotlin.reflect.KFunction + +/** + * 指令签名. 表示指令定义的需要的参数. + * + * @see AbstractCommandSignature + */ +@ExperimentalCommandDescriptors +public interface CommandSignature { + /** + * 接收者参数, 为 [CommandSender] 子类 + */ + @ConsoleExperimentalApi + public val receiverParameter: CommandReceiverParameter? + + /** + * 形式 值参数. + */ + public val valueParameters: List> + + /** + * 调用这个指令. + */ + public suspend fun call(resolvedCommandCall: ResolvedCommandCall) +} + +/** + * 来自 [KFunction] 反射得到的 [CommandSignature] + * + * @see CommandSignatureFromKFunctionImpl + */ +@ConsoleExperimentalApi +@ExperimentalCommandDescriptors +public interface CommandSignatureFromKFunction : CommandSignature { + public val originFunction: KFunction<*> +} + +/** + * @see CommandSignatureImpl + * @see CommandSignatureFromKFunctionImpl + */ +@ExperimentalCommandDescriptors +public abstract class AbstractCommandSignature : CommandSignature { + override fun toString(): String { + val receiverParameter = receiverParameter + return if (receiverParameter == null) { + "CommandSignatureVariant(${valueParameters.joinToString()})" + } else { + "CommandSignatureVariant($receiverParameter, ${valueParameters.joinToString()})" + } + } +} + +@ExperimentalCommandDescriptors +public open class CommandSignatureImpl( + override val receiverParameter: CommandReceiverParameter?, + override val valueParameters: List>, + private val onCall: suspend CommandSignatureImpl.(resolvedCommandCall: ResolvedCommandCall) -> Unit, +) : CommandSignature, AbstractCommandSignature() { + override suspend fun call(resolvedCommandCall: ResolvedCommandCall) { + return onCall(resolvedCommandCall) + } +} + +@ConsoleExperimentalApi +@ExperimentalCommandDescriptors +public open class CommandSignatureFromKFunctionImpl( + override val receiverParameter: CommandReceiverParameter?, + override val valueParameters: List>, + override val originFunction: KFunction<*>, + private val onCall: suspend CommandSignatureFromKFunctionImpl.(resolvedCommandCall: ResolvedCommandCall) -> Unit, +) : CommandSignatureFromKFunction, AbstractCommandSignature() { + override suspend fun call(resolvedCommandCall: ResolvedCommandCall) { + return onCall(resolvedCommandCall) + } +} \ No newline at end of file