param: added json_tok_array

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith 2018-08-29 16:58:07 -05:00 committed by Rusty Russell
parent aa60057134
commit 0b26a17a0f
5 changed files with 21 additions and 24 deletions

View File

@ -431,18 +431,10 @@ static void json_dev_query_scids(struct command *cmd,
if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &id),
p_req("scids", json_tok_tok, &scidstok),
p_req("scids", json_tok_array, &scidstok),
NULL))
return;
if (scidstok->type != JSMN_ARRAY) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%.*s' is not an array",
scidstok->end - scidstok->start,
buffer + scidstok->start);
return;
}
scids = tal_arr(cmd, struct short_channel_id, scidstok->size);
end = json_next(scidstok);
for (i = 0, t = scidstok + 1; t < end; t = json_next(t), i++) {

View File

@ -149,7 +149,7 @@ static void json_invoice(struct command *cmd,
p_req("label", json_tok_label, &label_val),
p_req("description", json_tok_escaped_string, &desc_val),
p_opt_def("expiry", json_tok_u64, &expiry, 3600),
p_opt("fallbacks", json_tok_tok, &fallbacks),
p_opt("fallbacks", json_tok_array, &fallbacks),
p_opt("preimage", json_tok_tok, &preimagetok),
NULL))
return;
@ -180,11 +180,6 @@ static void json_invoice(struct command *cmd,
const jsmntok_t *i, *end = json_next(fallbacks);
size_t n = 0;
if (fallbacks->type != JSMN_ARRAY) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"fallback must be an array");
return;
}
fallback_scripts = tal_arr(cmd, const u8 *, n);
for (i = fallbacks + 1; i < end; i = json_next(i)) {
tal_resize(&fallback_scripts, n+1);

View File

@ -92,6 +92,19 @@ void json_add_txid(struct json_result *result, const char *fieldname,
json_add_string(result, fieldname, hex);
}
bool json_tok_array(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
const jsmntok_t **arr)
{
if (tok->type == JSMN_ARRAY)
return (*arr = tok);
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be an array, not '%.*s'",
name, tok->end - tok->start, buffer + tok->start);
return false;
}
bool json_tok_bool(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
bool **b)

View File

@ -44,6 +44,11 @@ void json_add_pubkey(struct json_result *response,
void json_add_txid(struct json_result *result, const char *fieldname,
const struct bitcoin_txid *txid);
/* Extract json array token */
bool json_tok_array(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
const jsmntok_t **arr);
/* Extract boolean this (must be a true or false) */
bool json_tok_bool(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,

View File

@ -954,21 +954,13 @@ static void json_sendpay(struct command *cmd,
const char *description;
if (!param(cmd, buffer, params,
p_req("route", json_tok_tok, &routetok),
p_req("route", json_tok_array, &routetok),
p_req("payment_hash", json_tok_sha256, &rhash),
p_opt("description", json_tok_escaped_string, &description),
p_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (routetok->type != JSMN_ARRAY) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%.*s' is not an array",
routetok->end - routetok->start,
buffer + routetok->start);
return;
}
end = json_next(routetok);
n_hops = 0;
route = tal_arr(cmd, struct route_hop, n_hops);