]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: qapi: Modify values returned by virQEMUQAPISchemaPathGet
authorPeter Krempa <pkrempa@redhat.com>
Tue, 9 Apr 2019 12:09:33 +0000 (14:09 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 6 May 2019 07:46:06 +0000 (09:46 +0200)
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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/qemu/qemu_qapi.c
tests/qemumonitorjsontest.c

index 7155f2d084d54dc1bdcb6837e11e874b57ac8ddb..23cbac4405f4e080ce32e96176f256f88c826053 100644 (file)
@@ -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
index 41092e199ce1dea09dfe286fda69d2fa677a8f73..b5937349affe9f6c124b6b755435832eeec82b86 100644 (file)
@@ -3085,12 +3085,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);