All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [PULL 34/40] keyval: introduce keyval_merge
Date: Tue,  6 Jul 2021 12:01:35 +0200	[thread overview]
Message-ID: <20210706100141.303960-35-pbonzini@redhat.com> (raw)
In-Reply-To: <20210706100141.303960-1-pbonzini@redhat.com>

This patch introduces a function that merges two keyval-produced
(or keyval-like) QDicts.  It can be used to emulate the behavior of
.merge_lists = true QemuOpts groups, merging -readconfig sections and
command-line options in a single QDict, and also to implement -set.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/option.h    |  1 +
 tests/unit/test-keyval.c | 58 +++++++++++++++++++++++++++++
 util/keyval.c            | 80 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)

diff --git a/include/qemu/option.h b/include/qemu/option.h
index f73e0dc7d9..d89c66145a 100644
--- a/include/qemu/option.h
+++ b/include/qemu/option.h
@@ -149,5 +149,6 @@ QemuOptsList *qemu_opts_append(QemuOptsList *dst, QemuOptsList *list);
 
 QDict *keyval_parse(const char *params, const char *implied_key,
                     bool *help, Error **errp);
+void keyval_merge(QDict *old, const QDict *new, Error **errp);
 
 #endif
diff --git a/tests/unit/test-keyval.c b/tests/unit/test-keyval.c
index e20c07cf3e..af0581ae6c 100644
--- a/tests/unit/test-keyval.c
+++ b/tests/unit/test-keyval.c
@@ -747,6 +747,61 @@ static void test_keyval_visit_any(void)
     visit_free(v);
 }
 
+static void test_keyval_merge_dict(void)
+{
+    QDict *first = keyval_parse("opt1=abc,opt2.sub1=def,opt2.sub2=ghi,opt3=xyz",
+                                NULL, NULL, &error_abort);
+    QDict *second = keyval_parse("opt1=ABC,opt2.sub2=GHI,opt2.sub3=JKL",
+                                 NULL, NULL, &error_abort);
+    QDict *combined = keyval_parse("opt1=ABC,opt2.sub1=def,opt2.sub2=GHI,opt2.sub3=JKL,opt3=xyz",
+                                   NULL, NULL, &error_abort);
+    Error *err = NULL;
+
+    keyval_merge(first, second, &err);
+    g_assert(!err);
+    g_assert(qobject_is_equal(QOBJECT(combined), QOBJECT(first)));
+    qobject_unref(first);
+    qobject_unref(second);
+    qobject_unref(combined);
+}
+
+static void test_keyval_merge_list(void)
+{
+    QDict *first = keyval_parse("opt1.0=abc,opt2.0=xyz",
+                                NULL, NULL, &error_abort);
+    QDict *second = keyval_parse("opt1.0=def",
+                                 NULL, NULL, &error_abort);
+    QDict *combined = keyval_parse("opt1.0=abc,opt1.1=def,opt2.0=xyz",
+                                   NULL, NULL, &error_abort);
+    Error *err = NULL;
+
+    keyval_merge(first, second, &err);
+    g_assert(!err);
+    g_assert(qobject_is_equal(QOBJECT(combined), QOBJECT(first)));
+    qobject_unref(first);
+    qobject_unref(second);
+    qobject_unref(combined);
+}
+
+static void test_keyval_merge_conflict(void)
+{
+    QDict *first = keyval_parse("opt2=ABC",
+                                NULL, NULL, &error_abort);
+    QDict *second = keyval_parse("opt2.sub1=def,opt2.sub2=ghi",
+                                 NULL, NULL, &error_abort);
+    QDict *third = qdict_clone_shallow(first);
+    Error *err = NULL;
+
+    keyval_merge(first, second, &err);
+    error_free_or_abort(&err);
+    keyval_merge(second, third, &err);
+    error_free_or_abort(&err);
+
+    qobject_unref(first);
+    qobject_unref(second);
+    qobject_unref(third);
+}
+
 int main(int argc, char *argv[])
 {
     g_test_init(&argc, &argv, NULL);
@@ -760,6 +815,9 @@ int main(int argc, char *argv[])
     g_test_add_func("/keyval/visit/optional", test_keyval_visit_optional);
     g_test_add_func("/keyval/visit/alternate", test_keyval_visit_alternate);
     g_test_add_func("/keyval/visit/any", test_keyval_visit_any);
+    g_test_add_func("/keyval/merge/dict", test_keyval_merge_dict);
+    g_test_add_func("/keyval/merge/list", test_keyval_merge_list);
+    g_test_add_func("/keyval/merge/conflict", test_keyval_merge_conflict);
     g_test_run();
     return 0;
 }
diff --git a/util/keyval.c b/util/keyval.c
index be34928813..9bc380686a 100644
--- a/util/keyval.c
+++ b/util/keyval.c
@@ -310,6 +310,86 @@ static char *reassemble_key(GSList *key)
     return g_string_free(s, FALSE);
 }
 
