Skip to content

Commit

Permalink
Add ParseRange function to parse args of the form int/[int,int]
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Feb 28, 2019
1 parent 29c24b0 commit 7aa6a8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/rpc/util.cpp
Expand Up @@ -523,3 +523,17 @@ std::string RPCArg::ToString(const bool oneline) const
}
assert(false);
}

std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
{
if (value.isNum()) {
return {0, value.get_int64()};
}
if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
int64_t low = value[0].get_int64();
int64_t high = value[1].get_int64();
if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
return {low, high};
}
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
}
3 changes: 3 additions & 0 deletions src/rpc/util.h
Expand Up @@ -38,6 +38,9 @@ unsigned int ParseConfirmTarget(const UniValue& value);
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr);
UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string = "");

//! Parse a JSON range specified as int64, or [int64, int64]
std::pair<int64_t, int64_t> ParseRange(const UniValue& value);

struct RPCArg {
enum class Type {
OBJ,
Expand Down

0 comments on commit 7aa6a8a

Please sign in to comment.