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