-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put configuration parsing helper function defs into own header
This is so we can test them with GTest.
- Loading branch information
Showing
3 changed files
with
93 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#pragma once | ||
#include "configuration.h" | ||
#include "poco.h" | ||
|
||
#include <nlohmann/json.hpp> | ||
|
||
#include <filesystem> | ||
#include <string> | ||
|
||
namespace host { | ||
/// @brief Load value from JSON, printing an error message if it fails | ||
/// @param j JSON object | ||
/// @param key Key to value | ||
/// @throw ConfigurationError: Key not found | ||
/// @return Key value | ||
nlohmann::json get(const nlohmann::json &j, const std::string &key); | ||
|
||
/// @brief Get value from JSON object and store in out | ||
/// @tparam T Type of output object | ||
/// @param j JSON object | ||
/// @param key Key to value | ||
/// @param out Output object | ||
/// @return True if value was retrieved successfully, false otherwise | ||
template <class T> bool get_to(const nlohmann::json &j, const std::string &key, T &out); | ||
|
||
/// @brief Get value from JSON object and store in out, setting success flag | ||
/// @tparam T Type of output object | ||
/// @param j JSON object | ||
/// @param key Key to value | ||
/// @param out Output object | ||
/// @param success Success flag, set to false in case of failure | ||
/// @return True if value was retrieved successfully, false otherwise | ||
template <class T> | ||
bool get_to(const nlohmann::json &j, const std::string &key, T &out, bool &success); | ||
|
||
/// @brief Rebase path on base_dir | ||
/// @param path Initial path (relative or absolute) | ||
/// @param base_dir New base directory for relative path | ||
/// @throw ConfigurationError: If path does not exist | ||
void rebase_valid_path(std::filesystem::path &path, const std::filesystem::path &base_dir); | ||
|
||
/// @brief Get a valid path from a JSON object | ||
/// @param j JSON object | ||
/// @param key Key to value | ||
/// @param base_dir Base directory for relative path | ||
/// @param out Output variable | ||
/// @return True if value was retrieved successfully and is valid path, false otherwise | ||
bool get_valid_path_to(const nlohmann::json &j, const std::string &key, | ||
const std::filesystem::path &base_dir, std::filesystem::path &out); | ||
|
||
/// @brief Get a valid path from a JSON object | ||
/// @param j JSON object | ||
/// @param key Key to value | ||
/// @param base_dir Base directory for relative path | ||
/// @param out Output variable | ||
/// @param success Success flag, set to false in case of failure | ||
void get_valid_path_to(const nlohmann::json &j, const std::string &key, | ||
const std::filesystem::path &base_dir, std::filesystem::path &out, | ||
bool &success); | ||
|
||
/// @brief Load FileInfo from JSON | ||
/// @param j Input JSON | ||
/// @param base_dir Base folder | ||
/// @return FileInfo | ||
/// @throw ConfigurationError: Invalid config file format | ||
poco::FileInfo get_file_info(const nlohmann::json &j, const std::filesystem::path &base_dir); | ||
|
||
/// @brief Load settings section of JSON | ||
/// @param j Input JSON | ||
/// @return SettingsInfo | ||
/// @throw ConfigurationError: Could not load settings | ||
poco::SettingsInfo get_settings(const nlohmann::json &j); | ||
|
||
/// @brief Load BaselineInfo from JSON | ||
/// @param j Input JSON | ||
/// @param base_dir Base folder | ||
/// @return BaselineInfo | ||
/// @throw ConfigurationError: One or more files could not be found | ||
poco::BaselineInfo get_baseline_info(const nlohmann::json &j, | ||
const std::filesystem::path &base_dir); | ||
|
||
/// @brief Load interventions from running section | ||
/// @param running Running section of JSON object | ||
/// @param config Config object to update | ||
/// @throw ConfigurationError: Could not load interventions | ||
void load_interventions(const nlohmann::json &running, Configuration &config); | ||
} // namespace host |