-
Notifications
You must be signed in to change notification settings - Fork 15.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nextgen Proto Pythonic API: “Add-on” proto for serialize/parse
- add google.protobuf.proto module - wrap generated SerializeToString and ParseFromString to the new module: def serialize(message: Message, deterministic: bool=None) -> bytes: """Return the serialized proto.""" def parse(message_class: typing.Type[Message], payload: bytes) -> Message: """Given a serialized proto, deserialize it into a Message.""" PiperOrigin-RevId: 632223409
- Loading branch information
1 parent
82e83dd
commit 495ba7b
Showing
2 changed files
with
73 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# Protocol Buffers - Google's data interchange format | ||
# Copyright 2008 Google Inc. All rights reserved. | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
"""Tests Nextgen Pythonic protobuf APIs.""" | ||
|
||
import unittest | ||
|
||
from google.protobuf import proto | ||
|
||
from google.protobuf.internal import test_util | ||
from google.protobuf.internal import testing_refleaks | ||
from google.protobuf.internal import _parameterized | ||
from google.protobuf import unittest_pb2 | ||
from google.protobuf import unittest_proto3_arena_pb2 | ||
|
||
@_parameterized.named_parameters(('_proto2', unittest_pb2), | ||
('_proto3', unittest_proto3_arena_pb2)) | ||
@testing_refleaks.TestCase | ||
class ProtoTest(unittest.TestCase): | ||
|
||
def testSerializeParse(self, message_module): | ||
msg = message_module.TestAllTypes() | ||
test_util.SetAllFields(msg) | ||
serialized_data = proto.serialize(msg) | ||
parsed_msg = proto.parse(message_module.TestAllTypes, serialized_data) | ||
self.assertEqual(msg, parsed_msg) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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,39 @@ | ||
# Protocol Buffers - Google's data interchange format | ||
# Copyright 2008 Google Inc. All rights reserved. | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
"""Contains the Nextgen Pythonic protobuf APIs.""" | ||
|
||
import typing | ||
|
||
from google.protobuf.message import Message | ||
|
||
def serialize(message: Message, deterministic: bool=None) -> bytes: | ||
"""Return the serialized proto. | ||
Args: | ||
message: The proto message to be serialized. | ||
deterministic: If true, requests deterministic serialization | ||
of the protobuf, with predictable ordering of map keys. | ||
Returns: | ||
A binary bytes representation of the message. | ||
""" | ||
return message.SerializeToString(deterministic=deterministic) | ||
|
||
def parse(message_class: typing.Type[Message], payload: bytes) -> Message: | ||
"""Given a serialized data in binary form, deserialize it into a Message. | ||
Args: | ||
message_class: The message meta class. | ||
payload: A serialized bytes in binary form. | ||
Returns: | ||
A new message deserialized from payload. | ||
""" | ||
new_message = message_class() | ||
new_message.ParseFromString(payload) | ||
return new_message |