Skip to content

Commit 9a0dc8e

Browse files
FedShatapolukhin
authored andcommitted
fix chaotic: fixup chrono milliseconds convert
Tests: протестировано CI --- Pull Request resolved: #1195 Co-authored-by: antoshkka <antoshkka@userver.tech> commit_hash:9a73d342ab276357c57b27707df398165dbff73c
1 parent 8b432b7 commit 9a0dc8e

4 files changed

Lines changed: 24 additions & 0 deletions

File tree

chaotic/include/userver/chaotic/io/std/chrono/milliseconds.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ USERVER_NAMESPACE_BEGIN
1414
namespace chaotic::convert {
1515

1616
template <typename T>
17+
requires std::is_integral_v<T> || std::is_floating_point_v<T>
1718
T Convert(std::chrono::milliseconds value, chaotic::convert::To<T>) {
1819
return utils::numeric_cast<T>(value.count());
1920
}
2021

22+
std::string Convert(const std::chrono::milliseconds& value, chaotic::convert::To<std::string>);
23+
2124
std::chrono::milliseconds Convert(const std::string& str, chaotic::convert::To<std::chrono::milliseconds>);
2225

2326
std::chrono::milliseconds Convert(std::string_view str, chaotic::convert::To<std::chrono::milliseconds>);

chaotic/integration_tests/schemas/custom_cpp_type.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ definitions:
2828
integer:
2929
type: integer
3030
x-usrv-cpp-type: std::chrono::milliseconds
31+
ms:
32+
type: string
33+
x-usrv-cpp-type: std::chrono::milliseconds
3134
sizet:
3235
type: integer
3336
minimum: 0

chaotic/integration_tests/tests/render/custom.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ TEST(Custom, Int) {
2222
EXPECT_EQ(custom2, custom);
2323
}
2424

25+
TEST(Custom, Ms) {
26+
auto json = formats::json::MakeObject("ms", "12ms");
27+
auto custom = json.As<ns::ObjWithCustom>();
28+
EXPECT_EQ(custom.ms, std::chrono::milliseconds(12));
29+
30+
auto json_back = formats::json::ValueBuilder{custom}.ExtractValue();
31+
EXPECT_EQ(json_back, json);
32+
33+
const auto custom2 = FromJsonString(ToString(json), formats::parse::To<ns::ObjWithCustom>{});
34+
EXPECT_EQ(custom2, custom);
35+
}
36+
2537
TEST(Custom, String) {
2638
auto json = formats::json::MakeObject("string", "make love");
2739
auto custom = json.As<ns::ObjWithCustom>();

chaotic/src/chaotic/io/std/chrono/milliseconds.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
#include <userver/utils/string_to_duration.hpp>
44

5+
#include <fmt/format.h>
6+
57
USERVER_NAMESPACE_BEGIN
68

79
namespace chaotic::convert {
810

11+
std::string Convert(const std::chrono::milliseconds& value, chaotic::convert::To<std::string>) {
12+
return fmt::format("{}ms", value.count());
13+
}
14+
915
std::chrono::milliseconds Convert(const std::string& str, chaotic::convert::To<std::chrono::milliseconds>) {
1016
return std::chrono::milliseconds{utils::StringToDuration(str)};
1117
}

0 commit comments

Comments
 (0)