params: removed the param_opt_tok macro

There doesn't seeem to be a need for this anymore (unless I'm missing something).
I added the sendpay_nulltok() unit test to confirm.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith 2018-07-04 11:50:20 -05:00 committed by Rusty Russell
parent 1b50ea2abd
commit 7d9ad89010
2 changed files with 28 additions and 15 deletions

View File

@ -15,7 +15,7 @@ struct param;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt_tok("note", &note),
param_opt("note", json_tok_tok, &note),
param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
@ -67,7 +67,9 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
const jsmntok_t *), \
(arg), 0
/*
* Same as above but for optional parameters.
* Similar to above but for optional parameters.
* @arg must be the address of a pointer. If found during parsing, it will be
* allocated, otherwise it will be set to NULL.
*/
#define param_opt(name, cb, arg) \
name"", \
@ -77,15 +79,4 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
const jsmntok_t *), \
(arg), sizeof(**arg)
/*
* For when you want an optional raw token.
*
* Note: weird sizeof() does type check that arg really is a (const) jsmntok_t **.
*/
#define param_opt_tok(name, arg) \
name"", \
json_tok_tok, \
(arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \
sizeof(const jsmntok_t *)
#endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */

View File

@ -175,7 +175,7 @@ static void tok_tok(void)
struct json *j = json_parse(cmd, "{}");
assert(param_parse(cmd, j->buffer, j->toks,
param_opt_tok("satoshi", &tok), NULL));
param_opt("satoshi", json_tok_tok, &tok), NULL));
/* make sure it *is* NULL */
assert(tok == NULL);
@ -417,16 +417,37 @@ static void sendpay(void)
if (!param_parse(cmd, j->buffer, j->toks,
param_req("route", json_tok_tok, &routetok),
param_req("cltv", json_tok_number, &cltv),
param_opt_tok("note", &note),
param_opt("note", json_tok_tok, &note),
param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
assert(false);
assert(note);
assert(!strncmp("hello there", j->buffer + note->start, note->end - note->start));
assert(msatoshi);
assert(*msatoshi == 547);
}
static void sendpay_nulltok(void)
{
struct json *j = json_parse(cmd, "[ 'A', '123']");
const jsmntok_t *routetok, *note = (void *) 65535;
u64 *msatoshi;
unsigned cltv;
if (!param_parse(cmd, j->buffer, j->toks,
param_req("route", json_tok_tok, &routetok),
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, &note),
param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
assert(false);
assert(note == NULL);
assert(msatoshi == NULL);
}
int main(void)
{
setup_locale();
@ -444,6 +465,7 @@ int main(void)
dup_names();
five_hundred_params();
sendpay();
sendpay_nulltok();
tal_free(tmpctx);
printf("run-params ok\n");
}