Move repeated #defines into tr_json_util.h and add documentation
[trust_router.git] / trp / trp_route_encoders.c
index 8e1809b..7cbb3ac 100644 (file)
@@ -44,6 +44,7 @@
 #include <trp_rtable.h>
 #include <trust_router/trp.h>
 #include <tr_util.h>
+#include <tr_json_util.h>
 
 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
  * returns comma+space separated string. */
@@ -84,12 +85,15 @@ char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
 /* helper */
 static json_t *expiry_to_json_string(TRP_ROUTE *route)
 {
-  struct timespec ts_zero = {0, 0};
+  struct timespec ts = {0}; /* initialization to zero is important */
   char *s = NULL;
   json_t *jstr = NULL;
 
-  if (tr_cmp_timespec(trp_route_get_expiry(route), &ts_zero) > 0) {
-    s = timespec_to_str(trp_route_get_expiry(route));
+  if (tr_cmp_timespec(trp_route_get_expiry(route), &ts) > 0) {
+    if (trp_route_get_expiry_realtime(route, &ts) == NULL)
+      s = strdup("error");
+    else
+      s = timespec_to_str(&ts);
 
     if (s) {
       jstr = json_string(s);
@@ -100,21 +104,6 @@ static json_t *expiry_to_json_string(TRP_ROUTE *route)
   return jstr;
 }
 
-/* helper for below */
-#define OBJECT_SET_OR_FAIL(jobj, key, val)     \
-do {                                           \
-  if (val)                                     \
-    json_object_set_new((jobj),(key),(val));   \
-  else                                         \
-    goto cleanup;                              \
-} while (0)
-
-#define OBJECT_SET_OR_SKIP(jobj, key, val)     \
-do {                                           \
-  if (val)                                     \
-    json_object_set_new((jobj),(key),(val));   \
-} while (0)
-
 json_t *trp_route_to_json(TRP_ROUTE *route)
 {
   json_t *route_json = NULL;