zbus-old icon indicating copy to clipboard operation
zbus-old copied to clipboard

Supprt skip_serializing_if attribute

Open zeenix opened this issue 5 years ago • 7 comments
trafficstars

In GitLab by @bilelmoussaoui on Sep 12, 2020, 22:40

Currently, having a TypeDict with Vec<T> means that the key associated to the Vec<T> will be serialized with an empty array as a value.

skip_serializing_if attribute would solve such use case as you can have

#[derive(SerializeDict, DeserializeDict, TypeDict, Debug)]
/// A notification
pub struct Notification {
    /// User-visible string to display as the title.
    pub title: String,
    /// Array of buttons to add to the notification.
    #[zvariant(skip_serializing_if = "Vec::is_empty")]
    pub buttons: Vec<Button>,
}

I have looked a bit how to implement this, would that be an interesting feature to have?

zeenix avatar Sep 12 '20 20:09 zeenix

I have looked a bit how to implement this, would that be an interesting feature to have?

Maybe but what's the use case?

zeenix avatar Sep 12 '20 21:09 zeenix

In GitLab by @bilelmoussaoui on Sep 12, 2020, 23:10

Well, the dbus interface complains about having a list of buttons that's empty. Not serializing if the Vec is empty makes providing a builder pattern around the Struct much easier instead of something like

    pub fn button(mut self, button: Button) -> Self {
        match self.buttons {
            Some(ref mut buttons) => buttons.push(button),
            None => {
                self.buttons.replace(vec![button]);
            }
        };
        self
    }

zeenix avatar Sep 12 '20 21:09 zeenix

In GitLab by @bilelmoussaoui on Sep 12, 2020, 23:16

I wonder if this issue & this one https://gitlab.freedesktop.org/zeenix/zbus/-/issues/32 are similar/duplicates

zeenix avatar Sep 12 '20 21:09 zeenix

Well, the dbus interface complains about having a list of buttons that's empty

Sounds like a bug in the dbus service. Having said that, if we can support this w/o complicating the code, I won't be against it.

zeenix avatar Sep 12 '20 21:09 zeenix

I wonder if this issue & this one https://gitlab.freedesktop.org/zeenix/zbus/-/issues/32 are similar/duplicates

That's similar but not exactly duplicate. That one is more about just skipping the field entirely and would typically go together with serde's skip attribute (if you don't want to serialize a field, you most likely also don't want to include it in the type signature string).

zeenix avatar Sep 12 '20 21:09 zeenix

In GitLab by @bilelmoussaoui on Sep 12, 2020, 23:37

I will wait till the skip attribute is implemented, I don't think adding an extra skip_serializing_if would complicate the code. I have looked a bit at how serde implements it. I have no idea how all the syn stuff works though.

zeenix avatar Sep 12 '20 21:09 zeenix

I don't think adding an extra skip_serializing_if would complicate the code.

Good. Just for the record, I wasn't implying it necessarily would. I was just clarifying my requirement for merging this feature. :)

zeenix avatar Sep 14 '20 11:09 zeenix