Skip to content

Commit

Permalink
Add std::chrono::milliseconds to configuration_json_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jun 26, 2024
1 parent e616f4b commit 09c0bee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/share/core_configuration/configuration_json_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "logger.hpp"
#include "memory_utility.hpp"
#include "types.hpp"
#include <chrono>
#include <gsl/gsl>
#include <pqrs/json.hpp>

Expand Down Expand Up @@ -98,10 +99,16 @@ class value_t final : public base_t {
pqrs::json::dump_for_error_message(*it)));
}

} else if constexpr (std::is_same<T, std::chrono::milliseconds>::value) {
pqrs::json::requires_number(*it, "`" + key_ + "`");

value_ = std::chrono::milliseconds(it->template get<int>());

} else {
if constexpr (std::is_same<T, bool>::value) {
pqrs::json::requires_boolean(*it, "`" + key_ + "`");
} else if constexpr (std::is_same<T, int>::value || std::is_same<T, double>::value) {
} else if constexpr (std::is_same<T, int>::value ||
std::is_same<T, double>::value) {
pqrs::json::requires_number(*it, "`" + key_ + "`");
}

Expand Down Expand Up @@ -136,8 +143,11 @@ class value_t final : public base_t {
json[key_] = lines;
}

} else if constexpr (std::is_same<T, std::chrono::milliseconds>::value) {
json[key_] = value_.count();

} else {
// For non-string values, save them as they are.
// For generic values, save them as they are.
json[key_] = value_;
}

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

std::chrono::milliseconds find_default_value(const std::chrono::milliseconds& value) const {
return find_default_value(value, std::chrono::milliseconds(0));
}

std::string find_default_value(const std::string& value) const {
return find_default_value(value, std::string(""));
}
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::chrono::milliseconds>("milliseconds", ms, std::chrono::milliseconds(3042));
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 @@ -51,6 +52,7 @@ class test_class final {
bool b;
double d;
int i;
std::chrono::milliseconds ms;
std::string s;
gsl::not_null<std::shared_ptr<test_object>> o;
std::vector<gsl::not_null<std::shared_ptr<test_object>>> a;
Expand All @@ -68,6 +70,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::chrono::milliseconds(3042) == actual.ms);
expect(std::string("42") == actual.s);
expect(42_i == actual.o->i);
expect(actual.a.empty());
Expand All @@ -85,6 +88,7 @@ void run_configuration_json_helper_test(void) {
"bool": false,
"double": 1.0,
"int": 2,
"milliseconds": 5,
"string": "4242",
"object": {
"int": 3
Expand All @@ -102,6 +106,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::chrono::milliseconds(5) == actual.ms);
expect(std::string("4242") == actual.s);
expect(3_i == actual.o->i);
expect(1_l == actual.a.size());
Expand All @@ -116,6 +121,7 @@ void run_configuration_json_helper_test(void) {
actual.b = true;
actual.d = 1042.0;
actual.i = 2042;
actual.ms = std::chrono::milliseconds(3042);
actual.s = "42";
actual.o->i = 42;
actual.a.clear();
Expand Down Expand Up @@ -265,6 +271,12 @@ void run_configuration_json_helper_test(void) {
test_class(json, krbn::core_configuration::error_handling::strict);
}));
}
{
expect(throws([] {
auto json = nlohmann::json::parse(R"( { "milliseconds": null } )");
test_class(json, krbn::core_configuration::error_handling::strict);
}));
}
{
expect(throws([] {
auto json = nlohmann::json::parse(R"( { "string": null } )");
Expand All @@ -288,6 +300,11 @@ void run_configuration_json_helper_test(void) {
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
expect(2042_i == actual.i);
}
{
auto json = nlohmann::json::parse(R"( { "milliseconds": null } )");
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
expect(std::chrono::milliseconds(3042) == actual.ms);
}
{
auto json = nlohmann::json::parse(R"( { "string": null } )");
auto actual = test_class(json, krbn::core_configuration::error_handling::loose);
Expand Down

0 comments on commit 09c0bee

Please sign in to comment.