]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: json: Split out array->strinlist conversion from virJSONValueObjectGetStringArray
authorPeter Krempa <pkrempa@redhat.com>
Thu, 1 Dec 2022 12:32:07 +0000 (13:32 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 2 Dec 2022 15:18:37 +0000 (16:18 +0100)
Introduce virJSONValueArrayToStringList which does only the conversion
from an array to a stringlist.

This will allow refactoring the callers to be more careful in case when
they want to handle the existance of the member in the parent object
differently.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/libvirt_private.syms
src/util/virjson.c
src/util/virjson.h

index ebd7bc61a8d128ba54430d55c6032c5d8d38baea..a5bb44a9f8a64cba4b38ab4999bf80d663a3da1e 100644 (file)
@@ -2564,6 +2564,7 @@ virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
 virJSONValueArraySize;
 virJSONValueArraySteal;
+virJSONValueArrayToStringList;
 virJSONValueCopy;
 virJSONValueFree;
 virJSONValueFromString;
index ef59b5deb4b554579d35b637387f16bab9b7d70c..5701c09bfdc119c4ab691838109c897ba5aabf08 100644 (file)
@@ -1316,10 +1316,7 @@ virJSONValueObjectStealObject(virJSONValue *object,
 char **
 virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
 {
-    g_auto(GStrv) ret = NULL;
     virJSONValue *data;
-    size_t n;
-    size_t i;
 
     data = virJSONValueObjectGetArray(object, key);
     if (!data) {
@@ -1329,32 +1326,40 @@ virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
         return NULL;
     }
 
-    n = virJSONValueArraySize(data);
-    ret = g_new0(char *, n + 1);
+    return virJSONValueArrayToStringList(data);
+}
+
+
+/**
+ * virJSONValueArrayToStringList:
+ * @data: a JSON array containing strings to convert
+ *
+ * Converts @data a JSON array containing strings to a NULL-terminated string
+ * list. @data must be a JSON array. In case @data is doesn't contain only
+ * strings an error is reported.
+ */
+char **
+virJSONValueArrayToStringList(virJSONValue *data)
+{
+    size_t n = virJSONValueArraySize(data);
+    g_auto(GStrv) ret = g_new0(char *, n + 1);
+    size_t i;
+
     for (i = 0; i < n; i++) {
         virJSONValue *child = virJSONValueArrayGet(data, i);
-        const char *tmp;
 
-        if (!child) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("%s array element is missing item %zu"),
-                           key, i);
+        if (!child ||
+            !(ret[i] = g_strdup(virJSONValueGetString(child)))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("JSON string array contains non-string element"));
             return NULL;
         }
-
-        if (!(tmp = virJSONValueGetString(child))) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           _("%s array element does not contain a string"),
-                           key);
-            return NULL;
-        }
-
-        ret[i] = g_strdup(tmp);
     }
 
     return g_steal_pointer(&ret);
 }
 
+
 /**
  * virJSONValueObjectForeachKeyValue:
  * @object: JSON object to iterate
index ce181dcb820bea23bebd893579ed6576d0306eee..49a89e0910b6219eab0c26f9184b3a38b8a3ba03 100644 (file)
@@ -172,6 +172,8 @@ virJSONValueObjectGetString(virJSONValue *object,
 char **
 virJSONValueObjectGetStringArray(virJSONValue *object,
                                  const char *key);
+char **
+virJSONValueArrayToStringList(virJSONValue *data);
 const char *
 virJSONValueObjectGetStringOrNumber(virJSONValue *object,
                                     const char *key);