Browse Source

params: shortened names

This is a cosmetic change only. No functional changes.

I shortened the names of macros and changed param_parse() to param().

Also went through params.h with a fine-toothed comb and updated the comments
to reflect the current API.

I wanted to change the files:

	params.c -> param.c
	params.h -> param.h
	run-params.c -> run->param.c

but that confused `git diff` for params.h so its best left for another PR.

I'm keeping #1682 updated locally with all these changes.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
ppa-0.6.1
Mark Beckwith 7 years ago
committed by Rusty Russell
parent
commit
cf12130627
  1. 13
      lightningd/params.c
  2. 99
      lightningd/params.h
  3. 143
      lightningd/test/run-params.c

13
lightningd/params.c

@ -269,10 +269,9 @@ static void check_params(struct param *params)
} }
#endif #endif
static bool param_parse_arr(struct command *cmd, static bool param_arr(struct command *cmd, const char *buffer,
const char *buffer, const jsmntok_t tokens[],
const jsmntok_t tokens[], struct param *params)
struct param *params)
{ {
#if DEVELOPER #if DEVELOPER
check_params(params); check_params(params);
@ -287,8 +286,8 @@ static bool param_parse_arr(struct command *cmd,
return false; return false;
} }
bool param_parse(struct command *cmd, const char *buffer, bool param(struct command *cmd, const char *buffer,
const jsmntok_t tokens[], ...) const jsmntok_t tokens[], ...)
{ {
struct param *params = tal_arr(cmd, struct param, 0); struct param *params = tal_arr(cmd, struct param, 0);
const char *name; const char *name;
@ -304,5 +303,5 @@ bool param_parse(struct command *cmd, const char *buffer,
} }
va_end(ap); va_end(ap);
return param_parse_arr(cmd, buffer, tokens, params); return param_arr(cmd, buffer, tokens, params);
} }

99
lightningd/params.h

