From 67c2ddf8a6213b022ca303dd8d10ff7f26dde860 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 11 Apr 2019 14:54:41 +0200 Subject: [PATCH] qemu: qapi: Implement worker for introspecting alternate types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/qemu/qemu_qapi.c | 34 ++++++++++++++++++++++++++++++++-- tests/qemumonitorjsontest.c | 3 +++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/qemu/qemu_qapi.c b/src/qemu/qemu_qapi.c index d401e9a303..4ed67b68bc 100644 --- a/src/qemu/qemu_qapi.c +++ b/src/qemu/qemu_qapi.c @@ -228,6 +228,35 @@ virQEMUQAPISchemaTraverseBuiltin(virJSONValuePtr cur, } +static int +virQEMUQAPISchemaTraverseAlternate(virJSONValuePtr cur, + struct virQEMUQAPISchemaTraverseContext *ctxt) +{ + struct virQEMUQAPISchemaTraverseContext savectxt = *ctxt; + virJSONValuePtr members; + virJSONValuePtr member; + const char *membertype; + int rc; + size_t i; + + if (!(members = virJSONValueObjectGetArray(cur, "members"))) + return -2; + + for (i = 0; i < virJSONValueArraySize(members); i++) { + if (!(member = virJSONValueArrayGet(members, i)) || + !(membertype = virJSONValueObjectGetString(member, "type"))) + continue; + + *ctxt = savectxt; + + if ((rc = virQEMUQAPISchemaTraverse(membertype, ctxt)) != 0) + return rc; + } + + return 0; +} + + /* The function must return 1 on successful query, 0 if the query was not found * -1 when a libvirt error is reported, -2 if the schema is invalid and -3 if * the query component is malformed. */ @@ -247,6 +276,7 @@ static const struct virQEMUQAPISchemaTraverseMetaType traverseMetaType[] = { { "event", virQEMUQAPISchemaTraverseCommand }, { "enum", virQEMUQAPISchemaTraverseEnum }, { "builtin", virQEMUQAPISchemaTraverseBuiltin }, + { "alternate", virQEMUQAPISchemaTraverseAlternate }, }; @@ -313,8 +343,8 @@ virQEMUQAPISchemaTraverse(const char *baseName, * If the name of any (sub)attribute starts with non-alphabetical symbols it * needs to be prefixed by a single space. * - * Array types are automatically flattened to the singular type. Alternate - * types are currently not supported. + * Array types are automatically flattened to the singular type. Alternates are + * iterated until first success. * * The above types can be chained arbitrarily using slashes to construct any * path into the schema tree, booleans must be always the last component as they diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index aebde20a5f..923f6d7d9c 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -3093,6 +3093,9 @@ mymain(void) DO_TEST_QAPI_QUERY("variant property", "blockdev-add/arg-type/+file/filename", 1, true); DO_TEST_QAPI_QUERY("enum value", "query-status/ret-type/status/^debug", 1, false); DO_TEST_QAPI_QUERY("builtin type", "query-qmp-schema/ret-type/name/!string", 1, false); + DO_TEST_QAPI_QUERY("alternate variant 1", "blockdev-add/arg-type/+qcow2/backing/!null", 1, false); + DO_TEST_QAPI_QUERY("alternate variant 2", "blockdev-add/arg-type/+qcow2/backing/!string", 1, false); + DO_TEST_QAPI_QUERY("alternate variant 3", "blockdev-add/arg-type/+qcow2/backing/+file/filename", 1, true); DO_TEST_QAPI_QUERY("nonexistent command", "nonexistent", 0, false); DO_TEST_QAPI_QUERY("nonexistent attr", "screendump/arg-type/nonexistent", 0, false);