Browse Source

param: add support for unused parameters

We can now set a flag to have param() ignore unexpected parameters.
Normally unexpected parameters are considered errors.
Needed by the check command.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
plugin-6
Mark Beckwith 6 years ago
committed by Rusty Russell
parent
commit
542f529ed1
  1. 1
      lightningd/jsonrpc.c
  2. 2
      lightningd/jsonrpc.h
  3. 6
      lightningd/param.c

1
lightningd/jsonrpc.c

@ -537,6 +537,7 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])
json_tok_len(id)); json_tok_len(id));
c->mode = CMD_NORMAL; c->mode = CMD_NORMAL;
c->ok = NULL; c->ok = NULL;
c->allow_unused = false;
list_add_tail(&jcon->commands, &c->list); list_add_tail(&jcon->commands, &c->list);
tal_add_destructor(c, destroy_command); tal_add_destructor(c, destroy_command);

2
lightningd/jsonrpc.h

@ -36,6 +36,8 @@ struct command {
/* This is created if mode is CMD_USAGE */ /* This is created if mode is CMD_USAGE */
const char *usage; const char *usage;
bool *ok; bool *ok;
/* Do not report unused parameters as errors (default false). */
bool allow_unused;
/* Have we started a json stream already? For debugging. */ /* Have we started a json stream already? For debugging. */
bool have_json_stream; bool have_json_stream;
}; };

6
lightningd/param.c

@ -79,7 +79,7 @@ static bool parse_by_position(struct command *cmd,
} }
/* check for unexpected trailing params */ /* check for unexpected trailing params */
if (tok != end) { if (!cmd->allow_unused && tok != end) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"too many parameters:" "too many parameters:"
" got %u, expected %zu", " got %u, expected %zu",
@ -117,13 +117,14 @@ static bool parse_by_name(struct command *cmd,
struct param *p = find_param(params, buffer + first->start, struct param *p = find_param(params, buffer + first->start,
first->end - first->start); first->end - first->start);
if (!p) { if (!p) {
if (!cmd->allow_unused) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"unknown parameter: '%.*s'", "unknown parameter: '%.*s'",
first->end - first->start, first->end - first->start,
buffer + first->start); buffer + first->start);
return false; return false;
} }
} else {
if (p->is_set) { if (p->is_set) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"duplicate json names: '%s'", p->name); "duplicate json names: '%s'", p->name);
@ -132,6 +133,7 @@ static bool parse_by_name(struct command *cmd,
if (!make_callback(cmd, p, buffer, first + 1)) if (!make_callback(cmd, p, buffer, first + 1))
return false; return false;
}
first = json_next(first + 1); first = json_next(first + 1);
} }
return post_check(cmd, params); return post_check(cmd, params);

Loading…
Cancel
Save