mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-26 05:48:20 +01:00
Merge bitcoin/bitcoin#29277: RPC: access RPC arguments by name
30a6c99935rpc: access some args by name (stickies-v)bbb31269bfrpc: add named arg helper (stickies-v)13525e0c24rpc: add arg helper unit test (stickies-v) Pull request description: Adds string overloads for the `RPCHelpMan::Arg` and `RPCHelpMan::MaybeArg` helpers to be able to access RPC arguments by name instead of index number. Especially in RPCs with a large number of parameters, this can be quite helpful. Example usage: ```cpp const auto action{self.Arg<std::string>("action")}; ``` Most of the LoC is adding test coverage and documentation updates. No behaviour change. An alternative approach to #27788 with significantly less overhaul. ACKs for top commit: fjahr: Code review ACK30a6c99935maflcko: ACK30a6c99935🥑 ryanofsky: Code review ACK30a6c99935. Nice change! Implementation is surprisingly simple and additional unit test coverage is welcome, too. Tree-SHA512: 4904f5f914fe1d421d32f60edb7c5a028c8ea0f140a2f207a106b4752d441164e073066a6bf2e17693f859fe847815a96609d3cf521e0ac4178d8cd09362ea3d
This commit is contained in:
@@ -404,18 +404,25 @@ public:
|
||||
|
||||
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
|
||||
* documentation, or a falsy value if no default was given.
|
||||
* @brief Helper to get a required or default-valued request argument.
|
||||
*
|
||||
* 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.
|
||||
* Use this function when the argument is required or when it has a default value. If the
|
||||
* argument is optional and may not be provided, use MaybeArg instead.
|
||||
*
|
||||
* The Type passed to this helper must match the corresponding
|
||||
* RPCArg::Type.
|
||||
* This function only works during m_fun(), i.e., it should only be used in
|
||||
* RPC method implementations. It 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 documentation.
|
||||
*
|
||||
* There are two overloads of this function:
|
||||
* - Use Arg<Type>(size_t i) to get the argument (or the default value) by index.
|
||||
* - Use Arg<Type>(const std::string& key) to get the argument (or the default value) by key.
|
||||
*
|
||||
* The Type passed to this helper must match the corresponding RPCArg::Type.
|
||||
*
|
||||
* @return The value of the RPC argument (or the default value) cast to type Type.
|
||||
*
|
||||
* @see MaybeArg for handling optional arguments without default values.
|
||||
*/
|
||||
template <typename R>
|
||||
auto Arg(size_t i) const
|
||||
@@ -429,6 +436,34 @@ public:
|
||||
return ArgValue<const R&>(i);
|
||||
}
|
||||
}
|
||||
template<typename R>
|
||||
auto Arg(std::string_view key) const
|
||||
{
|
||||
return Arg<R>(GetParamIndex(key));
|
||||
}
|
||||
/**
|
||||
* @brief Helper to get an optional request argument.
|
||||
*
|
||||
* Use this function when the argument is optional and does not have a default value. If the
|
||||
* argument is required or has a default value, use Arg instead.
|
||||
*
|
||||
* This function only works during m_fun(), i.e., it should only be used in
|
||||
* RPC method implementations. It internally checks whether the user-passed
|
||||
* argument isNull() and parses (from JSON) and returns the user-passed argument,
|
||||
* or a falsy value if no argument was passed.
|
||||
*
|
||||
* There are two overloads of this function:
|
||||
* - Use MaybeArg<Type>(size_t i) to get the optional argument by index.
|
||||
* - Use MaybeArg<Type>(const std::string& key) to get the optional argument by key.
|
||||
*
|
||||
* The Type passed to this helper must match the corresponding RPCArg::Type.
|
||||
*
|
||||
* @return For integral and floating-point types, a std::optional<Type> is returned.
|
||||
* For other types, a Type* pointer to the argument is returned. If the
|
||||
* argument is not provided, std::nullopt or a null pointer is returned.
|
||||
*
|
||||
* @see Arg for handling arguments that are required or have a default value.
|
||||
*/
|
||||
template <typename R>
|
||||
auto MaybeArg(size_t i) const
|
||||
{
|
||||
@@ -441,6 +476,11 @@ public:
|
||||
return ArgValue<const R*>(i);
|
||||
}
|
||||
}
|
||||
template<typename R>
|
||||
auto MaybeArg(std::string_view key) const
|
||||
{
|
||||
return MaybeArg<R>(GetParamIndex(key));
|
||||
}
|
||||
std::string ToString() const;
|
||||
/** Return the named args that need to be converted from string to another JSON type */
|
||||
UniValue GetArgMap() const;
|
||||
@@ -460,6 +500,8 @@ private:
|
||||
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;
|
||||
//! Return positional index of a parameter using its name as key.
|
||||
size_t GetParamIndex(std::string_view key) const;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user