rpc: refactor: use string_view in Arg/MaybeArg

Modernizes interface by not forcing users to deal with raw pointers,
without adding copying overhead. Generalizes the logic of whether
we return by value or by optional/pointer.

In cases where functions take a `const std::string&` and it would
be too much work to update them, a string copy is made (which was
already happening anyway).
This commit is contained in:
stickies-v
2025-07-14 16:53:46 +01:00
parent 75353a0163
commit b3bf18f0ba
18 changed files with 51 additions and 46 deletions

View File

@@ -14,6 +14,7 @@
#include <util/time.h>
#include <any>
#include <string_view>
#include <boost/test/unit_test.hpp>
@@ -618,9 +619,9 @@ BOOST_AUTO_TEST_CASE(rpc_arg_helper)
//! Check that `self.Arg` returns the same value as the `request.params` accessors
RPCHelpMan::RPCMethodImpl check_positional = [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
BOOST_CHECK_EQUAL(self.Arg<int>("req_int"), request.params[0].getInt<int>());
BOOST_CHECK_EQUAL(self.Arg<std::string>("req_str"), request.params[1].get_str());
BOOST_CHECK_EQUAL(self.Arg<std::string_view>("req_str"), request.params[1].get_str());
BOOST_CHECK_EQUAL(self.Arg<uint64_t>("def_uint64_t"), request.params[2].isNull() ? DEFAULT_UINT64_T : request.params[2].getInt<uint64_t>());
BOOST_CHECK_EQUAL(self.Arg<std::string>("def_string"), request.params[3].isNull() ? DEFAULT_STRING : request.params[3].get_str());
BOOST_CHECK_EQUAL(self.Arg<std::string_view>("def_string"), request.params[3].isNull() ? DEFAULT_STRING : request.params[3].get_str());
BOOST_CHECK_EQUAL(self.Arg<bool>("def_bool"), request.params[4].isNull() ? DEFAULT_BOOL : request.params[4].get_bool());
if (!request.params[5].isNull()) {
BOOST_CHECK_EQUAL(self.MaybeArg<double>("opt_double").value(), request.params[5].get_real());
@@ -628,10 +629,9 @@ BOOST_AUTO_TEST_CASE(rpc_arg_helper)
BOOST_CHECK(!self.MaybeArg<double>("opt_double"));
}
if (!request.params[6].isNull()) {
BOOST_CHECK(self.MaybeArg<std::string>("opt_string"));
BOOST_CHECK_EQUAL(*self.MaybeArg<std::string>("opt_string"), request.params[6].get_str());
BOOST_CHECK_EQUAL(self.MaybeArg<std::string_view>("opt_string"), request.params[6].get_str());
} else {
BOOST_CHECK(!self.MaybeArg<std::string>("opt_string"));
BOOST_CHECK(!self.MaybeArg<std::string_view>("opt_string"));
}
return UniValue{};
};