Skip to content

Commit 7aa6a8a

Browse files
committed
Add ParseRange function to parse args of the form int/[int,int]
1 parent 29c24b0 commit 7aa6a8a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/rpc/util.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,17 @@ std::string RPCArg::ToString(const bool oneline) const
523523
}
524524
assert(false);
525525
}
526+
527+
std::pair<int64_t, int64_t> ParseRange(const UniValue& value)
528+
{
529+
if (value.isNum()) {
530+
return {0, value.get_int64()};
531+
}
532+
if (value.isArray() && value.size() == 2 && value[0].isNum() && value[1].isNum()) {
533+
int64_t low = value[0].get_int64();
534+
int64_t high = value[1].get_int64();
535+
if (low > high) throw JSONRPCError(RPC_INVALID_PARAMETER, "Range specified as [begin,end] must not have begin after end");
536+
return {low, high};
537+
}
538+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified as end or as [begin,end]");
539+
}

src/rpc/util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ unsigned int ParseConfirmTarget(const UniValue& value);
3838
RPCErrorCode RPCErrorFromTransactionError(TransactionError terr);
3939
UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_string = "");
4040

41+
//! Parse a JSON range specified as int64, or [int64, int64]
42+
std::pair<int64_t, int64_t> ParseRange(const UniValue& value);
43+
4144
struct RPCArg {
4245
enum class Type {
4346
OBJ,

0 commit comments

Comments
 (0)