pb_io.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include <util/generic/fwd.h>
  3. #include <util/generic/flags.h>
  4. struct IBinSaver;
  5. namespace google {
  6. namespace protobuf {
  7. class Message;
  8. }
  9. }
  10. namespace NProtoBuf {
  11. using Message = ::google::protobuf::Message;
  12. }
  13. class IInputStream;
  14. class IOutputStream;
  15. namespace NProtoBuf {
  16. /* Parse base64 URL encoded serialized message from string.
  17. */
  18. void ParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven = false);
  19. bool TryParseFromBase64String(const TStringBuf dataBase64, Message& m, bool allowUneven = false);
  20. template <typename T>
  21. static T ParseFromBase64String(const TStringBuf& dataBase64, bool allowUneven = false) {
  22. T m;
  23. ParseFromBase64String(dataBase64, m, allowUneven);
  24. return m;
  25. }
  26. /* Serialize message into string and apply base64 URL encoding.
  27. */
  28. TString SerializeToBase64String(const Message& m);
  29. void SerializeToBase64String(const Message& m, TString& dataBase64);
  30. bool TrySerializeToBase64String(const Message& m, TString& dataBase64);
  31. const TString ShortUtf8DebugString(const Message& message);
  32. bool MergePartialFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage);
  33. bool MergeFromString(NProtoBuf::Message& m, const TStringBuf serializedProtoMessage);
  34. }
  35. int operator&(NProtoBuf::Message& m, IBinSaver& f);
  36. // Write a textual representation of the given message to the given file.
  37. void SerializeToTextFormat(const NProtoBuf::Message& m, const TString& fileName);
  38. void SerializeToTextFormat(const NProtoBuf::Message& m, IOutputStream& out);
  39. // Write a textual representation of the given message to the given output stream
  40. // with flags UseShortRepeatedPrimitives and UseUtf8StringEscaping set to true.
  41. void SerializeToTextFormatPretty(const NProtoBuf::Message& m, IOutputStream& out);
  42. // Write a textual representation of the given message to the given output stream
  43. // use enum id instead of enum name for all enum fields.
  44. void SerializeToTextFormatWithEnumId(const NProtoBuf::Message& m, IOutputStream& out);
  45. enum class EParseFromTextFormatOption : ui64 {
  46. // Unknown fields will be ignored by the parser
  47. AllowUnknownField = 1
  48. };
  49. Y_DECLARE_FLAGS(EParseFromTextFormatOptions, EParseFromTextFormatOption);
  50. // Parse a text-format protocol message from the given file into message object.
  51. void ParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
  52. const EParseFromTextFormatOptions options = {}, IOutputStream* warningStream = nullptr);
  53. // NOTE: will read `in` till the end.
  54. void ParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
  55. const EParseFromTextFormatOptions options = {}, IOutputStream* warningStream = nullptr);
  56. /* @return `true` if parsing was successfull and `false` otherwise.
  57. *
  58. * @see `ParseFromTextFormat`
  59. */
  60. bool TryParseFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
  61. const EParseFromTextFormatOptions options = {}, IOutputStream* warningStream = nullptr);
  62. // NOTE: will read `in` till the end.
  63. bool TryParseFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
  64. const EParseFromTextFormatOptions options = {}, IOutputStream* warningStream = nullptr);
  65. // @see `ParseFromTextFormat`
  66. template <typename T>
  67. static T ParseFromTextFormat(const TString& fileName,
  68. const EParseFromTextFormatOptions options = {}, IOutputStream* warningStream = nullptr) {
  69. T message;
  70. ParseFromTextFormat(fileName, message, options, warningStream);
  71. return message;
  72. }
  73. // @see `ParseFromTextFormat`
  74. // NOTE: will read `in` till the end.
  75. template <typename T>
  76. static T ParseFromTextFormat(IInputStream& in, const EParseFromTextFormatOptions options = {},
  77. IOutputStream* warningStream = nullptr) {
  78. T message;
  79. ParseFromTextFormat(in, message, options, warningStream);
  80. return message;
  81. }
  82. // Merge a text-format protocol message from the given file into message object.
  83. //
  84. // NOTE: Even when parsing failed and exception was thrown `m` may be different from its original
  85. // value. User must implement transactional logic around `MergeFromTextFormat` by himself.
  86. void MergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
  87. const EParseFromTextFormatOptions options = {});
  88. // NOTE: will read `in` till the end.
  89. void MergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
  90. const EParseFromTextFormatOptions options = {});
  91. /* @return `true` if parsing was successfull and `false` otherwise.
  92. *
  93. * @see `MergeFromTextFormat`
  94. */
  95. bool TryMergeFromTextFormat(const TString& fileName, NProtoBuf::Message& m,
  96. const EParseFromTextFormatOptions options = {});
  97. // NOTE: will read `in` till the end.
  98. bool TryMergeFromTextFormat(IInputStream& in, NProtoBuf::Message& m,
  99. const EParseFromTextFormatOptions options = {});
  100. // @see `MergeFromTextFormat`
  101. template <typename T>
  102. static T MergeFromTextFormat(const TString& fileName,
  103. const EParseFromTextFormatOptions options = {}) {
  104. T message;
  105. MergeFromTextFormat(fileName, message, options);
  106. return message;
  107. }
  108. // @see `MergeFromTextFormat`
  109. // NOTE: will read `in` till the end.
  110. template <typename T>
  111. static T MergeFromTextFormat(IInputStream& in,
  112. const EParseFromTextFormatOptions options = {}) {
  113. T message;
  114. MergeFromTextFormat(in, message, options);
  115. return message;
  116. }