Skip to content

Commit

Permalink
Make Snowflake.toString() more idiomatic, and deprecate .asString (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWoodworth committed Nov 25, 2021
1 parent 4f30665 commit a2912ab
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 85 deletions.
2 changes: 1 addition & 1 deletion common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class MessageFlags internal constructor(val code: Int) {
return builder.flags()
}

override fun toString(): String = "MessageFlags(flags=${flags.toString()})"
override fun toString(): String = "MessageFlags(flags=$flags)"

internal object Serializer : KSerializer<MessageFlags> {

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/kotlin/entity/Presence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data class DiscordPresenceUser(
override fun serialize(encoder: Encoder, value: DiscordPresenceUser) {
val jsonEncoder = encoder as? JsonEncoder ?: error("Can be serialized only by JSON")
val details = value.details.toMutableMap()
details["id"] = JsonPrimitive(value.id.asString)
details["id"] = JsonPrimitive(value.id.toString())

jsonEncoder.encodeJsonElement(JsonObject(details))
}
Expand Down Expand Up @@ -93,4 +93,4 @@ sealed class PresenceStatus(val value: String) {
encoder.encodeString(value.value)
}
}
}
}
6 changes: 5 additions & 1 deletion common/src/main/kotlin/entity/Snowflake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Snowflake : Comparable<Snowflake> {
/**
* A [String] representation of this Snowflake's [value].
*/
@Deprecated("Use toString() instead", ReplaceWith("toString()"))
val asString get() = value.toString()

/**
Expand All @@ -91,7 +92,10 @@ class Snowflake : Comparable<Snowflake> {
override fun compareTo(other: Snowflake): Int =
millisecondsSinceDiscordEpoch.compareTo(other.millisecondsSinceDiscordEpoch)

override fun toString(): String = "Snowflake(value=$value)"
/**
* A [String] representation of this Snowflake's [value].
*/
override fun toString(): String = value.toString()

override fun hashCode(): Int = value.hashCode()

Expand Down
48 changes: 24 additions & 24 deletions common/src/test/kotlin/json/ChannelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class ChannelTest {
val channel = Json.decodeFromString(DiscordChannel.serializer(), file("dmchannel"))

with(channel) {
lastMessageId.value!!.asString shouldBe "3343820033257021450"
lastMessageId.value!!.toString() shouldBe "3343820033257021450"
type.value shouldBe 1
id.asString shouldBe "319674150115610528"
id.toString() shouldBe "319674150115610528"
recipients.value!!.size shouldBe 1
with(recipients.value!!.first()) {
username shouldBe "test"
discriminator shouldBe "9999"
id.asString shouldBe "82198898841029460"
id.toString() shouldBe "82198898841029460"
avatar shouldBe "33ecab261d4681afa4d85a04691c4a01"
}
}
Expand All @@ -43,9 +43,9 @@ class ChannelTest {
name.value shouldBe "Test"
nsfw.asNullable shouldBe false
position.value shouldBe 0
guildId.value?.asString shouldBe "290926798629997250"
guildId.value?.toString() shouldBe "290926798629997250"
type.value shouldBe 4
id.asString shouldBe "399942396007890945"
id.toString() shouldBe "399942396007890945"
}
}

Expand All @@ -61,19 +61,19 @@ class ChannelTest {
with(recipients.value!!.first()) {
username shouldBe "test"
discriminator shouldBe "9999"
id.asString shouldBe "82198898841029460"
id.toString() shouldBe "82198898841029460"
avatar shouldBe "33ecab261d4681afa4d85a04691c4a01"
}
with(recipients.value!![1]) {
username shouldBe "test2"
discriminator shouldBe "9999"
id.asString shouldBe "82198810841029460"
id.toString() shouldBe "82198810841029460"
avatar shouldBe "33ecab261d4681afa4d85a10691c4a01"
}
lastMessageId.value?.asString shouldBe "3343820033257021450"
lastMessageId.value?.toString() shouldBe "3343820033257021450"
type.value shouldBe 3
id.asString shouldBe "319674150115710528"
ownerId.value?.asString shouldBe "82198810841029460"
id.toString() shouldBe "319674150115710528"
ownerId.value?.toString() shouldBe "82198810841029460"
}
}

