* Process named arguments into a vector of positional arguments, based on the * passed-in specification for the RPC call's arguments. */
| 393 | * passed-in specification for the RPC call's arguments. |
| 394 | */ |
| 395 | static inline JSONRPCRequest transformNamedArguments(const JSONRPCRequest& in, const std::vector<std::string>& argNames) |
| 396 | { |
| 397 | JSONRPCRequest out = in; |
| 398 | out.params = UniValue(UniValue::VARR); |
| 399 | // Build a map of parameters, and remove ones that have been processed, so that we can throw a focused error if |
| 400 | // there is an unknown one. |
| 401 | const std::vector<std::string>& keys = in.params.getKeys(); |
| 402 | const std::vector<UniValue>& values = in.params.getValues(); |
| 403 | std::unordered_map<std::string, const UniValue*> argsIn; |
| 404 | for (size_t i=0; i<keys.size(); ++i) { |
| 405 | argsIn[keys[i]] = &values[i]; |
| 406 | } |
| 407 | // Process expected parameters. |
| 408 | int hole = 0; |
| 409 | for (const std::string &argNamePattern: argNames) { |
| 410 | std::vector<std::string> vargNames; |
| 411 | boost::algorithm::split(vargNames, argNamePattern, boost::algorithm::is_any_of("|")); |
| 412 | auto fr = argsIn.end(); |
| 413 | for (const std::string & argName : vargNames) { |
| 414 | fr = argsIn.find(argName); |
| 415 | if (fr != argsIn.end()) { |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | if (fr != argsIn.end()) { |
| 420 | for (int i = 0; i < hole; ++i) { |
| 421 | // Fill hole between specified parameters with JSON nulls, |
| 422 | // but not at the end (for backwards compatibility with calls |
| 423 | // that act based on number of specified parameters). |
| 424 | out.params.push_back(UniValue()); |
| 425 | } |
| 426 | hole = 0; |
| 427 | out.params.push_back(*fr->second); |
| 428 | argsIn.erase(fr); |
| 429 | } else { |
| 430 | hole += 1; |
| 431 | } |
| 432 | } |
| 433 | // If there are still arguments in the argsIn map, this is an error. |
| 434 | if (!argsIn.empty()) { |
| 435 | throw JSONRPCError(RPC_INVALID_PARAMETER, "Unknown named parameter " + argsIn.begin()->first); |
| 436 | } |
| 437 | // Return request with named arguments transformed to positional arguments |
| 438 | return out; |
| 439 | } |
| 440 | |
| 441 | static bool ExecuteCommands(const std::vector<const CRPCCommand*>& commands, const JSONRPCRequest& request, UniValue& result) |
| 442 | { |
no test coverage detected