+/*
+ * Recursive worker for keyval_merge.
+ *
+ * @str is the path that led to the * current dictionary (to be used for
+ * error messages).  It is modified internally but restored before the
+ * function returns.
+ */
+static void keyval_do_merge(QDict *dest, const QDict *merged, GString *str, Error **errp)
+{
+    size_t save_len = str->len;
+    const QDictEntry *ent;
+    QObject *old_value;
+
+    for (ent = qdict_first(merged); ent; ent = qdict_next(merged, ent)) {
+        old_value = qdict_get(dest, ent->key);
+        if (old_value) {
+            if (qobject_type(old_value) != qobject_type(ent->value)) {
+                error_setg(errp, "Parameter '%s%s' used inconsistently",
+                           str->str, ent->key);
+                return;
+            } else if (qobject_type(ent->value) == QTYPE_QDICT) {
+                /* Merge sub-dictionaries.  */
+                g_string_append(str, ent->key);
+                g_string_append_c(str, '.');
+                keyval_do_merge(qobject_to(QDict, old_value),
+                                qobject_to(QDict, ent->value),
+                                str, errp);
+                g_string_truncate(str, save_len);
+                continue;
+            } else if (qobject_type(ent->value) == QTYPE_QLIST) {
+                /* Append to old list.  */
+                QList *old = qobject_to(QList, old_value);
+                QList *new = qobject_to(QList, ent->value);
+                const QListEntry *item;
+                QLIST_FOREACH_ENTRY(new, item) {
+                    qobject_ref(item->value);
+                    qlist_append_obj(old, item->value);
+                }
+                continue;
+            } else {
+                assert(qobject_type(ent->value) == QTYPE_QSTRING);
+            }
+        }
+
+        qobject_ref(ent->value);
+        qdict_put_obj(dest, ent->key, ent->value);
+    }
+}
+
+/* Merge the @merged dictionary into @dest.
+ *
+ * The dictionaries are expected to be returned by the keyval parser, and
+ * therefore the only expected scalar type is the string.  In case the same
+ * path is present in both @dest and @merged, the semantics are as follows:
+ *
+ * - lists are concatenated
+ *
+ * - dictionaries are merged recursively
+ *
+ * - for scalar values, @merged wins
+ *
+ * In case an error is reported, @dest may already have been modified.
+ *
+ * This function can be used to implement semantics analogous to QemuOpts's
+ * .merge_lists = true case, or to implement -set for options backed by QDicts.
+ *
+ * Note: while QemuOpts is commonly used so that repeated keys overwrite
+ * ("last one wins"), it can also be used so that repeated keys build up
+ * a list. keyval_merge() can only be used when the options' semantics are
+ * the former, not the latter.
+ */
+void keyval_merge(QDict *dest, const QDict *merged, Error **errp)
+{
+    GString *str;
+
+    str = g_string_new("");
+    keyval_do_merge(dest, merged, str, errp);
+    g_string_free(str, TRUE);
+}
+
 /*
  * Listify @cur recursively.
  * Replace QDicts whose keys are all valid list indexes by QLists.
-- 
2.31.1




  parent reply	other threads:[~2021-07-06 10:40 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-06 10:01 [PULL 00/40] Misc patches for 2021-07-06 Paolo Bonzini
2021-07-06 10:01 ` [PULL 01/40] Makefile: Remove /usr/bin/env wrapper from the SHELL variable Paolo Bonzini
2021-07-06 10:01 ` [PULL 02/40] softmmu/vl: Remove obsolete comment about the "frame" parameter Paolo Bonzini
2021-07-06 10:01 ` [PULL 03/40] ui: Fix the "-display sdl,window_close=..." parameter Paolo Bonzini
2021-07-06 10:01 ` [PULL 04/40] ui: Mark the '-no-quit' option as deprecated Paolo Bonzini
2021-07-06 10:01 ` [PULL 05/40] qemu-options: Improve the documentation of the -display options Paolo Bonzini
2021-07-06 10:01 ` [PULL 06/40] coverity-scan: Remove lm32 / unicore32 targets Paolo Bonzini
2021-07-06 10:01 ` [PULL 07/40] coverity-scan: switch to vpath build Paolo Bonzini
2021-07-06 10:01 ` [PULL 08/40] target/i386: Declare constants for XSAVE offsets Paolo Bonzini
2021-07-06 10:01 ` [PULL 09/40] target/i386: Consolidate the X86XSaveArea offset checks Paolo Bonzini
2021-07-06 10:01 ` [PULL 10/40] target/i386: Clarify the padding requirements of X86XSaveArea Paolo Bonzini
2021-07-06 10:01 ` [PULL 11/40] target/i386: Pass buffer and length to XSAVE helper Paolo Bonzini
2021-07-06 10:01 ` [PULL 12/40] target/i386: Make x86_ext_save_areas visible outside cpu.c Paolo Bonzini
2021-07-06 10:01 ` [PULL 13/40] target/i386: Observe XSAVE state area offsets Paolo Bonzini
2021-07-06 10:01 ` [PULL 14/40] target/i386: Populate x86_ext_save_areas offsets using cpuid where possible Paolo Bonzini
2021-07-06 10:01 ` [PULL 15/40] target/i386: Move X86XSaveArea into TCG Paolo Bonzini
2021-07-06 10:01 ` [PULL 16/40] configure: drop vte-2.90 check Paolo Bonzini
2021-07-06 10:01 ` [PULL 17/40] configure, meson: convert vte detection to meson Paolo Bonzini
2021-07-06 10:01 ` [PULL 18/40] configure, meson: convert virgl " Paolo Bonzini
2021-07-06 10:01 ` [PULL 19/40] configure, meson: convert libdaxctl " Paolo Bonzini
2021-07-06 10:01 ` [PULL 20/40] configure, meson: convert libpmem " Paolo Bonzini
2021-07-06 10:01 ` [PULL 21/40] configure, meson: convert liburing " Paolo Bonzini
2021-07-06 10:01 ` [PULL 22/40] configure, meson: convert libxml2 " Paolo Bonzini
2021-07-06 10:01 ` [PULL 23/40] meson: sort existing compiler tests Paolo Bonzini
2021-07-06 10:01 ` [PULL 24/40] meson: store dependency('threads') in a variable Paolo Bonzini
2021-07-06 10:01 ` [PULL 25/40] configure, meson: move CONFIG_IVSHMEM to meson Paolo Bonzini
2021-07-06 10:01 ` [PULL 26/40] configure: convert HAVE_BROKEN_SIZE_MAX " Paolo Bonzini
2021-07-06 10:01 ` [PULL 27/40] configure: convert compiler tests to meson, part 1 Paolo Bonzini
2021-07-06 10:01 ` [PULL 28/40] configure: convert compiler tests to meson, part 2 Paolo Bonzini
2021-07-06 10:01 ` [PULL 29/40] configure: convert compiler tests to meson, part 3 Paolo Bonzini
2021-07-06 10:01 ` [PULL 30/40] configure: convert compiler tests to meson, part 4 Paolo Bonzini
2021-07-06 10:01 ` [PULL 31/40] configure: convert compiler tests to meson, part 5 Paolo Bonzini
2021-07-06 10:01 ` [PULL 32/40] configure: convert compiler tests to meson, part 6 Paolo Bonzini
2021-07-06 10:01 ` [PULL 33/40] qom: export more functions for use with non-UserCreatable objects Paolo Bonzini
2021-07-06 10:01 ` Paolo Bonzini [this message]
2021-07-06 10:01 ` [PULL 35/40] keyval: introduce keyval_parse_into Paolo Bonzini
2021-07-06 10:01 ` [PULL 36/40] vl: switch -M parsing to keyval Paolo Bonzini
2021-07-08 16:31   ` Jason Andryuk
2021-07-08 16:31     ` Jason Andryuk
2021-07-13  2:15     ` [PATCH] vl: Parse legacy default_machine_opts Jason Andryuk
2021-07-22 14:12       ` Anthony PERARD via
2021-07-22 14:12         ` Anthony PERARD
2021-07-22 14:39         ` Paolo Bonzini
2021-07-22 14:39           ` Paolo Bonzini
2021-07-12 19:24   ` [PULL 36/40] vl: switch -M parsing to keyval Volker Rümelin
2021-07-13 19:43   ` Alex Williamson
2021-07-13 23:10     ` Alex Williamson
2021-07-14  6:16       ` Mark Cave-Ayland
2021-07-14 22:13         ` Alex Williamson
2021-07-22  8:19   ` Peter Krempa
2021-07-22 14:39     ` Paolo Bonzini
2021-07-22 14:50       ` Peter Krempa
2021-07-06 10:01 ` [PULL 37/40] machine: add smp compound property Paolo Bonzini
2021-07-13 10:37   ` Peter Maydell
2021-07-13 11:07     ` Markus Armbruster
2021-07-13 10:53   ` Peter Maydell
2021-07-06 10:01 ` [PULL 38/40] qemu-option: remove now-dead code Paolo Bonzini
2021-07-06 10:01 ` [PULL 39/40] Set icon for QEMU binary on Mac OS Paolo Bonzini
2021-07-06 10:01 ` [PULL 40/40] config-host.mak: remove unused compiler-related lines Paolo Bonzini
2021-07-06 12:10 ` [PULL 00/40] Misc patches for 2021-07-06 Peter Maydell

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=20210706100141.303960-35-pbonzini@redhat.com \
    --to=pbonzini@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.