any.py 975 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """Contains the Any helper APIs."""
  8. from typing import Optional
  9. from google.protobuf import descriptor
  10. from google.protobuf.message import Message
  11. from google.protobuf.any_pb2 import Any
  12. def pack(
  13. msg: Message,
  14. type_url_prefix: Optional[str] = 'type.googleapis.com/',
  15. deterministic: Optional[bool] = None,
  16. ) -> Any:
  17. any_msg = Any()
  18. any_msg.Pack(
  19. msg=msg, type_url_prefix=type_url_prefix, deterministic=deterministic
  20. )
  21. return any_msg
  22. def unpack(any_msg: Any, msg: Message) -> bool:
  23. return any_msg.Unpack(msg=msg)
  24. def type_name(any_msg: Any) -> str:
  25. return any_msg.TypeName()
  26. def is_type(any_msg: Any, des: descriptor.Descriptor) -> bool:
  27. return any_msg.Is(des)