[PATCH 03/11] util: json: Introduce helper to prepend string into a virJSONValueObject

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Libvirt treats the JSON objects as lists thus the values appear in the
order they were added. To avoid too much changes introduce a helper
which allows to prepend a string which will allow to keep certain
outputs in order.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/libvirt_private.syms |  1 +
 src/util/virjson.c       | 50 +++++++++++++++++++++++++++++++++-------
 src/util/virjson.h       |  1 +
 3 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index b8772d2895..d21f415f97 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2244,6 +2244,7 @@ virJSONValueObjectGetValue;
 virJSONValueObjectHasKey;
 virJSONValueObjectIsNull;
 virJSONValueObjectKeysNumber;
+virJSONValueObjectPrependString;
 virJSONValueObjectRemoveKey;
 virJSONValueObjectStealArray;
 virJSONValueObjectStealObject;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index d2664b9d57..1cf2fb1e5c 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -606,10 +606,11 @@ virJSONValueNewObject(void)
 }


-int
-virJSONValueObjectAppend(virJSONValuePtr object,
+static int
+virJSONValueObjectInsert(virJSONValuePtr object,
                          const char *key,
-                         virJSONValuePtr value)
+                         virJSONValuePtr value,
+                         bool prepend)
 {
     virJSONObjectPair pair = { NULL, value };
     int ret = -1;
@@ -628,8 +629,13 @@ virJSONValueObjectAppend(virJSONValuePtr object,
     if (VIR_STRDUP(pair.key, key) < 0)
         return -1;

-    ret = VIR_APPEND_ELEMENT(object->data.object.pairs,
-                             object->data.object.npairs, pair);
+    if (prepend) {
+        ret = VIR_INSERT_ELEMENT(object->data.object.pairs, 0,
+                                 object->data.object.npairs, pair);
+    } else {
+        ret = VIR_APPEND_ELEMENT(object->data.object.pairs,
+                                 object->data.object.npairs, pair);
+    }

     VIR_FREE(pair.key);
     return ret;
@@ -637,14 +643,24 @@ virJSONValueObjectAppend(virJSONValuePtr object,


 int
-virJSONValueObjectAppendString(virJSONValuePtr object,
+virJSONValueObjectAppend(virJSONValuePtr object,
+                         const char *key,
+                         virJSONValuePtr value)
+{
+    return virJSONValueObjectInsert(object, key, value, false);
+}
+
+
+static int
+virJSONValueObjectInsertString(virJSONValuePtr object,
                                const char *key,
-                               const char *value)
+                               const char *value,
+                               bool prepend)
 {
     virJSONValuePtr jvalue = virJSONValueNewString(value);
     if (!jvalue)
         return -1;
-    if (virJSONValueObjectAppend(object, key, jvalue) < 0) {
+    if (virJSONValueObjectInsert(object, key, jvalue, prepend) < 0) {
         virJSONValueFree(jvalue);
         return -1;
     }
@@ -652,6 +668,24 @@ virJSONValueObjectAppendString(virJSONValuePtr object,
 }


+int
+virJSONValueObjectAppendString(virJSONValuePtr object,
+                               const char *key,
+                               const char *value)
+{
+    return virJSONValueObjectInsertString(object, key, value, false);
+}
+
+
+int
+virJSONValueObjectPrependString(virJSONValuePtr object,
+                                const char *key,
+                                const char *value)
+{
+    return virJSONValueObjectInsertString(object, key, value, true);
+}
+
+
 int
 virJSONValueObjectAppendNumberInt(virJSONValuePtr object,
                                   const char *key,
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 41404a9a3e..a5f959595f 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -126,6 +126,7 @@ int virJSONValueObjectGetBoolean(virJSONValuePtr object, const char *key, bool *
 int virJSONValueObjectIsNull(virJSONValuePtr object, const char *key);

 int virJSONValueObjectAppendString(virJSONValuePtr object, const char *key, const char *value);
+int virJSONValueObjectPrependString(virJSONValuePtr object, const char *key, const char *value);
 int virJSONValueObjectAppendNumberInt(virJSONValuePtr object, const char *key, int number);
 int virJSONValueObjectAppendNumberUint(virJSONValuePtr object, const char *key, unsigned int number);
 int virJSONValueObjectAppendNumberLong(virJSONValuePtr object, const char *key, long long number);
-- 
2.21.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux