From dd95e5b1fa65b06d9206c016b1d53f73abd9da44 Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Mon, 19 Aug 2024 14:59:21 -0700 Subject: [PATCH] Nextgen Proto Pythonic API: Add any.py PiperOrigin-RevId: 664995003 --- python/google/protobuf/any.py | 39 +++++++++++++++++ python/google/protobuf/internal/any_test.py | 47 +++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 python/google/protobuf/any.py create mode 100644 python/google/protobuf/internal/any_test.py diff --git a/python/google/protobuf/any.py b/python/google/protobuf/any.py new file mode 100644 index 000000000000..81e7013efcdd --- /dev/null +++ b/python/google/protobuf/any.py @@ -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 Any helper APIs.""" + +from typing import Optional + +from google.protobuf import descriptor +from google.protobuf.message import Message + +from google.protobuf.any_pb2 import Any + + +def pack( + msg: Message, + type_url_prefix: Optional[str] = 'type.googleapis.com/', + deterministic: Optional[bool] = None, +) -> Any: + any_msg = Any() + any_msg.Pack( + msg=msg, type_url_prefix=type_url_prefix, deterministic=deterministic + ) + return any_msg + + +def unpack(any_msg: Any, msg: Message) -> bool: + return any_msg.Unpack(msg=msg) + + +def type_name(any_msg: Any) -> str: + return any_msg.TypeName() + + +def is_type(any_msg: Any, des: descriptor.Descriptor) -> bool: + return any_msg.Is(des) diff --git a/python/google/protobuf/internal/any_test.py b/python/google/protobuf/internal/any_test.py new file mode 100644 index 000000000000..74d74ef80f17 --- /dev/null +++ b/python/google/protobuf/internal/any_test.py @@ -0,0 +1,47 @@ +# -*- 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 proto Any APIs.""" + +import unittest + +from google.protobuf import any + +from google.protobuf import any_pb2 +from google.protobuf import unittest_pb2 + + +class AnyTest(unittest.TestCase): + + def test_pack_unpack(self): + all_types = unittest_pb2.TestAllTypes() + any_msg = any.pack(all_types) + all_descriptor = all_types.DESCRIPTOR + self.assertEqual( + any_msg.type_url, 'type.googleapis.com/%s' % all_descriptor.full_name + ) + unpacked_message = unittest_pb2.TestAllTypes() + self.assertTrue(any.unpack(any_msg, unpacked_message)) + + def test_type_name(self): + all_types = unittest_pb2.TestAllTypes() + any_msg = any.pack(all_types) + self.assertEqual(any.type_name(any_msg), 'protobuf_unittest.TestAllTypes') + + def test_is_type(self): + all_types = unittest_pb2.TestAllTypes() + any_msg = any.pack(all_types) + all_descriptor = all_types.DESCRIPTOR + self.assertTrue(any.is_type(any_msg, all_descriptor)) + + empty_any = any_pb2.Any() + self.assertFalse(any.is_type(empty_any, all_descriptor)) + + +if __name__ == '__main__': + unittest.main()