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