[LTP] [PATCH 4/9] metadata: data_storage: Add JSON null type
Cyril Hrubis
chrubis@suse.cz
Wed Dec 18 20:00:24 CET 2024
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
metadata/data_storage.h | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/metadata/data_storage.h b/metadata/data_storage.h
index 91ea70a02..e72be6187 100644
--- a/metadata/data_storage.h
+++ b/metadata/data_storage.h
@@ -16,6 +16,7 @@ enum data_type {
DATA_HASH,
DATA_STRING,
DATA_INT,
+ DATA_NULL,
};
struct data_node_array {
@@ -68,6 +69,8 @@ static inline const char* data_type_name(enum data_type type)
return "string";
case DATA_INT:
return "int";
+ case DATA_NULL:
+ return "null";
default:
return "???";
}
@@ -100,6 +103,18 @@ static inline struct data_node *data_node_int(long i)
return node;
}
+static inline struct data_node *data_node_null(void)
+{
+ struct data_node *node = malloc(sizeof(struct data_node));
+
+ if (!node)
+ return NULL;
+
+ node->type = DATA_NULL;
+
+ return node;
+}
+
#define MAX_ELEMS 100
static inline struct data_node *data_node_hash(void)
@@ -159,6 +174,7 @@ static inline void data_node_free(struct data_node *self)
switch (self->type) {
case DATA_STRING:
case DATA_INT:
+ case DATA_NULL:
break;
case DATA_HASH:
for (i = 0; i < self->hash.elems_used; i++) {
@@ -254,6 +270,10 @@ static inline void data_node_print_(struct data_node *self, unsigned int padd)
data_print_padd(padd);
printf("'%s'\n", self->string.val);
break;
+ case DATA_NULL:
+ data_print_padd(padd);
+ printf("null\n");
+ break;
case DATA_HASH:
for (i = 0; i < self->hash.elems_used; i++) {
data_print_padd(padd);
@@ -297,7 +317,6 @@ static inline void data_fprintf(FILE *f, unsigned int padd, const char *fmt, ...
va_end(va);
}
-
static inline void data_fprintf_esc(FILE *f, unsigned int padd, const char *str)
{
while (padd-- > 0)
@@ -344,6 +363,10 @@ static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int p
padd = do_padd ? padd : 0;
data_fprintf_esc(f, padd, self->string.val);
break;
+ case DATA_NULL:
+ padd = do_padd ? padd : 0;
+ data_fprintf(f, padd, "null");
+ break;
case DATA_HASH:
for (i = 0; i < self->hash.elems_used; i++) {
data_fprintf(f, padd, "\"%s\": ", self->hash.elems[i].id);
--
2.45.2
More information about the ltp
mailing list