Skip to content

Commit

Permalink
Allow default values in parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Dec 23, 2019
1 parent 5cf2ff7 commit ef430f3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ gradle-app.setting
# gradle/wrapper/gradle-wrapper.properties

.idea/
out/
out/
src/test/*
*.log
23 changes: 15 additions & 8 deletions src/main/kotlin/me/devoxin/flight/api/CommandWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Argument>,
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<Argument>,
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<KParameter, Any?>, complete: (Boolean, Throwable?) -> Unit) {
method.instanceParameter?.let {
args[it] = cog
}
args[contextParameter] = ctx

try {
Expand All @@ -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<KParameter, Any?>, complete: (Boolean, Throwable?) -> Unit) {
method.instanceParameter?.let {
args[it] = cog
}
args[contextParameter] = ctx
//suspendCoroutine<Unit> {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/me/devoxin/flight/arguments/ArgParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ArgParser(
}
}

if (!result.isPresent && !arg.optional) {
if (!result.isPresent && !arg.optional && !arg.isNullable) {
throw BadArgument(arg, argument)
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/me/devoxin/flight/arguments/Argument.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions src/main/kotlin/me/devoxin/flight/utils/Indexer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class Indexer : Closeable {
val type = p.type.jvmErasure.javaObjectType
val greedy = p.hasAnnotation<Greedy>()
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)
Expand Down

0 comments on commit ef430f3

Please sign in to comment.