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

Peter Krempa pkrempa at redhat.com
Wed Dec 22 14:24:06 UTC 2021


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 at redhat.com>
---
 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




More information about the libvir-list mailing list