jsonrpc: declare up front whether a response is success or fail.

Such an API is required for when we stream it directly.  Almost all our
handlers fit this pattern already, or nearly do.

We remove new_json_result() in favor of explicit json_stream_success()
and json_stream_fail(), but still allowing command_fail() if you just
want a simple all-in-one fail wrapper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-10-19 11:47:48 +10:30
parent 12adcda370
commit e46ce0fc84
21 changed files with 224 additions and 195 deletions

View File

@@ -703,9 +703,9 @@ void json_add_escaped_string(struct json_result *result, const char *fieldname,
tal_free(esc);
}
struct json_result *new_json_result(const tal_t *ctx)
static struct json_result *new_json_stream(struct command *cmd)
{
struct json_result *r = tal(ctx, struct json_result);
struct json_result *r = tal(cmd, struct json_result);
r->s = tal_strdup(r, "");
#if DEVELOPER
@@ -713,9 +713,29 @@ struct json_result *new_json_result(const tal_t *ctx)
#endif
r->indent = 0;
r->empty = true;
assert(!cmd->have_json_stream);
cmd->have_json_stream = true;
return r;
}
struct json_result *json_stream_success(struct command *cmd)
{
cmd->failcode = 0;
return new_json_stream(cmd);
}
struct json_result *json_stream_fail(struct command *cmd,
int code,
const char *errmsg)
{
assert(code);
assert(errmsg);
cmd->failcode = code;
cmd->errmsg = tal_strdup(cmd, errmsg);
return new_json_stream(cmd);
}
const char *json_result_string(const struct json_result *result)
{
#if DEVELOPER