json: add JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO()

Like JSON_BUILD_PAIR_FINITE_USEC() but also suppresses zero values. Useful for usec_t fields where zero means "disabled" or "not configured" (e.g. KeepAliveTimeUSec, DeferAcceptUSec, RandomizedDelayUSec) as opposed to timeout fields where zero would be unusual and worth showing.
This commit is contained in:
Ivan Kruglov
2026-05-12 08:51:26 -07:00
parent 8c0464b7b9
commit 5f1c10e7bc
2 changed files with 6 additions and 1 deletions
+2
View File
@@ -172,6 +172,7 @@ enum {
_JSON_BUILD_PAIR_UNSIGNED_NON_ZERO,
_JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL,
_JSON_BUILD_PAIR_FINITE_USEC,
_JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO,
_JSON_BUILD_PAIR_STRING_NON_EMPTY,
_JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY,
_JSON_BUILD_PAIR_STRV_NON_EMPTY,
@@ -222,6 +223,7 @@ enum {
#define JSON_BUILD_PAIR_UNSIGNED_NON_ZERO(name, u) _JSON_BUILD_PAIR_UNSIGNED_NON_ZERO, (const char*) { name }, (uint64_t) { u }
#define JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL(name, u, eq) _JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL, (const char*) { name }, (uint64_t) { u }, (uint64_t) { eq }
#define JSON_BUILD_PAIR_FINITE_USEC(name, u) _JSON_BUILD_PAIR_FINITE_USEC, (const char*) { name }, (usec_t) { u }
#define JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO(name, u) _JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO, (const char*) { name }, (usec_t) { u }
#define JSON_BUILD_PAIR_STRING_NON_EMPTY(name, s) _JSON_BUILD_PAIR_STRING_NON_EMPTY, (const char*) { name }, (const char*) { s }
#define JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY(name, s) _JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY, (const char*) { name }, (const char*) { s }
#define JSON_BUILD_PAIR_STRV_NON_EMPTY(name, l) _JSON_BUILD_PAIR_STRV_NON_EMPTY, (const char*) { name }, (char**) { l }
+4 -1
View File
@@ -4543,6 +4543,7 @@ _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) {
break;
}
case _JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO:
case _JSON_BUILD_PAIR_FINITE_USEC: {
const char *n;
usec_t u;
@@ -4555,7 +4556,9 @@ _public_ int sd_json_buildv(sd_json_variant **ret, va_list ap) {
n = va_arg(ap, const char *);
u = va_arg(ap, usec_t);
if (u != USEC_INFINITY && current->n_suppress == 0) {
if (u != USEC_INFINITY &&
(command != _JSON_BUILD_PAIR_FINITE_USEC_NON_ZERO || u > 0) &&
current->n_suppress == 0) {
r = sd_json_variant_new_string(&add, n);
if (r < 0)
goto finish;