lightningd: set filter when we see 'filter' object.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: JSON-RPC: `filter` object allows reduction of JSON response to (most) commands.
This commit is contained in:
Rusty Russell 2022-11-09 14:10:57 +10:30
parent 3b4c1968a3
commit 2a14afbf21

View file

@ -462,6 +462,16 @@ struct command_result *command_success(struct command *cmd,
{
assert(cmd);
assert(cmd->json_stream == result);
/* Filter will get upset if we close "result" object it didn't
* see! */
if (cmd->filter) {
const char *err = json_stream_detach_filter(tmpctx, result);
if (err)
json_add_string(result, "warning_parameter_filter",
err);
}
json_object_end(result);
json_object_end(result);
@ -606,6 +616,10 @@ struct json_stream *json_stream_success(struct command *cmd)
{
struct json_stream *r = json_start(cmd);
json_object_start(r, "result");
/* We have results? OK, start filtering */
if (cmd->filter)
json_stream_attach_filter(r, cmd->filter);
return r;
}
@ -869,7 +883,7 @@ REGISTER_PLUGIN_HOOK(rpc_command,
static struct command_result *
parse_request(struct json_connection *jcon, const jsmntok_t tok[])
{
const jsmntok_t *method, *id, *params, *jsonrpc;
const jsmntok_t *method, *id, *params, *filter, *jsonrpc;
struct command *c;
struct rpc_command_hook_payload *rpc_hook;
bool completed;
@ -882,6 +896,7 @@ parse_request(struct json_connection *jcon, const jsmntok_t tok[])
method = json_get_member(jcon->buffer, tok, "method");
params = json_get_member(jcon->buffer, tok, "params");
filter = json_get_member(jcon->buffer, tok, "filter");
id = json_get_member(jcon->buffer, tok, "id");
if (!id) {
@ -929,6 +944,13 @@ parse_request(struct json_connection *jcon, const jsmntok_t tok[])
"Expected string for method");
}
if (filter) {
struct command_result *ret;
ret = parse_filter(c, "filter", jcon->buffer, filter);
if (ret)
return ret;
}
/* Debug was too chatty, so we use IO here, even though we're
* actually just logging the id */
log_io(jcon->log, LOG_IO_IN, NULL, c->id, NULL, 0);