Code

store: Include service attributes in JSON output.
authorSebastian Harl <sh@tokkee.org>
Fri, 4 Jul 2014 19:06:21 +0000 (21:06 +0200)
committerSebastian Harl <sh@tokkee.org>
Fri, 4 Jul 2014 19:06:21 +0000 (21:06 +0200)
src/core/store.c
t/unit/core/store_test.c

index 1c04a77cb6a6c1ce252380211ee8bdb9de188309..605dfc08b71092756b732a0b77644548693a1586 100644 (file)
@@ -409,7 +409,7 @@ store_common_tojson(sdb_store_obj_t *obj, sdb_strbuf_t *buf)
  * of the serialized data.
  */
 static void
-store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
+store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf, int flags)
 {
        sdb_llist_iter_t *iter;
 
@@ -439,8 +439,14 @@ store_obj_tojson(sdb_llist_t *list, int type, sdb_strbuf_t *buf)
                                        SDB_DOUBLE_QUOTED);
                        sdb_strbuf_append(buf, "\"value\": %s, ", tmp);
                }
-
                store_common_tojson(sobj, buf);
+
+               if ((sobj->type == SDB_SERVICE)
+                               && (! (flags & SDB_SKIP_ATTRIBUTES))) {
+                       sdb_strbuf_append(buf, ", \"attributes\": ");
+                       store_obj_tojson(SVC(sobj)->attributes, SDB_ATTRIBUTE,
+                                       buf, flags);
+               }
                sdb_strbuf_append(buf, "}");
 
                if (sdb_llist_iter_has_next(iter))
@@ -619,12 +625,12 @@ sdb_store_host_tojson(sdb_store_obj_t *h, sdb_strbuf_t *buf, int flags)
 
        if (! (flags & SDB_SKIP_ATTRIBUTES)) {
                sdb_strbuf_append(buf, ", \"attributes\": ");
-               store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf);
+               store_obj_tojson(host->attributes, SDB_ATTRIBUTE, buf, flags);
        }
 
        if (! (flags & SDB_SKIP_SERVICES)) {
                sdb_strbuf_append(buf, ", \"services\": ");
-               store_obj_tojson(host->services, SDB_SERVICE, buf);
+               store_obj_tojson(host->services, SDB_SERVICE, buf, flags);
        }
 
        sdb_strbuf_append(buf, "}");
index 1eb387e34b855f0625b110715f2d7e919e6d3892..0d0c56bce6df859074e64590cf910eea3bfa67f5 100644 (file)
@@ -55,6 +55,14 @@ populate(void)
 
        sdb_store_service("h2", "s1", 1);
        sdb_store_service("h2", "s2", 1);
+
+       datum.type = SDB_TYPE_INTEGER;
+       datum.data.integer = 123;
+       sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
+
+       /* don't overwrite */
+       datum.data.integer = 666;
+       sdb_store_service_attr("h2", "s2", "k1", &datum, 2);
 } /* populate */
 
 START_TEST(test_store_host)
@@ -350,10 +358,16 @@ START_TEST(test_store_tojson)
                                        "\"services\": ["
                                                "{\"name\": \"s1\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []},"
+                                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                                       "\"attributes\": []},"
                                                "{\"name\": \"s2\", "
                                                        "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
-                                                       "\"update_interval\": \"0s\", \"backends\": []}"
+                                                       "\"update_interval\": \"0s\", \"backends\": [], "
+                                                       "\"attributes\": ["
+                                                               "{\"name\": \"k1\", \"value\": 123, "
+                                                                       "\"last_update\": \"1970-01-01 00:00:00 +0000\", "
+                                                                       "\"update_interval\": \"0s\", \"backends\": []}"
+                                                       "]}"
                                        "]}"
                        "]}" },
                { SDB_SKIP_SERVICES,