Skip to content

Commit

Permalink
Align ArchiveDuration with generated kord enums
Browse files Browse the repository at this point in the history
The changes are the same as in #861, I simply forgot ArchiveDuration
there.
  • Loading branch information
lukellmann committed Dec 26, 2023
1 parent 65101a4 commit 546908b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions common/api/common.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
39 changes: 29 additions & 10 deletions common/src/commonMain/kotlin/entity/DiscordChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ArchiveDuration> {
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<ArchiveDuration> by lazy(mode = PUBLICATION) {
listOf(Hour, Day, ThreeDays, Week)
}
public val entries: List<ArchiveDuration> 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)
}
}

Expand Down

0 comments on commit 546908b

Please sign in to comment.