This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
294 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# 0.2.0 | ||
|
||
## Additions | ||
|
||
* Added support for aliases | ||
|
||
#0.1.1 | ||
|
||
## Fixes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ands-runtime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/model/command/AliasInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.gitlab.kordlib.kordx.commands.model.command | ||
|
||
/** | ||
* Information on the command's relation to other aliases. | ||
* A command can either be a [Parent] of aliases, | ||
* a [Child] alias of another command, | ||
* or [not][None] be part of any alias structure. | ||
*/ | ||
sealed class AliasInfo<T : CommandEvent> { | ||
|
||
/** | ||
* The command is not part of any alias structure. It has neither a parent nor children. | ||
*/ | ||
class None<T : CommandEvent> : AliasInfo<T>() | ||
|
||
/** | ||
* The command is a parent, it contains [children] with the same functionality under a different [Command.name]. | ||
* | ||
* @param childrenNames the names of the child commands defined in [CommandBuilder.alias]. | ||
* @param commands A map of all registered commands, these are used to resolve the [children]. | ||
*/ | ||
class Parent<T : CommandEvent>( | ||
private val childrenNames: Set<String>, | ||
private val commands: Map<String, Command<*>> | ||
) : AliasInfo<T>() { | ||
|
||
/** | ||
* The child commands defined by invoking [CommandBuilder.alias]. These are identical copies of the parent | ||
* with a different [Command.name]. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
val children: List<Command<T>> | ||
get() = childrenNames.map { commands.getValue(it) as Command<T> } | ||
} | ||
|
||
/** | ||
* The command is a child, it contains a [parent] with the same functionality under a different [Command.name]. | ||
* | ||
* @param parentName The name of the parent command that created this command. | ||
* @param commands A map of all registered commands, these are used to resolve the [parent]. | ||
*/ | ||
class Child<T : CommandEvent>( | ||
private val parentName: String, | ||
private val commands: Map<String, Command<*>> | ||
) : AliasInfo<T>() { | ||
|
||
/** | ||
* The parent command that defined this alias, it is an identical copy of this command with a different name. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
val parent: Command<T> | ||
get() = commands[parentName] as Command<T> | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ntime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/model/exception/KordxException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
@file:Suppress("MemberVisibilityCanBePrivate", "CanBeParameter") | ||
|
||
package com.gitlab.kordlib.kordx.commands.model.exception | ||
|
||
import com.gitlab.kordlib.kordx.commands.model.command.Command | ||
import com.gitlab.kordlib.kordx.commands.model.command.CommandBuilder | ||
import com.gitlab.kordlib.kordx.commands.model.module.Module | ||
|
||
/** | ||
* Base class for all kordx.commands exceptions. | ||
*/ | ||
abstract class KordxException(override val message: String?) : Exception() | ||
|
||
/** | ||
* Exception thrown when an [CommandBuilder] detected an alias that is the same as the [CommandBuilder.name]. | ||
* | ||
* Kordx.commands expects all command names to be unique, this includes aliases. | ||
* | ||
* @param builder The builder that encountered the offending alias. | ||
* @param aliasName The offending alias. | ||
*/ | ||
class ConflictingAliasException( | ||
val builder: CommandBuilder<*, *, *>, | ||
val aliasName: String | ||
) : KordxException(""" | ||
Command '${builder.name}' has an alias '$aliasName' that that is identical to the command name. | ||
""".trimIndent()) | ||
|
||
/** | ||
* Exception thrown when an [Module] was created with the same name of another module. | ||
* Kordx.commands expects all module names to be unique. | ||
* | ||
* @param module The module that was already registered. | ||
* @param conflictingModule The module that was going to be added under the same name. | ||
*/ | ||
class DuplicateModuleNameException( | ||
val module: Module, | ||
val conflictingModule: Module | ||
) : KordxException(""" | ||
Module '${conflictingModule.name}' was registered, but another module with the same name already exists. | ||
""".trimIndent()) | ||
|
||
/** | ||
* Exception thrown when an [Command] was created with the same name of another command. | ||
* Kordx.commands expects all command names to be unique. | ||
* | ||
* @param command The command that was already registered. | ||
* @param conflictingCommand The command that was going to be added under the same name. | ||
*/ | ||
class DuplicateCommandNameException( | ||
val command: Command<*>, | ||
val conflictingCommand: Command<*> | ||
) : KordxException(""" | ||
Command '${conflictingCommand.name}' was registered, but another command with the same name already exists. | ||
""".trimIndent()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ime/src/main/kotlin/com/gitlab/kordlib/kordx/commands/model/processor/BuildEnvironment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.gitlab.kordlib.kordx.commands.model.processor | ||
|
||
import com.gitlab.kordlib.kordx.commands.model.command.Command | ||
import com.gitlab.kordlib.kordx.commands.model.exception.DuplicateCommandNameException | ||
import com.gitlab.kordlib.kordx.commands.model.exception.DuplicateModuleNameException | ||
import com.gitlab.kordlib.kordx.commands.model.module.Module | ||
import org.koin.core.Koin | ||
|
||
/** | ||
* Container used to store all build [commands] and [modules] in a [CommandProcessor]. | ||
* | ||
* @param koin The Koin instance for dependency injection post [CommandProcessor] build. | ||
*/ | ||
data class BuildEnvironment( | ||
val koin: Koin, | ||
val modules: MutableMap<String, Module> = mutableMapOf(), | ||
val commands: MutableMap<String, Command<*>> = mutableMapOf() | ||
) { | ||
|
||
/** | ||
* Adds the [module] to the [modules]. | ||
* | ||
* @throws DuplicateModuleNameException if a module with the same name is already registered. | ||
*/ | ||
fun addModule(module: Module) { | ||
if (modules.containsKey(module.name)) throw DuplicateModuleNameException(modules[module.name]!!, module) | ||
|
||
modules[module.name] = module | ||
} | ||
|
||
/** | ||
* Adds the [command] to the [commands]. | ||
* | ||
* @throws DuplicateCommandNameException if a command with the same name is already registered. | ||
*/ | ||
fun addCommand(command: Command<*>) { | ||
if (commands.containsKey(command.name)) throw DuplicateCommandNameException(commands[command.name]!!, command) | ||
|
||
commands[command.name] = command | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.