-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] try new protobuf #30556
[WIP] try new protobuf #30556
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
# | ||
|
||
"""For internal use only; no backwards-compatibility guarantees.""" | ||
|
||
# pytype: skip-file | ||
|
||
from typing import Type | ||
|
@@ -36,6 +35,10 @@ | |
|
||
message_types = (message.Message, ) | ||
|
||
_MICROS_TO_SECONDS = 1000000 | ||
_MICRO_TO_NANOSECONDS = 1000 | ||
_NANO_TO_SECONDS = 1000000000 | ||
|
||
|
||
@overload | ||
def pack_Any(msg): | ||
|
@@ -123,8 +126,25 @@ def pack_Struct(**kwargs): | |
def from_micros(cls, micros): | ||
# type: (Type[TimeMessageT], int) -> TimeMessageT | ||
result = cls() | ||
result.FromMicroseconds(micros) | ||
return result | ||
if isinstance(result, duration_pb2.Duration): | ||
result.FromMicroseconds(micros) | ||
return result | ||
elif isinstance(result, timestamp_pb2.Timestamp): | ||
result.seconds = micros // _MICROS_TO_SECONDS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add some comments here about why we do these conversions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. add brief comment why we don't use timestamp_pb2.Timestamp.FromMicroseconds() |
||
result.nanos = (micros % _MICROS_TO_SECONDS) * _MICRO_TO_NANOSECONDS | ||
return result | ||
else: | ||
raise RuntimeError('cannot convert the micro seconds to %s' % cls) | ||
|
||
|
||
def to_micros(value: Union[duration_pb2.Duration, timestamp_pb2.Timestamp]): | ||
if isinstance(value, duration_pb2.Duration): | ||
return value.ToMicroseconds() | ||
elif isinstance(value, timestamp_pb2.Timestamp): | ||
micros = value.seconds * _MICROS_TO_SECONDS | ||
return micros + value.nanos // _MICRO_TO_NANOSECONDS | ||
else: | ||
raise RuntimeError('cannot convert %s to micro seconds' % value) | ||
|
||
|
||
def to_Timestamp(time): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
import unittest | ||
|
||
from google.protobuf import duration_pb2 | ||
from google.protobuf import timestamp_pb2 | ||
|
||
from apache_beam.utils import proto_utils | ||
from apache_beam.utils.timestamp import MAX_TIMESTAMP | ||
|
||
|
||
class TestProtoUtils(unittest.TestCase): | ||
def test_from_micros_duration(self): | ||
ts = proto_utils.from_micros(duration_pb2.Duration, MAX_TIMESTAMP.micros) | ||
expected = duration_pb2.Duration( | ||
seconds=MAX_TIMESTAMP.seconds(), nanos=775000000) | ||
self.assertEqual(ts, expected) | ||
|
||
def test_from_micros_timestamp(self): | ||
ts = proto_utils.from_micros(timestamp_pb2.Timestamp, MAX_TIMESTAMP.micros) | ||
expected = timestamp_pb2.Timestamp( | ||
seconds=MAX_TIMESTAMP.seconds(), nanos=775000000) | ||
self.assertEqual(ts, expected) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto elsewhere