aboutsummaryrefslogtreecommitdiff
path: root/qobject/qdict.c
diff options
context:
space:
mode:
authorKevin Wolf <kwolf@redhat.com>2018-02-01 20:00:44 +0100
committerKevin Wolf <kwolf@redhat.com>2018-03-09 15:17:47 +0100
commitbcebf102ccc3c6db327f341adc379fdf0673ca6b (patch)
tree56c08493a098807202128cf4d8205c258fc02b9f /qobject/qdict.c
parent37974a97d4d29229255b39ddd16c31743e1880ed (diff)
qdict: Introduce qdict_rename_keys()
A few block drivers will need to rename .bdrv_create options for their QAPIfication, so let's have a helper function for that. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Reviewed-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'qobject/qdict.c')
-rw-r--r--qobject/qdict.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/qobject/qdict.c b/qobject/qdict.c
index 23df84f9cd..229b8c840b 100644
--- a/qobject/qdict.c
+++ b/qobject/qdict.c
@@ -1072,3 +1072,37 @@ void qdict_join(QDict *dest, QDict *src, bool overwrite)
entry = next;
}
}
+
+/**
+ * qdict_rename_keys(): Rename keys in qdict according to the replacements
+ * specified in the array renames. The array must be terminated by an entry
+ * with from = NULL.
+ *
+ * The renames are performed individually in the order of the array, so entries
+ * may be renamed multiple times and may or may not conflict depending on the
+ * order of the renames array.
+ *
+ * Returns true for success, false in error cases.
+ */
+bool qdict_rename_keys(QDict *qdict, const QDictRenames *renames, Error **errp)
+{
+ QObject *qobj;
+
+ while (renames->from) {
+ if (qdict_haskey(qdict, renames->from)) {
+ if (qdict_haskey(qdict, renames->to)) {
+ error_setg(errp, "'%s' and its alias '%s' can't be used at the "
+ "same time", renames->to, renames->from);
+ return false;
+ }
+
+ qobj = qdict_get(qdict, renames->from);
+ qobject_incref(qobj);
+ qdict_put_obj(qdict, renames->to, qobj);
+ qdict_del(qdict, renames->from);
+ }
+
+ renames++;
+ }
+ return true;
+}