Skip to content

Commit

Permalink
缩小AbstractSubCommandGroup的feature范围
Browse files Browse the repository at this point in the history
  • Loading branch information
hundun000 committed Aug 20, 2024
1 parent 3f812d6 commit e4fcda5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation
import net.mamoe.mirai.console.MiraiConsoleImplementation.ConsoleDataScope.Companion.get
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.allRegisteredCommands
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.SubCommandGroup.Description
import net.mamoe.mirai.console.command.SubCommandGroup.Name
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
import net.mamoe.mirai.console.command.descriptor.CommandArgumentParserException
import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser.Companion.map
import net.mamoe.mirai.console.command.descriptor.PermissionIdValueArgumentParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,26 @@ public abstract class CompositeCommand(
*/ // open since 2.12
public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext


/* *//**
/**
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* @param value 子指令名
*//*
*/
@Retention(RUNTIME)
@Target(FUNCTION)
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.SubCommand")
protected annotation class SubCommand(
@ResolveContext(COMMAND_NAME) vararg val value: String = [],
)

*//** 指令描述 *//*
/** 指令描述 */
@Retention(RUNTIME)
@Target(FUNCTION)
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.Description")
protected annotation class Description(val value: String)

*//** 参数名, 将参与构成 [usage] *//*
/** 参数名, 将参与构成 [usage] */
@ConsoleExperimentalApi("Classname might change")
@Retention(RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
@Deprecated(level = HIDDEN, message = "use SubCommandGroup.Name")
protected annotation class Name(val value: String)*/
protected annotation class Name(val value: String)
}


22 changes: 0 additions & 22 deletions mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,4 @@ public interface SubCommandGroup {
public annotation class FlattenSubCommands(
)

/**
* 1. 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* 2. 标记一个属性为子指令集合,且使用sub策略
* @param value 子指令名
*/
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
public annotation class SubCommand(
@ResolveContext(ResolveContext.Kind.COMMAND_NAME) vararg val value: String = [],
)

/** 指令描述 */
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
public annotation class Description(val value: String)

/** 参数名, 由具体Command决定用途 */
@ConsoleExperimentalApi("Classname might change")
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
public annotation class Name(val value: String)

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,28 @@ internal fun Any.flattenCommandComponents(): MessageChain = buildMessageChain {
internal object CompositeCommandSubCommandAnnotationResolver :
SubCommandAnnotationResolver<Command> {
override fun isDeclaredSubCommand(ownerCommand: Command, function: KFunction<*>) =
function.hasAnnotation<SubCommandGroup.SubCommand>()
function.hasAnnotation<CompositeCommand.SubCommand>()

override fun getDeclaredSubCommandNames(ownerCommand: Command, function: KFunction<*>): Array<out String> {
val annotated = function.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
return if (annotated.isEmpty()) arrayOf(function.name)
else annotated
}

override fun getAnnotatedName(ownerCommand: Command, parameter: KParameter): String? =
parameter.findAnnotation<SubCommandGroup.Name>()?.value
parameter.findAnnotation<CompositeCommand.Name>()?.value

override fun getDescription(ownerCommand: Command, function: KFunction<*>): String? =
function.findAnnotation<SubCommandGroup.Description>()?.value
function.findAnnotation<CompositeCommand.Description>()?.value

override fun isCombinedSubCommands(command: Command, kProperty: KProperty<*>): Boolean =
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<SubCommandGroup.SubCommand>()
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()

override fun getCombinedAdditionNames(command: Command, kProperty: KProperty<*>): Array<out String> {
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
emptyArray()
} else {
val annotated = kProperty.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = kProperty.findAnnotation<CompositeCommand.SubCommand>()!!.value
if (annotated.isEmpty()) arrayOf(kProperty.name)
else annotated
}
Expand All @@ -100,28 +100,26 @@ internal object CompositeCommandSubCommandAnnotationResolver :
internal object GroupedCommandSubCommandAnnotationResolver :
SubCommandAnnotationResolver<Any> {
override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) =
function.hasAnnotation<SubCommandGroup.SubCommand>()
function.hasAnnotation<CompositeCommand.SubCommand>()

override fun getDeclaredSubCommandNames(ownerCommand: Any, function: KFunction<*>): Array<out String> {
val annotated = function.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = function.findAnnotation<CompositeCommand.SubCommand>()!!.value
return if (annotated.isEmpty()) arrayOf(function.name)
else annotated
}

override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? =
parameter.findAnnotation<SubCommandGroup.Name>()?.value
override fun getAnnotatedName(ownerCommand: Any, parameter: KParameter): String? = null

override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? =
function.findAnnotation<SubCommandGroup.Description>()?.value
override fun getDescription(ownerCommand: Any, function: KFunction<*>): String? = null

override fun isCombinedSubCommands(command: Any, kProperty: KProperty<*>): Boolean =
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<SubCommandGroup.SubCommand>()
kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>() || kProperty.hasAnnotation<CompositeCommand.SubCommand>()

override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array<out String> {
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
emptyArray()
} else {
val annotated = kProperty.findAnnotation<SubCommandGroup.SubCommand>()!!.value
val annotated = kProperty.findAnnotation<CompositeCommand.SubCommand>()!!.value
if (annotated.isEmpty()) arrayOf(kProperty.name)
else annotated
}
Expand Down Expand Up @@ -152,11 +150,29 @@ internal object SimpleCommandSubCommandAnnotationResolver :
}

internal interface SubCommandAnnotationResolver<T> {
/**
* 判断ownerCommand中的一个function是否能成为SubCommand
*/
fun isDeclaredSubCommand(ownerCommand: T, function: KFunction<*>): Boolean
/**
* 得出ownerCommand中的一个function成为SubCommand时的名字列表
*/
fun getDeclaredSubCommandNames(ownerCommand: T, function: KFunction<*>): Array<out String>
/**
* 得出ownerCommand中的一个function成为SubCommand时其参数表中的参数的描述
*/
fun getAnnotatedName(ownerCommand: T, parameter: KParameter): String?
/**
* 得出ownerCommand中的一个function成为SubCommand时的描述
*/
fun getDescription(ownerCommand: T, function: KFunction<*>): String?
/**
* 判断ownerCommand中的一个kProperty是否能成为SubCommand
*/
fun isCombinedSubCommands(command: T, kProperty: KProperty<*>): Boolean
/**
* 得出ownerCommand中的一个kProperty成为SubCommand时的指令路径的增加部分
*/
fun getCombinedAdditionNames(command: T, kProperty: KProperty<*>): Array<out String>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.java.JCompositeCommand
import net.mamoe.mirai.console.command.java.JRawCommand
import net.mamoe.mirai.console.command.java.JSimpleCommand
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
import net.mamoe.mirai.console.internal.data.classifierAsKClass
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.safeCast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext
import net.mamoe.mirai.console.command.java.JCompositeCommand
import net.mamoe.mirai.console.command.java.JSimpleCommand
import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
import net.mamoe.mirai.console.internal.data.classifierAsKClass
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.MessageContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,41 @@ import java.time.temporal.TemporalAccessor
import kotlin.reflect.KClass
import kotlin.test.*

import net.mamoe.mirai.console.command.SubCommandGroup.SubCommand
import net.mamoe.mirai.console.command.SubCommandGroup.FlattenSubCommands

class MyUnifiedCommand : CompositeCommand(
owner, "testMyUnifiedCommand", "tsMUC"
) {
// 插件一个模块的部分功能
class ModuleAPart1 : AbstractSubCommandGroup() {
class ModuleAPartB : AbstractSubCommandGroup() {
@FlattenSubCommands
val partC = ModuleAPartC()

@SubCommand
fun function1(arg0: Int) {
fun functionB0(arg0: Int) {
Testing.ok(arg0)
}
@SubCommand("customNameB1")
fun functionB1(arg0: Int) {
Testing.ok(arg0)
}
}

// 插件一个模块的另一部分功能
class ModuleAPart2 : AbstractSubCommandGroup() {
class ModuleAPartC : AbstractSubCommandGroup() {
@SubCommand
fun function2(arg0: Int) {
fun functionC0(arg0: Int) {
Testing.ok(arg0)
}
@SubCommand("customNameC1")
fun functionC1(arg0: Int) {
Testing.ok(arg0)
}
}

class ModuleACommandGroup: AbstractSubCommandGroup() {
@SubCommand // 与在函数上标注这个注解类似, 它会带 `part1` 这个名称前缀来注册指令. 需要执行 /base part1 function1
val part1 = ModuleAPart1()
@SubCommand("part1NewName") // 也可以使用 SubCommand 的参数来覆盖名称 /base part1NewName function1
val part1b = ModuleAPart1()
@FlattenSubCommands // 新增, 不带前缀注册指令, 执行 /base function2
val part2 = ModuleAPart2()
}

@FlattenSubCommands
val moduleA = ModuleACommandGroup()
@FlattenSubCommands // 新增若干, 不带前缀注册指令, 执行 `/base function10` `/base functionCustomName11`
val partB = ModuleAPartB()

@SubCommand
fun about(arg0: Int) {
fun functionA0(arg0: Int) {
Testing.ok(arg0)
}
}
Expand Down Expand Up @@ -542,19 +541,22 @@ internal class InstanceTestCommand : AbstractConsoleInstanceTest() {
}

@Test
fun `container composite command executing`() = runBlocking {
fun `unified composite command executing`() = runBlocking {
unifiedCompositeCommand.withRegistration {
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "part1 function1 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "functionA0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "functionB0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "part1NewName function1 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameB1 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "function2 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "functionC0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "about 0"))
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0"))
})
}
}
Expand Down

0 comments on commit e4fcda5

Please sign in to comment.