mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-20 07:59:00 +00:00
util: json: Split out array->strinlist conversion from virJSONValueObjectGetStringArray
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>
This commit is contained in:
parent
962ce78175
commit
6765bdeaf7
@ -2564,6 +2564,7 @@ virJSONValueArrayForeachSteal;
|
|||||||
virJSONValueArrayGet;
|
virJSONValueArrayGet;
|
||||||
virJSONValueArraySize;
|
virJSONValueArraySize;
|
||||||
virJSONValueArraySteal;
|
virJSONValueArraySteal;
|
||||||
|
virJSONValueArrayToStringList;
|
||||||
virJSONValueCopy;
|
virJSONValueCopy;
|
||||||
virJSONValueFree;
|
virJSONValueFree;
|
||||||
virJSONValueFromString;
|
virJSONValueFromString;
|
||||||
|
@ -1316,10 +1316,7 @@ virJSONValueObjectStealObject(virJSONValue *object,
|
|||||||
char **
|
char **
|
||||||
virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
|
virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
|
||||||
{
|
{
|
||||||
g_auto(GStrv) ret = NULL;
|
|
||||||
virJSONValue *data;
|
virJSONValue *data;
|
||||||
size_t n;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
data = virJSONValueObjectGetArray(object, key);
|
data = virJSONValueObjectGetArray(object, key);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
@ -1329,32 +1326,40 @@ virJSONValueObjectGetStringArray(virJSONValue *object, const char *key)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
n = virJSONValueArraySize(data);
|
return virJSONValueArrayToStringList(data);
|
||||||
ret = g_new0(char *, n + 1);
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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++) {
|
for (i = 0; i < n; i++) {
|
||||||
virJSONValue *child = virJSONValueArrayGet(data, i);
|
virJSONValue *child = virJSONValueArrayGet(data, i);
|
||||||
const char *tmp;
|
|
||||||
|
|
||||||
if (!child) {
|
if (!child ||
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
!(ret[i] = g_strdup(virJSONValueGetString(child)))) {
|
||||||
_("%s array element is missing item %zu"),
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
||||||
key, i);
|
_("JSON string array contains non-string element"));
|
||||||
return NULL;
|
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);
|
return g_steal_pointer(&ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virJSONValueObjectForeachKeyValue:
|
* virJSONValueObjectForeachKeyValue:
|
||||||
* @object: JSON object to iterate
|
* @object: JSON object to iterate
|
||||||
|
@ -172,6 +172,8 @@ virJSONValueObjectGetString(virJSONValue *object,
|
|||||||
char **
|
char **
|
||||||
virJSONValueObjectGetStringArray(virJSONValue *object,
|
virJSONValueObjectGetStringArray(virJSONValue *object,
|
||||||
const char *key);
|
const char *key);
|
||||||
|
char **
|
||||||
|
virJSONValueArrayToStringList(virJSONValue *data);
|
||||||
const char *
|
const char *
|
||||||
virJSONValueObjectGetStringOrNumber(virJSONValue *object,
|
virJSONValueObjectGetStringOrNumber(virJSONValue *object,
|
||||||
const char *key);
|
const char *key);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user