Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions include/rfl/parsing/Parser_duration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,46 @@ struct Parser<R, W, std::chrono::duration<Rep, Period>, ProcessorsType> {
}
}

template <typename OtherDuration>
inline static constexpr bool samePeriodAs =
std::is_same_v<typename DurationType::period,
typename OtherDuration::period>;
Comment on lines +102 to +105

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This change to support arbitrary duration representations by checking the period is a great improvement. However, the pull request lacks tests for this new capability.

It would be beneficial to add a new test case that verifies parsing of a custom std::chrono::duration type which is not one of the standard std::chrono durations but shares a period with one of them. This will ensure the change works as expected and prevents future regressions.

For example, you could add a test with a struct containing std::chrono::duration<int, std::ratio<1>> and assert that it is correctly serialized with the unit "seconds".

namespace test_custom_duration {

struct TestStruct {
  // Not std::chrono::seconds, but should be treated as such.
  std::chrono::duration<int, std::ratio<1>> duration;
};

TEST(json, test_custom_duration) {
  const auto test = TestStruct{.duration = std::chrono::duration<int, std::ratio<1>>(10)};
  write_and_read(test, R"({"duration":{"count":10,"unit":"seconds"}})");
}

}


static auto make_unit() noexcept {
if constexpr (std::is_same_v<DurationType, std::chrono::nanoseconds>) {
if constexpr (samePeriodAs<std::chrono::nanoseconds>) {
return Unit::make<"nanoseconds">();

} else if constexpr (std::is_same_v<DurationType,
std::chrono::microseconds>) {
} else if constexpr (samePeriodAs<std::chrono::microseconds>) {
return Unit::make<"microseconds">();

} else if constexpr (std::is_same_v<DurationType,
std::chrono::milliseconds>) {
} else if constexpr (samePeriodAs<std::chrono::milliseconds>) {
return Unit::make<"milliseconds">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::seconds>) {
} else if constexpr (samePeriodAs<std::chrono::seconds>) {
return Unit::make<"seconds">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::minutes>) {
} else if constexpr (samePeriodAs<std::chrono::minutes>) {
return Unit::make<"minutes">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::hours>) {
} else if constexpr (samePeriodAs<std::chrono::hours>) {
return Unit::make<"hours">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::days>) {
} else if constexpr (samePeriodAs<std::chrono::days>) {
return Unit::make<"days">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::weeks>) {
} else if constexpr (samePeriodAs<std::chrono::weeks>) {
return Unit::make<"weeks">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::months>) {
} else if constexpr (samePeriodAs<std::chrono::months>) {
return Unit::make<"months">();

} else if constexpr (std::is_same_v<DurationType, std::chrono::years>) {
} else if constexpr (samePeriodAs<std::chrono::years>) {
return Unit::make<"years">();

} else {
static_assert(always_false_v<DurationType>, "Unsupported type.");
}
};
}
};

} // namespace rfl::parsing
Expand Down
29 changes: 29 additions & 0 deletions tests/json/test_chrono_duration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <cassert>
#include <chrono>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>

#include "write_and_read.hpp"

namespace test_chrono_duration {

struct TestStruct {
std::chrono::milliseconds ms_duration;

// Not std::chrono::seconds, but should be treated as such.
std::chrono::duration<int, std::ratio<1>> custom_duration;
};

TEST(json, test_chrono_duration) {
const auto test = TestStruct{
.ms_duration = std::chrono::milliseconds(1500),
.custom_duration = std::chrono::duration<int, std::ratio<1>>(10),
};

write_and_read(
test,
R"({"ms_duration":{"count":1500,"unit":"milliseconds"},"custom_duration":{"count":10,"unit":"seconds"}})");
}

} // namespace test_chrono_duration
Loading