Cleaned up if/branch commands and made branch types stricter.

This commit is contained in:
Jari Sundell
2026-06-15 11:55:30 +02:00
committed by GitHub
parent d08d7de20d
commit 662d67e861
+30 -23
View File
@@ -472,11 +472,14 @@ apply_to_throttle(const torrent::Object& rawArgs) {
// if (cond1) { branch1 } else if (cond2) { branch2 } else { branch3 } // if (cond1) { branch1 } else if (cond2) { branch2 } else { branch3 }
// <cond1>,<branch1>,<cond2>,<branch2>,<branch3> // <cond1>,<branch1>,<cond2>,<branch2>,<branch3>
torrent::Object torrent::Object
apply_if(rpc::target_type target, const torrent::Object& rawArgs, int flags) { apply_if(rpc::target_type target, const torrent::Object& raw_args, int flags) {
const torrent::Object::list_type& args = rawArgs.as_list(); auto& args = raw_args.as_list();
torrent::Object::list_const_iterator itr = args.begin(); auto itr = args.begin();
while (itr != args.end() && itr != --args.end()) { if (args.empty())
throw torrent::input_error("Empty argument list to " + std::string((flags & 0x1) ? "branch" : "if") + ".");
{
torrent::Object tmp; torrent::Object tmp;
const torrent::Object* conditional; const torrent::Object* conditional;
@@ -500,40 +503,44 @@ apply_if(rpc::target_type target, const torrent::Object& rawArgs, int flags) {
result = false; result = false;
break; break;
default: default:
throw torrent::input_error("Type not supported by 'if'."); throw torrent::input_error("Type not supported by " + std::string((flags & 0x1) ? "branch" : "if") + ".");
}; };
itr++; itr++;
if (result) if (!result && itr != args.end())
break; itr++;
itr++;
} }
if (itr == args.end()) if (itr == args.end())
return torrent::Object(); return torrent::Object();
if (flags & 0x1 && itr->is_string()) { if (flags & 0x1) {
return rpc::parse_command(target, itr->as_string().c_str(), itr->as_string().c_str() + itr->as_string().size()).first; if (itr->is_string())
return rpc::parse_command(target, itr->as_string().c_str(), itr->as_string().c_str() + itr->as_string().size()).first;
} else if (flags & 0x1 && itr->is_dict_key()) { if (itr->is_dict_key())
return rpc::commands.call_command(itr->as_dict_key().c_str(), itr->as_dict_obj(), target); return rpc::commands.call_command(itr->as_dict_key().c_str(), itr->as_dict_obj(), target);
} else if (flags & 0x1 && itr->is_list()) { if (itr->is_list()) {
// Move this into a special function or something. Also, might be for (const auto& cmd_itr : itr->as_list()) {
// nice to have a parse_command function that takes list if (cmd_itr.is_string())
// iterator... rpc::parse_command(target, cmd_itr.as_string().c_str(), cmd_itr.as_string().c_str() + cmd_itr.as_string().size());
for (const auto& cmdItr : itr->as_list()) else if (cmd_itr.is_dict_key())
if (cmdItr.is_string()) rpc::commands.call_command(cmd_itr.as_dict_key().c_str(), cmd_itr.as_dict_obj(), target);
rpc::parse_command(target, cmdItr.as_string().c_str(), cmdItr.as_string().c_str() + cmdItr.as_string().size());
return torrent::Object(); else
throw torrent::input_error("Invalid command type in branch list.");
}
} else { return torrent::Object();
return *itr; }
throw torrent::input_error("Invalid command type in branch.");
} }
return *itr;
} }
torrent::Object torrent::Object