From 8590dbedfb8c5769e2957a3c47f99f2d3399189f Mon Sep 17 00:00:00 2001 From: Mark Beckwith Date: Wed, 29 Aug 2018 13:43:28 -0500 Subject: [PATCH] param: make json_tok_label non-static Needed to do this so I could remove the implementation in the run-param test. Signed-off-by: Mark Beckwith --- lightningd/invoice.c | 20 ------------------- lightningd/json.c | 19 +++++++++++++++++++ lightningd/json.h | 6 ++++++ lightningd/test/run-param.c | 38 +++---------------------------------- 4 files changed, 28 insertions(+), 55 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 0f10421f1..57c62b9b1 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -102,26 +102,6 @@ static bool hsm_sign_b11(const u5 *u5bytes, return true; } -static bool json_tok_label(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - struct json_escaped **label) -{ - - if ((*label = json_tok_escaped_string(cmd, buffer, tok))) - return true; - - /* Allow literal numbers */ - if (json_tok_is_num(buffer, tok) && - ((*label = json_escaped_string_(cmd, buffer + tok->start, - tok->end - tok->start)))) - return true; - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a string or number, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; -} - static bool parse_fallback(struct command *cmd, const char *buffer, const jsmntok_t *fallback, const u8 **fallback_script) diff --git a/lightningd/json.c b/lightningd/json.c index 2717f6355..f621440f2 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -129,6 +129,25 @@ bool json_tok_double(struct command *cmd, const char *name, return false; } +bool json_tok_label(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + struct json_escaped **label) +{ + if ((*label = json_tok_escaped_string(cmd, buffer, tok))) + return true; + + /* Allow literal numbers */ + if (json_tok_is_num(buffer, tok) && + ((*label = json_escaped_string_(cmd, buffer + tok->start, + tok->end - tok->start)))) + return true; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a string or number, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + bool json_tok_number(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, unsigned int **num) diff --git a/lightningd/json.h b/lightningd/json.h index 9358110eb..f90da4213 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -16,6 +16,7 @@ struct bitcoin_txid; struct channel_id; struct command; +struct json_escaped; struct json_result; struct pubkey; struct route_hop; @@ -53,6 +54,11 @@ bool json_tok_double(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, double **num); +/* Extract a label. It is either an escaped string or a number. */ +bool json_tok_label(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + struct json_escaped **label); + /* Extract number from this (may be a string, or a number literal) */ bool json_tok_number(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/test/run-param.c b/lightningd/test/run-param.c index 66f489c03..64fb4b943 100644 --- a/lightningd/test/run-param.c +++ b/lightningd/test/run-param.c @@ -413,38 +413,6 @@ static void sendpay_nulltok(void) assert(msatoshi == NULL); } -/* - * New version of json_tok_label conforming to advanced style. This can eventually - * replace the existing json_tok_label. - */ -static bool json_tok_label_x(struct command *cmd, - const char *name, - const char *buffer, - const jsmntok_t *tok, - struct json_escaped **label) -{ - if ((*label = json_tok_escaped_string(cmd, buffer, tok))) - return true; - - /* Allow literal numbers */ - if (tok->type != JSMN_PRIMITIVE) - goto fail; - - for (int i = tok->start; i < tok->end; i++) - if (!cisdigit(buffer[i])) - goto fail; - - if ((*label = json_escaped_string_(cmd, buffer + tok->start, - tok->end - tok->start))) - return true; - -fail: - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a string or number, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; -} - static void advanced(void) { { @@ -456,7 +424,7 @@ static void advanced(void) const jsmntok_t *tok; assert(param(cmd, j->buffer, j->toks, - p_req("description", json_tok_label_x, &label), + p_req("description", json_tok_label, &label), p_req("msat", json_tok_msat, &msat), p_req("tok", json_tok_tok, &tok), p_opt("msat_opt1", json_tok_msat, &msat_opt1), @@ -474,8 +442,8 @@ static void advanced(void) struct json *j = json_parse(cmd, "[ 3 'foo' ]"); struct json_escaped *label, *foo; assert(param(cmd, j->buffer, j->toks, - p_req("label", json_tok_label_x, &label), - p_opt("foo", json_tok_label_x, &foo), + p_req("label", json_tok_label, &label), + p_opt("foo", json_tok_label, &foo), NULL)); assert(!strcmp(label->s, "3")); assert(!strcmp(foo->s, "foo"));