Skip to content

Commit

Permalink
Bring kord enum API closer to bit flags API (#861)
Browse files Browse the repository at this point in the history
This was done by only printing the raw value for Unknown entries in
toString() and replacing the public Unknown constructor with a from()
function in the companion object.
  • Loading branch information
lukellmann authored Sep 3, 2023
1 parent ccdd266 commit ffa8390
Show file tree
Hide file tree
Showing 53 changed files with 1,375 additions and 595 deletions.
44 changes: 44 additions & 0 deletions common/api/common.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,26 @@ public sealed class ActivityType(

final override fun hashCode(): Int = code.hashCode()

final override fun toString(): String = "ActivityType.${this::class.simpleName}(code=$code)"
final override fun toString(): String = if (this is Unknown) "ActivityType.Unknown(code=$code)"
else "ActivityType.${this::class.simpleName}"

/**
* An unknown [ActivityType].
*
* This is used as a fallback for [ActivityType]s that haven't been added to Kord yet.
*/
public class Unknown(
public class Unknown internal constructor(
code: Int,
) : ActivityType(code)
@Suppress(names = arrayOf("UNUSED_PARAMETER"))
unused: Nothing?,
) : ActivityType(code) {
@Deprecated(
message = "Replaced by 'ActivityType.from()'.",
replaceWith = ReplaceWith(expression = "ActivityType.from(code)", imports =
arrayOf("dev.kord.common.entity.ActivityType")),
)
public constructor(code: Int) : this(code, null)
}

public object Game : ActivityType(0)

Expand All @@ -60,16 +70,7 @@ public sealed class ActivityType(
encoder.encodeInt(value.code)
}

override fun deserialize(decoder: Decoder): ActivityType =
when (val code = decoder.decodeInt()) {
0 -> Game
1 -> Streaming
2 -> Listening
3 -> Watching
4 -> Custom
5 -> Competing
else -> Unknown(code)
}
override fun deserialize(decoder: Decoder): ActivityType = from(decoder.decodeInt())
}

public companion object {
Expand All @@ -87,5 +88,19 @@ public sealed class ActivityType(
)
}


/**
* Returns an instance of [ActivityType] with [ActivityType.code] equal to the specified
* [code].
*/
public fun from(code: Int): ActivityType = when (code) {
0 -> Game
1 -> Streaming
2 -> Listening
3 -> Watching
4 -> Custom
5 -> Competing
else -> Unknown(code, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,26 @@ public sealed class AllowedMentionType(
final override fun hashCode(): Int = value.hashCode()

final override fun toString(): String =
"AllowedMentionType.${this::class.simpleName}(value=$value)"
if (this is Unknown) "AllowedMentionType.Unknown(value=$value)"
else "AllowedMentionType.${this::class.simpleName}"

/**
* An unknown [AllowedMentionType].
*
* This is used as a fallback for [AllowedMentionType]s that haven't been added to Kord yet.
*/
public class Unknown(
public class Unknown internal constructor(
`value`: String,
) : AllowedMentionType(value)
@Suppress(names = arrayOf("UNUSED_PARAMETER"))
unused: Nothing?,
) : AllowedMentionType(value) {
@Deprecated(
message = "Replaced by 'AllowedMentionType.from()'.",
replaceWith = ReplaceWith(expression = "AllowedMentionType.from(value)", imports =
arrayOf("dev.kord.common.entity.AllowedMentionType")),
)
public constructor(`value`: String) : this(value, null)
}

/**
* Controls role mentions.
Expand All @@ -66,12 +76,7 @@ public sealed class AllowedMentionType(
}

override fun deserialize(decoder: Decoder): AllowedMentionType =
when (val value = decoder.decodeString()) {
"roles" -> RoleMentions
"users" -> UserMentions
"everyone" -> EveryoneMentions
else -> Unknown(value)
}
from(decoder.decodeString())
}

public companion object {
Expand All @@ -86,5 +91,16 @@ public sealed class AllowedMentionType(
)
}


/**
* Returns an instance of [AllowedMentionType] with [AllowedMentionType.value] equal to the
* specified [value].
*/
public fun from(`value`: String): AllowedMentionType = when (value) {
"roles" -> RoleMentions
"users" -> UserMentions
"everyone" -> EveryoneMentions
else -> Unknown(value, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ public sealed class ApplicationCommandOptionType(
final override fun hashCode(): Int = type.hashCode()

final override fun toString(): kotlin.String =
"ApplicationCommandOptionType.${this::class.simpleName}(type=$type)"
if (this is Unknown) "ApplicationCommandOptionType.Unknown(type=$type)"
else "ApplicationCommandOptionType.${this::class.simpleName}"

/**
* An unknown [ApplicationCommandOptionType].
*
* This is used as a fallback for [ApplicationCommandOptionType]s that haven't been added to
* Kord yet.
*/
public class Unknown(
public class Unknown internal constructor(
type: Int,
) : ApplicationCommandOptionType(type)
@Suppress(names = arrayOf("UNUSED_PARAMETER"))
unused: Nothing?,
) : ApplicationCommandOptionType(type) {
@Deprecated(
message = "Replaced by 'ApplicationCommandOptionType.from()'.",
replaceWith = ReplaceWith(expression = "ApplicationCommandOptionType.from(type)",
imports = arrayOf("dev.kord.common.entity.ApplicationCommandOptionType")),
)
public constructor(type: Int) : this(type, null)
}

public object SubCommand : ApplicationCommandOptionType(1)

Expand Down Expand Up @@ -86,20 +96,7 @@ public sealed class ApplicationCommandOptionType(
}

override fun deserialize(decoder: Decoder): ApplicationCommandOptionType =
when (val type = decoder.decodeInt()) {
1 -> SubCommand
2 -> SubCommandGroup
3 -> String
4 -> Integer
5 -> Boolean
6 -> User
7 -> Channel
8 -> Role
9 -> Mentionable
10 -> Number
11 -> Attachment
else -> Unknown(type)
}
from(decoder.decodeInt())
}

public companion object {
Expand All @@ -122,5 +119,24 @@ public sealed class ApplicationCommandOptionType(
)
}


/**
* Returns an instance of [ApplicationCommandOptionType] with
* [ApplicationCommandOptionType.type] equal to the specified [type].
*/
public fun from(type: Int): ApplicationCommandOptionType = when (type) {
1 -> SubCommand
2 -> SubCommandGroup
3 -> String
4 -> Integer
5 -> Boolean
6 -> User
7 -> Channel
8 -> Role
9 -> Mentionable
10 -> Number
11 -> Attachment
else -> Unknown(type, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,28 @@ public sealed class ApplicationCommandPermissionType(
final override fun hashCode(): Int = value.hashCode()

final override fun toString(): String =
"ApplicationCommandPermissionType.${this::class.simpleName}(value=$value)"
if (this is Unknown) "ApplicationCommandPermissionType.Unknown(value=$value)"
else "ApplicationCommandPermissionType.${this::class.simpleName}"

/**
* An unknown [ApplicationCommandPermissionType].
*
* This is used as a fallback for [ApplicationCommandPermissionType]s that haven't been added to
* Kord yet.
*/
public class Unknown(
public class Unknown internal constructor(
`value`: Int,
) : ApplicationCommandPermissionType(value)
@Suppress(names = arrayOf("UNUSED_PARAMETER"))
unused: Nothing?,
) : ApplicationCommandPermissionType(value) {
@Deprecated(
message = "Replaced by 'ApplicationCommandPermissionType.from()'.",
replaceWith = ReplaceWith(expression = "ApplicationCommandPermissionType.from(value)",
imports =
arrayOf("dev.kord.common.entity.ApplicationCommandPermissionType")),
)
public constructor(`value`: Int) : this(value, null)
}

public object Role : ApplicationCommandPermissionType(1)

Expand All @@ -58,12 +69,7 @@ public sealed class ApplicationCommandPermissionType(
}

override fun deserialize(decoder: Decoder): ApplicationCommandPermissionType =
when (val value = decoder.decodeInt()) {
1 -> Role
2 -> User
3 -> Channel
else -> Unknown(value)
}
from(decoder.decodeInt())
}

public companion object {
Expand All @@ -78,5 +84,16 @@ public sealed class ApplicationCommandPermissionType(
)
}


/**
* Returns an instance of [ApplicationCommandPermissionType] with
* [ApplicationCommandPermissionType.value] equal to the specified [value].
*/
public fun from(`value`: Int): ApplicationCommandPermissionType = when (value) {
1 -> Role
2 -> User
3 -> Channel
else -> Unknown(value, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,26 @@ public sealed class ApplicationCommandType(
final override fun hashCode(): Int = value.hashCode()

final override fun toString(): String =
"ApplicationCommandType.${this::class.simpleName}(value=$value)"
if (this is Unknown) "ApplicationCommandType.Unknown(value=$value)"
else "ApplicationCommandType.${this::class.simpleName}"

/**
* An unknown [ApplicationCommandType].
*
* This is used as a fallback for [ApplicationCommandType]s that haven't been added to Kord yet.
*/
public class Unknown(
public class Unknown internal constructor(
`value`: Int,
) : ApplicationCommandType(value)
@Suppress(names = arrayOf("UNUSED_PARAMETER"))
unused: Nothing?,
) : ApplicationCommandType(value) {
@Deprecated(
message = "Replaced by 'ApplicationCommandType.from()'.",
replaceWith = ReplaceWith(expression = "ApplicationCommandType.from(value)", imports =
arrayOf("dev.kord.common.entity.ApplicationCommandType")),
)
public constructor(`value`: Int) : this(value, null)
}

/**
* A text-based command that shows up when a user types `/`.
Expand All @@ -66,12 +76,7 @@ public sealed class ApplicationCommandType(
}

override fun deserialize(decoder: Decoder): ApplicationCommandType =
when (val value = decoder.decodeInt()) {
1 -> ChatInput
2 -> User
3 -> Message
else -> Unknown(value)
}
from(decoder.decodeInt())
}

public companion object {
Expand All @@ -86,5 +91,16 @@ public sealed class ApplicationCommandType(
)
}


/**
* Returns an instance of [ApplicationCommandType] with [ApplicationCommandType.value] equal
* to the specified [value].
*/
public fun from(`value`: Int): ApplicationCommandType = when (value) {
1 -> ChatInput
2 -> User
3 -> Message
else -> Unknown(value, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public sealed class ApplicationFlag(

final override fun hashCode(): Int = shift.hashCode()

final override fun toString(): String = if (this is Unknown)
"ApplicationFlag.Unknown(shift=$shift)" else "ApplicationFlag.${this::class.simpleName}"
final override fun toString(): String =
if (this is Unknown) "ApplicationFlag.Unknown(shift=$shift)"
else "ApplicationFlag.${this::class.simpleName}"

/**
* @suppress
Expand Down
Loading

0 comments on commit ffa8390

Please sign in to comment.