Expand All @@ -83,16 +83,16 @@ class ChannelTest {
val channel = Json.decodeFromString(DiscordChannel.serializer(), file("guildnewschannel"))

with(channel) {
id.asString shouldBe "41771983423143937"
guildId.value!!.asString shouldBe "41771983423143937"
id.toString() shouldBe "41771983423143937"
guildId.value!!.toString() shouldBe "41771983423143937"
name.value shouldBe "important-news"
type.value shouldBe 5
position.value shouldBe 6
permissionOverwrites.value!! shouldBe emptyList()
nsfw.value shouldBe true
topic.value shouldBe "Rumors about Half Life 3"
lastMessageId.value?.asString shouldBe "155117677105512449"
parentId.value?.asString shouldBe "399942396007890945"
lastMessageId.value?.toString() shouldBe "155117677105512449"
parentId.value?.toString() shouldBe "399942396007890945"
}
}

Expand All @@ -102,17 +102,17 @@ class ChannelTest {
val channel = Json.decodeFromString(DiscordChannel.serializer(), file("guildtextchannel"))

with(channel) {
id.asString shouldBe "41771983423143937"
guildId.value!!.asString shouldBe "41771983423143937"
id.toString() shouldBe "41771983423143937"
guildId.value!!.toString() shouldBe "41771983423143937"
name.value shouldBe "general"
type.value shouldBe 0
position.asNullable!! shouldBe 6
permissionOverwrites.value shouldBe emptyList()
rateLimitPerUser.asNullable shouldBe 2
nsfw.value shouldBe true
topic.value shouldBe "24/7 chat about how to gank Mike #2"
lastMessageId.value?.asString shouldBe "155117677105512449"
parentId.value?.asString shouldBe "399942396007890945"
lastMessageId.value?.toString() shouldBe "155117677105512449"
parentId.value?.toString() shouldBe "399942396007890945"
}
}

Expand All @@ -122,16 +122,16 @@ class ChannelTest {
val channel = Json.decodeFromString(DiscordChannel.serializer(), file("guildvoicechannel"))

with(channel) {
id.asString shouldBe "155101607195836416"
guildId.value!!.asString shouldBe "41771983423143937"
id.toString() shouldBe "155101607195836416"
guildId.value!!.toString() shouldBe "41771983423143937"
name.value shouldBe "ROCKET CHEESE"
type.value shouldBe 2
nsfw.asNullable shouldBe false
position.asNullable shouldBe 5
permissionOverwrites.value!! shouldBe emptyList()
bitrate.value shouldBe 64000
userLimit.value shouldBe 0
parentId.value?.asString shouldBe null
parentId.value?.toString() shouldBe null
}
}

