protobuf_compat.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import inspect
  2. from google.protobuf.json_format import MessageToDict, MessageToJson
  3. """
  4. This module provides a compatibility layer for different versions of the protobuf
  5. library.
  6. """
  7. _protobuf_has_old_arg_name_cached = None
  8. def _protobuf_has_old_arg_name():
  9. """Cache the inspect result to avoid doing it for every single message."""
  10. global _protobuf_has_old_arg_name_cached
  11. if _protobuf_has_old_arg_name_cached is None:
  12. params = inspect.signature(MessageToDict).parameters
  13. _protobuf_has_old_arg_name_cached = "including_default_value_fields" in params
  14. return _protobuf_has_old_arg_name_cached
  15. def rename_always_print_fields_with_no_presence(kwargs):
  16. """
  17. Protobuf version 5.26.0rc2 renamed argument for `MessageToDict` and `MessageToJson`:
  18. `including_default_value_fields` -> `always_print_fields_with_no_presence`.
  19. See https://github.com/protocolbuffers/protobuf/commit/06e7caba58ede0220b110b89d08f329e5f8a7537#diff-8de817c14d6a087981503c9aea38730b1b3e98f4e306db5ff9d525c7c304f234L129 # noqa: E501
  20. We choose to always use the new argument name. If user used the old arg, we raise an
  21. error.
  22. If protobuf does not have the new arg name but have the old arg name, we rename our
  23. arg to the old one.
  24. """
  25. old_arg_name = "including_default_value_fields"
  26. new_arg_name = "always_print_fields_with_no_presence"
  27. if old_arg_name in kwargs:
  28. raise ValueError(f"{old_arg_name} is deprecated, please use {new_arg_name}")
  29. if new_arg_name in kwargs and _protobuf_has_old_arg_name():
  30. kwargs[old_arg_name] = kwargs.pop(new_arg_name)
  31. return kwargs
  32. def message_to_dict(*args, **kwargs):
  33. kwargs = rename_always_print_fields_with_no_presence(kwargs)
  34. return MessageToDict(*args, **kwargs)
  35. def message_to_json(*args, **kwargs):
  36. kwargs = rename_always_print_fields_with_no_presence(kwargs)
  37. return MessageToJson(*args, **kwargs)