From 76c3456787e9b7b75e48e19347c19a51333936dd Mon Sep 17 00:00:00 2001 From: Jan-Philipp Litza Date: Fri, 27 May 2016 22:40:17 +0200 Subject: [PATCH] gluon-node-info: simplify respondd module code a bit --- package/gluon-node-info/src/respondd.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/package/gluon-node-info/src/respondd.c b/package/gluon-node-info/src/respondd.c index d74c69e6..3b0e07dc 100644 --- a/package/gluon-node-info/src/respondd.c +++ b/package/gluon-node-info/src/respondd.c @@ -69,6 +69,12 @@ static struct json_object * get_number(struct uci_context *ctx, struct uci_secti return jso; } +static void maybe_add_number(struct uci_context *ctx, struct uci_section *s, const char *name, struct json_object *parent) { + struct json_object *jso = get_number(ctx, s, name); + if (jso) + json_object_object_add(parent, name, jso); +} + static struct json_object * get_location(struct uci_context *ctx, struct uci_package *p) { struct uci_section *s = get_first_section(p, "location"); if (!s) @@ -80,17 +86,9 @@ static struct json_object * get_location(struct uci_context *ctx, struct uci_pac struct json_object *ret = json_object_new_object(); - struct json_object *latitude = get_number(ctx, s, "latitude"); - if (latitude) - json_object_object_add(ret, "latitude", latitude); - - struct json_object *longitude = get_number(ctx, s, "longitude"); - if (longitude) - json_object_object_add(ret, "longitude", longitude); - - struct json_object *altitude = get_number(ctx, s, "altitude"); - if (altitude) - json_object_object_add(ret, "altitude", altitude); + maybe_add_number(ctx, s, "latitude", ret); + maybe_add_number(ctx, s, "longitude", ret); + maybe_add_number(ctx, s, "altitude", ret); return ret; }