Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
Revise parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lrw04 committed Jul 13, 2022
1 parent 5c96d3e commit 66c8c21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
32 changes: 31 additions & 1 deletion config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,56 @@ Config Config::from_file(const std::filesystem::path& f) {
}

Config res;
res.nproc = r.value("nproc", std::thread::hardware_concurrency());
try {
CHECK_F((!r.contains("nproc")) || r.at("nproc").is_number_unsigned(),
"'nproc' is not an unsigned integer");
res.nproc = r.value("nproc", std::thread::hardware_concurrency());

CHECK_F(r.contains("report") && r.at("report").is_string(),
"'report' is missing or not a string");
res.report = std::filesystem::path(f.parent_path() / r.at("report"));

CHECK_F(r.contains("logs") && r.at("logs").is_object(),
"'logs' is missing or not an object");
CHECK_F(r.at("logs").contains("enable_file") &&
r.at("logs").at("enable_file").is_boolean(),
"'logs.enable_file' is missing or not boolean");
res.do_log_file = r.at("logs").at("enable_file");

if (res.do_log_file) {
CHECK_F(r.at("logs").contains("path") &&
r.at("logs").at("path").is_string(),
"'logs.path' is missing or not a string");
res.logs = std::filesystem::path(f.parent_path() /
r.at("logs").at("path"));
}

CHECK_F(r.at("logs").contains("enable_syslog") &&
r.at("logs").at("enable_syslog").is_boolean(),
"'logs.enable_syslog' is missing or not boolean");
res.do_syslog = r.at("logs").at("enable_syslog");

if (res.do_syslog) {
CHECK_F(r.at("logs").contains("syslog_name") &&
r.at("logs").at("syslog_name").is_string(),
"'logs.syslog_name' is missing or not a string");
res.syslog_name = r.at("logs").at("syslog_name");
}
if (res.do_log_file || res.do_syslog) {
CHECK_F(r.at("logs").contains("verbosity") &&
r.at("logs").at("verbosity").is_string(),
"'logs.verbosity' is missing or not a string");
std::string verbosity = r.at("logs").at("verbosity");
if (!stov.count(verbosity))
ABORT_F("Invalid verbosity: %s", verbosity.c_str());
res.verbosity = stov[verbosity];
}

CHECK_F((!r.contains("rand")) || r.at("rand").is_number_unsigned(),
"'rand' is not an unsigned integer");
res.rand = r.value("rand", 0);
for (auto j : r.at("jobs")) {
CHECK_F(j.is_string(), "Expected string for every job entry");
res.jobs.push_back(f.parent_path() / j);
}
} catch (const nlohmann::json::basic_json::out_of_range& e) {
Expand Down
12 changes: 11 additions & 1 deletion job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@ Job Job::from_file(const std::filesystem::path& c) {
} else {
res.exe = r.at("exe");
}
CHECK_F(r.contains("name") && r.at("name").is_string(),
"'name' is missing or not a string");
res.name = r.at("name");
CHECK_F(r.contains("cycle") && r.at("cycle").is_number_unsigned(),
"'cycle' is missing or not an unsigned integer");
res.cycle = r.at("cycle");
CHECK_F((!r.contains("delay")) || r.at("delay").is_number_unsigned(),
"'delay' is not an unsigned integer");
res.delay = r.value("delay", 0);
res.use_args = false;
if (r.contains("args")) {
CHECK_F(r.at("args").is_array(), "Expected array 'args'");
res.use_args = true;
for (const auto& arg : r.at("args")) res.args.push_back(arg);
for (const auto& arg : r.at("args")) {
CHECK_F(arg.is_string(),
"Expected string for every args entry");
res.args.push_back(arg);
}
}
} catch (const nlohmann::json::basic_json::out_of_range& e) {
ABORT_F("Invalid job configuration: %s", e.what());
Expand Down

0 comments on commit 66c8c21

Please sign in to comment.