218e99
From 4c0ae9bbc7424ca9fc220f56487e9927e8841f87 Mon Sep 17 00:00:00 2001
218e99
From: Kevin Wolf <kwolf@redhat.com>
218e99
Date: Tue, 5 Nov 2013 14:09:00 +0100
218e99
Subject: [PATCH 47/87] Implement qdict_flatten()
218e99
218e99
RH-Author: Kevin Wolf <kwolf@redhat.com>
218e99
Message-id: <1383660558-32096-7-git-send-email-kwolf@redhat.com>
218e99
Patchwork-id: 55385
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 06/24] Implement qdict_flatten()
218e99
Bugzilla: 978402
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
RH-Acked-by: Max Reitz <mreitz@redhat.com>
218e99
218e99
qdict_flatten(): For each nested QDict with key x, all fields with key y
218e99
are moved to this QDict and their key is renamed to "x.y". This operation
218e99
is applied recursively for nested QDicts.
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
(cherry picked from commit f660dc6a2e97756596b2e79ce6127a3034f2308b)
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
---
218e99
 include/qapi/qmp/qdict.h |  1 +
218e99
 qobject/qdict.c          | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
218e99
 2 files changed, 52 insertions(+)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 include/qapi/qmp/qdict.h |    1 +
218e99
 qobject/qdict.c          |   51 ++++++++++++++++++++++++++++++++++++++++++++++
218e99
 2 files changed, 52 insertions(+), 0 deletions(-)
218e99
218e99
diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
218e99
index 685b2e3..d6855d1 100644
218e99
--- a/include/qapi/qmp/qdict.h
218e99
+++ b/include/qapi/qmp/qdict.h
218e99
@@ -65,5 +65,6 @@ int qdict_get_try_bool(const QDict *qdict, const char *key, int def_value);
218e99
 const char *qdict_get_try_str(const QDict *qdict, const char *key);
218e99
 
218e99
 QDict *qdict_clone_shallow(const QDict *src);
218e99
+void qdict_flatten(QDict *qdict);
218e99
 
218e99
 #endif /* QDICT_H */
218e99
diff --git a/qobject/qdict.c b/qobject/qdict.c
218e99
index ed381f9..472f106 100644
218e99
--- a/qobject/qdict.c
218e99
+++ b/qobject/qdict.c
218e99
@@ -476,3 +476,54 @@ static void qdict_destroy_obj(QObject *obj)
218e99
 
218e99
     g_free(qdict);
218e99
 }
218e99
+
218e99
+static void qdict_do_flatten(QDict *qdict, QDict *target, const char *prefix)
218e99
+{
218e99
+    QObject *value;
218e99
+    const QDictEntry *entry, *next;
218e99
+    const char *new_key;
218e99
+    bool delete;
218e99
+
218e99
+    entry = qdict_first(qdict);
218e99
+
218e99
+    while (entry != NULL) {
218e99
+
218e99
+        next = qdict_next(qdict, entry);
218e99
+        value = qdict_entry_value(entry);
218e99
+        new_key = NULL;
218e99
+        delete = false;
218e99
+
218e99
+        if (prefix) {
218e99
+            qobject_incref(value);
218e99
+            new_key = g_strdup_printf("%s.%s", prefix, entry->key);
218e99
+            qdict_put_obj(target, new_key, value);
218e99
+            delete = true;
218e99
+        }
218e99
+
218e99
+        if (qobject_type(value) == QTYPE_QDICT) {
218e99
+            qdict_do_flatten(qobject_to_qdict(value), target,
218e99
+                             new_key ? new_key : entry->key);
218e99
+            delete = true;
218e99
+        }
218e99
+
218e99
+        if (delete) {
218e99
+            qdict_del(qdict, entry->key);
218e99
+
218e99
+            /* Restart loop after modifying the iterated QDict */
218e99
+            entry = qdict_first(qdict);
218e99
+            continue;
218e99
+        }
218e99
+
218e99
+        entry = next;
218e99
+    }
218e99
+}
218e99
+
218e99
+/**
218e99
+ * qdict_flatten(): For each nested QDict with key x, all fields with key y
218e99
+ * are moved to this QDict and their key is renamed to "x.y". This operation
218e99
+ * is applied recursively for nested QDicts.
218e99
+ */
218e99
+void qdict_flatten(QDict *qdict)
218e99
+{
218e99
+    qdict_do_flatten(qdict, qdict, NULL);
218e99
+}
218e99
-- 
218e99
1.7.1
218e99