Adjusts values before serialization.
(proto_type: str, wraps: str, value: Any)
| 383 | |
| 384 | |
| 385 | def _preprocess_single(proto_type: str, wraps: str, value: Any) -> bytes: |
| 386 | """Adjusts values before serialization.""" |
| 387 | if proto_type in ( |
| 388 | TYPE_ENUM, |
| 389 | TYPE_BOOL, |
| 390 | TYPE_INT32, |
| 391 | TYPE_INT64, |
| 392 | TYPE_UINT32, |
| 393 | TYPE_UINT64, |
| 394 | ): |
| 395 | return encode_varint(value) |
| 396 | elif proto_type in (TYPE_SINT32, TYPE_SINT64): |
| 397 | # Handle zig-zag encoding. |
| 398 | return encode_varint(value << 1 if value >= 0 else (value << 1) ^ (~0)) |
| 399 | elif proto_type in FIXED_TYPES: |
| 400 | return struct.pack(_pack_fmt(proto_type), value) |
| 401 | elif proto_type == TYPE_STRING: |
| 402 | return value.encode("utf-8") |
| 403 | elif proto_type == TYPE_MESSAGE: |
| 404 | if isinstance(value, datetime): |
| 405 | # Convert the `datetime` to a timestamp message. |
| 406 | value = _Timestamp.from_datetime(value) |
| 407 | elif isinstance(value, timedelta): |
| 408 | # Convert the `timedelta` to a duration message. |
| 409 | value = _Duration.from_timedelta(value) |
| 410 | elif wraps: |
| 411 | if value is None: |
| 412 | return b"" |
| 413 | value = _get_wrapper(wraps)(value=value) |
| 414 | |
| 415 | return bytes(value) |
| 416 | |
| 417 | return value |
| 418 | |
| 419 | |
| 420 | def _len_preprocessed_single(proto_type: str, wraps: str, value: Any) -> int: |
no test coverage detected