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>
This commit is contained in:
Mark Beckwith 2018-11-26 09:30:37 -06:00 committed by Rusty Russell
parent 7cd6d39276
commit 542f529ed1
3 changed files with 19 additions and 14 deletions

View File

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

View File

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

View File

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