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