-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fea6dd3
commit a3fc56a
Showing
6 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Union, Mapping, Iterable, Dict | ||
|
||
from .base import Serializer, Deserializer, SerializationContext | ||
from .exceptions import SerializationError | ||
|
||
from google.protobuf.message import Message, DecodeError, EncodeError | ||
from google.protobuf.json_format import MessageToDict, ParseDict, ParseError | ||
|
||
__all__ = ("ProtobufSerializer", "ProtobufDeserializer") | ||
|
||
|
||
class ProtobufSerializer(Serializer): | ||
def __init__(self, msg_type: Message, deterministic: bool = False): | ||
""" | ||
Serializer that returns data in protobuf format. | ||
:param msg_type: protobuf message class. | ||
:param deterministic: If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys | ||
Default - `False` | ||
""" | ||
super().__init__() | ||
self._msg_type = msg_type | ||
self._deterministic = deterministic | ||
|
||
def __call__(self, value: Dict, ctx: SerializationContext) -> Union[str, bytes]: | ||
msg = self._msg_type() | ||
|
||
try: | ||
return ParseDict(value, msg).SerializeToString( | ||
deterministic=self._deterministic | ||
) | ||
except (EncodeError, ParseError) as exc: | ||
raise SerializationError(str(exc)) from exc | ||
|
||
|
||
class ProtobufDeserializer(Deserializer): | ||
def __init__( | ||
self, | ||
msg_type: Message, | ||
use_integers_for_enums: bool = False, | ||
): | ||
""" | ||
Deserializer that parses protobuf data into a dictionary suitable for a StreamingDataframe. | ||
:param msg_type: protobuf message class. | ||
:param use_integers_for_enums: If true, use integers instead of enum names. | ||
Default - `False` | ||
""" | ||
super().__init__() | ||
self._msg_type = msg_type | ||
self._use_integers_for_enums = use_integers_for_enums | ||
|
||
def __call__( | ||
self, value: bytes, ctx: SerializationContext | ||
) -> Union[Iterable[Mapping], Mapping]: | ||
msg = self._msg_type() | ||
|
||
try: | ||
msg.ParseFromString(value) | ||
return MessageToDict( | ||
msg, | ||
always_print_fields_with_no_presence=True, | ||
use_integers_for_enums=self._use_integers_for_enums, | ||
) | ||
except DecodeError as exc: | ||
raise SerializationError(str(exc)) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
syntax = "proto3"; | ||
|
||
package test; | ||
|
||
enum TestEnum { | ||
A = 0; | ||
B = 1; | ||
} | ||
|
||
message Test { | ||
string name = 1; | ||
int32 id = 2; | ||
TestEnum enum = 3; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters