From ef430f308630313b389d00557835e77e4b9c83a2 Mon Sep 17 00:00:00 2001 From: Devoxin Date: Mon, 23 Dec 2019 00:20:58 +0000 Subject: [PATCH] Allow default values in parameters --- .gitignore | 4 +++- .../me/devoxin/flight/api/CommandWrapper.kt | 23 ++++++++++++------- .../me/devoxin/flight/arguments/ArgParser.kt | 4 ++-- .../me/devoxin/flight/arguments/Argument.kt | 2 +- .../kotlin/me/devoxin/flight/utils/Indexer.kt | 4 ++-- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 8e4d0c9..5e13201 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,6 @@ gradle-app.setting # gradle/wrapper/gradle-wrapper.properties .idea/ -out/ \ No newline at end of file +out/ +src/test/* +*.log \ No newline at end of file diff --git a/src/main/kotlin/me/devoxin/flight/api/CommandWrapper.kt b/src/main/kotlin/me/devoxin/flight/api/CommandWrapper.kt index b15e49d..3ee5ba6 100644 --- a/src/main/kotlin/me/devoxin/flight/api/CommandWrapper.kt +++ b/src/main/kotlin/me/devoxin/flight/api/CommandWrapper.kt @@ -8,22 +8,26 @@ import kotlin.coroutines.suspendCoroutine import kotlin.reflect.KFunction import kotlin.reflect.KParameter import kotlin.reflect.full.callSuspendBy +import kotlin.reflect.full.instanceParameter class CommandWrapper( - val name: String, - val arguments: List, - val category: String, - val properties: Command, - val async: Boolean, - val method: KFunction<*>, - val cog: Cog, - internal val contextParameter: KParameter + val name: String, + val arguments: List, + val category: String, + val properties: Command, + val async: Boolean, + val method: KFunction<*>, + val cog: Cog, + private val contextParameter: KParameter ) { /** * Calls the related method with the given args. */ fun execute(ctx: Context, args: HashMap, complete: (Boolean, Throwable?) -> Unit) { + method.instanceParameter?.let { + args[it] = cog + } args[contextParameter] = ctx try { @@ -39,6 +43,9 @@ class CommandWrapper( * Calls the related method with the given args, except in an async manner. */ suspend fun executeAsync(ctx: Context, args: HashMap, complete: (Boolean, Throwable?) -> Unit) { + method.instanceParameter?.let { + args[it] = cog + } args[contextParameter] = ctx //suspendCoroutine { try { diff --git a/src/main/kotlin/me/devoxin/flight/arguments/ArgParser.kt b/src/main/kotlin/me/devoxin/flight/arguments/ArgParser.kt index faedd5e..b8854aa 100644 --- a/src/main/kotlin/me/devoxin/flight/arguments/ArgParser.kt +++ b/src/main/kotlin/me/devoxin/flight/arguments/ArgParser.kt @@ -105,7 +105,7 @@ class ArgParser( } } - if (!result.isPresent && !arg.optional) { + if (!result.isPresent && !arg.optional && !arg.isNullable) { throw BadArgument(arg, argument) } @@ -134,7 +134,7 @@ class ArgParser( for (arg in cmd.arguments) { val res = parser.parse(arg) - if (res != null || arg.valueRequired) { + if (res != null || (arg.isNullable && !arg.optional)) { //This will only place the argument into the map if the value is null, // or if the parameter requires a value (i.e. marked nullable). //Commands marked optional already have a parameter so they don't need user-provided values diff --git a/src/main/kotlin/me/devoxin/flight/arguments/Argument.kt b/src/main/kotlin/me/devoxin/flight/arguments/Argument.kt index ed2bc11..06b2b86 100644 --- a/src/main/kotlin/me/devoxin/flight/arguments/Argument.kt +++ b/src/main/kotlin/me/devoxin/flight/arguments/Argument.kt @@ -7,6 +7,6 @@ class Argument( val type: Class<*>, val greedy: Boolean, val optional: Boolean, // Denotes that a parameter has a default value. - val valueRequired: Boolean, // Denotes whether it's marked nullable, thus always requires a value. + val isNullable: Boolean, internal val kparam: KParameter ) diff --git a/src/main/kotlin/me/devoxin/flight/utils/Indexer.kt b/src/main/kotlin/me/devoxin/flight/utils/Indexer.kt index 0c05a84..3826607 100644 --- a/src/main/kotlin/me/devoxin/flight/utils/Indexer.kt +++ b/src/main/kotlin/me/devoxin/flight/utils/Indexer.kt @@ -92,9 +92,9 @@ class Indexer : Closeable { val type = p.type.jvmErasure.javaObjectType val greedy = p.hasAnnotation() val optional = p.isOptional - val valueRequired = p.type.isMarkedNullable + val isNullable = p.type.isMarkedNullable - arguments.add(Argument(pName, type, greedy, optional, valueRequired, p)) + arguments.add(Argument(pName, type, greedy, optional, isNullable, p)) } return CommandWrapper(name, arguments, category, properties, async, meth, cog, ctxParam)