Skip to content

Commit 78c3081

Browse files
author
MarcoFalke
committed
Merge bitcoin#26506: refactor: rpc: use convenience fn to auto parse non-string parameters
6d0ab07 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 6d0ab07 Tree-SHA512: 8cbf68a17cfbdce1e31a19916f447a2965c139fdef00c19e32a9b679f4a4015dfe69ceea0bbe1723711e1c5033ea8d4005d1f4485dfbeea22226140f8cbe8aa3
2 parents 500f25d + 6d0ab07 commit 78c3081

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
@@ -227,11 +227,16 @@ class CRPCConvertTable
227227
public:
228228
CRPCConvertTable();
229229

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

@@ -263,14 +268,7 @@ UniValue RPCConvertValues(const std::string &strMethod, const std::vector<std::s
263268

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

276274
return params;
@@ -284,7 +282,7 @@ UniValue RPCConvertNamedValues(const std::string &strMethod, const std::vector<s
284282
for (const std::string &s: strParams) {
285283
size_t pos = s.find('=');
286284
if (pos == std::string::npos) {
287-
positional_args.push_back(rpcCvtTable.convert(strMethod, positional_args.size()) ? ParseNonRFCJSONValue(s) : s);
285+
positional_args.push_back(rpcCvtTable.ArgToUniValue(s, strMethod, positional_args.size()));
288286
continue;
289287
}
290288

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

306298
if (!positional_args.empty()) {

0 commit comments

Comments
 (0)