Merge bitcoin/bitcoin#26506: refactor: rpc: use convenience fn to auto parse non-string parameters

6d0ab07e817725369df699791b9c5b2fae204990 refactor: use convenience fn to auto parse non-string parameters (stickies-v)

Pull request description:

  Minimizes code duplication and improves function naming by having a single (overloaded) convenience function `ParseIfNonString` that both checks if the parameter is a non-string parameter and automatically parses the value if so.

ACKs for top commit:
  aureleoules:
    ACK 6d0ab07e817725369df699791b9c5b2fae204990

Tree-SHA512: 8cbf68a17cfbdce1e31a19916f447a2965c139fdef00c19e32a9b679f4a4015dfe69ceea0bbe1723711e1c5033ea8d4005d1f4485dfbeea22226140f8cbe8aa3
This commit is contained in:
MarcoFalke 2023-01-18 13:12:02 +01:00 committed by pasta
parent 662302c42b
commit 8cc5f11a2f
No known key found for this signature in database
GPG Key ID: E2F3D7916E722D38

View File

@ -250,11 +250,16 @@ private:
public: public:
CRPCConvertTable(); CRPCConvertTable();
bool convert(const std::string& method, int idx) { /** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
return (members.count(std::make_pair(method, idx)) > 0); 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;
} }
}; };
@ -286,14 +291,7 @@ UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::s
for (unsigned int idx = 0; idx < strParams.size(); idx++) { for (unsigned int idx = 0; idx < strParams.size(); idx++) {
const std::string& strVal = strParams[idx]; const std::string& strVal = strParams[idx];
params.push_back(rpcCvtTable.ArgToUniValue(strVal, strMethod, 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));
}
} }
return params; return params;
@ -307,7 +305,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
for (const std::string &s: strParams) { for (const std::string &s: strParams) {
size_t pos = s.find('='); size_t pos = s.find('=');
if (pos == std::string::npos) { 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; continue;
} }
@ -317,13 +315,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
// Intentionally overwrite earlier named values with later ones as a // Intentionally overwrite earlier named values with later ones as a
// convenience for scripts and command line users that want to merge // convenience for scripts and command line users that want to merge
// options. // options.
if (!rpcCvtTable.convert(strMethod, name)) { params.pushKV(name, rpcCvtTable.ArgToUniValue(value, 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));
}
} }
if (!positional_args.empty()) { if (!positional_args.empty()) {