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

Using serde_with::rust::sets_last_value_wins with serde_as notation #534

Closed
seowalex opened this issue Dec 15, 2022 · 5 comments · Fixed by #563
Closed

Using serde_with::rust::sets_last_value_wins with serde_as notation #534

seowalex opened this issue Dec 15, 2022 · 5 comments · Fixed by #563
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@seowalex
Copy link

I would like to use serde_with::rust::sets_last_value_wins with the serde_as notation, but that does not seem to be possible. Am I missing something?

@jonasbb jonasbb added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Dec 16, 2022
@jonasbb
Copy link
Owner

jonasbb commented Dec 16, 2022

Hi, thanks for raising the issue. That is indeed currently not possible, as this module and the related ones were never updated. I don't think there is any technical challenge here, just a bit of busy work.
This part of the guide contains the relevant part for converting. https://docs.rs/serde_with/latest/serde_with/guide/serde_as/index.html#using-serde_as-on-types-without-serializeas-and-serialize-implementations
Additionally, the documentation would need to be updated too.

If you need this urgently, you can also create a conversion type in your codebase. You can follow the guide and replace MODULE with serde_with::rust::sets_last_value_wins.

@seowalex
Copy link
Author

I am interested in implementing this, but what would the syntax look like in the serde_as notation?

@jonasbb
Copy link
Owner

jonasbb commented Dec 16, 2022

You need a new type and implement the traits for it (see link).

pub struct SetLastValueWins<V>(PhantomData<V>);

You should then be able to use it like any other serde_as attribute:

#[serde_as(as = "SetLastValueWins<_>")]
BTreeSet<u32>,

@seowalex
Copy link
Author

seowalex commented Dec 16, 2022

pub(crate) struct SetLastValueWins<T>(PhantomData<T>);

impl<T, U> SerializeAs<IndexSet<T>> for SetLastValueWins<U>
where
    T: Eq + Hash + Serialize,
{
    fn serialize_as<S>(value: &IndexSet<T>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        sets_last_value_wins::serialize(value, serializer)
    }
}

impl<'de, T, U> DeserializeAs<'de, IndexSet<T>> for SetLastValueWins<U>
where
    T: Eq + Hash + Deserialize<'de>,
{
    fn deserialize_as<D>(deserializer: D) -> Result<IndexSet<T>, D::Error>
    where
        D: Deserializer<'de>,
    {
        sets_last_value_wins::deserialize(deserializer)
    }
}

With my implementation above, it seems like it does not use the serde_conv defined adapters. For example, when I try to use the above code with #[serde_as(as = "Option<SetLastValueWins<CustomSerdeConv>>")], I get an error complaining about an invalid type as I am trying to deserialise a string into a struct.

@jonasbb
Copy link
Owner

jonasbb commented Dec 16, 2022

In this case, you will need to adapt the existing code a bit. The values need to be transformed, since you cannot directly serialize a SerializeAs. You might want to look at the documentation of the traits: https://docs.rs/serde_with/2.1.0/serde_with/trait.SerializeAs.html#differences-to-serialize especially how the SerializeAsWrap is working.

bors bot added a commit that referenced this issue Mar 4, 2023
563: Add serde_as compatible versions for duplicate key handling r=jonasbb a=jonasbb

This adds `serde_as` compatible versions for these four modules:
`maps_duplicate_key_is_error`, `maps_first_key_wins`,
`sets_duplicate_value_is_error`, and `sets_last_value_wins`.

Missing:
- [x] Documentation

Closes #534

Co-authored-by: Jonas Bushart <[email protected]>
@bors bors bot closed this as completed in 8a0b890 Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants