Skip to content

Commit

Permalink
Support std::string in configuration_json_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 22, 2024
1 parent 21c4625 commit 36b6ad3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/share/core_configuration/configuration_json_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class value_t final : public base_t {
pqrs::json::requires_boolean(*it, "`" + key_ + "`");
} else if constexpr (std::is_same<T, int>::value || std::is_same<T, double>::value) {
pqrs::json::requires_number(*it, "`" + key_ + "`");
} else if constexpr (std::is_same<T, std::string>::value) {
pqrs::json::requires_string(*it, "`" + key_ + "`");
}

value_ = it->template get<T>();
Expand All @@ -79,7 +81,7 @@ class value_t final : public base_t {
if (error_handling == error_handling::strict) {
throw;
} else {
logger::get_logger()->error(e.what());
logger::get_logger()->warn(e.what());
}
}
}
Expand Down Expand Up @@ -218,7 +220,8 @@ class helper_values final {
void update_value(const nlohmann::json& json,
error_handling error_handling) {
for (const auto& v : values_) {
v->update_value(json, error_handling);
v->update_value(json,
error_handling);
}
}

Expand Down Expand Up @@ -263,6 +266,10 @@ class helper_values final {
return find_default_value(value, 0);
}

std::string find_default_value(const std::string& value) const {
return find_default_value(value, std::string(""));
}

template <typename T>
void set_default_value(const T& value, const T& default_value) const {
if (auto v = find_value(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class test_class final {
helper_values.push_back_value<bool>("bool", b, true);
helper_values.push_back_value<double>("double", d, 1042.0);
helper_values.push_back_value<int>("int", i, 2042);
helper_values.push_back_value<std::string>("string", s, "42");
helper_values.push_back_object<test_object>("object", o);
helper_values.push_back_array<test_object>("array", a);

Expand All @@ -50,6 +51,7 @@ class test_class final {
bool b;
double d;
int i;
std::string s;
gsl::not_null<std::shared_ptr<test_object>> o;
std::vector<gsl::not_null<std::shared_ptr<test_object>>> a;
krbn::core_configuration::configuration_json_helper::helper_values helper_values;
Expand All @@ -66,6 +68,7 @@ void run_configuration_json_helper_test(void) {
expect(true == actual.b);
expect(1042.0_d == actual.d);
expect(2042_i == actual.i);
expect(std::string("42") == actual.s);
expect(42_i == actual.o->i);
expect(actual.a.empty());

Expand All @@ -82,6 +85,7 @@ void run_configuration_json_helper_test(void) {
"bool": false,
"double": 1.0,
"int": 2,
"string": "4242",
"object": {
"int": 3
},
Expand All @@ -98,6 +102,7 @@ void run_configuration_json_helper_test(void) {
expect(false == actual.b);
expect(1.0_d == actual.d);
expect(2_i == actual.i);
expect(std::string("4242") == actual.s);
expect(3_i == actual.o->i);
expect(1_l == actual.a.size());
expect(4_i == actual.a[0]->i);
Expand All @@ -111,6 +116,7 @@ void run_configuration_json_helper_test(void) {
actual.b = true;
actual.d = 1042.0;
actual.i = 2042;
actual.s = "42";
actual.o->i = 42;
actual.a.clear();

Expand All @@ -125,6 +131,7 @@ void run_configuration_json_helper_test(void) {
expect(true == actual.helper_values.find_default_value(actual.b));
expect(1042.0_d == actual.helper_values.find_default_value(actual.d));
expect(2042_i == actual.helper_values.find_default_value(actual.i));
expect(std::string("42") == actual.helper_values.find_default_value(actual.s));
};

"set_default_value"_test = [] {
Expand Down Expand Up @@ -210,4 +217,56 @@ void run_configuration_json_helper_test(void) {
expect(nlohmann::json::object() == actual.to_json()) << UT_SHOW_LINE;
}
};

//
// Invalid values
//

// strict
{
expect(throws([] {
auto json = nlohmann::json::parse(R"( { "bool": null } )");
test_class(json, krbn::core_configuration::error_handling::strict);
}));
}
{
expect(throws([] {
auto json = nlohmann::json::parse(R"( { "double": null } )");
test_class(json, krbn::core_configuration::error_handling::strict);
}));
}
{
expect(throws([] {
auto json = nlohmann::json::parse(R"( { "int": null } )");
test_class(json, krbn::core_configuration::error_handling::strict);
}));
}
{
expect(throws([] {
auto json = nlohmann::json::parse(R"( { "string": null } )");
test_class(json, krbn::core_configuration::error_handling::strict);
}));
}

// loose
{
auto json = nlohmann::json::parse(R"( { "bool": null } )");
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
expect(true == actual.b);
}
{
auto json = nlohmann::json::parse(R"( { "double": null } )");
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
expect(1042.0_d == actual.d);
}
{
auto json = nlohmann::json::parse(R"( { "int": null } )");
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
expect(2042_i == actual.i);
}
{
auto json = nlohmann::json::parse(R"( { "string": null } )");
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
expect(std::string("42") == actual.s);
}
}

0 comments on commit 36b6ad3

Please sign in to comment.