From 542f529ed15ca695510b5767ee70942e33f05910 Mon Sep 17 00:00:00 2001 From: Mark Beckwith Date: Mon, 26 Nov 2018 09:30:37 -0600 Subject: [PATCH] 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 --- lightningd/jsonrpc.c | 1 + lightningd/jsonrpc.h | 2 ++ lightningd/param.c | 32 +++++++++++++++++--------------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index f29aeec62..ba5ac1b72 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -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); diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index 63b23768d..b461c6a06 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -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; }; diff --git a/lightningd/param.c b/lightningd/param.c index 6766f699e..1a83d5a3a 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -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 (p->is_set) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "duplicate json names: '%s'", p->name); - 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 (!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);