aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/interop
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/protobuf/interop
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/protobuf/interop')
-rw-r--r--library/cpp/protobuf/interop/cast.cpp23
-rw-r--r--library/cpp/protobuf/interop/cast.h15
-rw-r--r--library/cpp/protobuf/interop/ut/cast_ut.cpp52
-rw-r--r--library/cpp/protobuf/interop/ut/ya.make15
-rw-r--r--library/cpp/protobuf/interop/ya.make15
5 files changed, 120 insertions, 0 deletions
diff --git a/library/cpp/protobuf/interop/cast.cpp b/library/cpp/protobuf/interop/cast.cpp
new file mode 100644
index 00000000000..c4cd59b417e
--- /dev/null
+++ b/library/cpp/protobuf/interop/cast.cpp
@@ -0,0 +1,23 @@
+#include <library/cpp/protobuf/interop/cast.h>
+
+#include <google/protobuf/duration.pb.h>
+#include <google/protobuf/timestamp.pb.h>
+#include <google/protobuf/util/time_util.h>
+
+namespace NProtoInterop {
+ google::protobuf::Duration CastToProto(TDuration duration) {
+ return google::protobuf::util::TimeUtil::MicrosecondsToDuration(duration.MicroSeconds());
+ }
+
+ google::protobuf::Timestamp CastToProto(TInstant instant) {
+ return google::protobuf::util::TimeUtil::MicrosecondsToTimestamp(instant.MicroSeconds());
+ }
+
+ TDuration CastFromProto(const google::protobuf::Duration& duration) {
+ return TDuration::MicroSeconds(google::protobuf::util::TimeUtil::DurationToMicroseconds(duration));
+ }
+
+ TInstant CastFromProto(const google::protobuf::Timestamp& timestamp) {
+ return TInstant::MicroSeconds(google::protobuf::util::TimeUtil::TimestampToMicroseconds(timestamp));
+ }
+}
diff --git a/library/cpp/protobuf/interop/cast.h b/library/cpp/protobuf/interop/cast.h
new file mode 100644
index 00000000000..b1c295236eb
--- /dev/null
+++ b/library/cpp/protobuf/interop/cast.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <util/datetime/base.h>
+
+namespace google::protobuf {
+ class Duration;
+ class Timestamp;
+}
+
+namespace NProtoInterop {
+ google::protobuf::Duration CastToProto(TDuration duration);
+ google::protobuf::Timestamp CastToProto(TInstant instant);
+ TDuration CastFromProto(const google::protobuf::Duration& message);
+ TInstant CastFromProto(const google::protobuf::Timestamp& message);
+}
diff --git a/library/cpp/protobuf/interop/ut/cast_ut.cpp b/library/cpp/protobuf/interop/ut/cast_ut.cpp
new file mode 100644
index 00000000000..6ef055b6512
--- /dev/null
+++ b/library/cpp/protobuf/interop/ut/cast_ut.cpp
@@ -0,0 +1,52 @@
+#include <library/cpp/protobuf/interop/cast.h>
+#include <library/cpp/testing/unittest/registar.h>
+
+#include <google/protobuf/duration.pb.h>
+#include <google/protobuf/timestamp.pb.h>
+
+static constexpr ui64 MicroSecondsInSecond = 1000 * 1000;
+static constexpr ui64 NanoSecondsInMicroSecond = 1000;
+
+Y_UNIT_TEST_SUITE(TCastTest) {
+ Y_UNIT_TEST(TimestampFromProto) {
+ const ui64 now = TInstant::Now().MicroSeconds();
+
+ google::protobuf::Timestamp timestamp;
+ timestamp.set_seconds(now / MicroSecondsInSecond);
+ timestamp.set_nanos((now % MicroSecondsInSecond) * NanoSecondsInMicroSecond);
+
+ const TInstant instant = NProtoInterop::CastFromProto(timestamp);
+ UNIT_ASSERT_EQUAL(instant.MicroSeconds(), now);
+ }
+
+ Y_UNIT_TEST(DurationFromProto) {
+ const ui64 now = TInstant::Now().MicroSeconds();
+
+ google::protobuf::Duration message;
+ message.set_seconds(now / MicroSecondsInSecond);
+ message.set_nanos((now % MicroSecondsInSecond) * NanoSecondsInMicroSecond);
+
+ const TDuration duration = NProtoInterop::CastFromProto(message);
+ UNIT_ASSERT_EQUAL(duration.MicroSeconds(), now);
+ }
+
+ Y_UNIT_TEST(TimestampToProto) {
+ const TInstant instant = TInstant::Now();
+
+ google::protobuf::Timestamp timestamp = NProtoInterop::CastToProto(instant);
+ const ui64 microSeconds = timestamp.seconds() * MicroSecondsInSecond +
+ timestamp.nanos() / NanoSecondsInMicroSecond;
+
+ UNIT_ASSERT_EQUAL(instant.MicroSeconds(), microSeconds);
+ }
+
+ Y_UNIT_TEST(DurationToProto) {
+ const TDuration duration = TDuration::Seconds(TInstant::Now().Seconds() / 2);
+
+ google::protobuf::Duration message = NProtoInterop::CastToProto(duration);
+ const ui64 microSeconds = message.seconds() * MicroSecondsInSecond +
+ message.nanos() / NanoSecondsInMicroSecond;
+
+ UNIT_ASSERT_EQUAL(duration.MicroSeconds(), microSeconds);
+ }
+}
diff --git a/library/cpp/protobuf/interop/ut/ya.make b/library/cpp/protobuf/interop/ut/ya.make
new file mode 100644
index 00000000000..b9c634cb6b3
--- /dev/null
+++ b/library/cpp/protobuf/interop/ut/ya.make
@@ -0,0 +1,15 @@
+UNITTEST_FOR(library/cpp/protobuf/interop)
+
+OWNER(
+ paxakor
+)
+
+SRCS(
+ cast_ut.cpp
+)
+
+PEERDIR(
+ library/cpp/protobuf/interop
+)
+
+END()
diff --git a/library/cpp/protobuf/interop/ya.make b/library/cpp/protobuf/interop/ya.make
new file mode 100644
index 00000000000..618b5534597
--- /dev/null
+++ b/library/cpp/protobuf/interop/ya.make
@@ -0,0 +1,15 @@
+LIBRARY()
+
+OWNER(
+ paxakor
+)
+
+SRCS(
+ cast.cpp
+)
+
+PEERDIR(
+ contrib/libs/protobuf
+)
+
+END()