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

zvariant::Dict please allow iterating the key/value pairs

Open zeenix opened this issue 3 years ago • 3 comments
trafficstars

In GitLab by @estokes on Oct 12, 2022, 22:46

Hi, at the moment there is no way to just iterate the key/value pairs of a zvariant::Dict. If you want to do it you need to convert it to a hashmap, and to do that you need to read the signature and pick a concrete type that implements Basic, it's actually quite a lot of code to do this, and what I (and probably others) really want is just to iterate the values. I see that it's stored as a Vec of DictEntries, which are just the key and value, so it would be trivial to allow this iteration.

Thanks, zbus is awesome!

zeenix avatar Oct 12 '22 20:10 zeenix

Hi there,

at the moment there is no way to just iterate the key/value pairs of a zvariant::Dict. If you want to do it you need to convert it to a hashmap, and to do that you need to read the signature and pick a concrete type that implements Basic, it's actually quite a lot of code to do this,

So Dict isn't really meant to be used directly. Typically you want to:

  1. use a HashMap itself. Type is implemented for that.
  2. if you need to (de)serialize it as a Value:
  3. use your own struct by making use of SerializeDict and DeserializeDict derives.
  4. Convert from/to HashMap to/from Value or OwnedValue types.

Having said that, I don't know your exact use case and I have no objections on providing the API you're requesting.

I'm having trouble finding a lot of time these days for zbus work so it'll take a while, unless you could contribute this.

zeenix avatar Oct 13 '22 08:10 zeenix

In GitLab by @estokes on Oct 14, 2022, 24:44

I'm bridging dbus values to another quite similar dynamically typed network protocol (netidx), so I really need to just iterate and translate the values directly (e.g. I have a recursive fn dbus_value_to_netidx_value(v: &zvariant::Value) -> Value { ... }, all of the cases are quite straightforward except zvariant::Dict).

I would be happy to work on this.

zeenix avatar Oct 13 '22 22:10 zeenix

I'm bridging dbus values to another quite similar dynamically typed network protocol (netidx), so I really need to just iterate and translate the values directly (e.g. I have a recursive fn dbus_value_to_netidx_value(v: &zvariant::Value) -> Value { ... }, all of the cases are quite straightforward except zvariant::Dict).

I see. I just wanted to make sure you're not making your life harder than it needs to be. :smile:

I would be happy to work on this.

:thumbsup: I would prefer not to expose DictEntry in pub api btw and have a generic iterator so people can iterate over specific types.

zeenix avatar Oct 14 '22 10:10 zeenix