@ -2,36 +2,37 @@
#define LIGHTNING_LIGHTNINGD_PARAMS_H #define LIGHTNING_LIGHTNINGD_PARAMS_H
#include "config.h" #include "config.h"
struct param;
/* /*
Typesafe callback system for unmarshalling and validating json parameters. Typesafe callback system for unmarshalling and validating json parameters.
Typical usage: Typical usage:
unsigned cltv; unsigned cltv;
const jsmntok_t *note;
u64 *msatoshi; u64 *msatoshi;
const jsmntok_t *note;
if (!param_parse(cmd, buffer, tokens, u64 expiry;
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, &note), if (!param(cmd, buffer, params,
param_opt("msatoshi", json_tok_u64, &msatoshi), p_req("cltv", json_tok_number, &cltv),
NULL)) p_opt("msatoshi", json_tok_u64, &msatoshi),
p_opt_tok("note", &note),
p_opt_def("expiry", json_tok_u64, &expiry, 3600),
NULL))
return; return;
At this point in the code you can be assured the json tokens were successfully At this point in the code you can be assured the json tokens were successfully
parsed. If not, param_parse() returns false, having already called parsed. If not, param() returned false, having already called command_fail()
command_fail() with a descriptive error message. The data section of the json with a descriptive error message. The data section of the json result contains
result contains the offending parameter and its value. the offending parameter and its value.
cltv is a required parameter. It must be present in the json input and will
be set appropriately.
cltv is a required parameter, and is set correctly. msatoshi is optional. If not present it will be set to NULL.
note and msatoshi are optional parameters. Their argument will be set to NULL note is also optional. It uses a special callback that simply sets note to the
if they are not provided. appropriate value (or NULL) and lets the handler do the validating.
The note parameter uses a special callback, param_opt_tok: it expiry is also optional and will be set to a default value if not present.
simply sets note to the appropriate value (or NULL) and lets the
handler do the validating.
There are canned failure messages for common callbacks. An example: There are canned failure messages for common callbacks. An example:
@ -39,11 +40,16 @@ struct param;
Otherwise a generic message is provided. Otherwise a generic message is provided.
*/ */
bool param_parse(struct command *cmd, const char *buffer,
const jsmntok_t params[], ...);
/* /*
* This callback provided must follow this signature; e.g., * parse the json tokens. @params can be an array of values, or an object
* of named values.
*/
bool param(struct command *cmd, const char *buffer,
const jsmntok_t params[], ...);
/*
* The callback provided must follow this signature; e.g.,
* bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *arg) * bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *arg)
*/ */
typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg); typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
@ -55,36 +61,35 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
* *
* This operation is typesafe; i.e., a compilation error will occur if the types * This operation is typesafe; i.e., a compilation error will occur if the types
* of @arg and the last parameter of @cb do not match (see the weird 0*sizeof). * of @arg and the last parameter of @cb do not match (see the weird 0*sizeof).
*
* Returns an opaque pointer that can be later used in param_is_set().
*/ */
#define param_req(name, cb, arg) \ #define p_req(name, cb, arg) \
name"", \ name"", \
true, \ true, \
(cb), \ (cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \ (arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL, \ (const jsmntok_t *)NULL, \
(arg)) == true), \ (arg)) == true), \
(size_t)0 (size_t)0
/* /*
* Similar to 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 * @arg must be the address of a pointer. If found during parsing, it will be
* allocated, otherwise it will be set to NULL. * allocated, otherwise it will be set to NULL.
*/ */
#define param_opt(name, cb, arg) \ #define p_opt(name, cb, arg) \
name"", \ name"", \
false, \ false, \
(cb), \ (cb), \
(arg) + 0*sizeof((cb)((const char *)NULL, \ (arg) + 0*sizeof((cb)((const char *)NULL, \
(const jsmntok_t *)NULL,\ (const jsmntok_t *)NULL, \
*(arg)) == true), \ *(arg)) == true), \
sizeof(**(arg)) sizeof(**(arg))
/* /*
* Similar to param_req but for optional parameters with defaults. * Similar to p_req but for optional parameters with defaults.
* @arg will be set to @def if it isn't found during parsing. * @arg will be set to @def if it isn't found during parsing.
*/ */
#define param_opt_default(name, cb, arg, def) \ #define p_opt_def(name, cb, arg, def) \
name"", \ name"", \
false, \ false, \
(cb), \ (cb), \
@ -98,11 +103,11 @@ typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
* *
* Note: weird sizeof() does type check that arg really is a (const) jsmntok_t **. * Note: weird sizeof() does type check that arg really is a (const) jsmntok_t **.
*/ */
#define param_opt_tok(name, arg) \ #define p_opt_tok(name, arg) \
name"", \ name"", \
false, \ false, \
json_tok_tok, \ json_tok_tok, \
(arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \ (arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \
sizeof(const jsmntok_t *) sizeof(const jsmntok_t *)
#endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */ #endif /* LIGHTNING_LIGHTNINGD_PARAMS_H */

143
lightningd/test/run-params.c

@ -89,10 +89,10 @@ static struct json *json_parse(const tal_t * ctx, const char *str)
static void zero_params(void) static void zero_params(void)
{ {
struct json *j = json_parse(cmd, "{}"); struct json *j = json_parse(cmd, "{}");
assert(param_parse(cmd, j->buffer, j->toks, NULL)); assert(param(cmd, j->buffer, j->toks, NULL));
j = json_parse(cmd, "[]"); j = json_parse(cmd, "[]");
assert(param_parse(cmd, j->buffer, j->toks, NULL)); assert(param(cmd, j->buffer, j->toks, NULL));
} }
struct sanity { struct sanity {
@ -127,9 +127,9 @@ static void stest(const struct json *j, struct sanity *b)
{ {
u64 ival; u64 ival;
double dval; double dval;
if (!param_parse(cmd, j->buffer, j->toks, if (!param(cmd, j->buffer, j->toks,
param_req("u64", json_tok_u64, &ival), p_req("u64", json_tok_u64, &ival),
param_req("double", json_tok_double, &dval), NULL)) { p_req("double", json_tok_double, &dval), NULL)) {
assert(failed == true); assert(failed == true);
assert(b->failed == true); assert(b->failed == true);
assert(strstr(fail_msg, b->fail_str)); assert(strstr(fail_msg, b->fail_str));
@ -161,9 +161,8 @@ static void tok_tok(void)
const jsmntok_t *tok = NULL; const jsmntok_t *tok = NULL;
struct json *j = json_parse(cmd, "{ 'satoshi', '546' }"); struct json *j = json_parse(cmd, "{ 'satoshi', '546' }");
assert(param_parse(cmd, j->buffer, j->toks, assert(param(cmd, j->buffer, j->toks,
param_req("satoshi", json_tok_tok, p_req("satoshi", json_tok_tok, &tok), NULL));
&tok), NULL));
assert(tok); assert(tok);
assert(json_tok_number(j->buffer, tok, &n)); assert(json_tok_number(j->buffer, tok, &n));
assert(n == 546); assert(n == 546);
@ -174,8 +173,8 @@ static void tok_tok(void)
const jsmntok_t *tok = (const jsmntok_t *) 65535; const jsmntok_t *tok = (const jsmntok_t *) 65535;
struct json *j = json_parse(cmd, "{}"); struct json *j = json_parse(cmd, "{}");
assert(param_parse(cmd, j->buffer, j->toks, assert(param(cmd, j->buffer, j->toks,
param_opt_tok("satoshi", &tok), NULL)); p_opt_tok("satoshi", &tok), NULL));
/* make sure it *is* NULL */ /* make sure it *is* NULL */
assert(tok == NULL); assert(tok == NULL);
@ -186,14 +185,14 @@ static void tok_tok(void)
static void dup_names(void) static void dup_names(void)
{ {
struct json *j = struct json *j =
json_parse(cmd, json_parse(cmd,
"{ 'u64' : '42', 'u64' : '43', 'double' : '3.15' }"); "{ 'u64' : '42', 'u64' : '43', 'double' : '3.15' }");
u64 i; u64 i;
double d; double d;
assert(!param_parse(cmd, j->buffer, j->toks, assert(!param(cmd, j->buffer, j->toks,
param_req("u64", json_tok_u64, &i), p_req("u64", json_tok_u64, &i),
param_req("double", json_tok_double, &d), NULL)); p_req("double", json_tok_double, &d), NULL));
} }
static void null_params(void) static void null_params(void)
@ -206,15 +205,15 @@ static void null_params(void)
for (int i = 0; i < tal_count(ints) - 1; ++i) for (int i = 0; i < tal_count(ints) - 1; ++i)
ints[i] = i; ints[i] = i;
assert(param_parse(cmd, j->buffer, j->toks, assert(param(cmd, j->buffer, j->toks,
param_req("0", json_tok_u64, &ints[0]), p_req("0", json_tok_u64, &ints[0]),
param_req("1", json_tok_u64, &ints[1]), p_req("1", json_tok_u64, &ints[1]),
param_req("2", json_tok_u64, &ints[2]), p_req("2", json_tok_u64, &ints[2]),
param_req("3", json_tok_u64, &ints[3]), p_req("3", json_tok_u64, &ints[3]),
param_opt_default("4", json_tok_u64, &ints[4], 999), p_opt_def("4", json_tok_u64, &ints[4], 999),
param_opt("5", json_tok_u64, &intptrs[0]), p_opt("5", json_tok_u64, &intptrs[0]),
param_opt("6", json_tok_u64, &intptrs[1]), p_opt("6", json_tok_u64, &intptrs[1]),
NULL)); NULL));
for (int i = 0; i < tal_count(ints); ++i) for (int i = 0; i < tal_count(ints); ++i)
assert(ints[i] == i + 10); assert(ints[i] == i + 10);
for (int i = 0; i < tal_count(intptrs); ++i) for (int i = 0; i < tal_count(intptrs); ++i)
@ -227,15 +226,15 @@ static void null_params(void)
intptrs[i] = (void *)42; intptrs[i] = (void *)42;
j = json_parse(cmd, "[ '10', '11', '12', '13', '14']"); j = json_parse(cmd, "[ '10', '11', '12', '13', '14']");
assert(param_parse(cmd, j->buffer, j->toks, assert(param(cmd, j->buffer, j->toks,
param_req("0", json_tok_u64, &ints[0]), p_req("0", json_tok_u64, &ints[0]),
param_req("1", json_tok_u64, &ints[1]), p_req("1", json_tok_u64, &ints[1]),
param_req("2", json_tok_u64, &ints[2]), p_req("2", json_tok_u64, &ints[2]),
param_req("3", json_tok_u64, &ints[3]), p_req("3", json_tok_u64, &ints[3]),
param_opt("4", json_tok_u64, &intptrs[0]), p_opt("4", json_tok_u64, &intptrs[0]),
param_opt("5", json_tok_u64, &intptrs[1]), p_opt("5", json_tok_u64, &intptrs[1]),
param_opt_default("6", json_tok_u64, &ints[4], 888), p_opt_def("6", json_tok_u64, &ints[4], 888),
NULL)); NULL));
assert(*intptrs[0] == 14); assert(*intptrs[0] == 14);
assert(intptrs[1] == NULL); assert(intptrs[1] == NULL);
assert(ints[4] == 888); assert(ints[4] == 888);
@ -289,46 +288,45 @@ static void bad_programmer(void)
/* check for repeated names */ /* check for repeated names */
if (setjmp(jump) == 0) { if (setjmp(jump) == 0) {
param_parse(cmd, j->buffer, j->toks, param(cmd, j->buffer, j->toks,
param_req("repeat", json_tok_u64, &ival), p_req("repeat", json_tok_u64, &ival),
param_req("double", json_tok_double, &dval), p_req("double", json_tok_double, &dval),
param_req("repeat", json_tok_u64, &ival2), NULL); p_req("repeat", json_tok_u64, &ival2), NULL);
/* shouldn't get here */ /* shouldn't get here */
restore_assert(old_stderr); restore_assert(old_stderr);
assert(false); assert(false);
} }
if (setjmp(jump) == 0) { if (setjmp(jump) == 0) {
param_parse(cmd, j->buffer, j->toks, param(cmd, j->buffer, j->toks,
param_req("repeat", json_tok_u64, &ival), p_req("repeat", json_tok_u64, &ival),
param_req("double", json_tok_double, &dval), p_req("double", json_tok_double, &dval),
param_req("repeat", json_tok_u64, &ival), NULL); p_req("repeat", json_tok_u64, &ival), NULL);
restore_assert(old_stderr); restore_assert(old_stderr);
assert(false); assert(false);
} }
if (setjmp(jump) == 0) { if (setjmp(jump) == 0) {
param_parse(cmd, j->buffer, j->toks, param(cmd, j->buffer, j->toks,
param_req("u64", json_tok_u64, &ival), p_req("u64", json_tok_u64, &ival),
param_req("repeat", json_tok_double, &dval), p_req("repeat", json_tok_double, &dval),
param_req("repeat", json_tok_double, &dval), NULL); p_req("repeat", json_tok_double, &dval), NULL);
restore_assert(old_stderr); restore_assert(old_stderr);
assert(false); assert(false);
} }
/* check for repeated arguments */ /* check for repeated arguments */
if (setjmp(jump) == 0) { if (setjmp(jump) == 0) {
param_parse(cmd, j->buffer, j->toks, param(cmd, j->buffer, j->toks,
param_req("u64", json_tok_u64, &ival), p_req("u64", json_tok_u64, &ival),
param_req("repeated-arg", json_tok_u64, &ival), p_req("repeated-arg", json_tok_u64, &ival), NULL);
NULL);
restore_assert(old_stderr); restore_assert(old_stderr);
assert(false); assert(false);
} }
if (setjmp(jump) == 0) { if (setjmp(jump) == 0) {
param_parse(cmd, j->buffer, j->toks, param(cmd, j->buffer, j->toks,
param_req("u64", (param_cb)NULL, &ival), NULL); p_req("u64", (param_cb) NULL, &ival), NULL);
restore_assert(old_stderr); restore_assert(old_stderr);
assert(false); assert(false);
} }
@ -339,13 +337,11 @@ static void bad_programmer(void)
json_parse(cmd, "[ '25', '546', '26', '1.1' ]"); json_parse(cmd, "[ '25', '546', '26', '1.1' ]");
unsigned int msatoshi; unsigned int msatoshi;
double riskfactor; double riskfactor;
param_parse(cmd, j->buffer, j->toks, param(cmd, j->buffer, j->toks,
param_req("u64", json_tok_u64, &ival), p_req("u64", json_tok_u64, &ival),
param_req("double", json_tok_double, &dval), p_req("double", json_tok_double, &dval),
param_opt_default("msatoshi", p_opt_def("msatoshi", json_tok_number, &msatoshi, 100),
json_tok_number, &msatoshi, 100), p_req("riskfactor", json_tok_double, &riskfactor), NULL);
param_req("riskfactor", json_tok_double,
&riskfactor), NULL);
restore_assert(old_stderr); restore_assert(old_stderr);
assert(false); assert(false);
} }
@ -390,7 +386,7 @@ static void five_hundred_params(void)
/* first test object version */ /* first test object version */
struct json *j = json_parse(params, obj->s); struct json *j = json_parse(params, obj->s);
assert(param_parse_arr(cmd, j->buffer, j->toks, params)); assert(param_arr(cmd, j->buffer, j->toks, params));
for (int i = 0; i < tal_count(ints); ++i) { for (int i = 0; i < tal_count(ints); ++i) {
assert(ints[i] == i); assert(ints[i] == i);
ints[i] = 65535; ints[i] = 65535;
@ -398,7 +394,7 @@ static void five_hundred_params(void)
/* now test array */ /* now test array */
j = json_parse(params, arr->s); j = json_parse(params, arr->s);
assert(param_parse_arr(cmd, j->buffer, j->toks, params)); assert(param_arr(cmd, j->buffer, j->toks, params));
for (int i = 0; i < tal_count(ints); ++i) { for (int i = 0; i < tal_count(ints); ++i) {
assert(ints[i] == i); assert(ints[i] == i);
} }
@ -414,16 +410,17 @@ static void sendpay(void)
u64 *msatoshi; u64 *msatoshi;
unsigned cltv; unsigned cltv;
if (!param_parse(cmd, j->buffer, j->toks, if (!param(cmd, j->buffer, j->toks,
param_req("route", json_tok_tok, &routetok), p_req("route", json_tok_tok, &routetok),
param_req("cltv", json_tok_number, &cltv), p_req("cltv", json_tok_number, &cltv),
param_opt_tok("note", &note), p_opt_tok("note", &note),
param_opt("msatoshi", json_tok_u64, &msatoshi), p_opt("msatoshi", json_tok_u64, &msatoshi),
NULL)) NULL))
assert(false); assert(false);
assert(note); assert(note);
assert(!strncmp("hello there", j->buffer + note->start, note->end - note->start)); assert(!strncmp("hello there", j->buffer + note->start,
note->end - note->start));
assert(msatoshi); assert(msatoshi);
assert(*msatoshi == 547); assert(*msatoshi == 547);
} }
@ -436,12 +433,12 @@ static void sendpay_nulltok(void)
u64 *msatoshi; u64 *msatoshi;
unsigned cltv; unsigned cltv;
if (!param_parse(cmd, j->buffer, j->toks, if (!param(cmd, j->buffer, j->toks,
param_req("route", json_tok_tok, &routetok), p_req("route", json_tok_tok, &routetok),
param_req("cltv", json_tok_number, &cltv), p_req("cltv", json_tok_number, &cltv),
param_opt_tok("note", &note), p_opt_tok("note", &note),
param_opt("msatoshi", json_tok_u64, &msatoshi), p_opt("msatoshi", json_tok_u64, &msatoshi),
NULL)) NULL))
assert(false); assert(false);
assert(note == NULL); assert(note == NULL);

Loading…
Cancel
Save