Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Value/OwnedValue macro doesn't support renaming fields #330

Open
zeenix opened this issue Mar 1, 2023 · 2 comments
Open

Value/OwnedValue macro doesn't support renaming fields #330

zeenix opened this issue Mar 1, 2023 · 2 comments
Labels
4. newcomers Tasks that are good for new contributors bug Something isn't working zvariant_derive Issues/PRs related to zvariant_derive crate

Comments

@zeenix
Copy link
Collaborator

zeenix commented Mar 1, 2023

In GitLab by @wynprice999 on Mar 1, 2023, 22:01

It seems like the field names used for deserialization can't be renamed. I would expect using #[zvariant(rename = "...")] would rename the field, but it doesn't appear to have an effect:

Example expansion
#[derive(DeserializeDict, Value, OwnedValue)]
#[zvariant(signature = "dict")]
pub struct TrackMetadata {
    #[zvariant(rename = "otherfield")]
    pub field1: String,

    pub field2: String
}

Expands to the following:

#[zvariant(signature = "dict")]
pub struct TrackMetadata {
    #[zvariant(rename = "otherfield")]
    pub field1: String,
    pub field2: String,
}
...
impl ::std::convert::TryFrom<::zbus::zvariant::OwnedValue> for TrackMetadata {
    type Error = ::zbus::zvariant::Error;
    #[inline]
    fn try_from(
        value: ::zbus::zvariant::OwnedValue,
    ) -> ::zbus::zvariant::Result<Self> {
        let mut fields = <::std::collections::HashMap<
            ::std::string::String,
            ::zbus::zvariant::Value,
        >>::try_from(value)?;
        ::std::result::Result::Ok(Self {
            field1: fields
                .remove("field1")
                .ok_or_else(|| ::zbus::zvariant::Error::IncorrectType)?
                .downcast()
                .ok_or_else(|| ::zbus::zvariant::Error::IncorrectType)?,
            field2: fields
                .remove("field2")
                .ok_or_else(|| ::zbus::zvariant::Error::IncorrectType)?
                .downcast()
                .ok_or_else(|| ::zbus::zvariant::Error::IncorrectType)?,
        })
    }
}
impl From<TrackMetadata> for ::zbus::zvariant::OwnedValue {
    #[inline]
    fn from(s: TrackMetadata) -> Self {
        let mut fields = ::std::collections::HashMap::new();
        fields.insert("field1", ::zbus::zvariant::Value::from(s.field1));
        fields.insert("field2", ::zbus::zvariant::Value::from(s.field2));
        ::zbus::zvariant::Value::from(fields).into()
    }
}

From what I can tell, a solution would involve the following line: https://gitlab.freedesktop.org/dbus/zbus/-/blob/main/zvariant_derive/src/value.rs#L111

@zeenix
Copy link
Collaborator Author

zeenix commented Mar 2, 2023

@wynprice999 Thanks for reporting this. Yeah, renaming should work as you expect.

@zeenix
Copy link
Collaborator Author

zeenix commented Mar 2, 2023

@TurboCooler Hey, this seems right up your alley. ;)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
4. newcomers Tasks that are good for new contributors bug Something isn't working zvariant_derive Issues/PRs related to zvariant_derive crate
Projects
None yet
Development

No branches or pull requests

1 participant