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