Blob Blame History Raw
From 28699a44646cf4a754808fb2c815c741343264cd Mon Sep 17 00:00:00 2001
Message-Id: <28699a44646cf4a754808fb2c815c741343264cd@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Tue, 4 Feb 2020 15:08:17 +0100
Subject: [PATCH] util: json: Introduce virJSONValueArrayConcat
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add a helper that concatenates the second array into the first.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit 3b60a0c0276ae7200b0bff7394e0d1e00706a1dd)

https://bugzilla.redhat.com/show_bug.cgi?id=1207659
Message-Id: <d7f92bced2f95d7802dd3a823b7c6b966e454a7e.1580824112.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 31 +++++++++++++++++++++++++++++++
 src/util/virjson.h       |  2 ++
 3 files changed, 34 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index afa7d4fcae..bc2858fc00 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2279,6 +2279,7 @@ virISCSIScanTargets;
 virJSONStringReformat;
 virJSONValueArrayAppend;
 virJSONValueArrayAppendString;
+virJSONValueArrayConcat;
 virJSONValueArrayForeachSteal;
 virJSONValueArrayGet;
 virJSONValueArraySize;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 988a09e956..50993648eb 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -811,6 +811,37 @@ virJSONValueArrayAppendString(virJSONValuePtr object,
 }
 
 
+/**
+ * virJSONValueArrayConcat:
+ * @a: JSON value array (destination)
+ * @c: JSON value array (source)
+ *
+ * Merges the members of @c array into @a. The values are stolen from @c.
+ */
+int
+virJSONValueArrayConcat(virJSONValuePtr a,
+                        virJSONValuePtr c)
+{
+    size_t i;
+
+    if (a->type != VIR_JSON_TYPE_ARRAY ||
+        c->type != VIR_JSON_TYPE_ARRAY) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array"));
+        return -1;
+    }
+
+    a->data.array.values = g_renew(virJSONValuePtr, a->data.array.values,
+                                   a->data.array.nvalues + c->data.array.nvalues);
+
+    for (i = 0; i < c->data.array.nvalues; i++)
+        a->data.array.values[a->data.array.nvalues++] = g_steal_pointer(&c->data.array.values[i]);
+
+    c->data.array.nvalues = 0;
+
+    return 0;
+}
+
+
 int
 virJSONValueObjectHasKey(virJSONValuePtr object,
                          const char *key)
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 7a6b063b17..0894e91b59 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -71,6 +71,8 @@ virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
 
 int virJSONValueObjectAppend(virJSONValuePtr object, const char *key, virJSONValuePtr value);
 int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
+int virJSONValueArrayConcat(virJSONValuePtr a,
+                            virJSONValuePtr c);
 
 int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key);
 virJSONValuePtr virJSONValueObjectGet(virJSONValuePtr object, const char *key);
-- 
2.25.0