gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] branch master updated: replace key-value templating wit


From: gnunet
Subject: [taler-merchant] branch master updated: replace key-value templating with JSON templating
Date: Fri, 21 Aug 2020 10:52:26 +0200

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository merchant.

The following commit(s) were added to refs/heads/master by this push:
     new 1684fc6  replace key-value templating with JSON templating
1684fc6 is described below

commit 1684fc64333b97aee8e6128e96ff4920e494e2e0
Author: Florian Dold <florian.dold@gmail.com>
AuthorDate: Fri Aug 21 14:22:15 2020 +0530

    replace key-value templating with JSON templating
---
 src/backend/taler-merchant-httpd_get-orders-ID.c |  89 ++++++++--------
 src/backend/taler-merchant-httpd_get-tips-ID.c   |  23 ++---
 src/backend/taler-merchant-httpd_templating.c    | 125 ++---------------------
 src/backend/taler-merchant-httpd_templating.h    |  22 +---
 4 files changed, 64 insertions(+), 195 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_get-orders-ID.c 
b/src/backend/taler-merchant-httpd_get-orders-ID.c
index a02c45d..aabdde7 100644
--- a/src/backend/taler-merchant-httpd_get-orders-ID.c
+++ b/src/backend/taler-merchant-httpd_get-orders-ID.c
@@ -532,30 +532,30 @@ send_pay_request (struct GetOrderData *god,
       return MHD_NO;
     }
     {
-      struct KVC kvc[] = {
-        { "taler_pay_uri",
-          taler_pay_uri },
-        { "order_status_url",
-          order_status_url },
-        { "taler_pay_qrcode_svg",
-          qr },
-        { "order_summary",
-          get_order_summary (god) },
-        { NULL, NULL }
-      };
       enum GNUNET_GenericReturnValue res;
-
+      json_t *context;
+      context = json_pack ("{s:s, s:s, s:s, s:s:}",
+                           "taler_pay_uri",
+                           taler_pay_uri,
+                           "order_status_url",
+                           order_status_url,
+                           "taler_pay_qrcode_svg",
+                           qr,
+                           "order_summary",
+                           get_order_summary (god));
+      GNUNET_assert (NULL != context);
       res = TMH_return_from_template (god->sc.con,
                                       MHD_HTTP_PAYMENT_REQUIRED,
                                       "request_payment",
                                       taler_pay_uri,
-                                      kvc);
+                                      context);
       if (GNUNET_SYSERR == res)
       {
         GNUNET_break (0);
         ret = MHD_NO;
       }
       ret = MHD_YES;
+      json_decref (context);
     }
     GNUNET_free (qr);
   }
