remove cmd prefix from commands by delegate

This commit is contained in:
Sergey Chernov 2023-11-23 01:21:01 +03:00
parent ae3af68dab
commit 3a56e67c24
2 changed files with 18 additions and 3 deletions

View File

@ -22,13 +22,22 @@ inline fun <reified A, reified R> command(overrideName: String? = null): Command
class CommandDelegate<A, R>(
private val argsSerializer: KSerializer<A>,
private val resultSerializer: KSerializer<R>,
private val overrideName: String? = null
private val overrideName: String? = null,
) {
private var name: String = ""
operator fun getValue(nothing: Nothing?, property: KProperty<*>): Command<A, R> {
if (name.isEmpty()) {
name = overrideName ?: removeCmd(property.name)
}
return Command(
overrideName ?: property.name,
name,
argsSerializer,
resultSerializer
)
}
}
}
private fun removeCmd(name: String) =
if (name.startsWith("cmd"))
name.substring(3)
else name

View File

@ -17,4 +17,10 @@ class ToolsTest {
c[2] = 11u
assertFalse { isValidContrail(c) }
}
// @Test
// fun testRemoceCmd() {
// assertEquals("lalala", removeCmd("lalala"))
// assertEquals("lalala", removeCmd("cmdlalala"))
// }
}