Skip to content

Commit c6557a3

Browse files
committed
Merge branch 'rpcarg_type_per_name' into fee_histogram+pr15836_api
2 parents 1248d0d + c261227 commit c6557a3

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
@@ -852,9 +852,15 @@ UniValue RPCHelpMan::GetArgMap() const
852852
for (int i{0}; i < int(m_args.size()); ++i) {
853853
const auto& arg = m_args.at(i);
854854
std::vector<std::string> arg_names = SplitString(arg.m_names, '|');
855+
RPCArg::Type argtype = arg.m_type;
856+
size_t arg_num = 0;
855857
for (const auto& arg_name : arg_names) {
856-
push_back_arg_info(m_name, i, arg_name, arg.m_type);
857-
if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
858+
if (!arg.m_type_per_name.empty()) {
859+
argtype = arg.m_type_per_name.at(arg_num++);
860+
}
861+
862+
push_back_arg_info(m_name, i, arg_name, argtype);
863+
if (argtype == RPCArg::Type::OBJ_NAMED_PARAMS) {
858864
for (const auto& inner : arg.m_inner) {
859865
std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
860866
for (const std::string& inner_name : inner_names) {
@@ -905,13 +911,15 @@ UniValue RPCArg::MatchesType(const UniValue& request) const
905911
{
906912
if (m_opts.skip_type_check) return true;
907913
if (IsOptional() && request.isNull()) return true;
908-
const auto exp_type{ExpectedType(m_type)};
909-
if (!exp_type) return true; // nothing to check
914+
for (auto type : m_type_per_name.empty() ? std::vector<RPCArg::Type>{m_type} : m_type_per_name) {
915+
const auto exp_type{ExpectedType(type)};
916+
if (!exp_type) return true; // nothing to check
910917

911-
if (*exp_type != request.getType()) {
912-
return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));
918+
if (*exp_type == request.getType()) {
919+
return true;
920+
}
913921
}
914-
return true;
922+
return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*ExpectedType(m_type)));
915923
}
916924

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

src/rpc/util.h

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

21+
#include <algorithm>
2122
#include <cstddef>
2223
#include <cstdint>
2324
#include <functional>
@@ -210,6 +211,7 @@ struct RPCArg {
210211

211212
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)
212213
const Type m_type;
214+
const std::vector<Type> m_type_per_name;
213215
const std::vector<RPCArg> m_inner; //!< Only used for arrays or dicts
214216
const Fallback m_fallback;
215217
const std::string m_description;
@@ -230,6 +232,24 @@ struct RPCArg {
230232
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ && type != Type::OBJ_NAMED_PARAMS && type != Type::OBJ_USER_KEYS);
231233
}
232234

235+
RPCArg(
236+
std::string name,
237+
std::vector<Type> types,
238+
Fallback fallback,
239+
std::string description,
240+
std::vector<RPCArg> inner = {},
241+
RPCArgOptions opts = {})
242+
: m_names{std::move(name)},
243+
m_type{types.at(0)},
244+
m_type_per_name{std::move(types)},
245+
m_inner{std::move(inner)},
246+
m_fallback{std::move(fallback)},
247+
m_description{std::move(description)},
248+
m_opts{std::move(opts)}
249+
{
250+
CHECK_NONFATAL(m_type_per_name.size() == size_t(std::count(m_names.begin(), m_names.end(), '|')) + 1);
251+
}
252+
233253
RPCArg(
234254
std::string name,
235255
Type type,

0 commit comments

Comments
 (0)