Enhanced param parsing

[ Squashed into single commit --RR ]

This adds two new macros, `p_req_tal()` and `p_opt_tal()`. These support
callbacks that take a `struct command *` context.  Example:

	static bool json_tok_label_x(struct command *cmd,
                                      const char *name,
				      const char *buffer,
				      const jsmntok_t *tok,
				      struct json_escaped **label)

The above is taken from the run-param unit test (near the bottom of the diff).
The return value is true on success, or false (and it calls command_fail itself).

We can pretty much remove all remaining usage of `json_tok_tok` in the codebase
with this type of callback.
This commit is contained in:
Mark Beckwith
2018-08-09 21:15:30 -05:00
committed by Rusty Russell
parent d124f22421
commit bd5bf1f168
3 changed files with 224 additions and 27 deletions

View File

@@ -54,6 +54,15 @@ bool param(struct command *cmd, const char *buffer,
*/
typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
/*
* Advanced callback. Returns NULL on success, error message on failure.
*/
typedef bool(*param_cbx)(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
void **arg);
/*
* Add a handler to unmarshal a required json token into @arg. The handler must
* return true on success and false on failure. Upon failure, command_fail will be
@@ -65,12 +74,25 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
#define p_req(name, cb, arg) \
name"", \
true, \
false, \
(cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == true), \
(size_t)0
#define p_req_tal(name, cbx, arg) \
name"", \
true, \
true, \
(cbx), \
(arg) + 0*sizeof((cbx)((struct command *)NULL,\
(const char *)NULL, \
(const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == true), \
(size_t)0
/*
* Similar to above but for optional parameters.
* @arg must be the address of a pointer. If found during parsing, it will be
@@ -79,12 +101,25 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
#define p_opt(name, cb, arg) \
name"", \
false, \
false, \
(cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL, \
*(arg)) == true), \
sizeof(**(arg))
#define p_opt_tal(name, cbx, arg) \
name"", \
false, \
true, \
(cbx), \
(arg) + 0*sizeof((cbx)((struct command *)NULL,\
(const char *)NULL, \
(const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == true), \
sizeof(**(arg))
/*
* Similar to p_req but for optional parameters with defaults.
* @arg will be set to @def if it isn't found during parsing.
@@ -92,12 +127,25 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
#define p_opt_def(name, cb, arg, def) \
name"", \
false, \
false, \
(cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == true), \
((void)((*arg) = (def)), (size_t)0)
#define p_opt_def_tal(name, cbx, arg, def) \
name"", \
false, \
true, \
(cbx), \
(arg) + 0*sizeof((cbx)((struct command *)NULL,\
(const char *)NULL, \
(const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == true), \
({ (*arg) = tal((cmd), typeof(**arg)); (**arg) = (def); (size_t)0;})
/*
* For when you want an optional raw token.
*
@@ -106,6 +154,7 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
#define p_opt_tok(name, arg) \
name"", \
false, \
false, \
json_tok_tok, \
(arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \
sizeof(const jsmntok_t *)