Browse Source

json: move param node_id parser to common

Going to need this for a plugin that's external
travis-debug
lisa neigut 5 years ago
committed by Rusty Russell
parent
commit
19e65068a9
  1. 15
      common/json_tok.c
  2. 9
      common/json_tok.h
  3. 4
      common/test/run-param.c
  4. 17
      lightningd/json.c
  5. 6
      lightningd/json.h
  6. 4
      lightningd/test/run-jsonrpc.c

15
common/json_tok.c

@ -4,6 +4,7 @@
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/json_command.h>
#include <common/json_helpers.h>
#include <common/json_tok.h>
#include <common/jsonrpc_errors.h>
#include <common/param.h>
@ -199,3 +200,17 @@ struct command_result *param_sat_or_all(struct command *cmd, const char *name,
}
return param_sat(cmd, name, buffer, tok, sat);
}
struct command_result *param_node_id(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct node_id **id)
{
*id = tal(cmd, struct node_id);
if (json_to_node_id(buffer, tok, *id))
return NULL;
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a node id, not '%.*s'",
name, json_tok_full_len(tok),
json_tok_full(buffer, tok));
}

9
common/json_tok.h

@ -4,6 +4,7 @@
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <common/json.h>
#include <common/node_id.h>
struct amount_msat;
struct amount_sat;
@ -80,6 +81,14 @@ struct command_result *param_sat_or_all(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct amount_sat **sat);
/* Extract node_id from this string. Makes sure *id is valid. */
struct command_result *param_node_id(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct node_id **id);
/*
* Set the address of @out to @tok. Used as a callback by handlers that
* want to unmarshal @tok themselves.

4
common/test/run-param.c

@ -42,6 +42,10 @@ struct command_result *command_fail(struct command *cmd,
/* Generated stub for fromwire_fail */
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
/* Generated stub for json_to_node_id */
bool json_to_node_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct node_id *id UNNEEDED)
{ fprintf(stderr, "json_to_node_id called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
/* We do this lightningd-style: */

17
lightningd/json.c

@ -77,23 +77,6 @@ void json_add_txid(struct json_stream *result, const char *fieldname,
json_add_string(result, fieldname, hex);
}
struct command_result *param_node_id(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct node_id **id)
{
*id = tal(cmd, struct node_id);
if (json_to_node_id(buffer, tok, *id))
return NULL;
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a node id, not '%.*s'",
name, json_tok_full_len(tok),
json_tok_full(buffer, tok));
}
struct command_result *param_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey)

6
lightningd/json.h

@ -64,12 +64,6 @@ struct command_result *param_pubkey(struct command *cmd, const char *name,
struct command_result *param_txid(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct bitcoin_txid **txid);
/* Makes sure *id is valid. */
struct command_result *param_node_id(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct node_id **id);
struct command_result *param_short_channel_id(struct command *cmd,
const char *name,

4
lightningd/test/run-jsonrpc.c

@ -30,10 +30,6 @@ const char *feerate_name(enum feerate feerate UNNEEDED)
/* 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(); }
/* Generated stub for json_to_node_id */
bool json_to_node_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct node_id *id UNNEEDED)
{ fprintf(stderr, "json_to_node_id called!\n"); abort(); }
/* Generated stub for json_to_pubkey */
bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct pubkey *pubkey UNNEEDED)

Loading…
Cancel
Save