render / rpms / libvirt

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