userver: /data/code/userver/libraries/proto-structs/src/proto-structs/io/std/chrono/year_month_day_conv.cpp Source File
Loading...
Searching...
No Matches
year_month_day_conv.cpp
1#include <userver/proto-structs/io/std/chrono/year_month_day_conv.hpp>
2
3#include <google/type/date.pb.h>
4
5#include <userver/proto-structs/date.hpp>
6#include <userver/proto-structs/exceptions.hpp>
7#include <userver/proto-structs/io/context.hpp>
8#include <userver/utils/impl/internal_tag.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace proto_structs::io {
13
14std::chrono::year_month_day ReadProtoStruct(
15 ReadContext& ctx,
16 To<std::chrono::year_month_day>,
17 const ::google::type::Date& msg
18) {
19 try {
20 Date date(utils::impl::InternalTag{}, msg.year(), msg.month(), msg.day());
21
22 if (!date.HasYearMonthDay()) {
23 ctx.AddError("full date is expected for 'std::chrono::year_month_day' proto struct field");
24 return std::chrono::year_month_day{std::chrono::year{0}, std::chrono::month{0}, std::chrono::day{0}};
25 }
26
27 return date.ToChronoDate();
28 } catch (const ValueError& e) {
29 ctx.AddError(e.what());
30 return std::chrono::year_month_day{std::chrono::year{0}, std::chrono::month{0}, std::chrono::day{0}};
31 }
32}
33
34void WriteProtoStruct(WriteContext& ctx, const std::chrono::year_month_day& obj, ::google::type::Date& msg) {
35 try {
36 Date date{obj};
37 msg.set_year(date.YearNum());
38 msg.set_month(date.MonthNum());
39 msg.set_day(date.DayNum());
40 } catch (const ValueError& e) {
41 ctx.AddError(e.what());
42 }
43}
44
45} // namespace proto_structs::io
46
47USERVER_NAMESPACE_END