Fix parameters missing from services (#381)

This commit is contained in:
James Hilton-Balfe
2022-07-06 19:05:40 +01:00
committed by GitHub
parent bc13e7070d
commit 3fd5a0d662
9 changed files with 136 additions and 40 deletions

View File

@@ -379,15 +379,10 @@ def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes:
elif proto_type == TYPE_MESSAGE:
if isinstance(value, datetime):
# Convert the `datetime` to a timestamp message.
seconds = int(value.timestamp())
nanos = int(value.microsecond * 1e3)
value = _Timestamp(seconds=seconds, nanos=nanos)
value = _Timestamp.from_datetime(value)
elif isinstance(value, timedelta):
# Convert the `timedelta` to a duration message.
total_ms = value // timedelta(microseconds=1)
seconds = int(total_ms / 1e6)
nanos = int((total_ms % 1e6) * 1e3)
value = _Duration(seconds=seconds, nanos=nanos)
value = _Duration.from_timedelta(value)
elif wraps:
if value is None:
return b""
@@ -1505,6 +1500,15 @@ from .lib.google.protobuf import ( # noqa
class _Duration(Duration):
@classmethod
def from_timedelta(
cls, delta: timedelta, *, _1_microsecond: timedelta = timedelta(microseconds=1)
) -> "_Duration":
total_ms = delta // _1_microsecond
seconds = int(total_ms / 1e6)
nanos = int((total_ms % 1e6) * 1e3)
return cls(seconds, nanos)
def to_timedelta(self) -> timedelta:
return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3)
@@ -1518,6 +1522,12 @@ class _Duration(Duration):
class _Timestamp(Timestamp):
@classmethod
def from_datetime(cls, dt: datetime) -> "_Timestamp":
seconds = int(dt.timestamp())
nanos = int(dt.microsecond * 1e3)
return cls(seconds, nanos)
def to_datetime(self) -> datetime:
ts = self.seconds + (self.nanos / 1e9)
return datetime.fromtimestamp(ts, tz=timezone.utc)