1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-09 01:18:19 +03:00

json-util: Add JSON_BUILD_STRING_ORDERED_SET()

This commit is contained in:
Daan De Meyer 2024-08-07 09:45:44 +02:00
parent 02e875e1c2
commit ed207e5261
3 changed files with 54 additions and 0 deletions

View 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))

View 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;

View 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) {