@@ -1110,51 +1110,44 @@ TMH_get_orders_ID (const struct TMH_RequestHandler *rh,
                                              "during QR code generation");
         }
         {
-          struct KVC kvc[] = {
-            { "order_summary",
-              get_order_summary (god) },
-            { "refund_amount",
-              TALER_amount2s (&god->refund_amount) },
-            { "taler_refund_uri",
-              uri },
-            { "taler_refund_qrcode_svg",
-              qr },
-            { NULL, NULL }
-          };
-
+          json_t *context;
+          context = json_pack ("{s:s, s:s, s:s, s:s}"
+                               "order_summary",
+                               get_order_summary (god),
+                               "refund_amount",
+                               TALER_amount2s (&god->refund_amount),
+                               "taler_refund_uri",
+                               uri,
+                               "taler_refund_qrcode_svg",
+                               qr);
+          GNUNET_assert (NULL != context);
           res = TMH_return_from_template (god->sc.con,
                                           MHD_HTTP_OK,
                                           "offer_refund",
                                           uri,
-                                          kvc);
+                                          context);
+          json_decref (context);
         }
         GNUNET_free (uri);
         GNUNET_free (qr);
       }
       else
       {
-        char *j;
-
-        j = json_dumps (god->contract_terms,
-                        JSON_COMPACT | JSON_SORT_KEYS); /* use most efficient 
encoding */
-        {
-          struct KVC kvc[] = {
-            { "order_summary",
-              get_order_summary (god) },
-            { "refund_amount",
-              TALER_amount2s (&god->refund_amount) },
-            { "contract",
-              j },
-            { NULL, NULL }
-          };
-
-          res = TMH_return_from_template (god->sc.con,
-                                          MHD_HTTP_OK,
-                                          "show_order_details",
-                                          NULL,
-                                          kvc);
-        }
-        free (j);
+        json_t *context;
+        context = json_pack ("{s:O, s:s, s:s}",
+                             "contract_terms",
+                             god->contract_terms,
+                             "order_summary",
+                             get_order_summary (god),
+                             "refund_amount",
+                             TALER_amount2s (&god->refund_amount));
+        GNUNET_assert (NULL != context);
+        res = TMH_return_from_template (god->sc.con,
+                                        MHD_HTTP_OK,
+                                        "show_order_details",
+                                        NULL,
+                                        context);
+        json_decref (context);
       }
       if (GNUNET_SYSERR == res)
       {
diff --git a/src/backend/taler-merchant-httpd_get-tips-ID.c 
b/src/backend/taler-merchant-httpd_get-tips-ID.c
index 237c1a4..0d853d7 100644
--- a/src/backend/taler-merchant-httpd_get-tips-ID.c
+++ b/src/backend/taler-merchant-httpd_get-tips-ID.c
@@ -269,17 +269,15 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh,
       }
       else
       {
-        struct KVC kvc[] = {
-          { "remaining_tip",
-            TALER_amount2s (&remaining) },
-          { "tip_status_url",
-            tip_status_url },
-          { "taler_tip_uri",
-            uri },
-          { "taler_tip_qrcode_svg",
-            qr },
-          { NULL, NULL }
-        };
+        json_t *context;
+        context = json_pack ("{s:s, s:s, s:s, s:s}",
+                             "remaining_tip",
+                             TALER_amount2s (&remaining),
+                             "taler_tip_uri",
+                             uri,
+                             "taler_tip_qrcode_svg",
+                             qr);
+        GNUNET_assert (NULL != context);
 
         ret = TMH_return_from_template (connection,
                                         ( (0 == remaining.value) &&
@@ -291,7 +289,8 @@ TMH_get_tips_ID (const struct TMH_RequestHandler *rh,
                                         ? "depleted_tip"
                                         : "offer_tip",
                                         uri,
-                                        kvc);
+                                        context);
+        json_decref (context);
       }
       GNUNET_free (tip_status_url);
       GNUNET_free (uri);
diff --git a/src/backend/taler-merchant-httpd_templating.c 
b/src/backend/taler-merchant-httpd_templating.c
index 1b480a6..3d189a7 100644
--- a/src/backend/taler-merchant-httpd_templating.c
+++ b/src/backend/taler-merchant-httpd_templating.c
@@ -24,6 +24,7 @@
 #include <taler/taler_mhd_lib.h>
 #include "taler-merchant-httpd_templating.h"
 #include "../mustach/mustach.h"
+#include "../mustach/mustach-jansson.h"
 #include <gnunet/gnunet_mhd_compat.h>
 
 
@@ -61,113 +62,6 @@ static struct TVE *loaded;
 static unsigned int loaded_length;
 
 
-/**
- * Function called by Mustach to enter the section @a name.
- * As we do not support sections, we always return 0.
- *
- * @param cls a `struct KVC[]` array
- * @param name section to enter
- * @return 0 (do not enter)
- */
-static int
-m_enter (void *cls, const char *name)
-{
-  (void) cls;
-  (void) name;
-  return 0;
-}
-
-
-/**
- * Function called by mustach to activate the next item in the
- * section. Does nothing, as we do not support sections.
- *
- * @param cls a `struct KVC[]` array
- * @return 0 (no next item to activate)
- */
-static int
-m_next (void *cls)
-{
-  (void) cls;
-  return 0;
-}
-
-
-/**
- * Function called by Mustach to leave the current section.
- * As we do not support sections, we should never be called.
- *
- * @param cls a `struct KVC[]` array
- * @return 0 (not documented by mustach)
- */
-static int
-m_leave (void *cls)
-{
-  GNUNET_assert (0);
-  return 0;
-}
-
-
-/**
- * Return the value of @a name in @a sbuf.
- *
- * @param cls a `struct KVC[]` array
- * @param name the value to lookup
- * @param[out] sbuf where to return the data
- * @return mustach-defined status code
- */
-static int
-m_get (void *cls,
-       const char *name,
-       struct mustach_sbuf *sbuf)
-{
-  const struct KVC *kvc = cls;
-
-  for (unsigned int i = 0; NULL != kvc[i].name; i++)
-  {
-    if (0 == strcmp (name,
-                     kvc[i].name))
-    {
-      sbuf->value = kvc[i].value;
-      sbuf->releasecb = NULL;
-      sbuf->closure = NULL;
-      return MUSTACH_OK;
-    }
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-              "Template requires value for unexpected name `%s'\n",
-              name);
-  return MUSTACH_ERROR_ITEM_NOT_FOUND;
-}
-
-
-/**
- * Mustach callback at the end. Cleans up the @a cls.
- *
- * @param cls a `struct KVC[]` array
- * @param status status of mustach (ignored)
- */
-static void
-m_stop (void *cls,
-        int status)
-{
-  (void) cls;
-  (void) status;
-}
-
-
-/**
- * Our 'universal' callbacks for mustach.
- */
-static struct mustach_itf itf = {
-  .enter = &m_enter,
-  .next = &m_next,
-  .leave = &m_leave,
-  .get = &m_get,
-  .stop = &m_stop
-};
-
-
 /**
  * Load Mustach template into memory.  Note that we intentionally cache
  * failures, that is if we ever failed to load a template, we will never try
@@ -215,15 +109,15 @@ lookup_template (struct MHD_Connection *connection,
 
 
 /**
- * Load a @a template and substitute using @a kvc, returning
+ * Load a @a template and substitute using @a root, returning
  * the result to the @a connection with the given
  * @a http_status code.
  *
  * @param connection the connection we act upon
- * @param http_status desired HTTP status code
+ * @param http_status code to use on success
  * @param template basename of the template to load
  * @param taler_uri value for "Taler:" header to set, or NULL
- * @param kvc key value pairs to fill in
+ * @param root JSON object to pass as the root context
  * @return #GNUNET_OK on success (reply queued), #GNUNET_NO if an error was 
queued,
  *         #GNUNET_SYSERR on failure (to queue an error)
  */
@@ -232,7 +126,7 @@ TMH_return_from_template (struct MHD_Connection *connection,
                           unsigned int http_status,
                           const char *template,
                           const char *taler_uri,
-                          const struct KVC *kvc)
+                          json_t *root)
 {
   struct MHD_Response *reply;
   char *body;
@@ -258,11 +152,10 @@ TMH_return_from_template (struct MHD_Connection 
*connection,
       return GNUNET_NO;
     }
     if (0 !=
-        (eno = mustach (tmpl,
-                        &itf,
-                        (void *) kvc,
-                        &body,
-                        &body_size)))
+        (eno = mustach_jansson (tmpl,
+                                root,
+                                &body,
+                                &body_size)))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                   "mustach failed on template `%s' with error %d\n",
diff --git a/src/backend/taler-merchant-httpd_templating.h 
b/src/backend/taler-merchant-httpd_templating.h
index e3c9d53..6f22e79 100644
--- a/src/backend/taler-merchant-httpd_templating.h
+++ b/src/backend/taler-merchant-httpd_templating.h
@@ -23,25 +23,9 @@
 
 #include <microhttpd.h>
 
-/**
- * Entry in a key-value array we use as the mustach closure.
- */
-struct KVC
-{
-  /**
-   * A name, used as the key. NULL for the last entry.
-   */
-  const char *name;
-
-  /**
-   * 0-terminated string value to return for @e name.
-   */
-  const char *value;
-};
-
 
 /**
- * Load a @a template and substitute using @a kvc, returning
+ * Load a @a template and substitute using @a root, returning
  * the result to the @a connection with the given
  * @a http_status code.
  *
@@ -49,7 +33,7 @@ struct KVC
  * @param http_status code to use on success
  * @param template basename of the template to load
  * @param taler_uri value for "Taler:" header to set, or NULL
- * @param kvc key value pairs to fill in
+ * @param root JSON object to pass as the root context
  * @return #GNUNET_OK on success (reply queued), #GNUNET_NO if an error was 
queued,
  *         #GNUNET_SYSERR on failure (to queue an error)
  */
@@ -58,7 +42,7 @@ TMH_return_from_template (struct MHD_Connection *connection,
                           unsigned int http_status,
                           const char *template,
                           const char *taler_uri,
-                          const struct KVC *kvc);
+                          json_t *root);
 
 /**
  * Preload templates.

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]