diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-11-27 22:26:21 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-11-28 08:38:55 +0100 |
commit | 319a4f27c477cbc63decdc510b7ca698a7be44f4 (patch) | |
tree | 3b7810fb51304b549612d61a3e9f9929bd15b52f /src/shared | |
parent | busctl: support json mode also for 'busctl monitor' (diff) | |
download | systemd-319a4f27c477cbc63decdc510b7ca698a7be44f4.tar.xz systemd-319a4f27c477cbc63decdc510b7ca698a7be44f4.zip |
json: teach json builder "conditional" object fields
Quite often when we generate objects some fields should only be
generated in some conditions. Let's add high-level support for that.
Matching the existing JSON_BUILD_PAIR() this adds
JSON_BUILD_PAIR_CONDITIONAL() which is very similar, but takes an
additional parameter: a boolean condition. If "true" this acts like
JSON_BUILD_PAIR(), but if false then the whole pair is suppressed.
This sounds simply, but requires a tiny bit of complexity: when complex
sub-variants are used in fields, then we also need to suppress them.
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/json.c | 165 | ||||
-rw-r--r-- | src/shared/json.h | 2 |
2 files changed, 133 insertions, 34 deletions
diff --git a/src/shared/json.c b/src/shared/json.c index eec6ea7baf..420555ae54 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -2249,6 +2249,7 @@ typedef struct JsonStack { size_t n_elements, n_elements_allocated; unsigned line_before; unsigned column_before; + size_t n_suppress; /* When building: if > 0, suppress this many subsequent elements. If == (size_t) -1, suppress all subsequent elements */ } JsonStack; static void json_stack_release(JsonStack *s) { @@ -2656,6 +2657,8 @@ int json_buildv(JsonVariant **ret, va_list ap) { for (;;) { _cleanup_(json_variant_unrefp) JsonVariant *add = NULL; + size_t n_subtract = 0; /* how much to subtract from current->n_suppress, i.e. how many elements would + * have been added to the current variant */ JsonStack *current; int command; @@ -2679,9 +2682,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { p = va_arg(ap, const char *); - r = json_variant_new_string(&add, p); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_string(&add, p); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2703,9 +2710,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { j = va_arg(ap, intmax_t); - r = json_variant_new_integer(&add, j); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_integer(&add, j); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2727,9 +2738,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { j = va_arg(ap, uintmax_t); - r = json_variant_new_unsigned(&add, j); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_unsigned(&add, j); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2751,9 +2766,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { d = va_arg(ap, long double); - r = json_variant_new_real(&add, d); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_real(&add, d); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2775,9 +2794,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { b = va_arg(ap, int); - r = json_variant_new_boolean(&add, b); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_boolean(&add, b); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2796,9 +2819,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { goto finish; } - r = json_variant_new_null(&add); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_null(&add); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2816,12 +2843,16 @@ int json_buildv(JsonVariant **ret, va_list ap) { goto finish; } + /* Note that we don't care for current->n_suppress here, after all the variant is already + * allocated anyway... */ add = va_arg(ap, JsonVariant*); if (!add) add = JSON_VARIANT_MAGIC_NULL; else json_variant_ref(add); + n_subtract = 1; + if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; else if (current->expect == EXPECT_OBJECT_VALUE) @@ -2841,13 +2872,17 @@ int json_buildv(JsonVariant **ret, va_list ap) { l = va_arg(ap, const char *); - if (!l) - add = JSON_VARIANT_MAGIC_NULL; - else { + if (l) { + /* Note that we don't care for current->n_suppress here, we should generate parsing + * errors even in suppressed object properties */ + r = json_parse(l, &add, NULL, NULL); if (r < 0) goto finish; - } + } else + add = JSON_VARIANT_MAGIC_NULL; + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2881,6 +2916,10 @@ int json_buildv(JsonVariant **ret, va_list ap) { stack[n_stack++] = (JsonStack) { .expect = EXPECT_ARRAY_ELEMENT, + .n_suppress = current->n_suppress != 0 ? (size_t) -1 : 0, /* if we shall suppress the + * new array, then we should + * also suppress all array + * members */ }; break; @@ -2893,9 +2932,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { assert(n_stack > 1); - r = json_variant_new_array(&add, current->elements, current->n_elements); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_array(&add, current->elements, current->n_elements); + if (r < 0) + goto finish; + } + + n_subtract = 1; json_stack_release(current); n_stack--, current--; @@ -2912,9 +2955,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { l = va_arg(ap, char **); - r = json_variant_new_array_strv(&add, l); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_array_strv(&add, l); + if (r < 0) + goto finish; + } + + n_subtract = 1; if (current->expect == EXPECT_TOPLEVEL) current->expect = EXPECT_END; @@ -2948,6 +2995,10 @@ int json_buildv(JsonVariant **ret, va_list ap) { stack[n_stack++] = (JsonStack) { .expect = EXPECT_OBJECT_KEY, + .n_suppress = current->n_suppress != 0 ? (size_t) -1 : 0, /* if we shall suppress the + * new object, then we should + * also suppress all object + * members */ }; break; @@ -2961,9 +3012,13 @@ int json_buildv(JsonVariant **ret, va_list ap) { assert(n_stack > 1); - r = json_variant_new_object(&add, current->elements, current->n_elements); - if (r < 0) - goto finish; + if (current->n_suppress == 0) { + r = json_variant_new_object(&add, current->elements, current->n_elements); + if (r < 0) + goto finish; + } + + n_subtract = 1; json_stack_release(current); n_stack--, current--; @@ -2980,15 +3035,47 @@ int json_buildv(JsonVariant **ret, va_list ap) { n = va_arg(ap, const char *); - r = json_variant_new_string(&add, n); - if (r < 0) + if (current->n_suppress == 0) { + r = json_variant_new_string(&add, n); + if (r < 0) + goto finish; + } + + n_subtract = 1; + + current->expect = EXPECT_OBJECT_VALUE; + break; + } + + case _JSON_BUILD_PAIR_CONDITION: { + const char *n; + bool b; + + if (current->expect != EXPECT_OBJECT_KEY) { + r = -EINVAL; goto finish; + } + + b = va_arg(ap, int); + n = va_arg(ap, const char *); + + if (b && current->n_suppress == 0) { + r = json_variant_new_string(&add, n); + if (r < 0) + goto finish; + } + + n_subtract = 1; /* we generated one item */ + + if (!b && current->n_suppress != (size_t) -1) + current->n_suppress += 2; /* Suppress this one and the next item */ current->expect = EXPECT_OBJECT_VALUE; break; }} - if (add) { + /* If a variant was generated, add it to our current variant, but only if we are not supposed to suppress additions */ + if (add && current->n_suppress == 0) { if (!GREEDY_REALLOC(current->elements, current->n_elements_allocated, current->n_elements + 1)) { r = -ENOMEM; goto finish; @@ -2996,6 +3083,16 @@ int json_buildv(JsonVariant **ret, va_list ap) { current->elements[current->n_elements++] = TAKE_PTR(add); } + + /* If we are supposed to suppress items, let's subtract how many items where generated from that + * counter. Except if the counter is (size_t) -1, i.e. we shall suppress an infinite number of elements + * on this stack level */ + if (current->n_suppress != (size_t) -1) { + if (current->n_suppress <= n_subtract) /* Saturated */ + current->n_suppress = 0; + else + current->n_suppress -= n_subtract; + } } done: diff --git a/src/shared/json.h b/src/shared/json.h index c9482d23eb..278ff77d30 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -177,6 +177,7 @@ enum { _JSON_BUILD_OBJECT_BEGIN, _JSON_BUILD_OBJECT_END, _JSON_BUILD_PAIR, + _JSON_BUILD_PAIR_CONDITION, _JSON_BUILD_NULL, _JSON_BUILD_VARIANT, _JSON_BUILD_LITERAL, @@ -192,6 +193,7 @@ enum { #define JSON_BUILD_ARRAY(...) _JSON_BUILD_ARRAY_BEGIN, __VA_ARGS__, _JSON_BUILD_ARRAY_END #define JSON_BUILD_OBJECT(...) _JSON_BUILD_OBJECT_BEGIN, __VA_ARGS__, _JSON_BUILD_OBJECT_END #define JSON_BUILD_PAIR(n, ...) _JSON_BUILD_PAIR, ({ const char *_x = n; _x; }), __VA_ARGS__ +#define JSON_BUILD_PAIR_CONDITION(c, n, ...) _JSON_BUILD_PAIR_CONDITION, ({ bool _x = c; _x; }), ({ const char *_x = n; _x; }), __VA_ARGS__ #define JSON_BUILD_NULL _JSON_BUILD_NULL #define JSON_BUILD_VARIANT(v) _JSON_BUILD_VARIANT, ({ JsonVariant *_x = v; _x; }) #define JSON_BUILD_LITERAL(l) _JSON_BUILD_LITERAL, ({ const char *_x = l; _x; }) |