-
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 0942db8
Showing
6 changed files
with
133 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,56 @@ | ||
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, | ||
): | ||
""" | ||
Serializer that returns data in protobuf format. | ||
:param msg_type: protobuf message class. | ||
""" | ||
super().__init__() | ||
self._msg_type = msg_type | ||
|
||
def __call__(self, value: Dict, ctx: SerializationContext) -> Union[str, bytes]: | ||
msg = self._msg_type() | ||
|
||
try: | ||
return ParseDict(value, msg).SerializeToString(deterministic=True) | ||
except (EncodeError, ParseError) as exc: | ||
raise SerializationError(str(exc)) from exc | ||
|
||
|
||
class ProtobufDeserializer(Deserializer): | ||
def __init__( | ||
self, | ||
msg_type: Message, | ||
): | ||
""" | ||
Deserializer that parses protobuf data. | ||
:param msg_type: protobuf message class. | ||
""" | ||
super().__init__() | ||
self._msg_type = msg_type | ||
|
||
def __call__( | ||
self, value: bytes, ctx: SerializationContext | ||
) -> Union[Iterable[Mapping], Mapping]: | ||
msg = self._msg_type() | ||
|
||
try: | ||
msg.ParseFromString(value) | ||
return MessageToDict(msg) | ||
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,8 @@ | ||
syntax = "proto3"; | ||
|
||
package test; | ||
|
||
message Test { | ||
string name = 1; | ||
int32 id = 2; | ||
} |
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