gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] [gnunet] branch master updated: introduce GNUNET_JSON_spec_


From: gnunet
Subject: [GNUnet-SVN] [gnunet] branch master updated: introduce GNUNET_JSON_spec_mark_optional
Date: Mon, 22 Jul 2019 19:04:41 +0200

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

grothoff pushed a commit to branch master
in repository gnunet.

The following commit(s) were added to refs/heads/master by this push:
     new f75408217 introduce GNUNET_JSON_spec_mark_optional
f75408217 is described below

commit f754082176ba202596cedaec98ee06c2ca2e48e4
Author: Christian Grothoff <address@hidden>
AuthorDate: Mon Jul 22 19:03:25 2019 +0200

    introduce GNUNET_JSON_spec_mark_optional
---
 src/include/gnunet_json_lib.h | 15 ++++++++++
 src/json/json.c               | 64 ++++++++++++++++++++++++-------------------
 2 files changed, 51 insertions(+), 28 deletions(-)

diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index 32de2208a..72d2c4ebe 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -104,6 +104,11 @@ struct GNUNET_JSON_Specification
    * Where should we store the final size of @e ptr.
    */
   size_t *size_ptr;
+
+  /**
+   * Set to #GNUNET_YES if this component is optional.
+   */
+  int is_optional;
 };
 
 
@@ -147,6 +152,16 @@ struct GNUNET_JSON_Specification
 GNUNET_JSON_spec_end (void);
 
 
+/**
+ * Set the "optional" flag for a parser specification entry.
+ *
+ * @param spec specification to modify
+ * @return spec copy of @a spec with optional bit set
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec);
+
+
 /**
  * Variable size object (in network byte order, encoded using Crockford
  * Base32hex encoding).
diff --git a/src/json/json.c b/src/json/json.c
index fdce30488..068214f4e 100644
--- a/src/json/json.c
+++ b/src/json/json.c
@@ -11,7 +11,7 @@
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.
- 
+
   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -47,23 +47,20 @@ GNUNET_JSON_parse (const json_t *root,
                    const char **error_json_name,
                    unsigned int *error_line)
 {
-  unsigned int i;
-  json_t *pos;
-
   if (NULL == root)
     return GNUNET_SYSERR;
-  for (i=0;NULL != spec[i].parser;i++)
+  for (unsigned int i = 0; NULL != spec[i].parser; i++)
   {
+    json_t *pos;
+
     if (NULL == spec[i].field)
       pos = (json_t *) root;
     else
-      pos = json_object_get (root,
-                             spec[i].field);
-    if ( (NULL == pos) ||
-         (GNUNET_OK !=
-          spec[i].parser (spec[i].cls,
-                          pos,
-                          &spec[i])) )
+      pos = json_object_get (root, spec[i].field);
+    if ((NULL == pos) && (spec[i].is_optional))
+      continue;
+    if ((NULL == pos) ||
+        (GNUNET_OK != spec[i].parser (spec[i].cls, pos, &spec[i])))
     {
       if (NULL != error_json_name)
         *error_json_name = spec[i].field;
@@ -77,6 +74,22 @@ GNUNET_JSON_parse (const json_t *root,
 }
 
 
+/**
+ * Set the "optional" flag for a parser specification entry.
+ *
+ * @param spec specification to modify
+ * @return spec copy of @a spec with optional bit set
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec)
+{
+  struct GNUNET_JSON_Specification ret = spec;
+
+  ret.is_optional = GNUNET_YES;
+  return ret;
+}
+
+
 /**
  * Frees all elements allocated during a #GNUNET_JSON_parse()
  * operation.
@@ -86,10 +99,9 @@ GNUNET_JSON_parse (const json_t *root,
 void
 GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec)
 {
-  for (unsigned int i=0;NULL != spec[i].parser;i++)
+  for (unsigned int i = 0; NULL != spec[i].parser; i++)
     if (NULL != spec[i].cleaner)
-      spec[i].cleaner (spec[i].cls,
-                       &spec[i]);
+      spec[i].cleaner (spec[i].cls, &spec[i]);
 }
 
 
@@ -114,13 +126,11 @@ set_json (struct 
GNUNET_GETOPT_CommandLineProcessorContext *ctx,
   json_t **json = scls;
   json_error_t error;
 
-  *json = json_loads (value,
-                      JSON_REJECT_DUPLICATES,
-                      &error);
+  *json = json_loads (value, JSON_REJECT_DUPLICATES, &error);
   if (NULL == *json)
   {
     FPRINTF (stderr,
-             _("Failed to parse JSON in option `%s': %s (%s)\n"),
+             _ ("Failed to parse JSON in option `%s': %s (%s)\n"),
              option,
              error.text,
              error.source);
@@ -146,15 +156,13 @@ GNUNET_JSON_getopt (char shortName,
                     const char *description,
                     json_t **json)
 {
-  struct GNUNET_GETOPT_CommandLineOption clo = {
-    .shortName =  shortName,
-    .name = name,
-    .argumentHelp = argumentHelp,
-    .description = description,
-    .require_argument = 1,
-    .processor = &set_json,
-    .scls = (void *) json
-  };
+  struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+                                                .name = name,
+                                                .argumentHelp = argumentHelp,
+                                                .description = description,
+                                                .require_argument = 1,
+                                                .processor = &set_json,
+                                                .scls = (void *) json};
 
   return clo;
 }

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

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