mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-25 21:39:05 +01:00
rpc: Add MaybeArg() and Arg() default helper
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#define BITCOIN_RPC_UTIL_H
|
||||
|
||||
#include <addresstype.h>
|
||||
#include <consensus/amount.h>
|
||||
#include <node/transaction.h>
|
||||
#include <outputtype.h>
|
||||
#include <protocol.h>
|
||||
@@ -14,13 +15,29 @@
|
||||
#include <rpc/request.h>
|
||||
#include <script/script.h>
|
||||
#include <script/sign.h>
|
||||
#include <uint256.h>
|
||||
#include <univalue.h>
|
||||
#include <util/check.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
class JSONRPCRequest;
|
||||
enum ServiceFlags : uint64_t;
|
||||
enum class OutputType;
|
||||
enum class TransactionError;
|
||||
struct FlatSigningProvider;
|
||||
struct bilingual_str;
|
||||
|
||||
static constexpr bool DEFAULT_RPC_DOC_CHECK{
|
||||
#ifdef RPC_DOC_CHECK
|
||||
true
|
||||
@@ -383,6 +400,44 @@ public:
|
||||
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples, RPCMethodImpl fun);
|
||||
|
||||
UniValue HandleRequest(const JSONRPCRequest& request) const;
|
||||
/**
|
||||
* Helper to get a request argument.
|
||||
* This function only works during m_fun(), i.e. it should only be used in
|
||||
* RPC method implementations. The helper internally checks whether the
|
||||
* user-passed argument isNull() and parses (from JSON) and returns the
|
||||
* user-passed argument, or the default value derived from the RPCArg
|
||||
* documention, or a falsy value if no default was given.
|
||||
*
|
||||
* Use Arg<Type>(i) to get the argument or its default value. Otherwise,
|
||||
* use MaybeArg<Type>(i) to get the optional argument or a falsy value.
|
||||
*
|
||||
* The Type passed to this helper must match the corresponding
|
||||
* RPCArg::Type.
|
||||
*/
|
||||
template <typename R>
|
||||
auto Arg(size_t i) const
|
||||
{
|
||||
// Return argument (required or with default value).
|
||||
if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) {
|
||||
// Return numbers by value.
|
||||
return ArgValue<R>(i);
|
||||
} else {
|
||||
// Return everything else by reference.
|
||||
return ArgValue<const R&>(i);
|
||||
}
|
||||
}
|
||||
template <typename R>
|
||||
auto MaybeArg(size_t i) const
|
||||
{
|
||||
// Return optional argument (without default).
|
||||
if constexpr (std::is_integral_v<R> || std::is_floating_point_v<R>) {
|
||||
// Return numbers by value, wrapped in optional.
|
||||
return ArgValue<std::optional<R>>(i);
|
||||
} else {
|
||||
// Return other types by pointer.
|
||||
return ArgValue<const R*>(i);
|
||||
}
|
||||
}
|
||||
std::string ToString() const;
|
||||
/** Return the named args that need to be converted from string to another JSON type */
|
||||
UniValue GetArgMap() const;
|
||||
@@ -399,6 +454,9 @@ private:
|
||||
const std::vector<RPCArg> m_args;
|
||||
const RPCResults m_results;
|
||||
const RPCExamples m_examples;
|
||||
mutable const JSONRPCRequest* m_req{nullptr}; // A pointer to the request for the duration of m_fun()
|
||||
template <typename R>
|
||||
R ArgValue(size_t i) const;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user