aboutsummaryrefslogtreecommitdiff
path: root/src/rpc
diff options
context:
space:
mode:
authorstickies-v <stickies-v@protonmail.com>2022-11-15 15:56:19 +0000
committerstickies-v <stickies-v@protonmail.com>2023-01-04 16:06:34 +0000
commit6d0ab07e817725369df699791b9c5b2fae204990 (patch)
tree1febe470d0cf0e91691357a73edc879c54c15eee /src/rpc
parenta4baf3f177489f21b044468298825a70a1a15648 (diff)
downloadbitcoin-6d0ab07e817725369df699791b9c5b2fae204990.tar.xz
refactor: use convenience fn to auto parse non-string parameters
Minimizes code duplication and improves function naming by having a single (overloaded) convenience function that both checks if the parameter is a non-string parameter and automatically parses the value if so.
Diffstat (limited to 'src/rpc')
-rw-r--r--src/rpc/client.cpp32
1 files changed, 12 insertions, 20 deletions
diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp
index ea094976bf..55026d6b56 100644
--- a/src/rpc/client.cpp
+++ b/src/rpc/client.cpp
@@ -225,11 +225,16 @@ private:
public:
CRPCConvertTable();
- bool convert(const std::string& method, int idx) {
- return (members.count(std::make_pair(method, idx)) > 0);
+ /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
+ UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, int param_idx)
+ {
+ return members.count(std::make_pair(method, param_idx)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
}
- bool convert(const std::string& method, const std::string& name) {
- return (membersByName.count(std::make_pair(method, name)) > 0);
+
+ /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
+ UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, const std::string& param_name)
+ {
+ return membersByName.count(std::make_pair(method, param_name)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
}
};
@@ -261,14 +266,7 @@ UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::s
for (unsigned int idx = 0; idx < strParams.size(); idx++) {
const std::string& strVal = strParams[idx];
-
- if (!rpcCvtTable.convert(strMethod, idx)) {
- // insert string value directly
- params.push_back(strVal);
- } else {
- // parse string as JSON, insert bool/number/object/etc. value
- params.push_back(ParseNonRFCJSONValue(strVal));
- }
+ params.push_back(rpcCvtTable.ArgToUniValue(strVal, strMethod, idx));
}
return params;
@@ -282,7 +280,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
for (const std::string &s: strParams) {
size_t pos = s.find('=');
if (pos == std::string::npos) {
- positional_args.push_back(rpcCvtTable.convert(strMethod, positional_args.size()) ? ParseNonRFCJSONValue(s) : s);
+ positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size()));
continue;
}
@@ -292,13 +290,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
// Intentionally overwrite earlier named values with later ones as a
// convenience for scripts and command line users that want to merge
// options.
- if (!rpcCvtTable.convert(strMethod, name)) {
- // insert string value directly
- params.pushKV(name, value);
- } else {
- // parse string as JSON, insert bool/number/object/etc. value
- params.pushKV(name, ParseNonRFCJSONValue(value));
- }
+ params.pushKV(name, rpcCvtTable.ArgToUniValue(value, strMethod, name));
}
if (!positional_args.empty()) {