-
Notifications
You must be signed in to change notification settings - Fork 575
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
Make icon and unicode_emoji nullable in addition to optional #2539
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arqunis
added
fix
A solution to an existing bug.
breaking change
The public API is changed, resulting in miscompilations or unexpected new behaviour for users
enhancement
An improvement to Serenity.
and removed
fix
A solution to an existing bug.
labels
Sep 18, 2023
arqunis
approved these changes
Sep 18, 2023
Thanks! |
mkrasnitski
pushed a commit
to mkrasnitski/serenity
that referenced
this pull request
Sep 21, 2023
…ion to undefined (serenity-rs#2539) All `EditRole` fields are skipped when they are `None`. This does not work for `EditRole::icon` and `EditRole::unicode_emoji` because the field being `undefined` is different from the field being `null` at the API: if the field is `undefined` it will be kept unchanged, and if it is `null` it will be cleared. With a double `Option` it is possible to distinguish `undefined` from `null` from a set value. This is the [canonical](serde-rs/serde#984 (comment)) solution using serde (the example shows de-serialization but it works the same in reverse). Then, `None` is "keep field unchanged", `Some(None)` is "clear this field" and `Some(Some(value)` is "set this field to this value" # Breaking changes - `EditRole::unicode_emoji()` takes a `Option<String>` instead of `impl Into<String>`. - `EditRole::icon()` takes a `Option<&CreateAttachment>` instead of `&CreateAttachment`
mkrasnitski
pushed a commit
to mkrasnitski/serenity
that referenced
this pull request
Oct 17, 2023
…ion to undefined (serenity-rs#2539) All `EditRole` fields are skipped when they are `None`. This does not work for `EditRole::icon` and `EditRole::unicode_emoji` because the field being `undefined` is different from the field being `null` at the API: if the field is `undefined` it will be kept unchanged, and if it is `null` it will be cleared. With a double `Option` it is possible to distinguish `undefined` from `null` from a set value. This is the [canonical](serde-rs/serde#984 (comment)) solution using serde (the example shows de-serialization but it works the same in reverse). Then, `None` is "keep field unchanged", `Some(None)` is "clear this field" and `Some(Some(value)` is "set this field to this value" # Breaking changes - `EditRole::unicode_emoji()` takes a `Option<String>` instead of `impl Into<String>`. - `EditRole::icon()` takes a `Option<&CreateAttachment>` instead of `&CreateAttachment`
mkrasnitski
pushed a commit
to mkrasnitski/serenity
that referenced
this pull request
Oct 24, 2023
…ion to undefined (serenity-rs#2539) All `EditRole` fields are skipped when they are `None`. This does not work for `EditRole::icon` and `EditRole::unicode_emoji` because the field being `undefined` is different from the field being `null` at the API: if the field is `undefined` it will be kept unchanged, and if it is `null` it will be cleared. With a double `Option` it is possible to distinguish `undefined` from `null` from a set value. This is the [canonical](serde-rs/serde#984 (comment)) solution using serde (the example shows de-serialization but it works the same in reverse). Then, `None` is "keep field unchanged", `Some(None)` is "clear this field" and `Some(Some(value)` is "set this field to this value" # Breaking changes - `EditRole::unicode_emoji()` takes a `Option<String>` instead of `impl Into<String>`. - `EditRole::icon()` takes a `Option<&CreateAttachment>` instead of `&CreateAttachment`
arqunis
pushed a commit
to arqunis/serenity
that referenced
this pull request
Oct 24, 2023
…ion to undefined (serenity-rs#2539) All `EditRole` fields are skipped when they are `None`. This does not work for `EditRole::icon` and `EditRole::unicode_emoji` because the field being `undefined` is different from the field being `null` at the API: if the field is `undefined` it will be kept unchanged, and if it is `null` it will be cleared. With a double `Option` it is possible to distinguish `undefined` from `null` from a set value. This is the [canonical](serde-rs/serde#984 (comment)) solution using serde (the example shows de-serialization but it works the same in reverse). Then, `None` is "keep field unchanged", `Some(None)` is "clear this field" and `Some(Some(value)` is "set this field to this value" # Breaking changes - `EditRole::unicode_emoji()` takes a `Option<String>` instead of `impl Into<String>`. - `EditRole::icon()` takes a `Option<&CreateAttachment>` instead of `&CreateAttachment`
arqunis
pushed a commit
to arqunis/serenity
that referenced
this pull request
Oct 24, 2023
…ion to undefined (serenity-rs#2539) All `EditRole` fields are skipped when they are `None`. This does not work for `EditRole::icon` and `EditRole::unicode_emoji` because the field being `undefined` is different from the field being `null` at the API: if the field is `undefined` it will be kept unchanged, and if it is `null` it will be cleared. With a double `Option` it is possible to distinguish `undefined` from `null` from a set value. This is the [canonical](serde-rs/serde#984 (comment)) solution using serde (the example shows de-serialization but it works the same in reverse). Then, `None` is "keep field unchanged", `Some(None)` is "clear this field" and `Some(Some(value)` is "set this field to this value" # Breaking changes - `EditRole::unicode_emoji()` takes a `Option<String>` instead of `impl Into<String>`. - `EditRole::icon()` takes a `Option<&CreateAttachment>` instead of `&CreateAttachment`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
breaking change
The public API is changed, resulting in miscompilations or unexpected new behaviour for users
builder
Related to the `builder` module.
enhancement
An improvement to Serenity.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With this PR it is now possible to properly clear a role's icon/unicode_emoji.
The problem
In both
serenity/current
andserenity/next
allEditRole
fields are skipped when they areNone
. This does not work forEditRole::icon
andEditRole::unicode_emoji
because the field beingundefined
is different from the field beingnull
at the API1: if the field isundefined
it will be kept unchanged, and if it isnull
it will be cleared.The existing code then doesn't actually clear the icon/unicode_emoji when setting the other, but simply keeps it unchanged.
The solution
With a double Option it is possible to distinguish
undefined
fromnull
from a set value. This is the canonical solution using serde (the example shows de-serialization but it works the same in reverse).Then,
None
is "keep field unchanged",Some(None)
is "clear this field" andSome(Some(value)
is "set this field to this value"API breaks
EditRole::unicode_emoji()
takes aOption<String>
instead ofimpl Into<String>
.EditRole::icon()
takes aOption<&CreateAttachment>
instead of&CreateAttachment
Verified
EditRole::icon(Some(...))
sets the icon and clears the Unicode emoji.EditRole::icon(None)
clears both icon and Unicode emoji.EditRole::unicode_emoji(Some(...))
sets the Unicode emoji and clears the icon.EditRole::unicode_emoji(None)
clears both Unicode emoji and icon.cargo +nightly fmt --all
andcargo test --all
pass.Other stuff
For more control it would be possible now to remove the automatic clearing of the unicode_emoji/icon in
icon()
/unicode_emoji()
, but I didn't see any real use-cases for this.Footnotes
"All parameters to this endpoint are optional and nullable." - https://discord.com/developers/docs/resources/guild#modify-guild-role ↩