+38
-9
lines changedFilter options
+38
-9
lines changed Original file line number Diff line number Diff line change
@@ -7,14 +7,17 @@
7
7
8
8
"""Contains the Any helper APIs."""
9
9
10
-
from typing import Optional
10
+
from typing import Optional, TypeVar
11
11
12
12
from google.protobuf import descriptor
13
13
from google.protobuf.message import Message
14
14
15
15
from google.protobuf.any_pb2 import Any
16
16
17
17
18
+
_MessageT = TypeVar('_MessageT', bound=Message)
19
+
20
+
18
21
def pack(
19
22
msg: Message,
20
23
type_url_prefix: Optional[str] = 'type.googleapis.com/',
@@ -31,6 +34,17 @@ def unpack(any_msg: Any, msg: Message) -> bool:
31
34
return any_msg.Unpack(msg=msg)
32
35
33
36
37
+
def unpack_as(any_msg: Any, message_type: type[_MessageT]) -> _MessageT:
38
+
unpacked = message_type()
39
+
if unpack(any_msg, unpacked):
40
+
return unpacked
41
+
else:
42
+
raise TypeError(
43
+
f'Attempted to unpack {type_name(any_msg)} to'
44
+
f' {message_type.__qualname__}'
45
+
)
46
+
47
+
34
48
def type_name(any_msg: Any) -> str:
35
49
return any_msg.TypeName()
36
50
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
10
10
11
11
import unittest
12
12
13
-
from google.protobuf import any
13
+
from google.protobuf import any as proto_any
14
14
15
15
from google.protobuf import any_pb2
16
16
from google.protobuf import unittest_pb2
@@ -20,27 +20,42 @@ class AnyTest(unittest.TestCase):
20
20
21
21
def test_pack_unpack(self):
22
22
all_types = unittest_pb2.TestAllTypes()
23
-
any_msg = any.pack(all_types)
23
+
any_msg = proto_any.pack(all_types)
24
24
all_descriptor = all_types.DESCRIPTOR
25
25
self.assertEqual(
26
26
any_msg.type_url, 'type.googleapis.com/%s' % all_descriptor.full_name
27
27
)
28
+
29
+
# Any can be successfully unpacked to the correct message type.
28
30
unpacked_message = unittest_pb2.TestAllTypes()
29
-
self.assertTrue(any.unpack(any_msg, unpacked_message))
31
+
self.assertTrue(proto_any.unpack(any_msg, unpacked_message))
32
+
33
+
proto_any.unpack_as(any_msg, unittest_pb2.TestAllTypes)
34
+
35
+
# Any can't be unpacked to an incorrect message type.
36
+
self.assertFalse(
37
+
proto_any.unpack(any_msg, unittest_pb2.TestAllTypes.NestedMessage())
38
+
)
39
+
40
+
with self.assertRaises(TypeError) as catcher:
41
+
proto_any.unpack_as(any_msg, unittest_pb2.TestAllTypes.NestedMessage)
42
+
self.assertIn('Attempted to unpack', catcher.exception.args[0])
30
43
31
44
def test_type_name(self):
32
45
all_types = unittest_pb2.TestAllTypes()
33
-
any_msg = any.pack(all_types)
34
-
self.assertEqual(any.type_name(any_msg), 'proto2_unittest.TestAllTypes')
46
+
any_msg = proto_any.pack(all_types)
47
+
self.assertEqual(
48
+
proto_any.type_name(any_msg), 'proto2_unittest.TestAllTypes'
49
+
)
35
50
36
51
def test_is_type(self):
37
52
all_types = unittest_pb2.TestAllTypes()
38
-
any_msg = any.pack(all_types)
53
+
any_msg = proto_any.pack(all_types)
39
54
all_descriptor = all_types.DESCRIPTOR
40
-
self.assertTrue(any.is_type(any_msg, all_descriptor))
55
+
self.assertTrue(proto_any.is_type(any_msg, all_descriptor))
41
56
42
57
empty_any = any_pb2.Any()
43
-
self.assertFalse(any.is_type(empty_any, all_descriptor))
58
+
self.assertFalse(proto_any.is_type(empty_any, all_descriptor))
44
59
45
60
46
61
if __name__ == '__main__':
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4