summaryrefslogtreecommitdiffstats
path: root/src/shared
diff options
context:
space:
mode:
authorAdrian Vovk <adrianvovk@gmail.com>2024-07-03 23:51:35 +0200
committerAdrian Vovk <adrianvovk@gmail.com>2024-07-17 20:15:41 +0200
commitbf01601f11454cdf7f26c674893ccbca7c09a1ce (patch)
tree39feaf1448a24e9862ce5d7af53bc7b8c38c502c /src/shared
parentMerge pull request #33752 from DaanDeMeyer/lsm (diff)
downloadsystemd-bf01601f11454cdf7f26c674893ccbca7c09a1ce.tar.xz
systemd-bf01601f11454cdf7f26c674893ccbca7c09a1ce.zip
table: Add TABLE_SET_JSON_FIELD_NAME
Lets you conveniently set JSON field names in table_add_many. Especially useful for vertical tables. For example: table_add_many(t, TABLE_FIELD, "Display Name", TABLE_STRING, obj->display_name, TABLE_SET_JSON_FIELD_NAME, "displayName", TABLE_FIELD, "Timestamp", TABLE_TIMESTAMP, obj->timestamp, TABLE_SET_JSON_FIELD_NAME, "timestampUSec");
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/format-table.c14
-rw-r--r--src/shared/format-table.h1
2 files changed, 15 insertions, 0 deletions
diff --git a/src/shared/format-table.c b/src/shared/format-table.c
index 14eeb4647f..e538d96414 100644
--- a/src/shared/format-table.c
+++ b/src/shared/format-table.c
@@ -1184,6 +1184,20 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
goto check;
}
+ case TABLE_SET_JSON_FIELD_NAME: {
+ const char *n = va_arg(ap, const char*);
+ size_t idx;
+ if (t->vertical) {
+ assert(TABLE_CELL_TO_INDEX(last_cell) >= t->n_columns);
+ idx = TABLE_CELL_TO_INDEX(last_cell) / t->n_columns - 1;
+ } else {
+ idx = TABLE_CELL_TO_INDEX(last_cell);
+ assert(idx < t->n_columns);
+ }
+ r = table_set_json_field_name(t, idx, n);
+ goto check;
+ }
+
case _TABLE_DATA_TYPE_MAX:
/* Used as end marker */
va_end(ap);
diff --git a/src/shared/format-table.h b/src/shared/format-table.h
index dcd339727f..15c0547b94 100644
--- a/src/shared/format-table.h
+++ b/src/shared/format-table.h
@@ -74,6 +74,7 @@ typedef enum TableDataType {
TABLE_SET_BOTH_UNDERLINES,
TABLE_SET_URL,
TABLE_SET_UPPERCASE,
+ TABLE_SET_JSON_FIELD_NAME,
_TABLE_DATA_TYPE_INVALID = -EINVAL,
} TableDataType;