Browse Source
json_escaped.[ch], param.[ch] and jsonrpc_errors.h move from lightningd/ to common/. Tests moved too. We add a new 'common/json_tok.[ch]' for the common parameter parsing routines which a plugin might want, taking them out of lightningd/json.c (which now only contains the lightningd-specific ones). The rest is mainly fixing up includes. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>plugin-6
Rusty Russell
6 years ago
37 changed files with 374 additions and 341 deletions
@ -1,4 +1,4 @@ |
|||
#include <lightningd/json_escaped.h> |
|||
#include <common/json_escaped.h> |
|||
#include <stdio.h> |
|||
|
|||
struct json_escaped *json_escaped_string_(const tal_t *ctx, |
@ -0,0 +1,184 @@ |
|||
#include <ccan/mem/mem.h> |
|||
#include <ccan/str/hex/hex.h> |
|||
#include <ccan/tal/str/str.h> |
|||
#include <common/json_command.h> |
|||
#include <common/json_escaped.h> |
|||
#include <common/json_tok.h> |
|||
#include <common/jsonrpc_errors.h> |
|||
#include <common/param.h> |
|||
|
|||
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) |
|||
{ |
|||
*b = tal(cmd, bool); |
|||
if (tok->type == JSMN_PRIMITIVE) { |
|||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) { |
|||
**b = true; |
|||
return true; |
|||
} |
|||
if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) { |
|||
**b = false; |
|||
return true; |
|||
} |
|||
} |
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be 'true' or 'false', not '%.*s'", |
|||
name, tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_double(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
double **num) |
|||
{ |
|||
*num = tal(cmd, double); |
|||
if (json_to_double(buffer, tok, *num)) |
|||
return true; |
|||
|
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be a double, not '%.*s'", |
|||
name, tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_escaped_string(struct command *cmd, const char *name, |
|||
const char * buffer, const jsmntok_t *tok, |
|||
const char **str) |
|||
{ |
|||
struct json_escaped *esc = json_to_escaped_string(cmd, buffer, tok); |
|||
if (esc) { |
|||
*str = json_escaped_unescape(cmd, esc); |
|||
if (*str) |
|||
return true; |
|||
} |
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be a string, not '%.*s'" |
|||
" (note, we don't allow \\u)", |
|||
name, |
|||
tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_string(struct command *cmd, const char *name, |
|||
const char * buffer, const jsmntok_t *tok, |
|||
const char **str) |
|||
{ |
|||
*str = tal_strndup(cmd, buffer + tok->start, |
|||
tok->end - tok->start); |
|||
return true; |
|||
} |
|||
|
|||
bool json_tok_label(struct command *cmd, const char *name, |
|||
const char * buffer, const jsmntok_t *tok, |
|||
struct json_escaped **label) |
|||
{ |
|||
/* We accept both strings and number literals here. */ |
|||
*label = json_escaped_string_(cmd, buffer + tok->start, tok->end - tok->start); |
|||
if (*label && (tok->type == JSMN_STRING || json_tok_is_num(buffer, tok))) |
|||
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) |
|||
{ |
|||
*num = tal(cmd, unsigned int); |
|||
if (json_to_number(buffer, tok, *num)) |
|||
return true; |
|||
|
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be an integer, not '%.*s'", |
|||
name, tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_sha256(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
struct sha256 **hash) |
|||
{ |
|||
*hash = tal(cmd, struct sha256); |
|||
if (hex_decode(buffer + tok->start, |
|||
tok->end - tok->start, |
|||
*hash, sizeof(**hash))) |
|||
return true; |
|||
|
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be a 32 byte hex value, not '%.*s'", |
|||
name, tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_msat(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t * tok, |
|||
u64 **msatoshi_val) |
|||
{ |
|||
if (json_tok_streq(buffer, tok, "any")) { |
|||
*msatoshi_val = NULL; |
|||
return true; |
|||
} |
|||
*msatoshi_val = tal(cmd, u64); |
|||
|
|||
if (json_to_u64(buffer, tok, *msatoshi_val) && *msatoshi_val != 0) |
|||
return true; |
|||
|
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be a positive number or 'any', not '%.*s'", |
|||
name, |
|||
tok->end - tok->start, |
|||
buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_percent(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
double **num) |
|||
{ |
|||
*num = tal(cmd, double); |
|||
if (json_to_double(buffer, tok, *num) && **num >= 0.0) |
|||
return true; |
|||
|
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be a positive double, not '%.*s'", |
|||
name, tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_u64(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
uint64_t **num) |
|||
{ |
|||
*num = tal(cmd, uint64_t); |
|||
if (json_to_u64(buffer, tok, *num)) |
|||
return true; |
|||
|
|||
command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
|||
"'%s' should be an unsigned 64 bit integer, not '%.*s'", |
|||
name, tok->end - tok->start, buffer + tok->start); |
|||
return false; |
|||
} |
|||
|
|||
bool json_tok_tok(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t * tok, |
|||
const jsmntok_t **out) |
|||
{ |
|||
return (*out = tok); |
|||
} |
@ -0,0 +1,72 @@ |
|||
/* Helpers for use with param parsing. */ |
|||
#ifndef LIGHTNING_COMMON_JSON_TOK_H |
|||
#define LIGHTNING_COMMON_JSON_TOK_H |
|||
#include "config.h" |
|||
#include <common/json.h> |
|||
|
|||
struct command; |
|||
|
|||
/* 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, |
|||
bool **b); |
|||
|
|||
/* Extract double from this (must be a number literal) */ |
|||
bool json_tok_double(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
double **num); |
|||
|
|||
/* Extract an escaped string (and unescape it) */ |
|||
bool json_tok_escaped_string(struct command *cmd, const char *name, |
|||
const char * buffer, const jsmntok_t *tok, |
|||
const char **str); |
|||
|
|||
/* Extract a string */ |
|||
bool json_tok_string(struct command *cmd, const char *name, |
|||
const char * buffer, const jsmntok_t *tok, |
|||
const char **str); |
|||
|
|||
/* 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, |
|||
unsigned int **num); |
|||
|
|||
/* Extract sha256 hash */ |
|||
bool json_tok_sha256(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
struct sha256 **hash); |
|||
|
|||
/* Extract positive integer, or NULL if tok is 'any'. */ |
|||
bool json_tok_msat(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t * tok, |
|||
u64 **msatoshi_val); |
|||
|
|||
/* Extract double in range [0.0, 100.0] */ |
|||
bool json_tok_percent(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
double **num); |
|||
|
|||
/* Extract number from this (may be a string, or a number literal) */ |
|||
bool json_tok_u64(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t *tok, |
|||
uint64_t **num); |
|||
|
|||
/*
|
|||
* Set the address of @out to @tok. Used as a callback by handlers that |
|||
* want to unmarshal @tok themselves. |
|||
*/ |
|||
bool json_tok_tok(struct command *cmd, const char *name, |
|||
const char *buffer, const jsmntok_t * tok, |
|||
const jsmntok_t **out); |
|||
|
|||
#endif /* LIGHTNING_COMMON_JSON_TOK_H */ |
@ -1,12 +1,9 @@ |
|||
#include <ccan/asort/asort.h> |
|||
#include <ccan/tal/str/str.h> |
|||
#include <common/json_command.h> |
|||
#include <common/jsonrpc_errors.h> |
|||
#include <common/param.h> |
|||
#include <common/utils.h> |
|||
#include <lightningd/json.h> |
|||
#include <lightningd/jsonrpc.h> |
|||
#include <lightningd/jsonrpc_errors.h> |
|||
#include <lightningd/lightningd.h> |
|||
#include <lightningd/param.h> |
|||
|
|||
struct param { |
|||
const char *name; |
Loading…
Reference in new issue