From ffaf79ce48d2173371f7cbf07842b2bbbc6a993c Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 4 Jul 2014 21:06:21 +0200 Subject: [PATCH] store: Include service attributes in JSON output. --- src/core/store.c | 14 ++++++++++---- t/unit/core/store_test.c | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/core/store.c b/src/core/store.c index 1c04a77..605dfc0 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -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, "}"); diff --git a/t/unit/core/store_test.c b/t/unit/core/store_test.c index 1eb387e..0d0c56b 100644 --- a/t/unit/core/store_test.c +++ b/t/unit/core/store_test.c @@ -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, -- 2.30.2