[PATCH 1/5] util: json: Introduce virJSONValueObjectReplaceValue

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

 



The new helper replaces the 'value' part of the key-value tuple in an
object. The advantage of this new helper is that it preserves the
ordering of the key in the object when compared to a combination of
stealing the old key and adding a new value. This will be needed for a
new test/helper for validating and modifying qemu capabilities data.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 435ee8054c..431075899d 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2567,6 +2567,7 @@ virJSONValueObjectHasKey;
 virJSONValueObjectKeysNumber;
 virJSONValueObjectPrependString;
 virJSONValueObjectRemoveKey;
+virJSONValueObjectReplaceValue;
 virJSONValueObjectStealArray;
 virJSONValueObjectStealObject;
 virJSONValueToBuffer;
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 1c6fef22da..6e13e97e15 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1149,6 +1149,26 @@ virJSONValueObjectGetString(virJSONValue *object,
 }


+void
+virJSONValueObjectReplaceValue(virJSONValue *object,
+                               const char *key,
+                               virJSONValue **newval)
+{
+    size_t i;
+
+    if (object->type != VIR_JSON_TYPE_OBJECT ||
+        !*newval)
+        return;
+
+    for (i = 0; i < object->data.object.npairs; i++) {
+        if (STREQ(object->data.object.pairs[i].key, key)) {
+            virJSONValueFree(object->data.object.pairs[i].value);
+            object->data.object.pairs[i].value = g_steal_pointer(newval);
+        }
+    }
+}
+
+
 /**
  * virJSONValueObjectGetStringOrNumber:
  * @object: JSON value object
diff --git a/src/util/virjson.h b/src/util/virjson.h
index f0b8c419de..aced48a538 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -248,6 +248,12 @@ virJSONValueObjectRemoveKey(virJSONValue *object,
                             virJSONValue **value)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

+void
+virJSONValueObjectReplaceValue(virJSONValue *object,
+                               const char *key,
+                               virJSONValue **newval)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+
 int
 virJSONValueArrayAppendString(virJSONValue *object,
                               const char *value);
-- 
2.31.1




[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