summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2024-06-18 12:21:31 +0200
committerLennart Poettering <lennart@poettering.net>2024-06-19 22:50:02 +0200
commit004c69a27f4e2928c7a1963a4b79b2b66a1d3183 (patch)
tree204deceb7adfb1d37d01f81e688ee7482e322362 /src
parentMerge pull request #33034 from yuwata/update-kernel-headers (diff)
downloadsystemd-004c69a27f4e2928c7a1963a4b79b2b66a1d3183.tar.xz
systemd-004c69a27f4e2928c7a1963a4b79b2b66a1d3183.zip
sd-json: add sd_json_build() wrapper macro that implies SD_JSON_BUILD_OBJECT()
In 99% of uses of sd_json_build() we want to build an object as outermost construct. Let's shorten this most common case a bit, by adding sd_json_buildo() that implies this. This allows us to shorten much of our code, all across the tree.
Diffstat (limited to 'src')
-rw-r--r--src/shared/varlink.h20
-rw-r--r--src/systemd/sd-json.h8
-rw-r--r--src/test/test-json.c15
3 files changed, 43 insertions, 0 deletions
diff --git a/src/shared/varlink.h b/src/shared/varlink.h
index 6999f16380..739786b975 100644
--- a/src/shared/varlink.h
+++ b/src/shared/varlink.h
@@ -86,6 +86,8 @@ Varlink* varlink_close_unref(Varlink *v);
/* Enqueue method call, not expecting a reply */
int varlink_send(Varlink *v, const char *method, sd_json_variant *parameters);
int varlink_sendb(Varlink *v, const char *method, ...);
+#define varlink_sendbo(v, method, ...) \
+ varlink_sendb((v), (method), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Send method call and wait for reply */
int varlink_call_full(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags);
@@ -104,6 +106,8 @@ static inline int varlink_callb_full(Varlink *v, const char *method, sd_json_var
va_end(ap);
return r;
}
+#define varlink_callbo_full(v, method, ret_parameters, ret_error_id, ret_flags, ...) \
+ varlink_callb_full((v), (method), (ret_parameters), (ret_error_id), (ret_flags), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
static inline int varlink_callb(Varlink *v, const char *method, sd_json_variant **ret_parameters, const char **ret_error_id, ...) {
va_list ap;
int r;
@@ -113,7 +117,11 @@ static inline int varlink_callb(Varlink *v, const char *method, sd_json_variant
va_end(ap);
return r;
}
+#define varlink_callbo(v, method, ret_parameters, ret_error_id, ...) \
+ varlink_callb((v), (method), (ret_parameters), (ret_error_id), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
int varlink_callb_and_log(Varlink *v, const char *method, sd_json_variant **ret_parameters, ...);
+#define varlink_callbo_and_log(v, method, ret_parameters, ...) \
+ varlink_callb_and_log((v), (method), (ret_parameters), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Send method call and begin collecting all 'more' replies into an array, finishing when a final reply is sent */
int varlink_collect_full(Varlink *v, const char *method, sd_json_variant *parameters, sd_json_variant **ret_parameters, const char **ret_error_id, VarlinkReplyFlags *ret_flags);
@@ -121,22 +129,32 @@ static inline int varlink_collect(Varlink *v, const char *method, sd_json_varian
return varlink_collect_full(v, method, parameters, ret_parameters, ret_error_id, NULL);
}
int varlink_collectb(Varlink *v, const char *method, sd_json_variant **ret_parameters, const char **ret_error_id, ...);
+#define varlink_collectbo(v, method, ret_parameters, ret_error_id, ...) \
+ varlink_collectb((v), (method), (ret_parameters), (ret_error_id), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Enqueue method call, expect a reply, which is eventually delivered to the reply callback */
int varlink_invoke(Varlink *v, const char *method, sd_json_variant *parameters);
int varlink_invokeb(Varlink *v, const char *method, ...);
+#define varlink_invokebo(v, method, ...) \
+ varlink_invokeb((v), (method), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Enqueue method call, expect a reply now, and possibly more later, which are all delivered to the reply callback */
int varlink_observe(Varlink *v, const char *method, sd_json_variant *parameters);
int varlink_observeb(Varlink *v, const char *method, ...);
+#define varlink_observebo(v, method, ...) \
+ varlink_observeb((v), (method), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Enqueue a final reply */
int varlink_reply(Varlink *v, sd_json_variant *parameters);
int varlink_replyb(Varlink *v, ...);
+#define varlink_replybo(v, ...) \
+ varlink_replyb((v), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Enqueue a (final) error */
int varlink_error(Varlink *v, const char *error_id, sd_json_variant *parameters);
int varlink_errorb(Varlink *v, const char *error_id, ...);
+#define varlink_errorbo(v, error_id, ...) \
+ varlink_errorb((v), (error_id), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
int varlink_error_invalid_parameter(Varlink *v, sd_json_variant *parameters);
int varlink_error_invalid_parameter_name(Varlink *v, const char *name);
int varlink_error_errno(Varlink *v, int error);
@@ -144,6 +162,8 @@ int varlink_error_errno(Varlink *v, int error);
/* Enqueue a "more" reply */
int varlink_notify(Varlink *v, sd_json_variant *parameters);
int varlink_notifyb(Varlink *v, ...);
+#define varlink_notifybo(v, ...) \
+ varlink_notifyb((v), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
/* Ask for the current message to be dispatched again */
int varlink_dispatch_again(Varlink *v);
diff --git a/src/systemd/sd-json.h b/src/systemd/sd-json.h
index db37821f3a..bf3bf53b5e 100644
--- a/src/systemd/sd-json.h
+++ b/src/systemd/sd-json.h
@@ -158,6 +158,8 @@ int sd_json_variant_filter(sd_json_variant **v, char **to_remove);
int sd_json_variant_set_field(sd_json_variant **v, const char *field, sd_json_variant *value);
int sd_json_variant_set_fieldb(sd_json_variant **v, const char *field, ...);
+#define sd_json_variant_set_fieldbo(v, field, ...) \
+ sd_json_variant_set_fieldb((v), (field), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
int sd_json_variant_set_field_string(sd_json_variant **v, const char *field, const char *value);
int sd_json_variant_set_field_integer(sd_json_variant **v, const char *field, int64_t value);
int sd_json_variant_set_field_unsigned(sd_json_variant **v, const char *field, uint64_t value);
@@ -168,10 +170,14 @@ sd_json_variant* sd_json_variant_find(sd_json_variant *haystack, sd_json_variant
int sd_json_variant_append_array(sd_json_variant **v, sd_json_variant *element);
int sd_json_variant_append_arrayb(sd_json_variant **v, ...);
+#define sd_json_variant_append_arraybo(v, ...) \
+ sd_json_variant_append_arrayb((v), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
int sd_json_variant_append_array_nodup(sd_json_variant **v, sd_json_variant *element);
int sd_json_variant_merge_object(sd_json_variant **v, sd_json_variant *m);
int sd_json_variant_merge_objectb(sd_json_variant **v, ...);
+#define sd_json_variant_merge_objectbo(v, ...) \
+ sd_json_variant_merge_objectb((v), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
int sd_json_variant_sort(sd_json_variant **v);
int sd_json_variant_normalize(sd_json_variant **v);
@@ -267,6 +273,8 @@ typedef int (*sd_json_build_callback_t)(sd_json_variant **ret, const char *name,
#define SD_JSON_BUILD_PAIR_CALLBACK(name, c, u) SD_JSON_BUILD_PAIR(name, SD_JSON_BUILD_CALLBACK(c, u))
int sd_json_build(sd_json_variant **ret, ...);
+#define sd_json_buildo(ret, ...) \
+ sd_json_build((ret), SD_JSON_BUILD_OBJECT(__VA_ARGS__))
int sd_json_buildv(sd_json_variant **ret, 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
diff --git a/src/test/test-json.c b/src/test/test-json.c
index a3e7fe29af..69fb0859c7 100644
--- a/src/test/test-json.c
+++ b/src/test/test-json.c
@@ -412,6 +412,21 @@ TEST(build) {
assert_se(sd_json_variant_equal(ssv, ssv2));
}
+TEST(json_buildo) {
+ _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL;
+
+ assert_se(sd_json_buildo(&a,
+ SD_JSON_BUILD_PAIR("foo", SD_JSON_BUILD_INTEGER(4711)),
+ SD_JSON_BUILD_PAIR("bar", SD_JSON_BUILD_STRING("xxxx"))) >= 0);
+
+ assert_se(sd_json_build(&b,
+ SD_JSON_BUILD_OBJECT(
+ SD_JSON_BUILD_PAIR("bar", SD_JSON_BUILD_STRING("xxxx")),
+ SD_JSON_BUILD_PAIR("foo", SD_JSON_BUILD_INTEGER(4711)))) >= 0);
+
+ assert_se(sd_json_variant_equal(a, b));
+}
+
TEST(json_parse_file_empty) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;