1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-08 08:58:27 +03:00

json: rename json_append() → json_variant_merge_objectb()

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.
This commit is contained in:
Lennart Poettering 2023-08-24 09:41:48 +02:00
parent c91f581c47
commit e931768eb4
11 changed files with 46 additions and 53 deletions

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

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

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

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

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

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

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

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

View 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

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

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