All Downloads are FREE. Search and download functionalities are using the official Maven repository.

command.resolve.ResolvedCommandCall.kt Maven / Gradle / Ivy

There is a newer version: 2.16.0
Show newest version
/*
 * Copyright 2019-2020 Mamoe Technologies and contributors.
 *
 * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
 * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
 *
 * https://github.com/mamoe/mirai/blob/master/LICENSE
 */

package net.mamoe.mirai.console.command.resolve

import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.CompositeCommand
import net.mamoe.mirai.console.command.descriptor.*
import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser.Companion.parse
import net.mamoe.mirai.console.command.parse.CommandCall
import net.mamoe.mirai.console.command.parse.CommandValueArgument
import net.mamoe.mirai.console.command.parse.mapToTypeOrNull
import net.mamoe.mirai.console.internal.data.classifierAsKClass
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import net.mamoe.mirai.console.util.cast

/**
 * The resolved [CommandCall].
 *
 * ### Implementation details
 * [ResolvedCommandCall] should be _immutable_,
 * meaning all of its properties must be *pure* and should be implemented as an immutable property, or delegated by a lazy initializer.
 *
 * @see ResolvedCommandCallImpl
 */
@ExperimentalCommandDescriptors
public interface ResolvedCommandCall {
    /**
     * The [CommandSender] responsible to this call.
     */
    public val caller: CommandSender

    /**
     * The callee [Command]
     */
    public val callee: Command

    /**
     * The callee [CommandSignature], specifically a sub command from [CompositeCommand]
     */
    public val calleeSignature: CommandSignature

    /**
     * Original arguments
     */
    public val rawValueArguments: List

    /**
     * Resolved value arguments arranged mapping the [CommandSignature.valueParameters] by index.
     *
     * **Default implementation details**: Lazy calculation.
     */
    @ConsoleExperimentalApi
    public val resolvedValueArguments: List>

    public companion object
}

/**
 * Resolved [CommandValueParameter] for [ResolvedCommandCall.resolvedValueArguments]
 */
@ExperimentalCommandDescriptors
public data class ResolvedCommandValueArgument(
    val parameter: CommandValueParameter,
    /**
     * Argument value expected by the [parameter]
     */
    val value: T,
)

// Don't move into companion, compilation error
/**
 * Invoke this resolved call.
 */
@ExperimentalCommandDescriptors
public suspend inline fun ResolvedCommandCall.call() {
    return [email protected](this@call)
}

/**
 * Default implementation.
 */
@ExperimentalCommandDescriptors
public class ResolvedCommandCallImpl(
    override val caller: CommandSender,
    override val callee: Command,
    override val calleeSignature: CommandSignature,
    override val rawValueArguments: List,
    private val context: CommandArgumentContext,
) : ResolvedCommandCall {
    override val resolvedValueArguments: List> by lazy {
        calleeSignature.valueParameters.zip(rawValueArguments).map { (parameter, argument) ->
            val value = argument.mapToTypeOrNull(parameter.type) ?: context[parameter.type.classifierAsKClass()]?.parse(argument.value, caller)
            ?: throw  NoValueArgumentMappingException(argument, parameter.type)
            // TODO: 2020/10/17 consider vararg and optional
            ResolvedCommandValueArgument(parameter.cast(), value)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy