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

Align ArchiveDuration with generated kord enums #902

Merged
merged 1 commit into from
Dec 26, 2023
Merged
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 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