diff --git a/lightningd/params.h b/lightningd/params.h index 056c7da78..45e5d3db0 100644 --- a/lightningd/params.h +++ b/lightningd/params.h @@ -15,7 +15,7 @@ struct param; if (!param_parse(cmd, buffer, tokens, param_req("cltv", json_tok_number, &cltv), - param_opt_tok("note", ¬e), + param_opt("note", json_tok_tok, ¬e), 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 */ diff --git a/lightningd/test/run-params.c b/lightningd/test/run-params.c index 2180a64ea..83ac465c2 100644 --- a/lightningd/test/run-params.c +++ b/lightningd/test/run-params.c @@ -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", ¬e), + param_opt("note", json_tok_tok, ¬e), 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, ¬e), + 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"); }