]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
json: rename json_append() → json_variant_merge_objectb()
authorLennart Poettering <lennart@poettering.net>
Thu, 24 Aug 2023 07:41:48 +0000 (09:41 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 24 Aug 2023 11:20:39 +0000 (13:20 +0200)
json_append() is a useful wrapper around json_variant_merge(). However,
I think the naming sould be cleaned up a bit of both functions.

I thinker "merge" is the better word than "append", since it does
decidedly more than just append: it replaces existing fields of the same
name, hence "merge" sounds more appropriate. This is as opposed to the
similar operations for arrays, where no such override logic is applied
and we really just append, hence those functions are called "append"
already.

To make clearer that "merge" is about objects, and "append" about
arrays, also include "object" in the name.

Also, include "json_variant" in the name, like we do for almost all
other functions in the JSON API that take a JSON object as primary
input, and hence are kinda object methods.

Finally, let's follow the logic that helpers that combine json_build()
with some other operation get suffixed with "b" like we already have in
some cases.

Hence:

json_variant_merge() → json_variant_merge_object()
       json_append() → json_variant_merge_objectb()

This mirrors nicely the existing:
                       json_variant_append_array()
                       json_vairant_append_arrayb()

This also drops the variant of json_append() that takes a va_arg
parameter (i.e. json_appendv()). We have no user of that so far, and
given the nature as a helper function only I don#t see that happening,
and if it happens after all it's trivial to bring back.

src/fuzz/fuzz-json.c
src/home/homectl.c
src/home/homed-home.c
src/home/user-record-util.c
src/network/networkd-json.c
src/shared/bootspec.c
src/shared/elf-util.c
src/shared/json.c
src/shared/json.h
src/shared/userdb-dropin.c
src/test/test-json.c

index b2cca785015ad03a5d5e984e52649a4101a06efd..c83ec833281a60b28ee2a8e7b81f777182667577 100644 (file)
@@ -107,7 +107,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
         log_debug_errno(r, "json_variant_filter: %d/%m", r);
 
         /* I assume we can merge v with itself… */
-        r = json_variant_merge(&v, v);
+        r = json_variant_merge_object(&v, v);
         log_debug_errno(r, "json_variant_merge: %d/%m", r);
 
         r = json_variant_append_array(&v, v);
index 55323b44e10c121eff61b355c9ca0784d734c35a..b12114519708999b78969532918f0e8db20d2e52 100644 (file)
@@ -903,7 +903,7 @@ static int apply_identity_changes(JsonVariant **_v) {
         if (r < 0)
                 return log_error_errno(r, "Failed to filter identity: %m");
 
-        r = json_variant_merge(&v, arg_identity_extra);
+        r = json_variant_merge_object(&v, arg_identity_extra);
         if (r < 0)
                 return log_error_errno(r, "Failed to merge identities: %m");
 
@@ -948,7 +948,7 @@ static int apply_identity_changes(JsonVariant **_v) {
                                 if (!json_variant_equal(u, mmid))
                                         continue;
 
-                                r = json_variant_merge(&add, z);
+                                r = json_variant_merge_object(&add, z);
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to merge perMachine entry: %m");
 
@@ -959,7 +959,7 @@ static int apply_identity_changes(JsonVariant **_v) {
                         if (r < 0)
                                 return log_error_errno(r, "Failed to filter perMachine: %m");
 
-                        r = json_variant_merge(&add, arg_identity_extra_this_machine);
+                        r = json_variant_merge_object(&add, arg_identity_extra_this_machine);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to merge in perMachine fields: %m");
 
@@ -972,7 +972,7 @@ static int apply_identity_changes(JsonVariant **_v) {
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to filter resource limits: %m");
 
-                                r = json_variant_merge(&rlv, arg_identity_extra_rlimits);
+                                r = json_variant_merge_object(&rlv, arg_identity_extra_rlimits);
                                 if (r < 0)
                                         return log_error_errno(r, "Failed to set resource limits: %m");
 
@@ -1033,7 +1033,7 @@ static int apply_identity_changes(JsonVariant **_v) {
                 if (r < 0)
                         return log_error_errno(r, "Failed to filter identity (privileged part): %m");
 
-                r = json_variant_merge(&privileged, arg_identity_extra_privileged);
+                r = json_variant_merge_object(&privileged, arg_identity_extra_privileged);
                 if (r < 0)
                         return log_error_errno(r, "Failed to merge identities (privileged part): %m");
 
index 46d6fb589c16ddecd8a5dac207a0fd1cac0f9682..206f6480acac0a830c9a22a8d9427f1c216c4df3 100644 (file)
@@ -2562,7 +2562,7 @@ int home_augment_status(
         if (r < 0)
                 return r;
 
-        r = json_variant_merge(&m, status);
+        r = json_variant_merge_object(&m, status);
         if (r < 0)
                 return r;
 
index 58f8ecc6f043752d89cf5bd7bd1a87f68de88a3e..089cbb1a1d0ee47645f8fac6f563c47863d48e61 100644 (file)
@@ -353,7 +353,7 @@ int user_record_add_binding(
                 /* Merge the new entry with an old one, if that exists */
                 be = json_variant_ref(json_variant_by_key(binding, SD_ID128_TO_STRING(mid)));
                 if (be) {
-                        r = json_variant_merge(&be, new_binding_entry);
+                        r = json_variant_merge_object(&be, new_binding_entry);
                         if (r < 0)
                                 return r;
 
index 7e4e4ff22fac5616e70aa29fce999b53dc1fe52a..a9e33ac627ec44b2069cd2d1994bf67bb3245be3 100644 (file)
@@ -26,7 +26,7 @@ static int json_append_one(JsonVariant **v, const char *name, JsonVariant *w) {
         assert(v);
         assert(name);
 
-        return json_append(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_VARIANT_NON_NULL(name, w)));
+        return json_variant_merge_objectb(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_VARIANT_NON_NULL(name, w)));
 }
 
 static int address_build_json(Address *address, JsonVariant **ret) {
@@ -379,7 +379,8 @@ static int network_append_json(Network *network, JsonVariant **v) {
         if (!network)
                 return 0;
 
-        return json_append(v, JSON_BUILD_OBJECT(
+        return json_variant_merge_objectb(
+                        v, JSON_BUILD_OBJECT(
                                 JSON_BUILD_PAIR_STRING("NetworkFile", network->filename),
                                 JSON_BUILD_PAIR_STRV("NetworkFileDropins", network->dropins),
                                 JSON_BUILD_PAIR_BOOLEAN("RequiredForOnline", network->required_for_online),
@@ -418,7 +419,9 @@ static int device_append_json(sd_device *device, JsonVariant **v) {
         if (sd_device_get_property_value(device, "ID_MODEL_FROM_DATABASE", &model) < 0)
                 (void) sd_device_get_property_value(device, "ID_MODEL", &model);
 
-        return json_append(v, JSON_BUILD_OBJECT(
+        return json_variant_merge_objectb(
+                        v,
+                        JSON_BUILD_OBJECT(
                                 JSON_BUILD_PAIR_STRING_NON_EMPTY("LinkFile", link),
                                 JSON_BUILD_PAIR_STRV_NON_EMPTY("LinkFileDropins", link_dropins),
                                 JSON_BUILD_PAIR_STRING_NON_EMPTY("Path", path),
@@ -915,7 +918,7 @@ static int captive_portal_append_json(Link *link, JsonVariant **v) {
         if (r <= 0)
                 return r;
 
-        return json_append(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_STRING("CaptivePortal", captive_portal)));
+        return json_variant_merge_objectb(v, JSON_BUILD_OBJECT(JSON_BUILD_PAIR_STRING("CaptivePortal", captive_portal)));
 }
 
 static int dhcp_server_offered_leases_append_json(Link *link, JsonVariant **v) {
index 6459f5d2f870aeb565c56f667c11042ac9bdb0e8..8d16b36be156dab35432a7c6ba0ef25906cb7514 100644 (file)
@@ -1428,7 +1428,8 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
                                         return log_oom();
                         }
 
-                        r = json_append(&v, JSON_BUILD_OBJECT(
+                        r = json_variant_merge_objectb(
+                                        &v, JSON_BUILD_OBJECT(
                                                        JSON_BUILD_PAIR("type", JSON_BUILD_STRING(boot_entry_type_json_to_string(e->type))),
                                                        JSON_BUILD_PAIR_CONDITION(e->id, "id", JSON_BUILD_STRING(e->id)),
                                                        JSON_BUILD_PAIR_CONDITION(e->path, "path", JSON_BUILD_STRING(e->path)),
@@ -1451,7 +1452,8 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
                         /* Sanitizers (only memory sanitizer?) do not like function call with too many
                          * arguments and trigger false positive warnings. Let's not add too many json objects
                          * at once. */
-                        r = json_append(&v, JSON_BUILD_OBJECT(
+                        r = json_variant_merge_objectb(
+                                        &v, JSON_BUILD_OBJECT(
                                                        JSON_BUILD_PAIR("isReported", JSON_BUILD_BOOLEAN(e->reported_by_loader)),
                                                        JSON_BUILD_PAIR_CONDITION(e->tries_left != UINT_MAX, "triesLeft", JSON_BUILD_UNSIGNED(e->tries_left)),
                                                        JSON_BUILD_PAIR_CONDITION(e->tries_done != UINT_MAX, "triesDone", JSON_BUILD_UNSIGNED(e->tries_done)),
index da095935e52e73c50c2aa503c40030dd0ada0863..7428152faa3f635b01d95d7333aa250eb8510230 100644 (file)
@@ -403,7 +403,7 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
                         /* If we have a build-id, merge it in the same JSON object so that it appears all
                          * nicely together in the logs/metadata. */
                         if (id_json) {
-                                r = json_variant_merge(&v, id_json);
+                                r = json_variant_merge_object(&v, id_json);
                                 if (r < 0)
                                         return log_error_errno(r, "json_variant_merge of package meta with buildId failed: %m");
                         }
@@ -419,7 +419,7 @@ static int parse_package_metadata(const char *name, JsonVariant *id_json, Elf *e
                         if (r < 0)
                                 return log_error_errno(r, "Failed to build JSON object: %m");
 
-                        r = json_variant_merge(c->package_metadata, w);
+                        r = json_variant_merge_object(c->package_metadata, w);
                         if (r < 0)
                                 return log_error_errno(r, "json_variant_merge of package meta with buildId failed: %m");
 
@@ -712,7 +712,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
                 if (r < 0)
                         return log_warning_errno(r, "Failed to build JSON object: %m");
 
-                r = json_variant_merge(&elf_metadata, json_architecture);
+                r = json_variant_merge_object(&elf_metadata, json_architecture);
                 if (r < 0)
                         return log_warning_errno(r, "Failed to merge JSON objects: %m");
 
@@ -722,7 +722,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
 #endif
 
         /* We always at least have the ELF type, so merge that (and possibly the arch). */
-        r = json_variant_merge(&elf_metadata, package_metadata);
+        r = json_variant_merge_object(&elf_metadata, package_metadata);
         if (r < 0)
                 return log_warning_errno(r, "Failed to merge JSON objects: %m");
 
index 9b95e42893e4dc79f5b3a32c547e7b2429609c19..311b05441b338f7b6802a8728d1370c81259496f 100644 (file)
@@ -2013,7 +2013,7 @@ int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l) {
         return json_variant_set_field(v, field, m);
 }
 
-int json_variant_merge(JsonVariant **v, JsonVariant *m) {
+int json_variant_merge_object(JsonVariant **v, JsonVariant *m) {
         _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
         _cleanup_free_ JsonVariant **array = NULL;
         size_t v_elements, m_elements, k;
@@ -2076,6 +2076,20 @@ int json_variant_merge(JsonVariant **v, JsonVariant *m) {
         return 1;
 }
 
+int json_variant_merge_objectb(JsonVariant **v, ...) {
+        _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
+        va_list ap;
+        int r;
+
+        va_start(ap, v);
+        r = json_buildv(&w, ap);
+        va_end(ap);
+        if (r < 0)
+                return r;
+
+        return json_variant_merge_object(v, w);
+}
+
 int json_variant_append_array(JsonVariant **v, JsonVariant *element) {
         _cleanup_(json_variant_unrefp) JsonVariant *nv = NULL;
         bool blank;
@@ -4237,30 +4251,6 @@ int json_build(JsonVariant **ret, ...) {
         return r;
 }
 
-int json_appendv(JsonVariant **v, va_list ap) {
-        _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
-        int r;
-
-        assert(v);
-
-        r = json_buildv(&w, ap);
-        if (r < 0)
-                return r;
-
-        return json_variant_merge(v, w);
-}
-
-int json_append(JsonVariant **v, ...) {
-        va_list ap;
-        int r;
-
-        va_start(ap, v);
-        r = json_appendv(v, ap);
-        va_end(ap);
-
-        return r;
-}
-
 int json_log_internal(
                 JsonVariant *variant,
                 int level,
index a5eca65bf8fbc84234dd1e2cfd668becdba0f0a1..1fe67d820b81a769a89dd6547316cbb57da4e8f0 100644 (file)
@@ -216,7 +216,8 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element);
 int json_variant_append_arrayb(JsonVariant **v, ...);
 int json_variant_append_array_nodup(JsonVariant **v, JsonVariant *element);
 
-int json_variant_merge(JsonVariant **v, JsonVariant *m);
+int json_variant_merge_object(JsonVariant **v, JsonVariant *m);
+int json_variant_merge_objectb(JsonVariant **v, ...);
 
 int json_variant_strv(JsonVariant *v, char ***ret);
 
@@ -350,9 +351,6 @@ enum {
 
 int json_build(JsonVariant **ret, ...);
 int json_buildv(JsonVariant **ret, va_list ap);
- /* These two functions below are equivalent to json_build() (or json_buildv()) and json_variant_merge(). */
-int json_append(JsonVariant **v, ...);
-int json_appendv(JsonVariant **v, va_list ap);
 
 /* A bitmask of flags used by the dispatch logic. Note that this is a combined bit mask, that is generated from the bit
  * mask originally passed into json_dispatch(), the individual bitmask associated with the static JsonDispatch callout
index 533fd0f0d387b3eef3e13fdf56c781b823bb803f..a2d48fa03687f5f54dbff43f981b36824c8b8479 100644 (file)
@@ -64,7 +64,7 @@ static int load_user(
                 else if (r < 0)
                         return r;
                 else {
-                        r = json_variant_merge(&v, privileged_v);
+                        r = json_variant_merge_object(&v, privileged_v);
                         if (r < 0)
                                 return r;
 
@@ -208,7 +208,7 @@ static int load_group(
                 else if (r < 0)
                         return r;
                 else {
-                        r = json_variant_merge(&v, privileged_v);
+                        r = json_variant_merge_object(&v, privileged_v);
                         if (r < 0)
                                 return r;
 
index 64024958ac4a8999c42a37ec8fb9628988d524ce..46ef5bfa96004aec54e5e10620502f2761229b65 100644 (file)
@@ -648,7 +648,7 @@ TEST(variant) {
         test_variant_one("[ 0, -0, 0.0, -0.0, 0.000, -0.000, 0e0, -0e0, 0e+0, -0e-0, 0e-0, -0e000, 0e+000 ]", test_zeroes);
 }
 
-TEST(json_append) {
+TEST(json_variant_merge_objectb) {
         _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
 
         assert_se(json_build(&v, JSON_BUILD_OBJECT(
@@ -656,9 +656,9 @@ TEST(json_append) {
                                              JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")),
                                              JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0);
 
-        assert_se(json_append(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")))) >= 0);
-        assert_se(json_append(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("c", JSON_BUILD_STRING("y")))) >= 0);
-        assert_se(json_append(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_STRING("z")))) >= 0);
+        assert_se(json_variant_merge_objectb(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")))) >= 0);
+        assert_se(json_variant_merge_objectb(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("c", JSON_BUILD_STRING("y")))) >= 0);
+        assert_se(json_variant_merge_objectb(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_STRING("z")))) >= 0);
 
         assert_se(json_variant_equal(v, w));
 }