[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 16/18] Implement qdict_flatten()
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 16/18] Implement qdict_flatten() |
Date: |
Fri, 26 Jul 2013 22:20:30 +0200 |
qdict_flatten(): For each nested QDict with key x, all fields with key y
are moved to this QDict and their key is renamed to "x.y". This operation
is applied recursively for nested QDicts.
Signed-off-by: Kevin Wolf <address@hidden>
Reviewed-by: Eric Blake <address@hidden>
---
include/qapi/qmp/qdict.h | 1 +
qobject/qdict.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
index 685b2e3..d6855d1 100644
--- a/include/qapi/qmp/qdict.h
+++ b/include/qapi/qmp/qdict.h
@@ -65,5 +65,6 @@ int qdict_get_try_bool(const QDict *qdict, const char *key,
int def_value);
const char *qdict_get_try_str(const QDict *qdict, const char *key);
QDict *qdict_clone_shallow(const QDict *src);
+void qdict_flatten(QDict *qdict);
#endif /* QDICT_H */
diff --git a/qobject/qdict.c b/qobject/qdict.c
index ed381f9..472f106 100644
--- a/qobject/qdict.c
+++ b/qobject/qdict.c
@@ -476,3 +476,54 @@ static void qdict_destroy_obj(QObject *obj)
g_free(qdict);
}
+
+static void qdict_do_flatten(QDict *qdict, QDict *target, const char *prefix)
+{
+ QObject *value;
+ const QDictEntry *entry, *next;
+ const char *new_key;
+ bool delete;
+
+ entry = qdict_first(qdict);
+
+ while (entry != NULL) {
+
+ next = qdict_next(qdict, entry);
+ value = qdict_entry_value(entry);
+ new_key = NULL;
+ delete = false;
+
+ if (prefix) {
+ qobject_incref(value);
+ new_key = g_strdup_printf("%s.%s", prefix, entry->key);
+ qdict_put_obj(target, new_key, value);
+ delete = true;
+ }
+
+ if (qobject_type(value) == QTYPE_QDICT) {
+ qdict_do_flatten(qobject_to_qdict(value), target,
+ new_key ? new_key : entry->key);
+ delete = true;
+ }
+
+ if (delete) {
+ qdict_del(qdict, entry->key);
+
+ /* Restart loop after modifying the iterated QDict */
+ entry = qdict_first(qdict);
+ continue;
+ }
+
+ entry = next;
+ }
+}
+
+/**
+ * qdict_flatten(): For each nested QDict with key x, all fields with key y
+ * are moved to this QDict and their key is renamed to "x.y". This operation
+ * is applied recursively for nested QDicts.
+ */
+void qdict_flatten(QDict *qdict)
+{
+ qdict_do_flatten(qdict, qdict, NULL);
+}
--
1.8.1.4
- [Qemu-devel] [PULL 06/18] qapi: Flat unions with arbitrary discriminator, (continued)
- [Qemu-devel] [PULL 06/18] qapi: Flat unions with arbitrary discriminator, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 07/18] qapi: Add consume argument to qmp_input_get_object(), Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 08/18] qapi.py: Maintain a list of union types, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 09/18] qapi: Anonymous unions, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 10/18] block: Allow "driver" option on the top level, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 11/18] QemuOpts: Add qemu_opt_unset(), Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 12/18] blockdev: Rename I/O throttling options for QMP, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 13/18] qcow2: Use dashes instead of underscores in options, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 14/18] blockdev: Rename 'readonly' option to 'read-only', Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 15/18] blockdev: Split up 'cache' option, Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 16/18] Implement qdict_flatten(),
Kevin Wolf <=
- [Qemu-devel] [PULL 17/18] Implement sync modes for drive-backup., Kevin Wolf, 2013/07/26
- [Qemu-devel] [PULL 18/18] Add tests for sync modes 'TOP' and 'NONE', Kevin Wolf, 2013/07/26