Continue with grid experiment

This commit is contained in:
2025-01-17 10:39:56 +01:00
parent 9a9a9fb17a
commit c1d5dd74e3
12 changed files with 238 additions and 85 deletions

View File

@@ -11,47 +11,33 @@ namespace platform {
public:
JsonValidator(const json& schema) : schema(schema) {}
std::vector<std::string> validate(const std::string& fileName)
std::vector<std::string> validate_file(const std::string& fileName)
{
auto data = load_json_file(fileName);
return validate(data);
}
std::vector<std::string> validate(const json& data)
{
std::ifstream file(fileName);
if (!file.is_open()) {
return { "Error: Unable to open file." };
}
json data;
try {
file >> data;
}
catch (const json::parse_error& e) {
return { "Error: JSON parsing failed: " + std::string(e.what()) };
}
std::vector<std::string> errors;
// Validate the top-level object
validateObject("", schema, data, errors);
return errors;
}
void fix_it(const std::string& fileName)
json load_json_file(const std::string& fileName)
{
std::ifstream file(fileName);
if (!file.is_open()) {
std::cerr << "Error: Unable to open file for fixing." << std::endl;
return;
throw std::runtime_error("Error: Unable to open file " + fileName);
}
json data;
try {
file >> data;
}
catch (const json::parse_error& e) {
std::cerr << "Error: JSON parsing failed: " << e.what() << std::endl;
return;
}
file >> data;
file.close();
return data;
}
void fix_it(const std::string& fileName)
{
// Load JSON file
auto data = load_json_file(fileName);
// Fix fields
for (const auto& [key, value] : schema["properties"].items()) {
if (!data.contains(key)) {
@@ -77,7 +63,6 @@ namespace platform {
std::cerr << "Error: Unable to open file for writing." << std::endl;
return;
}
outFile << data.dump(4);
outFile.close();
}

View File

@@ -8,6 +8,8 @@
#include "common/Paths.h"
#include "common/Symbols.h"
#include "Result.h"
#include "JsonValidator.h"
#include "SchemaV1_0.h"
namespace platform {
std::string get_actual_date()
@@ -62,7 +64,19 @@ namespace platform {
{
return data;
}
void Result::check()
{
platform::JsonValidator validator(platform::SchemaV1_0::schema);
data["schema_version"] = "1.0";
std::vector<std::string> errors = validator.validate(data);
if (!errors.empty()) {
std::string message;
for (const auto& error : errors) {
message += " - " + error + "\n";
}
throw std::runtime_error("* Result file has validation errors:\n" + message);
}
}
void Result::save()
{
std::ofstream file(Paths::results() + getFilename());

View File

@@ -16,6 +16,7 @@ namespace platform {
Result();
Result& load(const std::string& path, const std::string& filename);
void save();
void check();
// Getters
json getJson();
std::string to_string(int maxModel, int maxTitle) const;