Skip to content

Commit

Permalink
Normalise error handling for coalescing converters
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Dec 16, 2023
1 parent 2934f51 commit 7c972da
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ public class ConverterBuilderClassBuilder : KoinComponent {
builder.append(" validator = validator,\n")
}

if (ConverterType.COALESCING in types) {
builder.append(" shouldThrow = !ignoreErrors,\n")
}

if (ConverterType.CHOICE in types) {
builder.append(" choices = choices,\n")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import dev.kord.core.event.interaction.AutoCompleteInteractionCreateEvent
public typealias Validator<T> = (suspend ValidationContext<T>.() -> Unit)?

/** Type alias representing a mutator callable. Keeps things relatively maintainable. **/
public typealias Mutator<T> = ((T) -> T)?
public typealias Mutator<T> = ((value: T) -> T)?

/** Type alias representing an autocomplete callable. **/
public typealias AutoCompleteCallback =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ package com.kotlindiscord.kord.extensions.commands.converters.builders

/** Converter builder for coalescing converters. **/
@Suppress("UnnecessaryAbstractClass") // Your face is an unnecessary abstract class
public abstract class CoalescingConverterBuilder<T> : ConverterBuilder<T>()
public abstract class CoalescingConverterBuilder<T> : ConverterBuilder<T>() {
/**
* Whether to ignore parsing errors when no values have been parsed out.
*
* This is intended for coalescing arguments at the end of a set of arguments, where that argument is required.
*/
public open var ignoreErrors: Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import com.kotlindiscord.kord.extensions.InvalidArgumentException

/** Converter builder for defaulting coalescing converters. **/
public abstract class DefaultingCoalescingConverterBuilder<T : Any> : CoalescingConverterBuilder<T>() {
/** Whether to ignore parsing errors when a value is provided. **/
public var ignoreErrors: Boolean = false
override var ignoreErrors: Boolean = false

/** Value to use when none is provided, or when there's a parsing error and [ignoreErrors] is `true`. **/
/** Value to use when none is provided, or when there's a parsing error and [ignoreErrors] is `true`. **/
public open lateinit var defaultValue: T

override fun validateArgument() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ package com.kotlindiscord.kord.extensions.commands.converters.builders

/** Converter builder for optional coalescing converters. **/
public abstract class OptionalCoalescingConverterBuilder<T : Any> : CoalescingConverterBuilder<T?>() {
/** Whether to ignore parsing errors when a value is provided. **/
public var ignoreErrors: Boolean = false
override var ignoreErrors: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import kotlinx.datetime.*
builderFields = [
"public var longHelp: Boolean = true",
"public var positiveOnly: Boolean = true",
"public var shouldThrow: Boolean = false"
],
)
public class DurationCoalescingConverter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import java.time.LocalDateTime
builderFields = [
"public var longHelp: Boolean = true",
"public var positiveOnly: Boolean = true",
"public var shouldThrow: Boolean = true"
],
)
public class J8DurationCoalescingConverter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import net.time4j.IsoUnit

builderFields = [
"public var longHelp: Boolean = true",
"public var shouldThrow: Boolean = true"
],
)
public class T4JDurationCoalescingConverter(
Expand Down

0 comments on commit 7c972da

Please sign in to comment.