aboutsummaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-04-05 17:15:10 +0200
committerChristian Grothoff <christian@grothoff.org>2022-04-05 17:15:16 +0200
commit2ea635b60d3fcc4a242089084441ea8e2747c3d4 (patch)
tree5cb5af404b2cf6fea4bc6245f8100e483bd0f852 /src/json
parent89332e84db275272b9ecdf8a18c28e001d772358 (diff)
downloadgnunet-2ea635b60d3fcc4a242089084441ea8e2747c3d4.tar.gz
gnunet-2ea635b60d3fcc4a242089084441ea8e2747c3d4.zip
add flag to return 'not present' status from GNUNET_JSON_spec_mark_optional
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/src/json/json.c b/src/json/json.c
index 6d11b4fdd..07ec158be 100644
--- a/src/json/json.c
+++ b/src/json/json.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014-2017, 2021 GNUnet e.V. 3 Copyright (C) 2014-2017, 2021, 2022 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -48,7 +48,11 @@ GNUNET_JSON_parse (const json_t *root,
48 if ( ( (NULL == pos) || 48 if ( ( (NULL == pos) ||
49 (json_is_null (pos) ) ) && 49 (json_is_null (pos) ) ) &&
50 (spec[i].is_optional) ) 50 (spec[i].is_optional) )
51 {
52 if (NULL != spec[i].missing)
53 *spec[i].missing = true;
51 continue; 54 continue;
55 }
52 if ( (NULL == pos) || 56 if ( (NULL == pos) ||
53 (GNUNET_OK != 57 (GNUNET_OK !=
54 spec[i].parser (spec[i].cls, 58 spec[i].parser (spec[i].cls,
@@ -67,17 +71,21 @@ GNUNET_JSON_parse (const json_t *root,
67 GNUNET_JSON_parse_free (spec); 71 GNUNET_JSON_parse_free (spec);
68 return GNUNET_SYSERR; 72 return GNUNET_SYSERR;
69 } 73 }
74 if (NULL != spec[i].missing)
75 *spec[i].missing = false;
70 } 76 }
71 return GNUNET_OK; /* all OK! */ 77 return GNUNET_OK; /* all OK! */
72} 78}
73 79
74 80
75struct GNUNET_JSON_Specification 81struct GNUNET_JSON_Specification
76GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec) 82GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec,
83 bool *missing)
77{ 84{
78 struct GNUNET_JSON_Specification ret = spec; 85 struct GNUNET_JSON_Specification ret = spec;
79 86
80 ret.is_optional = true; 87 ret.is_optional = true;
88 ret.missing = missing;
81 return ret; 89 return ret;
82} 90}
83 91
@@ -104,7 +112,7 @@ GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec)
104 * @param value actual value of the option as a string. 112 * @param value actual value of the option as a string.
105 * @return #GNUNET_OK if parsing the value worked 113 * @return #GNUNET_OK if parsing the value worked
106 */ 114 */
107static int 115static enum GNUNET_GenericReturnValue
108set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 116set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
109 void *scls, 117 void *scls,
110 const char *option, 118 const char *option,
@@ -113,7 +121,9 @@ set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
113 json_t **json = scls; 121 json_t **json = scls;
114 json_error_t error; 122 json_error_t error;
115 123
116 *json = json_loads (value, JSON_REJECT_DUPLICATES, &error); 124 *json = json_loads (value,
125 JSON_REJECT_DUPLICATES,
126 &error);
117 if (NULL == *json) 127 if (NULL == *json)
118 { 128 {
119 fprintf (stderr, 129 fprintf (stderr,
@@ -134,13 +144,15 @@ GNUNET_JSON_getopt (char shortName,
134 const char *description, 144 const char *description,
135 json_t **json) 145 json_t **json)
136{ 146{
137 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName, 147 struct GNUNET_GETOPT_CommandLineOption clo = {
138 .name = name, 148 .shortName = shortName,
139 .argumentHelp = argumentHelp, 149 .name = name,
140 .description = description, 150 .argumentHelp = argumentHelp,
141 .require_argument = 1, 151 .description = description,
142 .processor = &set_json, 152 .require_argument = 1,
143 .scls = (void *) json }; 153 .processor = &set_json,
154 .scls = (void *) json
155 };
144 156
145 return clo; 157 return clo;
146} 158}