Skip to content

Commit c261227

Browse files
committed
RPC: Support specifying different types for param aliases
1 parent 96ec3b6 commit c261227

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/rpc/util.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,15 @@ UniValue RPCHelpMan::GetArgMap() const
793793
for (int i{0}; i < int(m_args.size()); ++i) {
794794
const auto& arg = m_args.at(i);
795795
std::vector<std::string> arg_names = SplitString(arg.m_names, '|');
796+
RPCArg::Type argtype = arg.m_type;
797+
size_t arg_num = 0;
796798
for (const auto& arg_name : arg_names) {
797-
push_back_arg_info(m_name, i, arg_name, arg.m_type);
798-
if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
799+
if (!arg.m_type_per_name.empty()) {
800+
argtype = arg.m_type_per_name.at(arg_num++);
801+
}
802+
803+
push_back_arg_info(m_name, i, arg_name, argtype);
804+
if (argtype == RPCArg::Type::OBJ_NAMED_PARAMS) {
799805
for (const auto& inner : arg.m_inner) {
800806
std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
801807
for (const std::string& inner_name : inner_names) {
@@ -846,13 +852,15 @@ UniValue RPCArg::MatchesType(const UniValue& request) const
846852
{
847853
if (m_opts.skip_type_check) return true;
848854
if (IsOptional() && request.isNull()) return true;
849-
const auto exp_type{ExpectedType(m_type)};
850-
if (!exp_type) return true; // nothing to check
855+
for (auto type : m_type_per_name.empty() ? std::vector<RPCArg::Type>{m_type} : m_type_per_name) {
856+
const auto exp_type{ExpectedType(type)};
857+
if (!exp_type) return true; // nothing to check
851858

852-
if (*exp_type != request.getType()) {
853-
return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));
859+
if (*exp_type == request.getType()) {
860+
return true;
861+
}
854862
}
855-
return true;
863+
return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*ExpectedType(m_type)));
856864
}
857865

858866
std::string RPCArg::GetFirstName() const

src/rpc/util.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <univalue.h>
2020
#include <util/check.h>
2121

22+
#include <algorithm>
2223
#include <cstddef>
2324
#include <cstdint>
2425
#include <functional>
@@ -202,6 +203,7 @@ struct RPCArg {
202203

203204
const std::string m_names; //!< The name of the arg (can be empty for inner args, can contain multiple aliases separated by | for named request arguments)
204205
const Type m_type;
206+
const std::vector<Type> m_type_per_name;
205207
const std::vector<RPCArg> m_inner; //!< Only used for arrays or dicts
206208
const Fallback m_fallback;
207209
const std::string m_description;
@@ -222,6 +224,24 @@ struct RPCArg {
222224
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ && type != Type::OBJ_NAMED_PARAMS && type != Type::OBJ_USER_KEYS);
223225
}
224226

227+
RPCArg(
228+
std::string name,
229+
std::vector<Type> types,
230+
Fallback fallback,
231+
std::string description,
232+
std::vector<RPCArg> inner = {},
233+
RPCArgOptions opts = {})
234+
: m_names{std::move(name)},
235+
m_type{types.at(0)},
236+
m_type_per_name{std::move(types)},
237+
m_inner{std::move(inner)},
238+
m_fallback{std::move(fallback)},
239+
m_description{std::move(description)},
240+
m_opts{std::move(opts)}
241+
{
242+
CHECK_NONFATAL(m_type_per_name.size() == size_t(std::count(m_names.begin(), m_names.end(), '|')) + 1);
243+
}
244+
225245
RPCArg(
226246
std::string name,
227247
Type type,

0 commit comments

Comments
 (0)