1#include <userver/proto-structs/io/std/chrono/year_month_day.hpp>
6#include <google/type/date.pb.h>
8#include <userver/proto-structs/io/context.hpp>
10namespace proto_structs::io {
12constexpr int kMaxYearInGoogleDate = 9999;
13constexpr int kMinYearInGoogleDate = 1;
15std::chrono::year_month_day
16ReadProtoStruct(ReadContext& ctx, To<std::chrono::year_month_day>,
const ::google::type::Date& msg) {
17 std::chrono::year_month_day result = {};
19 if (msg.year() < kMinYearInGoogleDate || msg.year() > kMaxYearInGoogleDate) {
20 ctx.AddError(fmt::format(
21 "'year' component value {} is not in a valid range [{}, {}]",
30 std::chrono::year{msg.year()},
31 std::chrono::month{
static_cast<
unsigned>(msg.month())},
32 std::chrono::day{
static_cast<
unsigned>(msg.day())}};
35 ctx.AddError(fmt::format(
"{}/{}/{} (y/m/d) does not represent a valid date", msg.year(), msg.month(), msg.day())
43void WriteProtoStruct(WriteContext& ctx,
const std::chrono::year_month_day& obj, ::google::type::Date& msg) {
45 ctx.AddError(fmt::format(
46 "{}/{}/{} (y/m/d) does not represent a valid date",
47 static_cast<
int>(obj.year()),
48 static_cast<
unsigned>(obj.month()),
49 static_cast<
unsigned>(obj.day())
54 if (
static_cast<
int>(obj.year()) < kMinYearInGoogleDate ||
static_cast<
int>(obj.year()) > kMaxYearInGoogleDate) {
55 ctx.AddError(fmt::format(
56 "'year' component value {} is not in a valid range [{}, {}]",
57 static_cast<
int>(obj.year()),
64 msg.set_year(
static_cast<int32_t>(obj.year()));
65 msg.set_month(
static_cast<int32_t>(
static_cast<
unsigned>(obj.month())));
66 msg.set_day(
static_cast<int32_t>(
static_cast<
unsigned>(obj.day())));