]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: json: Introduce virJSONValueObjectReplaceValue
authorPeter Krempa <pkrempa@redhat.com>
Mon, 20 Dec 2021 14:55:26 +0000 (15:55 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 3 Jan 2022 12:14:43 +0000 (13:14 +0100)
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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/libvirt_private.syms
src/util/virjson.c
src/util/virjson.h

index 435ee8054c8226cc0cea530fbb6c660c172be250..431075899d14fa6611fce1653d59b847c265f3cb 100644 (file)
@@ -2567,6 +2567,7 @@ virJSONValueObjectHasKey;
 virJSONValueObjectKeysNumber;
 virJSONValueObjectPrependString;
 virJSONValueObjectRemoveKey;
+virJSONValueObjectReplaceValue;
 virJSONValueObjectStealArray;
 virJSONValueObjectStealObject;
 virJSONValueToBuffer;
index 1c6fef22daf6334819b2a134177df37154cc58b7..6e13e97e15590859eb8a362249c322c64f87088c 100644 (file)
@@ -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
index f0b8c419de26feb82bffbb9a84eef91467fca83c..aced48a5380fccabb86d6cbe29ff43540bda87a4 100644 (file)
@@ -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);