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

Deserialize time as Duration #586

Merged
merged 14 commits into from
Apr 16, 2022

Conversation

lukellmann
Copy link
Member

The Duration type is Kotlin's way to express time amounts.
However, it is not serializable by default and time is serialized in different units across Discord's API.

To make Duration usable in serializable classes, this PR introduces a serializer for every DurationUnit that (de-)serializes Durations as an integer number of the respective DurationUnit.

It also changes the types of rate_limt_per_user, max_age and afk_timeout to Duration, using the new serializers. ArchiveDuration now also uses Duration as the backing value.

@lukellmann
Copy link
Member Author

lukellmann commented Apr 3, 2022

In places where I had to annotate type parameters like

val rateLimitPerUser: Optional<@Serializable(with = DurationInSecondsSerializer::class) Duration> = Optional.Missing()

I've added import aliases to make it shorter and more readable

val rateLimitPerUser: Optional<@Serializable(InSeconds::class) Duration> = Optional.Missing()

Edit:
To make it even more readable, I've added some typealiases:

typealias DurationInSeconds = @Serializable(with = DurationInSecondsSerializer::class) Duration

val rateLimitPerUser: Optional<DurationInSeconds> = Optional.Missing()

@lukellmann
Copy link
Member Author

lukellmann commented Apr 3, 2022

I haven't changed the type to Duration in cases where time is expressed as an integer number of days yet.

I suggest to only do this in places where the time unit is not already part of the property name (like DiscordIntegration.expireGracePeriod) and don't change it where the unit is mentioned in the property name (like GuildBanCreateRequest.deleteMessageDays).

Edit:
I implemented my suggestion.

@HopeBaron HopeBaron merged commit d6f7eaf into kordlib:0.8.x Apr 16, 2022
@lukellmann lukellmann deleted the changes/serialize-durations branch April 17, 2022 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants