From: Kevin Wolf <kwolf@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: jcody@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org,
mreitz@redhat.com
Subject: Re: [Qemu-devel] [PATCH 12/18] block-qdict: Clean up qdict_crumple() a bit
Date: Thu, 14 Jun 2018 15:26:39 +0200 [thread overview]
Message-ID: <20180614132639.GG8564@localhost.localdomain> (raw)
In-Reply-To: <87a7rxmvpw.fsf@dusky.pond.sub.org>
Am 14.06.2018 um 13:52 hat Markus Armbruster geschrieben:
> Kevin Wolf <kwolf@redhat.com> writes:
>
> > Am 13.06.2018 um 17:23 hat Markus Armbruster geschrieben:
> >> Kevin Wolf <kwolf@redhat.com> writes:
> >>
> >> > Am 12.06.2018 um 14:58 hat Markus Armbruster geschrieben:
> >> >> When you mix scalar and non-scalar keys, whether you get an "already
> >> >> set as scalar" or an "already set as dict" error depends on qdict
> >> >> iteration order. Neither message makes much sense. Replace by
> >> >> ""Cannot mix scalar and non-scalar keys". This is similar to the
> >> >> message we get for mixing list and non-list keys.
> >> >>
> >> >> I find qdict_crumple()'s first loop hard to understand. Rearrange it
> >> >> and add a comment.
> >> >>
> >> >> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> >> >
> >> > To be honest, I found the old version of the loop more obvious.
> >> >
> >> >> qobject/block-qdict.c | 42 +++++++++++++++++++++---------------------
> >> >> 1 file changed, 21 insertions(+), 21 deletions(-)
> >> >>
> >> >> diff --git a/qobject/block-qdict.c b/qobject/block-qdict.c
> >> >> index a4e1c8d08f..35e9052816 100644
> >> >> --- a/qobject/block-qdict.c
> >> >> +++ b/qobject/block-qdict.c
> >> >> @@ -403,7 +403,7 @@ static int qdict_is_list(QDict *maybe_list, Error **errp)
> >> >> QObject *qdict_crumple(const QDict *src, Error **errp)
> >> >> {
> >> >> const QDictEntry *ent;
> >> >> - QDict *two_level, *multi_level = NULL;
> >> >> + QDict *two_level, *multi_level = NULL, *child_dict;
> >> >> QObject *dst = NULL, *child;
> >> >> size_t i;
> >> >> char *prefix = NULL;
> >> >> @@ -422,29 +422,29 @@ QObject *qdict_crumple(const QDict *src, Error **errp)
> >> >> }
> >> >>
> >> >> qdict_split_flat_key(ent->key, &prefix, &suffix);
> >> >> -
> >> >> child = qdict_get(two_level, prefix);
> >> >> + child_dict = qobject_to(QDict, child);
> >> >> +
> >> >> + if (child) {
> >> >> + /*
> >> >> + * An existing child must be a dict and @ent must be a
> >> >> + * dict member (i.e. suffix not null), or else @ent
> >> >> + * clashes.
> >> >> + */
> >> >> + if (!child_dict || !suffix) {
> >> >> + error_setg(errp,
> >> >> + "Cannot mix scalar and non-scalar keys");
> >> >> + goto error;
> >> >> + }
> >> >> + } else if (suffix) {
> >> >> + child_dict = qdict_new();
> >> >> + qdict_put(two_level, prefix, child_dict);
> >> >> + } else {
> >> >> + qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
> >> >> + }
> >> >
> >> > At least, can you please move the else branch to the if below so that
> >> > the addition of the new entry is symmetrical for both scalars and dicts?
> >> > As the code is, it mixes the conflict check, creation of the child dict
> >> > and addition of scalars (but not to the child dict) in a weird way in a
> >> > single if block.
> >> >
> >> > Or maybe organise it like this:
> >> >
> >> > if (child && !(child_dict && suffix)) {
> >> > error
> >> > }
> >> >
> >> > if (suffix) {
> >> > if (!child_dict) {
> >> > create it
> >> > add it to two_level
> >> > }
> >> > add entry to child_dict
> >> > } else {
> >> > add entry to two_level
> >> > }
> >>
> >> Fleshing out...
> >>
> >> if (child && !child_dict) {
> >> /*
> >> * @prefix already exists and it's a non-dictionary,
> >> * i.e. we've seen a scalar with key @prefix. The same
> >> * key can't occur twice, therefore suffix must be
> >> * non-null.
> >> */
> >> assert(suffix);
> >> /*
> >> * @ent has key @prefix.@suffix, but we've already seen
> >> * key @prefix: clash.
> >> */
> >> error_setg(errp, "Cannot mix scalar and non-scalar keys");
> >> goto error;
> >> }
> >
> > This catches "foo.bar" after "foo", but not "foo" after "foo.bar".
> >
> >> if (suffix) {
> >> if (!child_dict) {
> >> child_dict = qdict_new();
> >> qdict_put(two_level, prefix, child_dict);
> >> }
> >> qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
> >> } else {
> >> assert(!child);
> >
> > So "foo" after "foo.bar" would fail this assertion.
> >
> >> qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
> >> }
> >>
> >> Okay?
> >
> > Kevin
>
> I feel dumb. Next try:
>
> if (child) {
> /*
> * If @child_dict, then all previous keys with this prefix
> * had a suffix. If @suffix, this one has one as well,
> * and we're good, else there's a clash.
> */
> if (!child_dict || !suffix) {
> error_setg(errp, "Cannot mix scalar and non-scalar keys");
> goto error;
> }
> }
>
> if (suffix) {
> if (!child_dict) {
> child_dict = qdict_new();
> qdict_put(two_level, prefix, child_dict);
> }
> qdict_put_obj(child_dict, suffix, qobject_ref(ent->value));
> } else {
> qdict_put_obj(two_level, prefix, qobject_ref(ent->value));
> }
This one looks good to me. (Though I feel your comments explains the
!(child_dict && suffix) form that I chose rather than what you actually
have in the code. But I don't mind.)
Kevin
next prev parent reply other threads:[~2018-06-14 13:26 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-12 12:58 [Qemu-devel] [PATCH 00/18] block: Configuration fixes and rbd authentication Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 01/18] rbd: Drop deprecated -drive parameter "filename" Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 02/18] iscsi: " Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 03/18] block: Add block-specific QDict header Markus Armbruster
2018-06-12 15:02 ` Kevin Wolf
2018-06-12 16:40 ` Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 04/18] qobject: Move block-specific qdict code to block-qdict.c Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 05/18] block: Fix -blockdev for certain non-string scalars Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 06/18] block: Fix -drive " Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 07/18] block: Clean up a misuse of qobject_to() in .bdrv_co_create_opts() Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 08/18] block: Factor out qobject_input_visitor_new_flat_confused() Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 09/18] block: Make remaining uses of qobject input visitor more robust Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 10/18] block-qdict: Simplify qdict_flatten_qdict() Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 11/18] block-qdict: Tweak qdict_flatten_qdict(), qdict_flatten_qlist() Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 12/18] block-qdict: Clean up qdict_crumple() a bit Markus Armbruster
2018-06-12 15:39 ` Kevin Wolf
2018-06-13 15:23 ` Markus Armbruster
2018-06-14 8:40 ` Kevin Wolf
2018-06-14 8:46 ` Daniel P. Berrangé
2018-06-14 13:11 ` Markus Armbruster
2018-06-14 11:52 ` Markus Armbruster
2018-06-14 13:26 ` Kevin Wolf [this message]
2018-06-12 12:58 ` [Qemu-devel] [PATCH 13/18] block-qdict: Simplify qdict_is_list() some Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 14/18] check-block-qdict: Rename qdict_flatten()'s variables for clarity Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 15/18] check-block-qdict: Cover flattening of empty lists and dictionaries Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 16/18] block: Fix -blockdev / blockdev-add for empty objects and arrays Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 17/18] rbd: New parameter auth-client-required Markus Armbruster
2018-06-12 12:58 ` [Qemu-devel] [PATCH 18/18] rbd: New parameter key-secret Markus Armbruster
2018-06-12 13:20 ` Daniel P. Berrangé
2018-06-12 16:42 ` Markus Armbruster
2018-06-12 15:04 ` [Qemu-devel] [PATCH 00/18] block: Configuration fixes and rbd authentication Kevin Wolf
2018-06-12 16:41 ` Kevin Wolf
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=20180614132639.GG8564@localhost.localdomain \
--to=kwolf@redhat.com \
--cc=armbru@redhat.com \
--cc=jcody@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).