Generic config parsing utilities
This commit is contained in:
@@ -40,58 +40,61 @@ ConfigParser::parse_toml_string(const std::string &toml_content) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generic configuration parsing utilities
|
||||||
|
template <typename T>
|
||||||
|
void ConfigParser::parse_field(const auto §ion,
|
||||||
|
const std::string &field_name, T &target) {
|
||||||
|
if (section.contains(field_name)) {
|
||||||
|
target = toml::get<T>(section.at(field_name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Rep, typename Period>
|
||||||
|
void ConfigParser::parse_duration_field(
|
||||||
|
const auto §ion, const std::string &field_name,
|
||||||
|
std::chrono::duration<Rep, Period> &target) {
|
||||||
|
if (section.contains(field_name)) {
|
||||||
|
auto value = toml::get<int>(section.at(field_name));
|
||||||
|
target = std::chrono::duration<Rep, Period>{value};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigParser::parse_section(const auto &toml_data,
|
||||||
|
const std::string §ion_name,
|
||||||
|
auto parse_func) {
|
||||||
|
if (toml_data.contains(section_name)) {
|
||||||
|
const auto §ion = toml_data.at(section_name);
|
||||||
|
parse_func(section);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ConfigParser::parse_server_config(const auto &toml_data,
|
void ConfigParser::parse_server_config(const auto &toml_data,
|
||||||
ServerConfig &config) {
|
ServerConfig &config) {
|
||||||
if (toml_data.contains("server")) {
|
parse_section(toml_data, "server", [&](const auto &srv) {
|
||||||
const auto &srv = toml_data.at("server");
|
parse_field(srv, "bind_address", config.bind_address);
|
||||||
|
parse_field(srv, "port", config.port);
|
||||||
if (srv.contains("bind_address")) {
|
parse_field(srv, "max_request_size_bytes", config.max_request_size_bytes);
|
||||||
config.bind_address = toml::get<std::string>(srv.at("bind_address"));
|
});
|
||||||
}
|
|
||||||
if (srv.contains("port")) {
|
|
||||||
config.port = toml::get<int>(srv.at("port"));
|
|
||||||
}
|
|
||||||
if (srv.contains("max_request_size_bytes")) {
|
|
||||||
config.max_request_size_bytes =
|
|
||||||
toml::get<size_t>(srv.at("max_request_size_bytes"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigParser::parse_commit_config(const auto &toml_data,
|
void ConfigParser::parse_commit_config(const auto &toml_data,
|
||||||
CommitConfig &config) {
|
CommitConfig &config) {
|
||||||
if (toml_data.contains("commit")) {
|
parse_section(toml_data, "commit", [&](const auto &commit) {
|
||||||
const auto &commit = toml_data.at("commit");
|
parse_field(commit, "min_request_id_length", config.min_request_id_length);
|
||||||
|
parse_duration_field(commit, "request_id_retention_hours",
|
||||||
if (commit.contains("min_request_id_length")) {
|
config.request_id_retention_time);
|
||||||
config.min_request_id_length =
|
parse_field(commit, "request_id_retention_versions",
|
||||||
toml::get<size_t>(commit.at("min_request_id_length"));
|
config.request_id_retention_versions);
|
||||||
}
|
});
|
||||||
if (commit.contains("request_id_retention_hours")) {
|
|
||||||
auto hours = toml::get<int>(commit.at("request_id_retention_hours"));
|
|
||||||
config.request_id_retention_time = std::chrono::hours{hours};
|
|
||||||
}
|
|
||||||
if (commit.contains("request_id_retention_versions")) {
|
|
||||||
config.request_id_retention_versions =
|
|
||||||
toml::get<size_t>(commit.at("request_id_retention_versions"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigParser::parse_subscription_config(const auto &toml_data,
|
void ConfigParser::parse_subscription_config(const auto &toml_data,
|
||||||
SubscriptionConfig &config) {
|
SubscriptionConfig &config) {
|
||||||
if (toml_data.contains("subscription")) {
|
parse_section(toml_data, "subscription", [&](const auto &sub) {
|
||||||
const auto &sub = toml_data.at("subscription");
|
parse_field(sub, "max_buffer_size_bytes", config.max_buffer_size_bytes);
|
||||||
|
parse_duration_field(sub, "keepalive_interval_seconds",
|
||||||
if (sub.contains("max_buffer_size_bytes")) {
|
config.keepalive_interval);
|
||||||
config.max_buffer_size_bytes =
|
});
|
||||||
toml::get<size_t>(sub.at("max_buffer_size_bytes"));
|
|
||||||
}
|
|
||||||
if (sub.contains("keepalive_interval_seconds")) {
|
|
||||||
auto seconds = toml::get<int>(sub.at("keepalive_interval_seconds"));
|
|
||||||
config.keepalive_interval = std::chrono::seconds{seconds};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace weaseldb
|
} // namespace weaseldb
|
||||||
@@ -40,6 +40,19 @@ public:
|
|||||||
parse_toml_string(const std::string &toml_content);
|
parse_toml_string(const std::string &toml_content);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Generic configuration parsing utilities
|
||||||
|
template <typename T>
|
||||||
|
static void parse_field(const auto §ion, const std::string &field_name,
|
||||||
|
T &target);
|
||||||
|
|
||||||
|
template <typename Rep, typename Period>
|
||||||
|
static void parse_duration_field(const auto §ion,
|
||||||
|
const std::string &field_name,
|
||||||
|
std::chrono::duration<Rep, Period> &target);
|
||||||
|
|
||||||
|
static void parse_section(const auto &toml_data,
|
||||||
|
const std::string §ion_name, auto parse_func);
|
||||||
|
|
||||||
static void parse_server_config(const auto &toml_data, ServerConfig &config);
|
static void parse_server_config(const auto &toml_data, ServerConfig &config);
|
||||||
static void parse_commit_config(const auto &toml_data, CommitConfig &config);
|
static void parse_commit_config(const auto &toml_data, CommitConfig &config);
|
||||||
static void parse_subscription_config(const auto &toml_data,
|
static void parse_subscription_config(const auto &toml_data,
|
||||||
|
|||||||
Reference in New Issue
Block a user