|
| 1 | +#include "ConfigValidator.h" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +#include "Config.h" |
| 6 | + |
| 7 | +bool ConfigValidator::Validate(const ConfigData &config) { |
| 8 | + try { |
| 9 | + // Add more validations as needed |
| 10 | + return true; |
| 11 | + } catch (const std::exception &e) { |
| 12 | + std::cerr << "Validation failed: " << e.what() << std::endl; |
| 13 | + return false; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +bool ConfigValidator::ValidateAgainstSchema(const ConfigData &config, |
| 18 | + const YAML::Node &schema) { |
| 19 | + // Implement dynamic validation logic here (e.g., using Lua or schema-based |
| 20 | + // validation) |
| 21 | + return true; // Placeholder |
| 22 | +} |
| 23 | + |
| 24 | +// Recursive function to convert YAML node to Lua table using Sol2 |
| 25 | +sol::table ConfigValidator::PushYamlToLua(sol::state &lua, |
| 26 | + const YAML::Node &node) { |
| 27 | + sol::table lua_table = lua.create_table(); |
| 28 | + |
| 29 | + for (auto it = node.begin(); it != node.end(); ++it) { |
| 30 | + std::string key; |
| 31 | + try { |
| 32 | + key = it->first.as<std::string>(); |
| 33 | + } catch (const YAML::BadConversion &e) { |
| 34 | + // Handle invalid key conversion |
| 35 | + key = "invalid_key"; // Or handle appropriately |
| 36 | + } |
| 37 | + |
| 38 | + const YAML::Node &value = it->second; |
| 39 | + |
| 40 | + try { |
| 41 | + if (value.IsScalar()) { |
| 42 | + if (value.Tag() == "tag:yaml.org,2002:int") { |
| 43 | + lua_table[key] = value.as<int>(); |
| 44 | + } else if (value.Tag() == "tag:yaml.org,2002:float") { |
| 45 | + lua_table[key] = value.as<double>(); |
| 46 | + } else if (value.Tag() == "tag:yaml.org,2002:bool") { |
| 47 | + lua_table[key] = value.as<bool>(); |
| 48 | + } else if (value.IsNull()) { |
| 49 | + lua_table[key] = sol::lua_nil; |
| 50 | + } else { |
| 51 | + lua_table[key] = value.as<std::string>(); |
| 52 | + } |
| 53 | + } else if (value.IsMap()) { |
| 54 | + lua_table[key] = PushYamlToLua(lua, value); |
| 55 | + } else if (value.IsSequence()) { |
| 56 | + sol::table array_table = lua.create_table(); |
| 57 | + int index = 1; |
| 58 | + for (const auto &element : value) { |
| 59 | + if (element.IsScalar()) { |
| 60 | + if (element.Tag() == "tag:yaml.org,2002:int") { |
| 61 | + array_table[index++] = element.as<int>(); |
| 62 | + } else if (element.Tag() == "tag:yaml.org,2002:float") { |
| 63 | + array_table[index++] = element.as<double>(); |
| 64 | + } else if (element.Tag() == "tag:yaml.org,2002:bool") { |
| 65 | + array_table[index++] = element.as<bool>(); |
| 66 | + } else if (element.IsNull()) { |
| 67 | + array_table[index++] = sol::lua_nil; |
| 68 | + } else { |
| 69 | + array_table[index++] = element.as<std::string>(); |
| 70 | + } |
| 71 | + } else { |
| 72 | + array_table[index++] = PushYamlToLua(lua, element); |
| 73 | + } |
| 74 | + } |
| 75 | + lua_table[key] = array_table; |
| 76 | + } |
| 77 | + } catch (const YAML::BadConversion &e) { |
| 78 | + // Handle conversion error, possibly logging and setting Lua to nil or a |
| 79 | + // default value |
| 80 | + lua_table[key] = sol::lua_nil; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return lua_table; |
| 85 | +} |
| 86 | + |
| 87 | +// Load YAML configuration into Lua |
| 88 | +void ConfigValidator::LoadConfigToLua(sol::state &lua, |
| 89 | + const YAML::Node &config) { |
| 90 | + sol::table lua_config = PushYamlToLua(lua, config); |
| 91 | + lua["config"] = lua_config; |
| 92 | + // Debugging: Print Lua table contents |
| 93 | + std::cout << "Lua 'config' table contents:\n"; |
| 94 | + for (const auto &pair : lua_config) { |
| 95 | + std::string key = pair.first.as<std::string>(); |
| 96 | + sol::object value = pair.second; |
| 97 | + std::cout << key << " = "; |
| 98 | + switch (value.get_type()) { |
| 99 | + case sol::type::lua_nil: |
| 100 | + std::cout << "nil"; |
| 101 | + break; |
| 102 | + case sol::type::boolean: |
| 103 | + std::cout << (value.as<bool>() ? "true" : "false"); |
| 104 | + break; |
| 105 | + case sol::type::number: |
| 106 | + std::cout << value.as<double>(); |
| 107 | + break; |
| 108 | + case sol::type::string: |
| 109 | + std::cout << "\"" << value.as<std::string>() << "\""; |
| 110 | + break; |
| 111 | + case sol::type::table: |
| 112 | + std::cout << "table"; |
| 113 | + break; |
| 114 | + default: |
| 115 | + std::cout << "other"; |
| 116 | + break; |
| 117 | + } |
| 118 | + std::cout << "\n"; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +// Example validation function using Lua for dynamic rules |
| 123 | +bool ConfigValidator::ValidateAgainstLua(const ConfigData &config, |
| 124 | + const YAML::Node &schema, |
| 125 | + sol::state &lua) { |
| 126 | + LoadConfigToLua(lua, schema); |
| 127 | + |
| 128 | + // You can define or load Lua conditions here based on the schema |
| 129 | + std::string condition = R"( |
| 130 | + if physics.gravity > 9.8 and simulation.duration > 1000 then |
| 131 | + return false |
| 132 | + else |
| 133 | + return true |
| 134 | + end |
| 135 | + )"; |
| 136 | + |
| 137 | + sol::protected_function_result result = |
| 138 | + lua.safe_script(condition, sol::script_pass_on_error); |
| 139 | + if (!result.valid()) { |
| 140 | + sol::error err = result; |
| 141 | + std::cerr << "Lua validation error: " << err.what() << std::endl; |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + return result; |
| 146 | +} |
| 147 | + |
0 commit comments