Code

store_json: Moved special casing of parent objects into the caller.
[sysdb.git] / src / core / store_json.c
index 7362691398af584ed3725d220cbebb85a26d6906..639c9534aa8485fc9afce4e5e25ecf33058057eb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * SysDB - src/core/store_json.c
- * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2013-2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -48,6 +48,9 @@
  */
 
 struct sdb_store_json_formatter {
+       sdb_object_t super;
+
+       /* The string buffer to write to */
        sdb_strbuf_t *buf;
 
        /* The context describes the state of the formatter through
@@ -55,12 +58,36 @@ struct sdb_store_json_formatter {
        int context[8];
        size_t current;
 
-       /* The current host context when processing non-host objects */
-       sdb_store_obj_t *current_host;
-
        int type;
        int flags;
 };
+#define F(obj) ((sdb_store_json_formatter_t *)(obj))
+
+static int
+formatter_init(sdb_object_t *obj, va_list ap)
+{
+       sdb_store_json_formatter_t *f = F(obj);
+
+       f->buf = va_arg(ap, sdb_strbuf_t *);
+       if (! f->buf)
+               return -1;
+
+       f->type = va_arg(ap, int);
+       if ((f->type != SDB_HOST) && (f->type != SDB_SERVICE) && (f->type != SDB_METRIC))
+               return -1;
+
+       f->flags = va_arg(ap, int);
+
+       f->context[0] = 0;
+       f->current = 0;
+       return 0;
+} /* formatter_init */
+
+static sdb_type_t formatter_type = {
+       /* size = */ sizeof(sdb_store_json_formatter_t),
+       /* init = */ formatter_init,
+       /* destroy = */ NULL,
+};
 
 /*
  * private helper functions
@@ -103,17 +130,6 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
 
        assert(f && obj);
 
-       if ((f->type != SDB_HOST) && (f->type == obj->type)) {
-               /* create the host for the current entry first */
-               assert(obj->parent && (obj->parent->type == SDB_HOST));
-               if (f->current_host != obj->parent) {
-                       json_emit(f, obj->parent);
-                       sdb_strbuf_append(f->buf, ", \"%ss\": [",
-                                       SDB_STORE_TYPE_TO_NAME(obj->type));
-                       f->current_host = obj->parent;
-               }
-       }
-
        escape_string(SDB_OBJ(obj)->name, name);
        sdb_strbuf_append(f->buf, "{\"name\": %s, ", name);
        if (obj->type == SDB_ATTRIBUTE) {
@@ -169,27 +185,8 @@ json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
 sdb_store_json_formatter_t *
 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
 {
-       sdb_store_json_formatter_t *f;
-
-       if (! buf)
-               return NULL;
-
-       if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC))
-               return NULL;
-
-       f = calloc(1, sizeof(*f));
-       if (! f)
-               return NULL;
-
-       f->buf = buf;
-       f->context[0] = 0;
-       f->current = 0;
-
-       f->current_host = NULL;
-
-       f->type = type;
-       f->flags = flags;
-       return f;
+       return F(sdb_object_create("json-formatter", formatter_type,
+                               buf, type, flags));
 } /* sdb_store_json_formatter */
 
 int
@@ -198,16 +195,9 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
        if ((! f) || (! obj))
                return -1;
 
-       if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) {
-               sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host "
-                               "during %s JSON serialization",
-                               SDB_STORE_TYPE_TO_NAME(f->type));
-               return -1;
-       }
-
        /* first top-level object */
        if (! f->context[0]) {
-               if (obj->type != f->type) {
+               if ((obj->type != f->type) && (obj->type != SDB_HOST)) {
                        sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
                                        "as the first element during %s JSON serialization",
                                        SDB_STORE_TYPE_TO_NAME(obj->type),
@@ -217,42 +207,38 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
                if (f->flags & SDB_WANT_ARRAY)
                        sdb_strbuf_append(f->buf, "[");
                assert(f->current == 0);
-               f->context[0] = obj->type;
-               return json_emit(f, obj);
        }
-
-       if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
-               /* new entry of a previous type or a new type on the same level;
-                * rewind to the right state */
-               while (f->current > 0) {
-                       if (f->context[f->current] == obj->type)
-                               break;
-                       sdb_strbuf_append(f->buf, "}]");
-                       --f->current;
+       else {
+               if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) {
+                       /* new entry of a previous type or a new type on the same level;
+                        * rewind to the right state */
+                       while (f->current > 0) {
+                               if (f->context[f->current] == obj->type)
+                                       break;
+                               sdb_strbuf_append(f->buf, "}]");
+                               --f->current;
+                       }
                }
-       }
-       if ((obj->type == f->type) && (f->type != SDB_HOST))
-               if (obj->parent != f->current_host)
-                       sdb_strbuf_append(f->buf, "}]");
 
-       if (obj->type == f->context[f->current]) {
-               /* new entry of the same type */
-               sdb_strbuf_append(f->buf, "},");
-       }
-       else if ((f->context[f->current] == SDB_HOST)
-                       || (obj->type == SDB_ATTRIBUTE)) {
-               assert(obj->type != SDB_HOST);
-               /* all object types may be children of a host;
-                * attributes may be children of any type */
-               sdb_strbuf_append(f->buf, ", \"%ss\": [",
-                               SDB_STORE_TYPE_TO_NAME(obj->type));
-               ++f->current;
-       }
-       else {
-               sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
-                               "on level %zu during JSON serialization",
-                               SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
-               return -1;
+               if (obj->type == f->context[f->current]) {
+                       /* new entry of the same type */
+                       sdb_strbuf_append(f->buf, "},");
+               }
+               else if ((f->context[f->current] == SDB_HOST)
+                               || (obj->type == SDB_ATTRIBUTE)) {
+                       assert(obj->type != SDB_HOST);
+                       /* all object types may be children of a host;
+                        * attributes may be children of any type */
+                       sdb_strbuf_append(f->buf, ", \"%ss\": [",
+                                       SDB_STORE_TYPE_TO_NAME(obj->type));
+                       ++f->current;
+               }
+               else {
+                       sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
+                                       "on level %zu during JSON serialization",
+                                       SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
+                       return -1;
+               }
        }
 
        json_emit(f, obj);
@@ -326,12 +312,7 @@ sdb_store_json_finish(sdb_store_json_formatter_t *f)
                sdb_strbuf_append(f->buf, "}]");
                --f->current;
        }
-       if (f->context[0] != SDB_HOST) {
-               assert(f->type != SDB_HOST);
-               sdb_strbuf_append(f->buf, "}]}");
-       }
-       else
-               sdb_strbuf_append(f->buf, "}");
+       sdb_strbuf_append(f->buf, "}");
 
        if (f->flags & SDB_WANT_ARRAY)
                sdb_strbuf_append(f->buf, "]");