]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
json-util: Add JSON_BUILD_STRING_ORDERED_SET()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 7 Aug 2024 07:45:44 +0000 (09:45 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 3 Sep 2024 10:03:02 +0000 (12:03 +0200)
src/libsystemd/sd-json/json-util.h
src/libsystemd/sd-json/sd-json.c
src/test/test-json.c

index fdfd5522db01ffe00b46126e24ac630ebe3b42ea..b6832bd59a22668cf64c35837e08de59c0814f07 100644 (file)
@@ -138,6 +138,7 @@ enum {
         _JSON_BUILD_IOVEC_HEX,
         _JSON_BUILD_HW_ADDR,
         _JSON_BUILD_STRING_SET,
+        _JSON_BUILD_STRING_ORDERED_SET,
         _JSON_BUILD_STRING_UNDERSCORIFY,
         _JSON_BUILD_DUAL_TIMESTAMP,
 
@@ -166,6 +167,7 @@ enum {
 #define JSON_BUILD_ETHER_ADDR(v) SD_JSON_BUILD_BYTE_ARRAY(((const struct ether_addr*) { v })->ether_addr_octet, sizeof(struct ether_addr))
 #define JSON_BUILD_HW_ADDR(v) _JSON_BUILD_HW_ADDR, (const struct hw_addr_data*) { v }
 #define JSON_BUILD_STRING_SET(s) _JSON_BUILD_STRING_SET, (Set *) { s }
+#define JSON_BUILD_STRING_ORDERED_SET(s) _JSON_BUILD_STRING_ORDERED_SET, (OrderedSet *) { s }
 #define JSON_BUILD_STRING_UNDERSCORIFY(s) _JSON_BUILD_STRING_UNDERSCORIFY, (const char *) { s }
 #define JSON_BUILD_DUAL_TIMESTAMP(t) _JSON_BUILD_DUAL_TIMESTAMP, (dual_timestamp*) { t }
 
@@ -188,3 +190,4 @@ enum {
 #define JSON_BUILD_PAIR_ETHER_ADDR(name, v) SD_JSON_BUILD_PAIR(name, JSON_BUILD_ETHER_ADDR(v))
 #define JSON_BUILD_PAIR_HW_ADDR(name, v) SD_JSON_BUILD_PAIR(name, JSON_BUILD_HW_ADDR(v))
 #define JSON_BUILD_PAIR_STRING_SET(name, s) SD_JSON_BUILD_PAIR(name, JSON_BUILD_STRING_SET(s))
+#define JSON_BUILD_PAIR_STRING_ORDERED_SET(name, s) SD_JSON_BUILD_PAIR(name, JSON_BUILD_STRING_ORDERED_SET(s))
index 589bbe3a46ebca7dd700337f4e84834e21b0ab98..6391da6573738af683a6b3a6a3fefc66825b3325 100644 (file)
@@ -23,6 +23,7 @@
 #include "iovec-util.h"
 #include "json-internal.h"
 #include "json-util.h"
+#include "ordered-set.h"
 #include "macro.h"
 #include "math-util.h"
 #include "memory-util.h"
@@ -4089,6 +4090,42 @@ _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) {
                         break;
                 }
 
+                case _JSON_BUILD_STRING_ORDERED_SET: {
+                        OrderedSet *set;
+
+                        if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) {
+                                r = -EINVAL;
+                                goto finish;
+                        }
+
+                        set = va_arg(ap, OrderedSet*);
+
+                        if (current->n_suppress == 0) {
+                                _cleanup_free_ char **sv = NULL;
+
+                                sv = ordered_set_get_strv(set);
+                                if (!sv) {
+                                        r = -ENOMEM;
+                                        goto finish;
+                                }
+
+                                r = sd_json_variant_new_array_strv(&add, sv);
+                                if (r < 0)
+                                        goto finish;
+                        }
+
+                        n_subtract = 1;
+
+                        if (current->expect == EXPECT_TOPLEVEL)
+                                current->expect = EXPECT_END;
+                        else if (current->expect == EXPECT_OBJECT_VALUE)
+                                current->expect = EXPECT_OBJECT_KEY;
+                        else
+                                assert(current->expect == EXPECT_ARRAY_ELEMENT);
+
+                        break;
+                }
+
                 case _JSON_BUILD_DUAL_TIMESTAMP: {
                         dual_timestamp *ts;
 
index aea4bbaf3efb84cbdaa5963916d3413aa6a4467f..f9dc9e74c7baddc6a7e1855815cf911332818c49 100644 (file)
@@ -12,6 +12,7 @@
 #include "json-internal.h"
 #include "json-util.h"
 #include "math-util.h"
+#include "ordered-set.h"
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
@@ -414,6 +415,19 @@ TEST(build) {
         assert_se(sd_json_build(&ssv2, SD_JSON_BUILD_LITERAL("{\"zzz\":[\"kawumm\",\"pief\",\"xxxx\"]}")) >= 0);
 
         assert_se(sd_json_variant_equal(ssv, ssv2));
+
+        _cleanup_ordered_set_free_ OrderedSet *oss = NULL;
+        assert_se(ordered_set_ensure_put(&oss, &string_hash_ops_free, ASSERT_PTR(strdup("pief"))) >= 0);
+        assert_se(ordered_set_ensure_put(&oss, &string_hash_ops_free, ASSERT_PTR(strdup("xxxx"))) >= 0);
+        assert_se(ordered_set_ensure_put(&oss, &string_hash_ops_free, ASSERT_PTR(strdup("kawumm"))) >= 0);
+
+        _cleanup_(sd_json_variant_unrefp) sd_json_variant *ossv = NULL;
+        assert_se(sd_json_build(&ossv, SD_JSON_BUILD_OBJECT(SD_JSON_BUILD_PAIR("zzz", JSON_BUILD_STRING_ORDERED_SET(oss)))) >= 0);
+
+        _cleanup_(sd_json_variant_unrefp) sd_json_variant *ossv2 = NULL;
+        assert_se(sd_json_build(&ossv2, SD_JSON_BUILD_LITERAL("{\"zzz\":[\"pief\",\"xxxx\",\"kawumm\"]}")) >= 0);
+
+        assert_se(sd_json_variant_equal(ossv, ossv2));
 }
 
 TEST(json_buildo) {