From 7b23dd0329994394c3892dfec0c41920c1dcae81 Mon Sep 17 00:00:00 2001 From: solo Date: Thu, 26 May 2022 05:13:54 -0400 Subject: [PATCH] Fix Kotlin and Java interop issues. (#343) The following PR fixes interop issues between Kotlin and Java. Currently, there is no way to go from `Command.Builder` -> `MutableCommandBuilder`, nor from `MutableCommandBuilder` -> `Command.Builder`. This PR fixes it by introducing - A new constructor for `MutableCommandBuilder`: ```kotlin public constructor(commandBuilder: Command.Builder, commandManager: CommandManager) ``` - Making the `commandBuilder` variable public - Adding the following extensions ```kotlin public fun Command.Builder.toMutable(commandManager: CommandManager): MutableCommandBuilder public fun Command.Builder.mutate(commandManager: CommandManager, lambda: MutableCommandBuilder.() -> Unit): MutableCommandBuilder ``` --- .../kotlin/MutableCommandBuilder.kt | 34 +++++++++---------- .../extension/CommandBuildingExtensions.kt | 22 ++++++++++++ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt index 1a1d8374..bbdeb5cd 100644 --- a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt +++ b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/MutableCommandBuilder.kt @@ -39,11 +39,22 @@ import kotlin.reflect.KClass * A mutable [Command.Builder] wrapper, providing functions to assist in creating commands using the * Kotlin builder DSL style * + * @property commandBuilder the command builder the mutate + * @property commandManager the command manager which will own this command + * @constructor Create a new [MutableCommandBuilder] * @since 1.3.0 */ -public class MutableCommandBuilder { - private val commandManager: CommandManager - private var commandBuilder: Command.Builder +public class MutableCommandBuilder( + commandBuilder: Command.Builder, + private val commandManager: CommandManager, +) { + /** + * The command builder that is being mutated by this [MutableCommandBuilder] instance. + * + * This is public so that this can be returned to a command builder for interop with java apis. + */ + public var commandBuilder: Command.Builder = commandBuilder + private set /** * Create a new [MutableCommandBuilder] @@ -64,10 +75,7 @@ public class MutableCommandBuilder { description: Description = Description.empty(), aliases: Array = emptyArray(), commandManager: CommandManager - ) { - this.commandManager = commandManager - this.commandBuilder = commandManager.commandBuilder(name, description, *aliases) - } + ) : this(commandManager.commandBuilder(name, description, *aliases), commandManager) /** * Create a new [MutableCommandBuilder] @@ -83,10 +91,7 @@ public class MutableCommandBuilder { description: ArgumentDescription = ArgumentDescription.empty(), aliases: Array = emptyArray(), commandManager: CommandManager - ) { - this.commandManager = commandManager - this.commandBuilder = commandManager.commandBuilder(name, description, *aliases) - } + ) : this(commandManager.commandBuilder(name, description, *aliases), commandManager) /** * Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it @@ -133,11 +138,6 @@ public class MutableCommandBuilder { lambda(this) } - private constructor(commandManager: CommandManager, commandBuilder: Command.Builder) { - this.commandManager = commandManager - this.commandBuilder = commandBuilder - } - /** * Build a [Command] from the current state of this builder * @@ -184,7 +184,7 @@ public class MutableCommandBuilder { * @since 1.3.0 */ public fun copy(): MutableCommandBuilder = - MutableCommandBuilder(this.commandManager, this.commandBuilder) + MutableCommandBuilder(this.commandBuilder, this.commandManager) /** * Make a new copy of this [MutableCommandBuilder] and invoke the provided receiver lambda on it diff --git a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt index 7d265050..57b5b19b 100644 --- a/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt +++ b/cloud-kotlin/cloud-kotlin-extensions/src/main/kotlin/cloud/commandframework/kotlin/extension/CommandBuildingExtensions.kt @@ -129,6 +129,28 @@ public fun CommandManager.command( public fun Command.Builder.senderType(type: KClass): Command.Builder = senderType(type.java) +/** + * Create a new [MutableCommandBuilder]. + * + * @param commandManager the command manager, which will own this command. + * @since 1.7.0 + */ +public fun Command.Builder.toMutable( + commandManager: CommandManager +): MutableCommandBuilder = MutableCommandBuilder(this, commandManager) + +/** + * Create a new [MutableCommandBuilder] and invoke the provided receiver lambda on it. + * + * @param commandManager the command manager, which will own this command. + * @param lambda receiver lambda, which will be invoked on the new builder. + * @since 1.7.0 + */ +public fun Command.Builder.mutate( + commandManager: CommandManager, + lambda: MutableCommandBuilder.() -> Unit +): MutableCommandBuilder = MutableCommandBuilder(this, commandManager).also(lambda) + /** * Get a [Description], defaulting to [Description.empty] *