#include "flow.hpp"
#include "collector_task.hpp"
#include "tweaker_task.hpp"
#include "sysinfo_task.hpp"
#include <stdexcept>
namespace doWhile::doct::data
{
static const std::string TYPE = "type";
void from_json(const nlohmann::json& json, Flow& flow)
{
flow.hostname = json.at("hostname");
const nlohmann::json& jsonTasks = json.at("tasks");
for (nlohmann::json jsonTask : jsonTasks)
{
if (jsonTask.at(TYPE) == "CollectorTask")
{
auto task = std::make_shared<CollectorTask>();
from_json(jsonTask, *task);
flow.tasks.push_back(task);
}
else if (jsonTask.at(TYPE) == "TweakerTask")
{
auto task = std::make_shared<TweakerTask>();
from_json(jsonTask, *task);
flow.tasks.push_back(task);
}
else if (jsonTask.at(TYPE) == "SysInfoTask")
{
auto task = std::make_shared<SysInfoTask>();
from_json(jsonTask, *task);
flow.tasks.push_back(task);
}
else
{
throw std::invalid_argument("Error while parsing flow file, unknown task type: " + jsonTask.at(TYPE).get<std::string>());
}
}
}
}