33 lines
1.2 KiB
C++
33 lines
1.2 KiB
C++
#include <fstream>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
|
|
#include <configuration/configuration.h>
|
|
|
|
json ParseConfiguration(const std::string& path_to_configuration, const json& required_keys) {
|
|
std::ifstream input(path_to_configuration);
|
|
auto configuration = json::parse(input);
|
|
|
|
for (const auto& [key, value] : required_keys.items()) {
|
|
// Проверяем наличие ключа на "нулевом" уровне
|
|
if (!configuration.contains(key)) {
|
|
std::ostringstream message;
|
|
message << "configuration doesn't contain required key '" << key << "'";
|
|
throw std::runtime_error(message.str());
|
|
}
|
|
|
|
// Проверяем наличие вложенных ключей
|
|
auto configuration_value = configuration[key];
|
|
|
|
for (const auto& item : value) {
|
|
if (!configuration_value.contains(item)) {
|
|
std::ostringstream message;
|
|
message << "configuration doesn't contain required key '" << item << "' in '" << key << "'";
|
|
throw std::runtime_error(message.str());
|
|
}
|
|
}
|
|
}
|
|
|
|
return configuration;
|
|
}
|