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