GNOME Bugzilla – Bug 786737
No g_variant_get() example for dicts
Last modified: 2017-09-11 08:47:02 UTC
The “GVariant Format Strings” section[0] of the documentation is generally a very useful reference for how to construct and deconstruct GVariants of different types. But the section on dictionaries[1] only shows how to construct a dictionary – it does not show how to extract a dictionary, and extract entries from it. For a motivating example, suppose you are handling org.freedesktop.DBus.ObjectManager.InterfacesAdded and for some reason are not using generated code. You get a GVariant of type "(oa{sa{sv})" and you want to do the equivalent of v[1]["com.example.Foo"]["Bar"] to pluck a property out of the dictionary. How do you do it? I believe the answer is something like: GVariant *a; GVariant *c; GVariantDict d; guint32 e; g_variant_get (v, "(oa{?*})", NULL, &a); c = g_variant_lookup_value (a, "com.example.Foo", "a{sv}"); g_variant_dict_init (&d, c); g_variant_dict_lookup (&d, "Bar", "u", &e); and if you wanted to get many values from the outer, non-vardict dictionary, you'd want to scan it with GVariantIter. [0] https://developer.gnome.org/glib/stable/gvariant-format-strings.html [1] https://developer.gnome.org/glib/stable/gvariant-format-strings.html#gvariant-format-strings-dictionaries
Created attachment 358355 [details] [review] [PATCH] gvariant: Add g_variant_get() example for dicts Small patch to add the missing example.
Review of attachment 358355 [details] [review]: Thanks for the patch. :-) How does moving §Dictionary after §GVariant * improve clarity? ::: docs/reference/glib/gvariant-varargs.xml @@ +908,3 @@ + + <para> + To extract data from nested Dictionaries you can go through a vardict. s/Dictionaries/dictionaries/ @@ +923,3 @@ +{ + GVariant *a; + GVariant *b; Some more descriptive variable names than `a`, `b` and `e` would be good.
> Thanks for the patch. :-) How does moving §Dictionary after §GVariant * improve clarity? Well, in §Dictionary we are actively using §GVariant *, that's why it looked to me a good thing moving it. Thanks for the review. I'll prepare a v2.
Created attachment 359401 [details] [review] [PATCH] gvariant: Add g_variant_get() example for dicts V2
Review of attachment 359401 [details] [review]: OK.