After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 786737 - No g_variant_get() example for dicts
No g_variant_get() example for dicts
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: docs
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2017-08-24 09:14 UTC by Will Thompson
Modified: 2017-09-11 08:47 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
[PATCH] gvariant: Add g_variant_get() example for dicts (3.60 KB, patch)
2017-08-24 15:34 UTC, Carlo Caione
none Details | Review
[PATCH] gvariant: Add g_variant_get() example for dicts (3.66 KB, patch)
2017-09-08 14:18 UTC, Carlo Caione
committed Details | Review

Description Will Thompson 2017-08-24 09:14:36 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
Comment 1 Carlo Caione 2017-08-24 15:34:24 UTC
Created attachment 358355 [details] [review]
[PATCH] gvariant: Add g_variant_get() example for dicts

Small patch to add the missing example.
Comment 2 Philip Withnall 2017-09-08 13:54:32 UTC
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.
Comment 3 Carlo Caione 2017-09-08 14:03:02 UTC
> 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.
Comment 4 Carlo Caione 2017-09-08 14:18:22 UTC
Created attachment 359401 [details] [review]
[PATCH] gvariant: Add g_variant_get() example for dicts

V2
Comment 5 Philip Withnall 2017-09-11 08:45:55 UTC
Review of attachment 359401 [details] [review]:

OK.