Skip to content

Commit

Permalink
update GroupedCommandSubCommandAnnotationResolver and Test
Browse files Browse the repository at this point in the history
  • Loading branch information
hundun000 committed Aug 21, 2024
1 parent bf685fc commit 1d3e03d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import net.mamoe.mirai.console.internal.command.CommandReflector
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.DeprecationLevel.*
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,26 @@ internal object CompositeCommandSubCommandAnnotationResolver :

@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
internal object GroupedCommandSubCommandAnnotationResolver :
SubCommandAnnotationResolver<Any> {
override fun isDeclaredSubCommand(ownerCommand: Any, function: KFunction<*>) =
SubCommandAnnotationResolver<SubCommandGroup> {
override fun isDeclaredSubCommand(ownerCommand: SubCommandGroup, function: KFunction<*>) =
function.hasAnnotation<CompositeCommand.SubCommand>()

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

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

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

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

override fun getCombinedAdditionNames(command: Any, kProperty: KProperty<*>): Array<out String> {
override fun getCombinedAdditionNames(command: SubCommandGroup, kProperty: KProperty<*>): Array<out String> {
return if (kProperty.hasAnnotation<SubCommandGroup.FlattenSubCommands>()) {
emptyArray()
} else {
Expand Down Expand Up @@ -516,4 +518,4 @@ internal class SubCommandReflector<T: Any>(

private fun KParameter.nameForCommandParameter(): String? =
annotationResolver.getAnnotatedName(owner, this) ?: this.name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,29 @@ import kotlin.test.*

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

/**
* 测试:CompositeCommand下接3种子节点;AbstractSubCommandGroup下接3种子节点;
*
*
* [MyUnifiedCommand : CompositeCommand]
* |
* -------------------------------------------------------
* | \ \
* [ModuleB : AbstractSubCommandGroup] [functionA0:KFunction] [ModuleE : CompositeCommand]
* |
* -----------------------------------------------------------------
* | \ \
* [ModuleC : AbstractSubCommandGroup] [ModuleD : CompositeCommand] [functionB0/functionB1:KFunction]
*
*/
class MyUnifiedCommand : CompositeCommand(
owner, "testMyUnifiedCommand", "tsMUC"
) {
class ModuleAPartB : AbstractSubCommandGroup() {
class ModuleB : AbstractSubCommandGroup() {
@FlattenSubCommands
val moduleC = ModuleC()
@FlattenSubCommands
val partC = ModuleAPartC()
val moduleDInB = ModuleD()

@SubCommand
fun functionB0(arg0: Int) {
Expand All @@ -54,7 +71,7 @@ class MyUnifiedCommand : CompositeCommand(
}
}

class ModuleAPartC : AbstractSubCommandGroup() {
class ModuleC : AbstractSubCommandGroup() {
@SubCommand
fun functionC0(arg0: Int) {
Testing.ok(arg0)
Expand All @@ -65,8 +82,33 @@ class MyUnifiedCommand : CompositeCommand(
}
}

@FlattenSubCommands // 新增若干, 不带前缀注册指令, 执行 `/base function10` `/base functionCustomName11`
val partB = ModuleAPartB()
class ModuleD : CompositeCommand(owner, "USELESS") {
@SubCommand
fun functionD0(arg0: Int) {
Testing.ok(arg0)
}
@SubCommand("customNameD1")
fun functionD1(arg0: Int) {
Testing.ok(arg0)
}
}

class ModuleE : CompositeCommand(owner, "USELESS") {
@SubCommand
fun functionE0(arg0: Int) {
Testing.ok(arg0)
}
@SubCommand("customNameE1")
fun functionE1(arg0: Int) {
Testing.ok(arg0)
}
}

@FlattenSubCommands
val moduleB = ModuleB()

@FlattenSubCommands
val moduleE = ModuleE()

@SubCommand
fun functionA0(arg0: Int) {
Expand Down Expand Up @@ -558,6 +600,21 @@ internal class InstanceTestCommand : AbstractConsoleInstanceTest() {
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameC1 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "functionD0 0"))
})
assertEquals(0, withTesting {
assertSuccess(unifiedCompositeCommand.execute(sender, "customNameD1 0"))
})
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionA0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionB0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameB1 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionC0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameC1 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionD0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameD1 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} functionE0 <arg0>"))
assertEquals(true, unifiedCompositeCommand.usage.contains("/${unifiedCompositeCommand.primaryName} customNameE1 <arg0>"))
}
}

Expand Down

0 comments on commit 1d3e03d

Please sign in to comment.