qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eduardo Habkost <ehabkost@redhat.com>
To: qemu-devel@nongnu.org
Cc: Markus Armbruster <armbru@redhat.com>, Eric Blake <eblake@redhat.com>
Subject: [Qemu-devel] [RFC 3/3] qmp: Include parent types on 'qom-list-types' output
Date: Fri, 19 May 2017 23:09:14 -0300	[thread overview]
Message-ID: <20170520020914.14562-4-ehabkost@redhat.com> (raw)
In-Reply-To: <20170520020914.14562-1-ehabkost@redhat.com>

Include list of parent types of each type on 'qom-list-types' output.

Without this, there's no way to figure out the parents of a given type
without making additional 'qom-list-types' queries.

In addition to the test case for the new feature, update the
abstract-interface test case to use the new field and avoid the
"qom-list-types implements=object" trick.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
I would to include a "interfaces" field in the future, too, but
this would require extending the core QOM API to allow that.
---
 qapi-schema.json               |  6 ++++-
 qmp.c                          | 16 +++++++++++
 tests/device-introspect-test.c | 61 +++++++++++++++++++++++++++++++++++-------
 3 files changed, 72 insertions(+), 11 deletions(-)

diff --git a/qapi-schema.json b/qapi-schema.json
index cbf1e2a837..169dfd2d7f 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -3037,12 +3037,16 @@
 # @abstract: the type is abstract and can't be directly instantiated
 #            (since 2.10)
 #
+# @parent-types: parent types.  Parent types implemented by the type.  Note that
+#           this doesn't include interface names also implemented by the type.
+#           (since 2.10)
+#
 # Since: 1.1
 #
 # Notes: This command is experimental and may change syntax in future releases.
 ##
 { 'struct': 'ObjectTypeInfo',
-  'data': { 'name': 'str', 'abstract': 'bool' } }
+  'data': { 'name': 'str', 'abstract': 'bool', 'parent-types': [ 'str' ] } }
 
 ##
 # @qom-list-types:
diff --git a/qmp.c b/qmp.c
index 8066705dbe..5b50f6f650 100644
--- a/qmp.c
+++ b/qmp.c
@@ -436,6 +436,21 @@ void qmp_change(const char *device, const char *target,
     }
 }
 
+static strList *qom_type_get_parents(ObjectClass *klass)
+{
+    ObjectClass *parent = object_class_get_parent(klass);
+    strList *r = NULL;
+
+    if (!parent) {
+        return NULL;
+    }
+
+    r = g_new0(strList, 1);
+    r->value = g_strdup(object_class_get_name(parent));
+    r->next = qom_type_get_parents(parent);
+    return r;
+}
+
 static void qom_list_types_tramp(ObjectClass *klass, void *data)
 {
     ObjectTypeInfoList *e, **pret = data;
@@ -444,6 +459,7 @@ static void qom_list_types_tramp(ObjectClass *klass, void *data)
     info = g_malloc0(sizeof(*info));
     info->name = g_strdup(object_class_get_name(klass));
     info->abstract = object_class_is_abstract(klass);
+    info->parent_types = qom_type_get_parents(klass);
 
     e = g_malloc0(sizeof(*e));
     e->value = info;
diff --git a/tests/device-introspect-test.c b/tests/device-introspect-test.c
index d5aacb4ed1..350b862f3b 100644
--- a/tests/device-introspect-test.c
+++ b/tests/device-introspect-test.c
@@ -61,6 +61,20 @@ static QDict *type_list_find(QList *types, const char *name)
     return NULL;
 }
 
+static bool str_list_has(QList *strlist, const char *str)
+{
+    QListEntry *e;
+
+    QLIST_FOREACH_ENTRY(strlist, e) {
+        const char *s = qstring_get_str(qobject_to_qstring(qlist_entry_obj(e)));
+        if (!strcmp(s, str)) {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 static QList *device_type_list(bool abstract)
 {
     return qom_list_types("device", abstract);
@@ -103,6 +117,31 @@ static void test_device_intro_list(void)
     qtest_end();
 }
 
+/*
+ * Ensure all entries returned by qom-list-types implements=<parent>
+ * have <parent> in parent-types.
+ */
+static void test_qom_list_parents(const char *parent)
+{
+    QList *types;
+    QListEntry *e;
+
+    types = qom_list_types(parent, true);
+
+    QLIST_FOREACH_ENTRY(types, e) {
+        QDict *d = qobject_to_qdict(qlist_entry_obj(e));
+
+        /* The parent type being queried is included in the list too, skip it */
+        if (!strcmp(qdict_get_str(d, "name"), parent)) {
+            continue;
+        }
+
+        g_assert(str_list_has(qdict_get_qlist(d, "parent-types"), parent));
+    }
+
+    QDECREF(types);
+}
+
 static void test_qom_list_fields(void)
 {
     QList *all_types;
@@ -123,6 +162,10 @@ static void test_qom_list_fields(void)
         g_assert(abstract == expected_abstract);
     }
 
+    test_qom_list_parents("object");
+    test_qom_list_parents("device");
+    test_qom_list_parents("sys-bus-device");
+
     QDECREF(all_types);
     QDECREF(non_abstract);
     qtest_end();
@@ -165,23 +208,22 @@ static void test_device_intro_concrete(void)
 static void test_abstract_interfaces(void)
 {
     QList *all_types;
-    QList *obj_types;
     QListEntry *e;
 
     qtest_start(common_args);
-    /*
-     * qom-list-types implements=interface returns all types that
-     * implement _any_ interface (not just interface types), but
-     * we can filter them out because interfaces don't implement
-     * the "object" type.
-     */
+
     all_types = qom_list_types("interface", true);
-    obj_types = qom_list_types("object", true);
 
     QLIST_FOREACH_ENTRY(all_types, e) {
         QDict *d = qobject_to_qdict(qlist_entry_obj(e));
 
-        if (type_list_find(obj_types, qdict_get_str(d, "name"))) {
+        /*
+         * qom-list-types implements=interface returns all types
+         * that implement _any_ interface (not just interface
+         * types), so skip the ones that don't have "interface"
+         * on parent-types.
+         */
+        if (!str_list_has(qdict_get_qlist(d, "parent-types"), "interface")) {
             /* Not an interface type */
             continue;
         }
@@ -190,7 +232,6 @@ static void test_abstract_interfaces(void)
     }
 
     QDECREF(all_types);
-    QDECREF(obj_types);
     qtest_end();
 }
 
-- 
2.11.0.259.g40922b1

  parent reply	other threads:[~2017-05-20  2:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-20  2:09 [Qemu-devel] [RFC 0/3] qmp: Return extra information on qom-list-types Eduardo Habkost
2017-05-20  2:09 ` [Qemu-devel] [RFC 1/3] tests: Simplify abstract-interfaces check with a helper Eduardo Habkost
2017-05-20  2:09 ` [Qemu-devel] [RFC 2/3] qmp: Include 'abstract' field on 'qom-list-types' output Eduardo Habkost
2017-05-20  2:09 ` Eduardo Habkost [this message]
2017-05-23 14:12 ` [Qemu-devel] [RFC 0/3] qmp: Return extra information on qom-list-types Markus Armbruster
2017-05-23 14:34   ` Eduardo Habkost
2017-05-24 12:13     ` Markus Armbruster
2017-05-24 13:13       ` Eduardo Habkost
2017-05-24 14:31       ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170520020914.14562-4-ehabkost@redhat.com \
    --to=ehabkost@redhat.com \
    --cc=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).