render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
dc2b6e
From b7d9527c9d9cc782933a5b852869cbd10e370a3a Mon Sep 17 00:00:00 2001
dc2b6e
Message-Id: <b7d9527c9d9cc782933a5b852869cbd10e370a3a@dist-git>
dc2b6e
From: Peter Krempa <pkrempa@redhat.com>
dc2b6e
Date: Thu, 1 Dec 2022 13:32:07 +0100
dc2b6e
Subject: [PATCH] util: json: Split out array->strinlist conversion from
dc2b6e
 virJSONValueObjectGetStringArray
dc2b6e
dc2b6e
Introduce virJSONValueArrayToStringList which does only the conversion
dc2b6e
from an array to a stringlist.
dc2b6e
dc2b6e
This will allow refactoring the callers to be more careful in case when
dc2b6e
they want to handle the existance of the member in the parent object
dc2b6e
differently.
dc2b6e
dc2b6e
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
dc2b6e
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
dc2b6e
(cherry picked from commit 6765bdeaf7e9cbdb4c39d47f3b77fb28a498408a)
dc2b6e
https://bugzilla.redhat.com/show_bug.cgi?id=2149752
dc2b6e
---
dc2b6e
 src/libvirt_private.syms |  1 +
dc2b6e
 src/util/virjson.c       | 43 ++++++++++++++++++++++------------------
dc2b6e
 src/util/virjson.h       |  2 ++
dc2b6e
 3 files changed, 27 insertions(+), 19 deletions(-)
dc2b6e
dc2b6e
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
dc2b6e
index 5b7a056151..fa734dfd33 100644
dc2b6e
--- a/src/libvirt_private.syms
dc2b6e
+++ b/src/libvirt_private.syms
dc2b6e
@@ -2513,6 +2513,7 @@ virJSONValueArrayForeachSteal;
dc2b6e
 virJSONValueArrayGet;
dc2b6e
 virJSONValueArraySize;
dc2b6e
 virJSONValueArraySteal;
dc2b6e
+virJSONValueArrayToStringList;
dc2b6e
 virJSONValueCopy;
dc2b6e
 virJSONValueFree;
dc2b6e
 virJSONValueFromString;
dc2b6e
diff --git a/src/util/virjson.c b/src/util/virjson.c
dc2b6e
index 6e13e97e15..5f1565107d 100644
dc2b6e
--- a/src/util/virjson.c
dc2b6e
+++ b/src/util/virjson.c
dc2b6e
@@ -1312,10 +1312,7 @@ virJSONValueObjectStealObject(virJSONValue *object,
dc2b6e
 char **
dc2b6e
 virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
dc2b6e
 {
dc2b6e
-    g_auto(GStrv) ret = NULL;
dc2b6e
     virJSONValue *data;
dc2b6e
-    size_t n;
dc2b6e
-    size_t i;
dc2b6e
 
dc2b6e
     data = virJSONValueObjectGetArray(object, key);
dc2b6e
     if (!data) {
dc2b6e
@@ -1325,32 +1322,40 @@ virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
dc2b6e
         return NULL;
dc2b6e
     }
dc2b6e
 
dc2b6e
-    n = virJSONValueArraySize(data);
dc2b6e
-    ret = g_new0(char *, n + 1);
dc2b6e
+    return virJSONValueArrayToStringList(data);
dc2b6e
+}
dc2b6e
+
dc2b6e
+
dc2b6e
+/**
dc2b6e
+ * virJSONValueArrayToStringList:
dc2b6e
+ * @data: a JSON array containing strings to convert
dc2b6e
+ *
dc2b6e
+ * Converts @data a JSON array containing strings to a NULL-terminated string
dc2b6e
+ * list. @data must be a JSON array. In case @data is doesn't contain only
dc2b6e
+ * strings an error is reported.
dc2b6e
+ */
dc2b6e
+char **
dc2b6e
+virJSONValueArrayToStringList(virJSONValue *data)
dc2b6e
+{
dc2b6e
+    size_t n = virJSONValueArraySize(data);
dc2b6e
+    g_auto(GStrv) ret = g_new0(char *, n + 1);
dc2b6e
+    size_t i;
dc2b6e
+
dc2b6e
     for (i = 0; i < n; i++) {
dc2b6e
         virJSONValue *child = virJSONValueArrayGet(data, i);
dc2b6e
-        const char *tmp;
dc2b6e
 
dc2b6e
-        if (!child) {
dc2b6e
-            virReportError(VIR_ERR_INTERNAL_ERROR,
dc2b6e
-                           _("%s array element is missing item %zu"),
dc2b6e
-                           key, i);
dc2b6e
+        if (!child ||
dc2b6e
+            !(ret[i] = g_strdup(virJSONValueGetString(child)))) {
dc2b6e
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
dc2b6e
+                           _("JSON string array contains non-string element"));
dc2b6e
             return NULL;
dc2b6e
         }
dc2b6e
-
dc2b6e
-        if (!(tmp = virJSONValueGetString(child))) {
dc2b6e
-            virReportError(VIR_ERR_INTERNAL_ERROR,
dc2b6e
-                           _("%s array element does not contain a string"),
dc2b6e
-                           key);
dc2b6e
-            return NULL;
dc2b6e
-        }
dc2b6e
-
dc2b6e
-        ret[i] = g_strdup(tmp);
dc2b6e
     }
dc2b6e
 
dc2b6e
     return g_steal_pointer(&ret;;
dc2b6e
 }
dc2b6e
 
dc2b6e
+
dc2b6e
 /**
dc2b6e
  * virJSONValueObjectForeachKeyValue:
dc2b6e
  * @object: JSON object to iterate
dc2b6e
diff --git a/src/util/virjson.h b/src/util/virjson.h
dc2b6e
index aced48a538..c9f83ab2bc 100644
dc2b6e
--- a/src/util/virjson.h
dc2b6e
+++ b/src/util/virjson.h
dc2b6e
@@ -172,6 +172,8 @@ virJSONValueObjectGetString(virJSONValue *object,
dc2b6e
 char **
dc2b6e
 virJSONValueObjectGetStringArray(virJSONValue *object,
dc2b6e
                                  const char *key);
dc2b6e
+char **
dc2b6e
+virJSONValueArrayToStringList(virJSONValue *data);
dc2b6e
 const char *
dc2b6e
 virJSONValueObjectGetStringOrNumber(virJSONValue *object,
dc2b6e
                                     const char *key);
dc2b6e
-- 
dc2b6e
2.39.0
dc2b6e