|
|
|
#ifndef LIGHTNING_LIGHTNINGD_PARAM_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_PARAM_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, &note),
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.
7 years ago
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Typesafe callback system for unmarshalling and validating json parameters.
|
|
|
|
|
|
|
|
Typical usage:
|
|
|
|
unsigned *cltv;
|
|
|
|
u64 *msatoshi;
|
|
|
|
const jsmntok_t *note;
|
|
|
|
u64 *expiry;
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_req("cltv", json_tok_number, &cltv),
|
|
|
|
p_opt("msatoshi", json_tok_u64, &msatoshi),
|
|
|
|
p_opt_tok("note", ¬e),
|
|
|
|
p_opt_def("expiry", json_tok_u64, &expiry, 3600),
|
|
|
|
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, &note),
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.
7 years ago
|
|
|
return;
|
|
|
|
|
|
|
|
See json_invoice() for a good example. The common callbacks can be found in
|
|
|
|
lightningd/json.c. Use them as an example for writing your own custom
|
|
|
|
callbacks.
|
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, &note),
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.
7 years ago
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the json tokens. @params can be an array of values or an object
|
|
|
|
* of named values.
|
|
|
|
*/
|
|
|
|
bool param(struct command *cmd, const char *buffer,
|
|
|
|
const jsmntok_t params[], ...);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The callback signature. Callbacks must return true on success. On failure they
|
|
|
|
* must call comand_fail and return false.
|
|
|
|
*/
|
|
|
|
typedef bool(*param_cbx)(struct command *cmd,
|
|
|
|
const char *name,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
void **arg);
|
|
|
|
|
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, &note),
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.
7 years ago
|
|
|
/*
|
|
|
|
* Add a required parameter.
|
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, &note),
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.
7 years ago
|
|
|
*/
|
|
|
|
#define p_req(name, cbx, arg) \
|
|
|
|
name"", \
|
|
|
|
true, \
|
|
|
|
(cbx), \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == 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, &note),
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.
7 years ago
|
|
|
/*
|
|
|
|
* Add an optional parameter. *arg is set to NULL if it isn't found.
|
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, &note),
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.
7 years ago
|
|
|
*/
|
|
|
|
#define p_opt(name, cbx, arg) \
|
|
|
|
name"", \
|
|
|
|
false, \
|
|
|
|
(cbx), \
|
|
|
|
({ *arg = NULL; \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == true); })
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an optional parameter. *arg is set to @def if it isn't found.
|
|
|
|
*/
|
|
|
|
#define p_opt_def(name, cbx, arg, def) \
|
|
|
|
name"", \
|
|
|
|
false, \
|
|
|
|
(cbx), \
|
|
|
|
({ (*arg) = tal((cmd), typeof(**arg)); \
|
|
|
|
(**arg) = (def); \
|
|
|
|
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const char *)NULL, \
|
|
|
|
(const jsmntok_t *)NULL, \
|
|
|
|
(arg)) == true); })
|
|
|
|
|
|
|
|
#endif /* LIGHTNING_LIGHTNINGD_PARAM_H */
|