Skip to content

Commit 6d0ab07

Browse files
committed
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.
1 parent a4baf3f commit 6d0ab07

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

src/rpc/client.cpp

+12-20
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,16 @@ class CRPCConvertTable
225225
public:
226226
CRPCConvertTable();
227227

228-
bool convert(const std::string& method, int idx) {
229-
return (members.count(std::make_pair(method, idx)) > 0);
228+
/** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
229+
UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, int param_idx)
230+
{
231+
return members.count(std::make_pair(method, param_idx)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
230232
}
231-
bool convert(const std::string& method, const std::string& name) {
232-
return (membersByName.count(std::make_pair(method, name)) > 0);
233+
234+
/** Return arg_value as UniValue, and first parse it if it is a non-string parameter */
235+
UniValue ArgToUniValue(const std::string& arg_value, const std::string& method, const std::string& param_name)
236+
{
237+
return membersByName.count(std::make_pair(method, param_name)) > 0 ? ParseNonRFCJSONValue(arg_value) : arg_value;
233238
}
234239
};
235240

@@ -261,14 +266,7 @@ UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::s
261266

262267
for (unsigned int idx = 0; idx < strParams.size(); idx++) {
263268
const std::string& strVal = strParams[idx];
264-
265-
if (!rpcCvtTable.convert(strMethod, idx)) {
266-
// insert string value directly
267-
params.push_back(strVal);
268-
} else {
269-
// parse string as JSON, insert bool/number/object/etc. value
270-
params.push_back(ParseNonRFCJSONValue(strVal));
271-
}
269+
params.push_back(rpcCvtTable.ArgToUniValue(strVal, strMethod, idx));
272270
}
273271

274272
return params;
@@ -282,7 +280,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
282280
for (const std::string &s: strParams) {
283281
size_t pos = s.find('=');
284282
if (pos == std::string::npos) {
285-
positional_args.push_back(rpcCvtTable.convert(strMethod, positional_args.size()) ? ParseNonRFCJSONValue(s) : s);
283+
positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size()));
286284
continue;
287285
}
288286

@@ -292,13 +290,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
292290
// Intentionally overwrite earlier named values with later ones as a
293291
// convenience for scripts and command line users that want to merge
294292
// options.
295-
if (!rpcCvtTable.convert(strMethod, name)) {
296-
// insert string value directly
297-
params.pushKV(name, value);
298-
} else {
299-
// parse string as JSON, insert bool/number/object/etc. value
300-
params.pushKV(name, ParseNonRFCJSONValue(value));
301-
}
293+
params.pushKV(name, rpcCvtTable.ArgToUniValue(value, strMethod, name));
302294
}
303295

304296
if (!positional_args.empty()) {

0 commit comments

Comments
 (0)