Received: from eggs.gnu.org ([2001:4830:134:3::10]:33790) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bq46V-0000IQ-Me for qemu-devel@nongnu.org; Fri, 30 Sep 2016 16:08:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bq46T-0002Vr-8E for qemu-devel@nongnu.org; Fri, 30 Sep 2016 16:08:18 -0400 References: <1475246744-29302-1-git-send-email-berrange@redhat.com> <1475246744-29302-13-git-send-email-berrange@redhat.com> From: Eric Blake Message-ID: Date: Fri, 30 Sep 2016 15:08:09 -0500 MIME-Version: 1.0 In-Reply-To: <1475246744-29302-13-git-send-email-berrange@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="mGpauRdeUxQ35wRro08ksWkHv050IQXFi" Subject: Re: [Qemu-devel] [PATCH v14 12/21] option: allow qemu_opts_to_qdict to merge repeated options List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Daniel P. Berrange" , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Paolo Bonzini , =?UTF-8?Q?Andreas_F=c3=a4rber?= This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --mGpauRdeUxQ35wRro08ksWkHv050IQXFi From: Eric Blake To: "Daniel P. Berrange" , qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Paolo Bonzini , =?UTF-8?Q?Andreas_F=c3=a4rber?= Message-ID: Subject: Re: [PATCH v14 12/21] option: allow qemu_opts_to_qdict to merge repeated options References: <1475246744-29302-1-git-send-email-berrange@redhat.com> <1475246744-29302-13-git-send-email-berrange@redhat.com> In-Reply-To: <1475246744-29302-13-git-send-email-berrange@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 09/30/2016 09:45 AM, Daniel P. Berrange wrote: > If given an option string such as >=20 > size=3D1024,nodes=3D10,nodes=3D4-5,nodes=3D1-2,policy=3Dbind >=20 > the qemu_opts_to_qdict() method will currently overwrite > the values for repeated option keys, so only the last > value is in the returned dict: >=20 > size=3DQString("1024") > nodes=3DQString("1-2") > policy=3DQString("bind") >=20 > With this change the caller can optionally ask for all > the repeated values to be stored in a QList. In the > above example that would result in 'nodes' being a > QList, so the returned dict would contain >=20 > size=3DQString("1024") > nodes=3DQList([QString("10"), > QString("4-5"), > QString("1-2")]) > policy=3DQString("bind") I think the last time around you were converting keys (turning "nodes" into "nodes.0"; I think your idea here of keeping a single key tied to QList makes more sense. >=20 > Note that the conversion has no way of knowing whether > any given key is expected to be a list upfront - it can > only figure that out when seeing the first duplicated > key. Thus the caller has to be prepared to deal with the > fact that if a key 'foo' is a list, then the returned > qdict may contain either a QString or a QList for the > key 'foo'. >=20 > In a third mode, it is possible to ask for repeated > options to be reported as an error, rather than silently > dropping all but the last one. >=20 > All existing callers are all converted to explicitly > request the historical behaviour of only reporting the > last key. Later patches will make use of the new modes. >=20 > Signed-off-by: Daniel P. Berrange > --- > -QDict *qemu_opts_to_qdict(const QemuOpts *opts, QDict *qdict); > +typedef enum { > + /* Last occurrence of a duplicate option silently replaces earlier= */ > + QEMU_OPTS_REPEAT_POLICY_LAST, > + /* Each occurrence of a duplicate option converts value to a QList= */ > + QEMU_OPTS_REPEAT_POLICY_ALL, > + /* First occurrence of a duplicate option causes an error */ > + QEMU_OPTS_REPEAT_POLICY_ERROR, > +} QemuOptsRepeatPolicy; > + Nicer. Thanks! > +++ b/tests/test-qemu-opts.c > @@ -421,6 +421,130 @@ static void test_qemu_opts_set(void) > g_assert(opts =3D=3D NULL); > } > =20 > +static void test_qemu_opts_to_qdict_repeat_all(void) > +{ > + QemuOpts *opts; > + QDict *dict; > + QList *list; > + const QListEntry *ent; > + QString *str; > + > + /* dynamically initialized (parsed) opts */ > + opts =3D qemu_opts_parse(&opts_list_03, > + "size=3D1024,nodes=3D10,nodes=3D4-5,nodes=3D= 1-2,policy=3Dbind", > + false, NULL); > + g_assert(opts); > + > + dict =3D qemu_opts_to_qdict(opts, NULL, QEMU_OPTS_REPEAT_POLICY_AL= L, > + &error_abort); > + g_assert(dict); > + > + g_assert_cmpstr(qdict_get_str(dict, "size"), =3D=3D, "1024"); > + g_assert(qdict_haskey(dict, "nodes")); > + list =3D qobject_to_qlist(qdict_get(dict, "nodes")); > + g_assert(list); > + > + ent =3D qlist_first(list); > + g_assert(ent); > + str =3D qobject_to_qstring(ent->value); > + g_assert(str); > + g_assert_cmpstr(qstring_get_str(str), =3D=3D, "10"); > + > + ent =3D qlist_next(ent); > + g_assert(ent); > + str =3D qobject_to_qstring(ent->value); > + g_assert(str); > + g_assert_cmpstr(qstring_get_str(str), =3D=3D, "4-5"); > + > + ent =3D qlist_next(ent); > + g_assert(ent); > + str =3D qobject_to_qstring(ent->value); > + g_assert(str); > + g_assert_cmpstr(qstring_get_str(str), =3D=3D, "1-2"); Yes, this is different from v13, but does look nicer with the value turning into QList instead of the key being rewritten into dotted form. > +++ b/util/qemu-option.c > @@ -1057,23 +1057,73 @@ void qemu_opts_absorb_qdict(QemuOpts *opts, QDi= ct *qdict, Error **errp) > * The QDict values are of type QString. > * TODO We'll want to use types appropriate for opt->desc->type, but > * this is enough for now. > + * > + * If the @opts contains multiple occurrences of the same key, > + * then the @repeatPolicy parameter determines how they are to > + * be handled. Traditional behaviour was to only store the > + * last occurrence, but if @repeatPolicy is set to > + * QEMU_OPTS_REPEAT_POLICY_ALL, then a QList will be returned > + * containing all values, for any key with multiple occurrences. > + * The QEMU_OPTS_REPEAT_POLICY_ERROR value can be used to fail > + * conversion with an error if multiple occurrences of a key > + * are seen. > */ > -QDict *qemu_opts_to_qdict(const QemuOpts *opts, QDict *qdict) > +QDict *qemu_opts_to_qdict(const QemuOpts *opts, QDict *qdict, > + QemuOptsRepeatPolicy repeatPolicy, > + Error **errp) Reviewed-by: Eric Blake --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --mGpauRdeUxQ35wRro08ksWkHv050IQXFi Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJX7sYpAAoJEKeha0olJ0NqXfoIAJGgt+fwMOnidzT+MM+vKs8D IC82VEW7wPOM42dT23k4E+geKWhcmAaZxMOVfAmDQJsQac4qVRIYILed3nmX9pvk tcyzlWDi6xAMtCsww2PFVGO6REjNMI7ZWVcZJJk2dfg78hfTkLCx930md2OVqKND AlEfv+a692cT7XEVA5L2tS6A5f+ZddowyFZjddZEZ2ZjY1zBnwpUsYnQjBYOZoMW OqHYugFPQmifxKgP16yDJT9XIPF9NxB4VJNbhD8ROwidNDDLN3qpBmozZl9QL5jy 0zjfw2LVA+MK+BmPCu7051VbuCTn2m2++j2SH/B560IxsRQp4oXjK9MD0avTgoQ= =H8J9 -----END PGP SIGNATURE----- --mGpauRdeUxQ35wRro08ksWkHv050IQXFi--