[libvirt] [PATCH 06/21] qemu: qapi: Modify values returned by virQEMUQAPISchemaPathGet

Peter Krempa pkrempa at redhat.com
Mon Apr 15 16:01:59 UTC 2019


Return 1 if the schema entry was found optionally returning it rather
than depending on the returned object.

Some callers don't care which schema object belongs to the query, but
rather only want to know whether it exists. Additionally this will allow
introducing boolean queries for checking if enum values exist.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_qapi.c        | 26 ++++++++++----------------
 tests/qemumonitorjsontest.c | 12 ++++++------
 2 files changed, 16 insertions(+), 22 deletions(-)

diff --git a/src/qemu/qemu_qapi.c b/src/qemu/qemu_qapi.c
index 7155f2d084..23cbac4405 100644
--- a/src/qemu/qemu_qapi.c
+++ b/src/qemu/qemu_qapi.c
@@ -118,8 +118,9 @@ virQEMUQAPISchemaTraverse(const char *baseName,
             return 0;

         if (!*query) {
-            *type = base;
-            return 0;
+            if (type)
+                *type = base;
+            return 1;
         }

         if (!(metatype = virJSONValueObjectGetString(base, "meta-type")))
@@ -175,7 +176,7 @@ virQEMUQAPISchemaTraverse(const char *baseName,
  * virQEMUQAPISchemaPathGet:
  * @query: string specifying the required data type (see below)
  * @schema: hash table containing the schema data
- * @entry: filled with the located schema object requested by @query
+ * @entry: filled with the located schema object requested by @query (optional)
  *
  * Retrieves the requested schema entry specified by @query to @entry. The
  * @query parameter has the following syntax which is very closely tied to the
@@ -201,8 +202,8 @@ virQEMUQAPISchemaTraverse(const char *baseName,
  * The above types can be chained arbitrarily using slashes to construct any
  * path into the schema tree.
  *
- * Returns 0 on success (including if the requested schema was not found) and
- * fills @entry appropriately. On failure returns -1 and sets an appropriate
+ * Returns 1 if @query was found in @schema filling @entry if non-NULL, 0 if
+ * @query was not found in @schema and -1 on other errors along with an appropriate
  * error message.
  */
 int
@@ -212,7 +213,8 @@ virQEMUQAPISchemaPathGet(const char *query,
 {
     VIR_AUTOSTRINGLIST elems = NULL;

-    *entry = NULL;
+    if (entry)
+        *entry = NULL;

     if (!(elems = virStringSplit(query, "/", 0)))
         return -1;
@@ -222,10 +224,7 @@ virQEMUQAPISchemaPathGet(const char *query,
         return -1;
     }

-    if (virQEMUQAPISchemaTraverse(elems[0], elems + 1, schema, entry) < 0)
-        return -1;
-
-    return 0;
+    return virQEMUQAPISchemaTraverse(elems[0], elems + 1, schema, entry);
 }


@@ -233,12 +232,7 @@ bool
 virQEMUQAPISchemaPathExists(const char *query,
                             virHashTablePtr schema)
 {
-    virJSONValuePtr entry;
-
-    if (virQEMUQAPISchemaPathGet(query, schema, &entry))
-        return false;
-
-    return !!entry;
+    return virQEMUQAPISchemaPathGet(query, schema, NULL) == 1;
 }

 static int
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index bcd7d37b03..339ee20b29 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -3087,12 +3087,12 @@ mymain(void)
             ret = -1; \
     } while (0)

-    DO_TEST_QAPI_QUERY("command", "blockdev-add", 0, true);
-    DO_TEST_QAPI_QUERY("event", "RTC_CHANGE", 0, true);
-    DO_TEST_QAPI_QUERY("object property", "screendump/arg-type/device", 0, true);
-    DO_TEST_QAPI_QUERY("optional property", "block-commit/arg-type/*top", 0, true);
-    DO_TEST_QAPI_QUERY("variant", "blockdev-add/arg-type/+file", 0, true);
-    DO_TEST_QAPI_QUERY("variant property", "blockdev-add/arg-type/+file/filename", 0, true);
+    DO_TEST_QAPI_QUERY("command", "blockdev-add", 1, true);
+    DO_TEST_QAPI_QUERY("event", "RTC_CHANGE", 1, true);
+    DO_TEST_QAPI_QUERY("object property", "screendump/arg-type/device", 1, true);
+    DO_TEST_QAPI_QUERY("optional property", "block-commit/arg-type/*top", 1, true);
+    DO_TEST_QAPI_QUERY("variant", "blockdev-add/arg-type/+file", 1, true);
+    DO_TEST_QAPI_QUERY("variant property", "blockdev-add/arg-type/+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);
-- 
2.20.1




More information about the libvir-list mailing list