fbe740
From 28699a44646cf4a754808fb2c815c741343264cd Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <28699a44646cf4a754808fb2c815c741343264cd@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Tue, 4 Feb 2020 15:08:17 +0100
fbe740
Subject: [PATCH] util: json: Introduce virJSONValueArrayConcat
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Add a helper that concatenates the second array into the first.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
(cherry picked from commit 3b60a0c0276ae7200b0bff7394e0d1e00706a1dd)
fbe740
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1207659
fbe740
Message-Id: <d7f92bced2f95d7802dd3a823b7c6b966e454a7e.1580824112.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 src/libvirt_private.syms |  1 +
fbe740
 src/util/virjson.c       | 31 +++++++++++++++++++++++++++++++
fbe740
 src/util/virjson.h       |  2 ++
fbe740
 3 files changed, 34 insertions(+)
fbe740
fbe740
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
fbe740
index afa7d4fcae..bc2858fc00 100644
fbe740
--- a/src/libvirt_private.syms
fbe740
+++ b/src/libvirt_private.syms
fbe740
@@ -2279,6 +2279,7 @@ virISCSIScanTargets;
fbe740
 virJSONStringReformat;
fbe740
 virJSONValueArrayAppend;
fbe740
 virJSONValueArrayAppendString;
fbe740
+virJSONValueArrayConcat;
fbe740
 virJSONValueArrayForeachSteal;
fbe740
 virJSONValueArrayGet;
fbe740
 virJSONValueArraySize;
fbe740
diff --git a/src/util/virjson.c b/src/util/virjson.c
fbe740
index 988a09e956..50993648eb 100644
fbe740
--- a/src/util/virjson.c
fbe740
+++ b/src/util/virjson.c
fbe740
@@ -811,6 +811,37 @@ virJSONValueArrayAppendString(virJSONValuePtr object,
fbe740
 }
fbe740
 
fbe740
 
fbe740
+/**
fbe740
+ * virJSONValueArrayConcat:
fbe740
+ * @a: JSON value array (destination)
fbe740
+ * @c: JSON value array (source)
fbe740
+ *
fbe740
+ * Merges the members of @c array into @a. The values are stolen from @c.
fbe740
+ */
fbe740
+int
fbe740
+virJSONValueArrayConcat(virJSONValuePtr a,
fbe740
+                        virJSONValuePtr c)
fbe740
+{
fbe740
+    size_t i;
fbe740
+
fbe740
+    if (a->type != VIR_JSON_TYPE_ARRAY ||
fbe740
+        c->type != VIR_JSON_TYPE_ARRAY) {
fbe740
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array"));
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    a->data.array.values = g_renew(virJSONValuePtr, a->data.array.values,
fbe740
+                                   a->data.array.nvalues + c->data.array.nvalues);
fbe740
+
fbe740
+    for (i = 0; i < c->data.array.nvalues; i++)
fbe740
+        a->data.array.values[a->data.array.nvalues++] = g_steal_pointer(&c->data.array.values[i]);
fbe740
+
fbe740
+    c->data.array.nvalues = 0;
fbe740
+
fbe740
+    return 0;
fbe740
+}
fbe740
+
fbe740
+
fbe740
 int
fbe740
 virJSONValueObjectHasKey(virJSONValuePtr object,
fbe740
                          const char *key)
fbe740
diff --git a/src/util/virjson.h b/src/util/virjson.h
fbe740
index 7a6b063b17..0894e91b59 100644
fbe740
--- a/src/util/virjson.h
fbe740
+++ b/src/util/virjson.h
fbe740
@@ -71,6 +71,8 @@ virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
fbe740
 
fbe740
 int virJSONValueObjectAppend(virJSONValuePtr object, const char *key, virJSONValuePtr value);
fbe740
 int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
fbe740
+int virJSONValueArrayConcat(virJSONValuePtr a,
fbe740
+                            virJSONValuePtr c);
fbe740
 
fbe740
 int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key);
fbe740
 virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key);
fbe740
-- 
fbe740
2.25.0
fbe740