Expand All @@ -141,8 +141,8 @@ class ChannelTest {
val channel = Json.decodeFromString(DiscordChannel.serializer(), file("storechannel"))

with(channel) {
id.asString shouldBe "41771983423143937"
guildId.value!!.asString shouldBe "41771983423143937"
id.toString() shouldBe "41771983423143937"
guildId.value!!.toString() shouldBe "41771983423143937"
name.value shouldBe "buy dota-2"
type.value shouldBe 6
position.asNullable shouldBe 0
Expand All @@ -151,4 +151,4 @@ class ChannelTest {
parentId shouldBe null
}
}
}
}
2 changes: 1 addition & 1 deletion common/src/test/kotlin/json/EmojiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EmojiTest {
val emoji = Json.decodeFromString(DiscordEmoji.serializer(), file("customemoji"))

with(emoji) {
id!!.asString shouldBe "41771983429993937"
id!!.toString() shouldBe "41771983429993937"
name shouldBe "LUL"
}

Expand Down
14 changes: 7 additions & 7 deletions common/src/test/kotlin/json/MessageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MessageTest {
embeds shouldBe emptyList()
timestamp shouldBe "2017-07-11T17:27:07.299000+00:00"
mentionEveryone shouldBe false
id.asString shouldBe "334385199974967042"
id.toString() shouldBe "334385199974967042"
pinned shouldBe false
editedTimestamp shouldBe null
with(author) {
Expand Down Expand Up @@ -71,25 +71,25 @@ fun `User serialization`() {
embeds shouldBe emptyList()
timestamp shouldBe "2017-07-11T17:27:07.299000+00:00"
mentionEveryone shouldBe false
id.asString shouldBe "334385199974967042"
id.toString() shouldBe "334385199974967042"
pinned shouldBe false
editedTimestamp shouldBe null
with(author) {
username shouldBe "Mason"
discriminator shouldBe "9999"
id.asString shouldBe "53908099506183680"
id.toString() shouldBe "53908099506183680"
avatar shouldBe "a_bab14f271d565501444b2ca3be944b25"
}
mentionRoles shouldBe emptyList()
content shouldBe "Big news! In this <#278325129692446722> channel!"
channelId.asString shouldBe "290926798999357250"
channelId.toString() shouldBe "290926798999357250"
mentions shouldBe emptyList()
type.code shouldBe 0
flags shouldBe MessageFlags(MessageFlag.IsCrossPost.code)
with(messageReference.value!!) {
channelId.value?.asString shouldBe "278325129692446722"
guildId.value!!.asString shouldBe "278325129692446720"
id.value!!.asString shouldBe "306588351130107906"
channelId.value?.toString() shouldBe "278325129692446722"
guildId.value!!.toString() shouldBe "278325129692446720"
id.value!!.toString() shouldBe "306588351130107906"
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/src/test/kotlin/json/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserTest {
val user = Json.decodeFromString(DiscordUser.serializer(), file("user"))

with(user) {
id.asString shouldBe "80351110224678912"
id.toString() shouldBe "80351110224678912"
username shouldBe "Nelly"
discriminator shouldBe "1337"
avatar shouldBe "8342729096ea3675442027381ff50dfe"
Expand Down
4 changes: 2 additions & 2 deletions common/src/test/kotlin/json/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ infix fun OptionalBoolean.shouldBe(value: Boolean){
}

infix fun Snowflake?.shouldBe(value: String?){
Assertions.assertEquals(value, this?.asString)
Assertions.assertEquals(value, this?.toString())
}

infix fun OptionalSnowflake?.shouldBe(value: String?){
Assertions.assertEquals(value, this?.value?.asString)
Assertions.assertEquals(value, this?.value?.toString())
}

infix fun VerificationLevel?.shouldBe(value: VerificationLevel?){
Expand Down
4 changes: 2 additions & 2 deletions common/src/test/kotlin/json/VoiceStateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class VoiceStateTest {
val state = Json.decodeFromString(DiscordVoiceState.serializer(), file("voicestate"))

with(state) {
channelId!!.asString shouldBe "157733188964188161"
userId.asString shouldBe "80351110224678912"
channelId!!.toString() shouldBe "157733188964188161"
userId.toString() shouldBe "80351110224678912"
sessionId shouldBe "90326bd25d71d39b9ef95b299e3872ff"
deaf shouldBe false
mute shouldBe false
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/behavior/MemberBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface MemberBehavior : KordEntity, UserBehavior {
/**
* The raw mention for this member's nickname.
*/
public val nicknameMention: String get() = "<@!${id.asString}>"
public val nicknameMention: String get() = "<@!$id>"

/**
* Requests to get the this behavior as a [Member].
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/behavior/RoleBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface RoleBehavior : KordEntity, Strategizable {
public val mention: String
get() {
return if (guildId == id) "@everyone"
else "<@&${id.asString}>"
else "<@&$id>"
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/behavior/UserBehavior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.util.*
*/
public interface UserBehavior : KordEntity, Strategizable {

public val mention: String get() = "<@${id.asString}>"
public val mention: String get() = "<@$id>"

/**
* Requests to get the this behavior as a [Member] in the [Guild] with the [guildId].
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/kotlin/entity/Guild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public class Guild(
* Gets the banner url in the specified format.
*/
public fun getBannerUrl(format: Image.Format): String? =
data.banner?.let { "https://cdn.discordapp.com/banners/${id.asString}/$it.${format.extension}" }
data.banner?.let { "https://cdn.discordapp.com/banners/$id/$it.${format.extension}" }

/**
* Requests to get the banner image in the specified [format], if present.
Expand Down Expand Up @@ -421,7 +421,7 @@ public class Guild(
* Gets the discovery splash url in the specified [format], if present.
*/
public fun getDiscoverySplashUrl(format: Image.Format): String? =
data.splash.value?.let { "discovery-splashes/${id.asString}/${it}.${format.extension}" }
data.splash.value?.let { "discovery-splashes/$id/${it}.${format.extension}" }

/**
* Requests to get the splash image in the specified [format], if present.
Expand All @@ -438,7 +438,7 @@ public class Guild(
* Gets the icon url, if present.
*/
public fun getIconUrl(format: Image.Format): String? =
data.icon?.let { "https://cdn.discordapp.com/icons/${id.asString}/$it.${format.extension}" }
data.icon?.let { "https://cdn.discordapp.com/icons/$id/$it.${format.extension}" }

/**
* Requests to get the icon image in the specified [format], if present.
Expand Down Expand Up @@ -482,7 +482,7 @@ public class Guild(
* Gets the splash url in the specified [format], if present.
*/
public fun getSplashUrl(format: Image.Format): String? =
data.splash.value?.let { "https://cdn.discordapp.com/splashes/${id.asString}/$it.${format.extension}" }
data.splash.value?.let { "https://cdn.discordapp.com/splashes/$id/$it.${format.extension}" }

/**
* Requests to get the splash image in the specified [format], if present.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/entity/GuildEmoji.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class GuildEmoji(
get() = data.guildId

public val mention: String
get() = if (isAnimated) "<a:$name:${id.asString}>" else "<:$name:${id.asString}>"
get() = if (isAnimated) "<a:$name:$id>" else "<:$name:$id>"

/**
* Whether this emoji can be used, may be false due to loss of Server Boosts.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/entity/PartialGuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class PartialGuild(
* Gets the icon url, if present.
*/
public fun getIconUrl(format: Image.Format): String? =
data.icon?.let { "https://cdn.discordapp.com/icons/${id.asString}/$it.${format.extension}" }
data.icon?.let { "https://cdn.discordapp.com/icons/$id/$it.${format.extension}" }

/**
* Requests to get the icon image in the specified [format], if present.
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/entity/ReactionEmoji.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public sealed class ReactionEmoji {
public data class Custom(val id: Snowflake, override val name: String, val isAnimated: Boolean) : ReactionEmoji() {

override val urlFormat: String
get() = "$name:${id.asString}"
get() = "$name:$id"

override val mention: String
get() = if (isAnimated) "<a:$name:${id.asString}>" else "<:$name:${id.asString}>"
get() = if (isAnimated) "<a:$name:$id>" else "<:$name:$id>"


override fun toString(): String = "Custom(id=$id, name=$name, isAnimated=$isAnimated)"
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/entity/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public open class User(
public val accentColor: Color? get() = data.accentColor?.let { Color(it) }

public fun getBannerUrl(format: Image.Format): String? =
data.banner?.let { "https://cdn.discordapp.com/banners/${id.asString}/$it.${format.extension}" }
data.banner?.let { "https://cdn.discordapp.com/banners/$id/$it.${format.extension}" }


override fun hashCode(): Int = id.hashCode()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/entity/channel/TopGuildChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface TopGuildChannel : GuildChannel, TopGuildChannelBehavior {
public suspend fun getEffectivePermissions(memberId: Snowflake): Permissions {
val member = supplier.getMemberOrNull(guildId, memberId)
require(member != null) {
"member ${memberId.asString} is not in guild ${guildId.asString}"
"member $memberId is not in guild $guildId"
}

val base = member.getPermissions()
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/kotlin/exception/EntityNotFoundException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class EntityNotFoundException : Exception {

@PublishedApi
internal inline fun entityNotFound(entityType: String, id: Snowflake): Nothing =
throw EntityNotFoundException("$entityType with id ${id.asString} was not found.")
throw EntityNotFoundException("$entityType with id $id was not found.")

@PublishedApi
internal inline fun guildEntityNotFound(entityType: String, guildId: Snowflake, id: Snowflake): Nothing =
throw EntityNotFoundException("$entityType with id ${id.asString} in guild ${guildId.asString} was not found.")
throw EntityNotFoundException("$entityType with id $id in guild $guildId was not found.")


public inline fun guildNotFound(guildId: Snowflake): Nothing =
Expand All @@ -32,7 +32,7 @@ public class EntityNotFoundException : Exception {
guildEntityNotFound("Member", guildId = guildId, id = userId)

public inline fun messageNotFound(channelId: Snowflake, messageId: Snowflake): Nothing =
throw EntityNotFoundException("Message with id ${messageId.asString} in channel ${channelId.asString} was not found")
throw EntityNotFoundException("Message with id $messageId in channel $channelId was not found")

public inline fun userNotFound(userId: Snowflake): Nothing =
entityNotFound("User", userId)
Expand Down
Loading

0 comments on commit a2912ab

Please sign in to comment.