Merge branch 'rpcarg_type_per_name' into rpc_descriptorprocesspsbt_opts-28

This commit is contained in:
Luke Dashjr 2024-09-04 02:41:50 +00:00
commit 390e177215
2 changed files with 35 additions and 7 deletions

View File

@ -852,9 +852,15 @@ UniValue RPCHelpMan::GetArgMap() const
for (int i{0}; i < int(m_args.size()); ++i) {
const auto& arg = m_args.at(i);
std::vector<std::string> arg_names = SplitString(arg.m_names, '|');
RPCArg::Type argtype = arg.m_type;
size_t arg_num = 0;
for (const auto& arg_name : arg_names) {
push_back_arg_info(m_name, i, arg_name, arg.m_type);
if (arg.m_type == RPCArg::Type::OBJ_NAMED_PARAMS) {
if (!arg.m_type_per_name.empty()) {
argtype = arg.m_type_per_name.at(arg_num++);
}
push_back_arg_info(m_name, i, arg_name, argtype);
if (argtype == RPCArg::Type::OBJ_NAMED_PARAMS) {
for (const auto& inner : arg.m_inner) {
std::vector<std::string> inner_names = SplitString(inner.m_names, '|');
for (const std::string& inner_name : inner_names) {
@ -905,13 +911,15 @@ UniValue RPCArg::MatchesType(const UniValue& request) const
{
if (m_opts.skip_type_check) return true;
if (IsOptional() && request.isNull()) return true;
const auto exp_type{ExpectedType(m_type)};
if (!exp_type) return true; // nothing to check
for (auto type : m_type_per_name.empty() ? std::vector<RPCArg::Type>{m_type} : m_type_per_name) {
const auto exp_type{ExpectedType(type)};
if (!exp_type) return true; // nothing to check
if (*exp_type != request.getType()) {
return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*exp_type));
if (*exp_type == request.getType()) {
return true;
}
}
return true;
return strprintf("JSON value of type %s is not of expected type %s", uvTypeName(request.getType()), uvTypeName(*ExpectedType(m_type)));
}
std::string RPCArg::GetFirstName() const

View File

@ -18,6 +18,7 @@
#include <univalue.h>
#include <util/check.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <functional>
@ -210,6 +211,7 @@ struct RPCArg {
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)
const Type m_type;
const std::vector<Type> m_type_per_name;
const std::vector<RPCArg> m_inner; //!< Only used for arrays or dicts
const Fallback m_fallback;
const std::string m_description;
@ -230,6 +232,24 @@ struct RPCArg {
CHECK_NONFATAL(type != Type::ARR && type != Type::OBJ && type != Type::OBJ_NAMED_PARAMS && type != Type::OBJ_USER_KEYS);
}
RPCArg(
std::string name,
std::vector<Type> types,
Fallback fallback,
std::string description,
std::vector<RPCArg> inner = {},
RPCArgOptions opts = {})
: m_names{std::move(name)},
m_type{types.at(0)},
m_type_per_name{std::move(types)},
m_inner{std::move(inner)},
m_fallback{std::move(fallback)},
m_description{std::move(description)},
m_opts{std::move(opts)}
{
CHECK_NONFATAL(m_type_per_name.size() == size_t(std::count(m_names.begin(), m_names.end(), '|')) + 1);
}
RPCArg(
std::string name,
Type type,