From 3a56e67c24af1b77f09e0cd8227ccc6ced751c22 Mon Sep 17 00:00:00 2001 From: sergeych Date: Thu, 23 Nov 2023 01:21:01 +0300 Subject: [PATCH] remove cmd prefix from commands by delegate --- .../net/sergeych/kiloparsec/CommandDelegate.kt | 15 ++++++++++++--- src/commonTest/kotlin/ToolsTest.kt | 6 ++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/commonMain/kotlin/net/sergeych/kiloparsec/CommandDelegate.kt b/src/commonMain/kotlin/net/sergeych/kiloparsec/CommandDelegate.kt index 8d91a33..33d68d6 100644 --- a/src/commonMain/kotlin/net/sergeych/kiloparsec/CommandDelegate.kt +++ b/src/commonMain/kotlin/net/sergeych/kiloparsec/CommandDelegate.kt @@ -22,13 +22,22 @@ inline fun command(overrideName: String? = null): Command class CommandDelegate( private val argsSerializer: KSerializer, private val resultSerializer: KSerializer, - private val overrideName: String? = null + private val overrideName: String? = null, ) { + private var name: String = "" operator fun getValue(nothing: Nothing?, property: KProperty<*>): Command { + if (name.isEmpty()) { + name = overrideName ?: removeCmd(property.name) + } return Command( - overrideName ?: property.name, + name, argsSerializer, resultSerializer ) } -} \ No newline at end of file +} + +private fun removeCmd(name: String) = + if (name.startsWith("cmd")) + name.substring(3) + else name diff --git a/src/commonTest/kotlin/ToolsTest.kt b/src/commonTest/kotlin/ToolsTest.kt index ae19541..f2a9ba5 100644 --- a/src/commonTest/kotlin/ToolsTest.kt +++ b/src/commonTest/kotlin/ToolsTest.kt @@ -17,4 +17,10 @@ class ToolsTest { c[2] = 11u assertFalse { isValidContrail(c) } } + +// @Test +// fun testRemoceCmd() { +// assertEquals("lalala", removeCmd("lalala")) +// assertEquals("lalala", removeCmd("cmdlalala")) +// } } \ No newline at end of file