Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lightweight Snowflakes #717

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,066 changes: 536 additions & 530 deletions common/api/common.api

Large diffs are not rendered by default.

26 changes: 9 additions & 17 deletions common/src/main/kotlin/entity/Snowflake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@ import kotlin.time.TimeMark
* but [equals] only returns `true` if [compareTo] returns `0`.
*/
@Serializable(with = Snowflake.Serializer::class)
public class Snowflake : Comparable<Snowflake> {

@JvmInline
public value class Snowflake
/**
* Creates a Snowflake from a given ULong [value].
*
* Values are [coerced in][coerceIn] [validValues].
*/
constructor(
/**
* The raw value of this Snowflake as specified
* [here](https://discord.com/developers/docs/reference#snowflakes).
*/
public val value: ULong

/**
* Creates a Snowflake from a given ULong [value].
*
* Values are [coerced in][coerceIn] [validValues].
*/
public constructor(value: ULong) {
this.value = value.coerceIn(validValues)
}

) : Comparable<Snowflake> {
/**
* Creates a Snowflake from a given String [value], parsing it as a [ULong] value.
*
Expand Down Expand Up @@ -156,11 +153,6 @@ public class Snowflake : Comparable<Snowflake> {
*/
override fun toString(): String = value.toString()

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

override fun equals(other: Any?): Boolean = other is Snowflake && this.value == other.value


public companion object {
// see https://discord.com/developers/docs/reference#snowflakes-snowflake-id-format-structure-left-to-right

Expand Down
11 changes: 3 additions & 8 deletions common/src/main/kotlin/entity/optional/OptionalSnowflake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ public sealed class OptionalSnowflake {
* @param uLongValue the raw value this optional wraps.
* See [Snowflake.value] and [Snowflake.validValues] for more details.
*/
public class Value(private val uLongValue: ULong) : OptionalSnowflake() {

public constructor(value: Snowflake) : this(value.value)

override val value: Snowflake get() = Snowflake(uLongValue)

public class Value(override val value: Snowflake) : OptionalSnowflake() {
/**
* Destructures this optional to its [value].
*/
Expand All @@ -95,7 +90,7 @@ public sealed class OptionalSnowflake {
override val descriptor: SerialDescriptor = ULong.serializer().descriptor

override fun deserialize(decoder: Decoder): OptionalSnowflake =
Value(decoder.decodeInline(descriptor).decodeLong().toULong())
Value(Snowflake(decoder.decodeInline(descriptor).decodeLong().toULong()))

override fun serialize(encoder: Encoder, value: OptionalSnowflake) = when (value) {
Missing -> Unit // ignore value
Expand All @@ -113,7 +108,7 @@ public val OptionalSnowflake?.value: Snowflake?
OptionalSnowflake.Missing, null -> null
}

public fun Snowflake.optionalSnowflake(): OptionalSnowflake.Value = OptionalSnowflake.Value(this.value)
public fun Snowflake.optionalSnowflake(): OptionalSnowflake.Value = OptionalSnowflake.Value(Snowflake(this.value))

@JvmName("optionalNullable")
public fun Snowflake?.optionalSnowflake(): OptionalSnowflake.Value? = this?.optionalSnowflake()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public fun KMutableProperty0<OptionalSnowflake>.delegate(): ReadWriteProperty<An

override fun setValue(thisRef: Any?, property: KProperty<*>, value: Snowflake?) {
val optional = if (value == null) OptionalSnowflake.Missing
else OptionalSnowflake.Value(value.value)
else OptionalSnowflake.Value(Snowflake(value.value))
this@delegate.set(optional)
}
}
Expand Down
3,795 changes: 1,900 additions & 1,895 deletions core/api/core.api

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions core/src/test/kotlin/BoxedSnowflake.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dev.kord.common.entity.Snowflake

/**
* A class that wraps a Snowflake, because you can't use `lateinit` with value classes
*/
data class BoxedSnowflake(val value: Snowflake)
5 changes: 3 additions & 2 deletions core/src/test/kotlin/live/AbstractLiveEntityTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package live

import BoxedSnowflake
import dev.kord.cache.api.DataCache
import dev.kord.common.entity.Snowflake
import dev.kord.core.ClientResources
Expand Down Expand Up @@ -76,14 +77,14 @@ abstract class AbstractLiveEntityTest<LIVE : AbstractLiveKordEntity> {

protected lateinit var kord: Kord

protected lateinit var guildId: Snowflake
protected lateinit var guildId: BoxedSnowflake

lateinit var live: LIVE

@BeforeAll
open fun onBeforeAll() = runBlocking {
kord = createKord()
guildId = randomId()
guildId = BoxedSnowflake(randomId())
}

@AfterAll
Expand Down
Loading