From 546908b72e8fd82418ee2c671e9f9c1588ea8fe8 Mon Sep 17 00:00:00 2001 From: lukellmann Date: Tue, 26 Dec 2023 02:02:51 +0100 Subject: [PATCH] Align ArchiveDuration with generated kord enums The changes are the same as in #861, I simply forgot ArchiveDuration there. --- common/api/common.api | 1 + .../kotlin/entity/DiscordChannel.kt | 39 ++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/common/api/common.api b/common/api/common.api index e1f58056afa..67937468a2c 100644 --- a/common/api/common.api +++ b/common/api/common.api @@ -802,6 +802,7 @@ public abstract class dev/kord/common/entity/ArchiveDuration { } public final class dev/kord/common/entity/ArchiveDuration$Companion { + public final fun from-LRDsOJo (J)Ldev/kord/common/entity/ArchiveDuration; public final fun getEntries ()Ljava/util/List; public final fun serializer ()Lkotlinx/serialization/KSerializer; } diff --git a/common/src/commonMain/kotlin/entity/DiscordChannel.kt b/common/src/commonMain/kotlin/entity/DiscordChannel.kt index ca02af1cc4e..1ab958590d3 100644 --- a/common/src/commonMain/kotlin/entity/DiscordChannel.kt +++ b/common/src/commonMain/kotlin/entity/DiscordChannel.kt @@ -117,6 +117,8 @@ import kotlinx.datetime.Instant import kotlinx.serialization.KSerializer import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import kotlin.LazyThreadSafetyMode.PUBLICATION @@ -244,36 +246,53 @@ public sealed class ArchiveDuration( this === other || (other is ArchiveDuration && this.duration == other.duration) final override fun hashCode(): Int = duration.hashCode() - final override fun toString(): String = "ArchiveDuration.${this::class.simpleName}(duration=$duration)" + final override fun toString(): String = + if (this is Unknown) "ArchiveDuration.Unknown(duration=$duration)" + else "ArchiveDuration.${this::class.simpleName}" /** * An unknown [ArchiveDuration]. * * This is used as a fallback for [ArchiveDuration]s that haven't been added to Kord yet. */ - public class Unknown(duration: Duration) : ArchiveDuration(duration) + public class Unknown internal constructor( + duration: Duration, + @Suppress("UNUSED_PARAMETER") unused: Nothing?, + ) : ArchiveDuration(duration) { + @Deprecated( + "Replaced by 'ArchiveDuration.from()'.", + ReplaceWith("ArchiveDuration.from(duration)", imports = ["dev.kord.common.entity.ArchiveDuration"]), + DeprecationLevel.WARNING, + ) + public constructor(duration: Duration) : this(duration, unused = null) + } + public object Hour : ArchiveDuration(60.minutes) public object Day : ArchiveDuration(1440.minutes) public object ThreeDays : ArchiveDuration(4320.minutes) public object Week : ArchiveDuration(10080.minutes) internal object Serializer : KSerializer { - override val descriptor get() = DurationInMinutesSerializer.descriptor + override val descriptor = + PrimitiveSerialDescriptor("dev.kord.common.entity.ArchiveDuration", PrimitiveKind.LONG) override fun serialize(encoder: Encoder, value: ArchiveDuration) = encoder.encodeSerializableValue(DurationInMinutesSerializer, value.duration) - override fun deserialize(decoder: Decoder): ArchiveDuration { - val duration = decoder.decodeSerializableValue(DurationInMinutesSerializer) - return entries.firstOrNull { it.duration == duration } ?: Unknown(duration) - } + override fun deserialize(decoder: Decoder) = from(decoder.decodeSerializableValue(DurationInMinutesSerializer)) } public companion object { /** A [List] of all known [ArchiveDuration]s. */ - public val entries: List by lazy(mode = PUBLICATION) { - listOf(Hour, Day, ThreeDays, Week) - } + public val entries: List by lazy(mode = PUBLICATION) { listOf(Hour, Day, ThreeDays, Week) } + + private val entriesByDuration by lazy(mode = PUBLICATION) { entries.associateBy(ArchiveDuration::duration) } + + /** + * Returns an instance of [ArchiveDuration] with [ArchiveDuration.duration] equal to the specified [duration]. + */ + public fun from(duration: Duration): ArchiveDuration = + entriesByDuration[duration] ?: Unknown(duration, unused = null) } }