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
|
|
|
#ifndef LIGHTNING_LIGHTNINGD_PARAMS_H
|
|
|
|
#define LIGHTNING_LIGHTNINGD_PARAMS_H
|
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/ccan/typesafe_cb/typesafe_cb.h>
|
|
|
|
|
|
|
|
struct param;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Typesafe callback system for unmarshalling and validating json parameters.
|
|
|
|
|
|
|
|
Typical usage:
|
|
|
|
unsigned cltv;
|
|
|
|
const jsmntok_t *note;
|
2018-07-02 02:53:11 +02:00
|
|
|
u64 *msatoshi;
|
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 (!param_parse(cmd, buffer, tokens,
|
|
|
|
param_req("cltv", json_tok_number, &cltv),
|
2018-07-02 02:53:11 +02:00
|
|
|
param_opt_tok("note", ¬e),
|
|
|
|
param_opt("msatoshi", json_tok_u64, &msatoshi),
|
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
|
|
|
NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
At this point in the code you can be assured the json tokens were successfully
|
|
|
|
parsed. If not, param_parse() returned NULL, having already called
|
|
|
|
command_fail() with a descriptive error message. The data section of the json
|
|
|
|
result contains the offending parameter and its value.
|
|
|
|
|
|
|
|
cltv is a required parameter, and is set correctly.
|
|
|
|
|
2018-07-02 02:53:11 +02:00
|
|
|
note and msatoshi are optional parameters. Their argument will be set to NULL
|
|
|
|
if they are not provided.
|
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-02 02:53:11 +02:00
|
|
|
The note parameter uses a special callback, param_opt_tok: it
|
|
|
|
simply sets note to the appropriate value (or NULL) and lets the
|
|
|
|
handler do the validating.
|
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
|
|
|
|
|
|
|
There are canned failure messages for common callbacks. An example:
|
|
|
|
|
|
|
|
'msatoshi' should be an unsigned 64 bit integer, not '123z'
|
|
|
|
|
|
|
|
Otherwise a generic message is provided.
|
|
|
|
*/
|
|
|
|
struct param **param_parse(struct command *cmd, const char *buffer,
|
|
|
|
const jsmntok_t params[], ...);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback provided must follow this signature; e.g.,
|
|
|
|
* bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *arg)
|
|
|
|
*/
|
|
|
|
typedef bool(*param_cb)(const char *buffer, const jsmntok_t *tok, void *arg);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a handler to unmarshal a required json token into @arg. The handler must
|
|
|
|
* return true on success and false on failure. Upon failure, command_fail will be
|
|
|
|
* called with a descriptive error message.
|
|
|
|
*
|
|
|
|
* This operation is typesafe; i.e., a compilation error will occur if the types
|
|
|
|
* of @arg and the last parameter of @cb do not match.
|
|
|
|
*
|
|
|
|
* Returns an opaque pointer that can be later used in param_is_set().
|
|
|
|
*/
|
|
|
|
#define param_req(name, cb, arg) \
|
2018-07-02 03:11:08 +02:00
|
|
|
param_add_(NULL, name"", \
|
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
|
|
|
typesafe_cb_preargs(bool, void *, \
|
|
|
|
(cb), (arg), \
|
|
|
|
const char *, \
|
|
|
|
const jsmntok_t *), \
|
2018-07-02 02:53:11 +02:00
|
|
|
(arg), 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
|
|
|
/*
|
|
|
|
* Same as above but for optional parameters.
|
|
|
|
*/
|
2018-07-02 02:53:11 +02:00
|
|
|
#define param_opt(ctx, name, cb, arg) \
|
2018-07-02 03:11:08 +02:00
|
|
|
param_add_(ctx, name"", \
|
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
|
|
|
typesafe_cb_preargs(bool, void *, \
|
2018-07-02 02:53:11 +02:00
|
|
|
(cb), *(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, ¬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
|
|
|
const char *, \
|
|
|
|
const jsmntok_t *), \
|
2018-07-02 02:53:11 +02:00
|
|
|
(arg), sizeof(**arg))
|
2018-07-02 03:11:08 +02:00
|
|
|
struct param * param_add_(const tal_t *ctx, const char *name, param_cb cb, void *arg, size_t argsize);
|
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-02 02:53:11 +02:00
|
|
|
#define param_opt_tok(ctx, name, arg) \
|
2018-07-02 03:11:08 +02:00
|
|
|
param_opt_add_(ctx, name"", arg)
|
2018-07-02 02:53:11 +02:00
|
|
|
|
2018-07-02 03:11:08 +02:00
|
|
|
struct param *param_opt_add_(const tal_t *ctx, const char *name, const jsmntok_t **tok);
|
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 /* LIGHTNING_LIGHTNINGD_PARAMS_H */
|