Skip to content
Snippets Groups Projects
Commit 549e428d authored by Romain Lacroix's avatar Romain Lacroix
Browse files

changing the from_yaml() templated functions to constructors

parent 63e1c73d
Branches new-jenkins
No related tags found
No related merge requests found
......@@ -71,6 +71,11 @@ bool sbi_interface::validate() {
return true;
}
sbi_interface::sbi_interface(YAML::Node const& node) {
m_api_version = node["api_version"].as<std::string>();
m_url = node["url"].as<std::string>();
}
std::string sbi_interface::to_string(const std::string& indent) const {
std::string out;
unsigned int inner_width = COLUMN_WIDTH;
......@@ -99,6 +104,11 @@ const std::string& sbi_interface::get_url() const {
return m_url;
}
local_interface::local_interface(YAML::Node const& node) {
m_port = node["port"].as<uint16_t>();
m_if_name = node["name"].as<std::string>();
}
bool local_interface::validate() {
unsigned int _mtu{};
in_addr _addr4{};
......@@ -170,6 +180,18 @@ uint16_t local_interface::get_port() const {
return m_port;
}
local_sbi_interface::local_sbi_interface(YAML::Node const& node) {
auto iface = local_interface{node};
m_port = iface.m_port;
m_if_name = iface.m_if_name;
m_api_version = node["api_version"].as<std::string>();
auto http_version = node["http_version"].as<std::string>();
if (http_version == "2") {
m_use_http2 = true;
}
}
bool local_sbi_interface::validate() {
bool sbi_validate = validate_sbi_api_version(m_api_version);
if (!sbi_validate) {
......@@ -217,6 +239,10 @@ bool network_interface::validate_sbi_api_version(const std::string& v) {
return true;
}
string_config_value::string_config_value(YAML::Node const& node) {
m_value = node.as<std::string>();
}
std::string string_config_value::to_string(const std::string&) const {
std::string out;
return out.append(m_value);
......@@ -235,6 +261,10 @@ const std::string& string_config_value::get_value() const {
return m_value;
}
option_config_value::option_config_value(YAML::Node const& node) {
m_value = node.as<bool>();
}
std::string option_config_value::to_string(const std::string&) const {
std::string val = m_value ? "Yes" : "No";
return val;
......
......@@ -33,6 +33,7 @@
#include <netinet/in.h>
#include <vector>
#include <memory>
#include <yaml-cpp/yaml.h>
namespace oai::config {
......@@ -92,13 +93,15 @@ class config_type {
class string_config_value : public config_type {
friend class factory;
friend class yaml_file_iface;
private:
std::string m_value;
std::string m_regex;
public:
string_config_value(YAML::Node const&);
string_config_value() = default;
[[nodiscard]] std::string to_string(const std::string& indent) const override;
[[nodiscard]] bool validate() override;
[[nodiscard]] config_type_e get_config_type() const override;
......@@ -108,12 +111,14 @@ class string_config_value : public config_type {
class option_config_value : public config_type {
friend class factory;
friend class yaml_file_iface;
private:
bool m_value = false;
public:
option_config_value(YAML::Node const&);
option_config_value() = default;
[[nodiscard]] std::string to_string(const std::string& indent) const override;
[[nodiscard]] bool validate() override;
[[nodiscard]] config_type_e get_config_type() const override;
......@@ -127,13 +132,14 @@ class network_interface : public config_type {
};
class sbi_interface : public network_interface {
friend class yaml_file_iface;
private:
std::string m_api_version;
std::string m_url;
public:
sbi_interface(YAML::Node const&);
sbi_interface() = default;
[[nodiscard]] bool validate() override;
[[nodiscard]] std::string to_string(const std::string& indent) const override;
[[nodiscard]] config_type_e get_config_type() const override;
......@@ -143,7 +149,7 @@ class sbi_interface : public network_interface {
};
class local_interface : public network_interface {
friend class yaml_file_iface;
friend class local_sbi_interface;
private:
std::string m_if_name{};
......@@ -153,6 +159,9 @@ class local_interface : public network_interface {
uint16_t m_port{};
public:
local_interface(YAML::Node const&);
local_interface() = default;
[[nodiscard]] bool validate() override;
[[nodiscard]] std::string to_string(const std::string& indent) const override;
[[nodiscard]] config_type_e get_config_type() const override;
......@@ -165,13 +174,14 @@ class local_interface : public network_interface {
};
class local_sbi_interface : public local_interface {
friend class yaml_file_iface;
private:
std::string m_api_version;
bool m_use_http2 = false;
public:
local_sbi_interface(YAML::Node const&);
local_sbi_interface() = default;
[[nodiscard]] bool validate() override;
[[nodiscard]] std::string to_string(const std::string& indent) const override;
......@@ -186,4 +196,4 @@ class factory {
static string_config_value get_string_config(const std::string& val);
};
} // namespace oai::config
\ No newline at end of file
} // namespace oai::config
......@@ -77,10 +77,7 @@ template<class T>
bool oai::config::yaml_file::convert_type(
const std::string& key, const YAML::Node& node, config_iface& config) {
try {
T conf_val;
if (!from_yaml(node, conf_val)) {
return false;
}
T conf_val{node};
std::unique_ptr<config_type> conf = std::make_unique<T>(conf_val);
config.set_configuration(key, std::move(conf));
return true;
......@@ -89,57 +86,3 @@ bool oai::config::yaml_file::convert_type(
return false;
}
}
bool oai::config::yaml_file_iface::from_yaml(
const YAML::Node& node, oai::config::option_config_value& val) {
val.m_value = node.as<bool>();
return true;
}
bool oai::config::yaml_file_iface::from_yaml(
const YAML::Node& node, oai::config::string_config_value& val) {
val.m_value = node.as<std::string>();
return true;
}
bool oai::config::yaml_file_iface::from_yaml(
const YAML::Node& node, oai::config::sbi_interface& val) {
if (!node["url"] || !node["api_version"]) {
return false;
}
val.m_api_version = node["api_version"].as<std::string>();
val.m_url = node["url"].as<std::string>();
return true;
}
bool oai::config::yaml_file_iface::from_yaml(
const YAML::Node& node, oai::config::local_interface& val) {
if (!node["name"] || !node["port"]) {
return false;
}
val.m_port = node["port"].as<uint16_t>();
val.m_if_name = node["name"].as<std::string>();
return true;
}
bool oai::config::yaml_file_iface::from_yaml(
const YAML::Node& node, oai::config::local_sbi_interface& val) {
local_interface iface;
if (!from_yaml(node, iface)) {
return false;
}
if (!node["api_version"] || !node["http_version"]) {
return false;
}
val.m_port = iface.m_port;
val.m_if_name = iface.m_if_name;
val.m_api_version = node["api_version"].as<std::string>();
auto http_version = node["http_version"].as<std::string>();
if (http_version == "2") {
val.m_use_http2 = true;
}
return true;
}
......@@ -47,18 +47,6 @@ class yaml_file_iface {
*/
virtual void read_from_file(
const std::string& file_path, config_iface& config) = 0;
protected:
static bool from_yaml(
const YAML::Node& node, oai::config::option_config_value& val);
static bool from_yaml(
const YAML::Node& node, oai::config::string_config_value& val);
static bool from_yaml(
const YAML::Node& node, oai::config::sbi_interface& val);
static bool from_yaml(
const YAML::Node& node, oai::config::local_interface& val);
static bool from_yaml(
const YAML::Node& node, oai::config::local_sbi_interface& val);
};
class yaml_file : public yaml_file_iface {
......@@ -74,4 +62,4 @@ class yaml_file : public yaml_file_iface {
const std::string& conf, const YAML::Node& node, config_iface& config);
};
} // namespace oai::config
\ No newline at end of file
} // namespace oai::config
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment