CommandParameter optionality and UserDefinedType

This commit is contained in:
Him188 2020-10-09 12:52:47 +08:00
parent 86c3b18bca
commit ccc9128023
3 changed files with 42 additions and 19 deletions

View File

@ -31,16 +31,18 @@ public interface CommandSignatureVariant {
@ExperimentalCommandDescriptors
public interface ICommandParameter<T : Any?> {
public val name: String
public val type: KType
/**
* If [isOptional] is `false`, [defaultValue] is always `null`.
* Otherwise [defaultValue] may be `null` iff [T] is nullable.
*/
public val defaultValue: T?
public val isOptional: Boolean
public companion object {
@get:JvmStatic
@ExperimentalCommandDescriptors
public val ICommandParameter<*>.isOptional: Boolean
get() = this.defaultValue != null
}
/**
* Reified type of [T]
*/
public val type: KType
}
@ExperimentalCommandDescriptors
@ -54,9 +56,10 @@ public sealed class CommandValueParameter<T> : ICommandParameter<T> {
public class StringConstant(
public override val name: String,
public override val defaultValue: String,
) : CommandValueParameter<String>() {
override val type: KType get() = STRING_TYPE
public override val type: KType get() = STRING_TYPE
public override val defaultValue: Nothing? get() = null
public override val isOptional: Boolean get() = false
private companion object {
@OptIn(ExperimentalStdlibApi::class)
@ -64,6 +67,27 @@ public sealed class CommandValueParameter<T> : ICommandParameter<T> {
}
}
public class UserDefinedType<T>(
public override val name: String,
public override val defaultValue: T?,
public override val isOptional: Boolean,
public override val type: KType,
) : CommandValueParameter<T>() {
public companion object {
@JvmStatic
public inline fun <reified T : Any> createOptional(name: String, defaultValue: T): UserDefinedType<T> {
@OptIn(ExperimentalStdlibApi::class)
return UserDefinedType(name, defaultValue, true, typeOf<T>())
}
@JvmStatic
public inline fun <reified T : Any> createRequired(name: String): UserDefinedType<T> {
@OptIn(ExperimentalStdlibApi::class)
return UserDefinedType(name, null, false, typeOf<T>())
}
}
}
/**
* Extended by [CommandValueArgumentParser]
*/

View File

@ -14,8 +14,6 @@ package net.mamoe.mirai.console.command.parse
import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.resolve.CommandCallResolver
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
/**
* Unresolved [CommandCall].
@ -33,10 +31,11 @@ public interface CommandCall {
* Explicit value arguments
*/
public val valueArguments: List<CommandValueArgument>
}
/**
* Custom data for [CommandCallResolver]
*/
@ConsoleExperimentalApi
public val customData: Map<Any, Any>
}
@ExperimentalCommandDescriptors
public class CommandCallImpl(
override val caller: CommandSender,
override val calleeName: String,
override val valueArguments: List<CommandValueArgument>,
) : CommandCall

View File

@ -15,7 +15,7 @@ import net.mamoe.mirai.console.extension.AbstractExtensionPoint
import net.mamoe.mirai.console.extension.InstanceExtension
/**
* The resolver for a [CommandCall]
* The provider of [CommandCallParser]
*/
@ExperimentalCommandDescriptors
public interface CommandCallParserProvider : InstanceExtension<CommandCallParser> {