2018-07-02 03:01:29 +02:00
|
|
|
#include "config.h"
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <lightningd/jsonrpc.h>
|
|
|
|
|
2018-08-10 22:16:22 +02:00
|
|
|
#include <lightningd/json.c>
|
2018-07-14 18:11:29 +02:00
|
|
|
#include <lightningd/param.c>
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
#include <common/json.c>
|
|
|
|
#include <common/json_escaped.c>
|
|
|
|
#include <ccan/array_size/array_size.h>
|
2018-07-04 03:46:52 +02:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <unistd.h>
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-07-28 19:03:09 +02:00
|
|
|
char *fail_msg = NULL;
|
2018-07-28 18:28:38 +02:00
|
|
|
bool failed = false;
|
|
|
|
|
|
|
|
static bool check_fail(void) {
|
|
|
|
if (!failed)
|
|
|
|
return false;
|
|
|
|
failed = false;
|
|
|
|
return true;
|
|
|
|
}
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
struct command *cmd;
|
|
|
|
|
|
|
|
void command_fail(struct command *cmd, int code, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fail_msg = tal_vfmt(cmd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_fail_detailed(struct command *cmd, int code,
|
|
|
|
const struct json_result *data, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fail_msg = tal_vfmt(cmd, fmt, ap);
|
|
|
|
fail_msg =
|
|
|
|
tal_fmt(cmd, "%s data: %s", fail_msg, json_result_string(data));
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
2018-08-10 17:00:34 +02:00
|
|
|
/* Generated stub for fmt_wireaddr_without_port */
|
|
|
|
char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); }
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
/* Generated stub for json_tok_wtx */
|
2018-07-20 03:14:02 +02:00
|
|
|
bool json_tok_wtx(struct wallet_tx * tx UNNEEDED, const char * buffer UNNEEDED,
|
2018-07-29 07:51:38 +02:00
|
|
|
const jsmntok_t * sattok UNNEEDED, u64 max UNNEEDED)
|
2018-07-20 03:14:02 +02:00
|
|
|
{ fprintf(stderr, "json_tok_wtx called!\n"); abort(); }
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
|
|
|
struct json {
|
|
|
|
jsmntok_t *toks;
|
|
|
|
char *buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void convert_quotes(char *first)
|
|
|
|
{
|
|
|
|
while (*first != '\0') {
|
|
|
|
if (*first == '\'')
|
|
|
|
*first = '"';
|
|
|
|
first++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct json *json_parse(const tal_t * ctx, const char *str)
|
|
|
|
{
|
|
|
|
struct json *j = tal(ctx, struct json);
|
|
|
|
j->buffer = tal_strdup(j, str);
|
|
|
|
convert_quotes(j->buffer);
|
|
|
|
|
|
|
|
j->toks = tal_arr(j, jsmntok_t, 50);
|
|
|
|
assert(j->toks);
|
|
|
|
jsmn_parser parser;
|
|
|
|
|
|
|
|
again:
|
|
|
|
jsmn_init(&parser);
|
|
|
|
int ret = jsmn_parse(&parser, j->buffer, strlen(j->buffer), j->toks,
|
|
|
|
tal_count(j->toks));
|
|
|
|
if (ret == JSMN_ERROR_NOMEM) {
|
|
|
|
tal_resize(&j->toks, tal_count(j->toks) * 2);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zero_params(void)
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "{}");
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param(cmd, j->buffer, j->toks, NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
j = json_parse(cmd, "[]");
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param(cmd, j->buffer, j->toks, NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct sanity {
|
|
|
|
char *str;
|
|
|
|
bool failed;
|
|
|
|
int ival;
|
|
|
|
double dval;
|
|
|
|
char *fail_str;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sanity buffers[] = {
|
|
|
|
// pass
|
|
|
|
{"['42', '3.15']", false, 42, 3.15, NULL},
|
|
|
|
{"{ 'u64' : '42', 'double' : '3.15' }", false, 42, 3.15, NULL},
|
|
|
|
|
|
|
|
// fail
|
|
|
|
{"{'u64':'42', 'double':'3.15', 'extra':'stuff'}", true, 0, 0,
|
|
|
|
"unknown parameter"},
|
|
|
|
{"['42', '3.15', 'stuff']", true, 0, 0, "too many"},
|
|
|
|
{"['42', '3.15', 'null']", true, 0, 0, "too many"},
|
|
|
|
|
|
|
|
// not enough
|
|
|
|
{"{'u64':'42'}", true, 0, 0, "missing required"},
|
|
|
|
{"['42']", true, 0, 0, "missing required"},
|
|
|
|
|
|
|
|
// fail wrong type
|
2018-08-13 18:16:41 +02:00
|
|
|
{"{'u64':'hello', 'double':'3.15'}", true, 0, 0, "be an unsigned 64"},
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
{"['3.15', '3.15', 'stuff']", true, 0, 0, "integer"},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void stest(const struct json *j, struct sanity *b)
|
|
|
|
{
|
2018-08-13 18:16:41 +02:00
|
|
|
u64 *ival;
|
2018-08-13 18:42:11 +02:00
|
|
|
double *dval;
|
2018-07-12 20:04:45 +02:00
|
|
|
if (!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("u64", json_tok_u64, &ival),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("double", json_tok_double, &dval), NULL)) {
|
2018-07-28 18:28:38 +02:00
|
|
|
assert(check_fail());
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(b->failed == true);
|
2018-08-13 18:16:41 +02:00
|
|
|
if (!strstr(fail_msg, b->fail_str)) {
|
|
|
|
printf("%s != %s\n", fail_msg, b->fail_str);
|
|
|
|
assert(false);
|
|
|
|
}
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
} else {
|
2018-07-28 18:28:38 +02:00
|
|
|
assert(!check_fail());
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(b->failed == false);
|
2018-08-13 18:16:41 +02:00
|
|
|
assert(*ival == 42);
|
2018-08-13 18:42:11 +02:00
|
|
|
assert(*dval > 3.1499 && b->dval < 3.1501);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sanity(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(buffers); ++i) {
|
|
|
|
struct json *j = json_parse(cmd, buffers[i].str);
|
|
|
|
assert(j->toks->type == JSMN_OBJECT
|
|
|
|
|| j->toks->type == JSMN_ARRAY);
|
|
|
|
stest(j, &buffers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure toks are passed through correctly, and also make sure
|
|
|
|
* optional missing toks are set to NULL.
|
|
|
|
*/
|
|
|
|
static void tok_tok(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unsigned int n;
|
|
|
|
const jsmntok_t *tok = NULL;
|
|
|
|
struct json *j = json_parse(cmd, "{ 'satoshi', '546' }");
|
|
|
|
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
2018-08-10 17:00:34 +02:00
|
|
|
p_req_tal("satoshi", json_tok_tok, &tok), NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(tok);
|
2018-08-10 22:16:22 +02:00
|
|
|
assert(json_to_number(j->buffer, tok, &n));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(n == 546);
|
|
|
|
}
|
|
|
|
// again with missing optional parameter
|
|
|
|
{
|
|
|
|
/* make sure it is *not* NULL */
|
|
|
|
const jsmntok_t *tok = (const jsmntok_t *) 65535;
|
|
|
|
|
|
|
|
struct json *j = json_parse(cmd, "{}");
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
2018-08-10 17:00:34 +02:00
|
|
|
p_opt_tal("satoshi", json_tok_tok, &tok), NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
/* make sure it *is* NULL */
|
|
|
|
assert(tok == NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for valid but duplicate json name-value pairs */
|
2018-07-04 03:46:52 +02:00
|
|
|
static void dup_names(void)
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
{
|
|
|
|
struct json *j =
|
2018-07-12 20:04:45 +02:00
|
|
|
json_parse(cmd,
|
|
|
|
"{ 'u64' : '42', 'u64' : '43', 'double' : '3.15' }");
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-08-13 18:16:41 +02:00
|
|
|
u64 *i;
|
2018-08-13 18:42:11 +02:00
|
|
|
double *d;
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("u64", json_tok_u64, &i),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("double", json_tok_double, &d), NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void null_params(void)
|
|
|
|
{
|
2018-08-13 18:16:41 +02:00
|
|
|
uint64_t **intptrs = tal_arr(cmd, uint64_t *, 7);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
/* no null params */
|
|
|
|
struct json *j =
|
|
|
|
json_parse(cmd, "[ '10', '11', '12', '13', '14', '15', '16']");
|
|
|
|
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("0", json_tok_u64, &intptrs[0]),
|
|
|
|
p_req_tal("1", json_tok_u64, &intptrs[1]),
|
|
|
|
p_req_tal("2", json_tok_u64, &intptrs[2]),
|
|
|
|
p_req_tal("3", json_tok_u64, &intptrs[3]),
|
|
|
|
p_opt_def_tal("4", json_tok_u64, &intptrs[4], 999),
|
|
|
|
p_opt_tal("5", json_tok_u64, &intptrs[5]),
|
|
|
|
p_opt_tal("6", json_tok_u64, &intptrs[6]),
|
2018-07-12 20:04:45 +02:00
|
|
|
NULL));
|
2018-08-13 18:16:41 +02:00
|
|
|
for (int i = 0; i < tal_count(intptrs); ++i) {
|
|
|
|
assert(intptrs[i]);
|
|
|
|
assert(*intptrs[i] == i + 10);
|
|
|
|
}
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
/* missing at end */
|
|
|
|
j = json_parse(cmd, "[ '10', '11', '12', '13', '14']");
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("0", json_tok_u64, &intptrs[0]),
|
|
|
|
p_req_tal("1", json_tok_u64, &intptrs[1]),
|
|
|
|
p_req_tal("2", json_tok_u64, &intptrs[2]),
|
|
|
|
p_req_tal("3", json_tok_u64, &intptrs[3]),
|
|
|
|
p_opt_tal("4", json_tok_u64, &intptrs[4]),
|
|
|
|
p_opt_tal("5", json_tok_u64, &intptrs[5]),
|
|
|
|
p_opt_def_tal("6", json_tok_u64, &intptrs[6], 888),
|
2018-07-12 20:04:45 +02:00
|
|
|
NULL));
|
2018-08-13 18:16:41 +02:00
|
|
|
assert(*intptrs[0] == 10);
|
|
|
|
assert(*intptrs[1] == 11);
|
|
|
|
assert(*intptrs[2] == 12);
|
|
|
|
assert(*intptrs[3] == 13);
|
|
|
|
assert(*intptrs[4] == 14);
|
|
|
|
assert(!intptrs[5]);
|
|
|
|
assert(*intptrs[6] == 888);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/*
|
|
|
|
* Check to make sure there are no programming mistakes.
|
|
|
|
*/
|
|
|
|
static void bad_programmer(void)
|
|
|
|
{
|
2018-08-13 18:16:41 +02:00
|
|
|
u64 *ival;
|
|
|
|
u64 *ival2;
|
2018-08-13 18:42:11 +02:00
|
|
|
double *dval;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
struct json *j = json_parse(cmd, "[ '25', '546', '26' ]");
|
|
|
|
|
|
|
|
/* check for repeated names */
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("repeat", json_tok_u64, &ival),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("double", json_tok_double, &dval),
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("repeat", json_tok_u64, &ival2), NULL));
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(check_fail());
|
2018-07-28 19:59:06 +02:00
|
|
|
assert(strstr(fail_msg, "developer error"));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("repeat", json_tok_u64, &ival),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("double", json_tok_double, &dval),
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("repeat", json_tok_u64, &ival), NULL));
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(check_fail());
|
2018-07-28 19:59:06 +02:00
|
|
|
assert(strstr(fail_msg, "developer error"));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("u64", json_tok_u64, &ival),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("repeat", json_tok_double, &dval),
|
|
|
|
p_req_tal("repeat", json_tok_double, &dval), NULL));
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(check_fail());
|
2018-07-28 19:59:06 +02:00
|
|
|
assert(strstr(fail_msg, "developer error"));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
/* check for repeated arguments */
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("u64", json_tok_u64, &ival),
|
|
|
|
p_req_tal("repeated-arg", json_tok_u64, &ival), NULL));
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(check_fail());
|
2018-07-28 19:59:06 +02:00
|
|
|
assert(strstr(fail_msg, "developer error"));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
|
|
|
p_req("u64", (param_cb) NULL, &ival), NULL));
|
|
|
|
assert(check_fail());
|
2018-07-28 19:59:06 +02:00
|
|
|
assert(strstr(fail_msg, "developer error"));
|
2018-07-28 19:03:09 +02:00
|
|
|
|
|
|
|
/* Add required param after optional */
|
|
|
|
j = json_parse(cmd, "[ '25', '546', '26', '1.1' ]");
|
2018-08-10 22:16:22 +02:00
|
|
|
unsigned int *msatoshi;
|
2018-08-13 18:42:11 +02:00
|
|
|
double *riskfactor;
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
2018-08-13 18:16:41 +02:00
|
|
|
p_req_tal("u64", json_tok_u64, &ival),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("double", json_tok_double, &dval),
|
2018-08-10 22:16:22 +02:00
|
|
|
p_opt_def_tal("msatoshi", json_tok_number, &msatoshi, 100),
|
2018-08-13 18:42:11 +02:00
|
|
|
p_req_tal("riskfactor", json_tok_double, &riskfactor), NULL));
|
2018-08-10 22:16:22 +02:00
|
|
|
assert(*msatoshi);
|
|
|
|
assert(*msatoshi == 100);
|
2018-07-28 19:03:09 +02:00
|
|
|
assert(check_fail());
|
2018-07-28 19:59:06 +02:00
|
|
|
assert(strstr(fail_msg, "developer error"));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void add_members(struct param **params,
|
|
|
|
struct json_result *obj,
|
2018-08-10 22:16:22 +02:00
|
|
|
struct json_result *arr, unsigned int **ints)
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < tal_count(ints); ++i) {
|
2018-07-02 03:11:08 +02:00
|
|
|
char *name = tal_fmt(tmpctx, "%i", i);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
json_add_num(obj, name, i);
|
|
|
|
json_add_num(arr, NULL, i);
|
2018-07-11 04:29:53 +02:00
|
|
|
param_add(params, name, true,
|
2018-08-10 22:16:22 +02:00
|
|
|
NULL,
|
|
|
|
typesafe_cb_preargs(bool, void **,
|
2018-07-04 03:46:07 +02:00
|
|
|
json_tok_number,
|
|
|
|
&ints[i],
|
2018-08-10 22:16:22 +02:00
|
|
|
struct command *,
|
|
|
|
const char *,
|
2018-07-04 03:46:07 +02:00
|
|
|
const char *,
|
|
|
|
const jsmntok_t *),
|
2018-07-04 18:03:12 +02:00
|
|
|
&ints[i], 0);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A roundabout way of initializing an array of ints to:
|
|
|
|
* ints[0] = 0, ints[1] = 1, ... ints[499] = 499
|
|
|
|
*/
|
|
|
|
static void five_hundred_params(void)
|
|
|
|
{
|
2018-07-04 03:46:07 +02:00
|
|
|
struct param *params = tal_arr(NULL, struct param, 0);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-08-10 22:16:22 +02:00
|
|
|
unsigned int **ints = tal_arr(params, unsigned int*, 500);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
struct json_result *obj = new_json_result(params);
|
|
|
|
struct json_result *arr = new_json_result(params);
|
|
|
|
json_object_start(obj, NULL);
|
|
|
|
json_array_start(arr, NULL);
|
2018-07-04 03:46:07 +02:00
|
|
|
add_members(¶ms, obj, arr, ints);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
json_object_end(obj);
|
|
|
|
json_array_end(arr);
|
|
|
|
|
|
|
|
/* first test object version */
|
|
|
|
struct json *j = json_parse(params, obj->s);
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param_arr(cmd, j->buffer, j->toks, params));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
for (int i = 0; i < tal_count(ints); ++i) {
|
2018-08-10 22:16:22 +02:00
|
|
|
assert(ints[i]);
|
|
|
|
assert(*ints[i] == i);
|
|
|
|
*ints[i] = 65535;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now test array */
|
|
|
|
j = json_parse(params, arr->s);
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(param_arr(cmd, j->buffer, j->toks, params));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
for (int i = 0; i < tal_count(ints); ++i) {
|
2018-08-10 22:16:22 +02:00
|
|
|
assert(*ints[i] == i);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sendpay(void)
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 'A', '123', 'hello there' '547']");
|
|
|
|
|
|
|
|
const jsmntok_t *routetok, *note;
|
2018-07-02 02:53:11 +02:00
|
|
|
u64 *msatoshi;
|
2018-08-10 22:16:22 +02:00
|
|
|
unsigned *cltv;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
2018-07-12 20:04:45 +02:00
|
|
|
if (!param(cmd, j->buffer, j->toks,
|
2018-08-10 17:00:34 +02:00
|
|
|
p_req_tal("route", json_tok_tok, &routetok),
|
2018-08-10 22:16:22 +02:00
|
|
|
p_req_tal("cltv", json_tok_number, &cltv),
|
2018-08-10 17:00:34 +02:00
|
|
|
p_opt_tal("note", json_tok_tok, ¬e),
|
2018-08-13 18:16:41 +02:00
|
|
|
p_opt_tal("msatoshi", json_tok_u64, &msatoshi),
|
2018-07-12 20:04:45 +02:00
|
|
|
NULL))
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
|
2018-07-02 02:53:11 +02:00
|
|
|
assert(note);
|
2018-07-12 20:04:45 +02:00
|
|
|
assert(!strncmp("hello there", j->buffer + note->start,
|
|
|
|
note->end - note->start));
|
2018-07-02 02:53:11 +02:00
|
|
|
assert(msatoshi);
|
|
|
|
assert(*msatoshi == 547);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:50:20 +02:00
|
|
|
static void sendpay_nulltok(void)
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 'A', '123']");
|
|
|
|
|
|
|
|
const jsmntok_t *routetok, *note = (void *) 65535;
|
|
|
|
u64 *msatoshi;
|
2018-08-10 22:16:22 +02:00
|
|
|
unsigned *cltv;
|
2018-07-04 18:50:20 +02:00
|
|
|
|
2018-07-12 20:04:45 +02:00
|
|
|
if (!param(cmd, j->buffer, j->toks,
|
2018-08-10 17:00:34 +02:00
|
|
|
p_req_tal("route", json_tok_tok, &routetok),
|
2018-08-10 22:16:22 +02:00
|
|
|
p_req_tal("cltv", json_tok_number, &cltv),
|
2018-08-10 17:00:34 +02:00
|
|
|
p_opt_tal("note", json_tok_tok, ¬e),
|
2018-08-13 18:16:41 +02:00
|
|
|
p_opt_tal("msatoshi", json_tok_u64, &msatoshi),
|
2018-07-12 20:04:45 +02:00
|
|
|
NULL))
|
2018-07-04 18:50:20 +02:00
|
|
|
assert(false);
|
|
|
|
|
|
|
|
assert(note == NULL);
|
|
|
|
assert(msatoshi == NULL);
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:15:30 +02:00
|
|
|
static 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);
|
|
|
|
|
2018-08-13 18:16:41 +02:00
|
|
|
if (json_to_u64(buffer, tok, *msatoshi_val) && *msatoshi_val != 0)
|
2018-08-10 04:15:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 'lightning', 24, 'tok', 543 ]");
|
|
|
|
|
|
|
|
struct json_escaped *label;
|
|
|
|
u64 *msat;
|
|
|
|
u64 *msat_opt1, *msat_opt2;
|
|
|
|
const jsmntok_t *tok;
|
|
|
|
|
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
|
|
|
p_req_tal("description", json_tok_label_x, &label),
|
|
|
|
p_req_tal("msat", json_tok_msat, &msat),
|
2018-08-10 17:00:34 +02:00
|
|
|
p_req_tal("tok", json_tok_tok, &tok),
|
2018-08-10 04:15:30 +02:00
|
|
|
p_opt_tal("msat_opt1", json_tok_msat, &msat_opt1),
|
|
|
|
p_opt_tal("msat_opt2", json_tok_msat, &msat_opt2),
|
|
|
|
NULL));
|
|
|
|
assert(label != NULL);
|
|
|
|
assert(!strcmp(label->s, "lightning"));
|
|
|
|
assert(*msat == 24);
|
|
|
|
assert(tok);
|
|
|
|
assert(msat_opt1);
|
|
|
|
assert(*msat_opt1 == 543);
|
|
|
|
assert(msat_opt2 == NULL);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 3 'foo' ]");
|
|
|
|
struct json_escaped *label, *foo;
|
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
|
|
|
p_req_tal("label", json_tok_label_x, &label),
|
|
|
|
p_opt_tal("foo", json_tok_label_x, &foo),
|
|
|
|
NULL));
|
|
|
|
assert(!strcmp(label->s, "3"));
|
|
|
|
assert(!strcmp(foo->s, "foo"));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
u64 *msat;
|
|
|
|
u64 *msat2;
|
|
|
|
struct json *j = json_parse(cmd, "[ 3 ]");
|
|
|
|
assert(param(cmd, j->buffer, j->toks,
|
|
|
|
p_opt_def_tal("msat", json_tok_msat, &msat, 23),
|
|
|
|
p_opt_def_tal("msat2", json_tok_msat, &msat2, 53),
|
|
|
|
NULL));
|
|
|
|
assert(*msat == 3);
|
|
|
|
assert(msat2);
|
|
|
|
assert(*msat2 == 53);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void advanced_fail(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 'anyx' ]");
|
|
|
|
u64 *msat;
|
|
|
|
assert(!param(cmd, j->buffer, j->toks,
|
|
|
|
p_req_tal("msat", json_tok_msat, &msat),
|
|
|
|
NULL));
|
|
|
|
assert(check_fail());
|
|
|
|
assert(strstr(fail_msg, "'msat' should be a positive"
|
|
|
|
" number or 'any', not 'anyx'"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
setup_locale();
|
2018-07-02 02:53:11 +02:00
|
|
|
setup_tmpctx();
|
|
|
|
cmd = tal(tmpctx, struct command);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
fail_msg = tal_arr(cmd, char, 10000);
|
|
|
|
|
|
|
|
zero_params();
|
|
|
|
sanity();
|
|
|
|
tok_tok();
|
|
|
|
null_params();
|
|
|
|
#if DEVELOPER
|
|
|
|
bad_programmer();
|
|
|
|
#endif
|
2018-07-04 03:46:52 +02:00
|
|
|
dup_names();
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
five_hundred_params();
|
|
|
|
sendpay();
|
2018-07-04 18:50:20 +02:00
|
|
|
sendpay_nulltok();
|
2018-08-10 04:15:30 +02:00
|
|
|
advanced();
|
|
|
|
advanced_fail();
|
2018-07-02 02:53:11 +02:00
|
|
|
tal_free(tmpctx);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
printf("run-params ok\n");
|
|
|
|
}
|