From f10826ed25d34c2a82eb71860adb8e6b260e4fa1 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 6 Dec 2024 09:32:54 -0800 Subject: [PATCH] Add absl::Cord overloads to FromString and ToString methods on MessageLite. This removes an impedance gap when migrating string fields to ctype=CORD PiperOrigin-RevId: 703524999 --- src/google/protobuf/message_lite.cc | 24 +++++------ src/google/protobuf/message_lite.h | 66 +++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/src/google/protobuf/message_lite.cc b/src/google/protobuf/message_lite.cc index d2c19da69070..efaad9d81195 100644 --- a/src/google/protobuf/message_lite.cc +++ b/src/google/protobuf/message_lite.cc @@ -443,20 +443,20 @@ struct SourceWrapper { } // namespace internal -bool MessageLite::MergeFromCord(const absl::Cord& cord) { - return ParseFrom(internal::SourceWrapper(&cord)); +bool MessageLite::MergeFromString(const absl::Cord& data) { + return ParseFrom(internal::SourceWrapper(&data)); } -bool MessageLite::MergePartialFromCord(const absl::Cord& cord) { - return ParseFrom(internal::SourceWrapper(&cord)); +bool MessageLite::MergePartialFromString(const absl::Cord& data) { + return ParseFrom(internal::SourceWrapper(&data)); } -bool MessageLite::ParseFromCord(const absl::Cord& cord) { - return ParseFrom(internal::SourceWrapper(&cord)); +bool MessageLite::ParseFromString(const absl::Cord& data) { + return ParseFrom(internal::SourceWrapper(&data)); } -bool MessageLite::ParsePartialFromCord(const absl::Cord& cord) { - return ParseFrom(internal::SourceWrapper(&cord)); +bool MessageLite::ParsePartialFromString(const absl::Cord& data) { + return ParseFrom(internal::SourceWrapper(&data)); } // =================================================================== @@ -640,13 +640,13 @@ std::string MessageLite::SerializePartialAsString() const { return output; } -bool MessageLite::AppendToCord(absl::Cord* output) const { +bool MessageLite::AppendToString(absl::Cord* output) const { ABSL_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this); return AppendPartialToCord(output); } -bool MessageLite::AppendPartialToCord(absl::Cord* output) const { +bool MessageLite::AppendPartialToString(absl::Cord* output) const { // For efficiency, we'd like to pass a size hint to CordOutputStream with // the exact total size expected. const size_t size = ByteSizeLong(); @@ -692,12 +692,12 @@ bool MessageLite::AppendPartialToCord(absl::Cord* output) const { return true; } -bool MessageLite::SerializeToCord(absl::Cord* output) const { +bool MessageLite::SerializeToString(absl::Cord* output) const { output->Clear(); return AppendToCord(output); } -bool MessageLite::SerializePartialToCord(absl::Cord* output) const { +bool MessageLite::SerializePartialToString(absl::Cord* output) const { output->Clear(); return AppendPartialToCord(output); } diff --git a/src/google/protobuf/message_lite.h b/src/google/protobuf/message_lite.h index fa83a979bf82..c3d70de64521 100644 --- a/src/google/protobuf/message_lite.h +++ b/src/google/protobuf/message_lite.h @@ -654,16 +654,20 @@ class PROTOBUF_EXPORT MessageLite { // missing required fields. ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromBoundedZeroCopyStream( io::ZeroCopyInputStream* input, int size); - // Parses a protocol buffer contained in a string. Returns true on success. - // This function takes a string in the (non-human-readable) binary wire - // format, matching the encoding output by MessageLite::SerializeToString(). - // If you'd like to convert a human-readable string into a protocol buffer - // object, see google::protobuf::TextFormat::ParseFromString(). + // Parses a protocol buffer contained in a string or Cord. Returns true on + // success. This function takes a string in the (non-human-readable) binary + // wire format, matching the encoding output by + // MessageLite::SerializeToString(). If you'd like to convert a human-readable + // string into a protocol buffer object, see + // google::protobuf::TextFormat::ParseFromString(). ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromString(absl::string_view data); + ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromString(const absl::Cord& data); // Like ParseFromString(), but accepts messages that are missing // required fields. ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString( absl::string_view data); + ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromString( + const absl::Cord& data); // Parse a protocol buffer contained in an array of bytes. ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromArray(const void* data, int size); // Like ParseFromArray(), but accepts messages that are missing @@ -694,6 +698,12 @@ class PROTOBUF_EXPORT MessageLite { // Merge a protocol buffer contained in a string. bool MergeFromString(absl::string_view data); + bool MergeFromString(const absl::Cord& data); + + // Like MergeFromString(), but accepts messages that are missing required + // fields. + bool MergePartialFromString(absl::string_view data); + bool MergePartialFromString(const absl::Cord& data); // Serialization --------------------------------------------------- @@ -714,8 +724,12 @@ class PROTOBUF_EXPORT MessageLite { // Serialize the message and store it in the given string. All required // fields must be set. bool SerializeToString(std::string* output) const; + // Serialize the message and store it in the given Cord. All required + // fields must be set. + bool SerializeToString(absl::Cord* output) const; // Like SerializeToString(), but allows missing required fields. bool SerializePartialToString(std::string* output) const; + bool SerializePartialToString(absl::Cord* output) const; // Serialize the message and store it in the given byte array. All required // fields must be set. bool SerializeToArray(void* data, int size) const; @@ -746,26 +760,45 @@ class PROTOBUF_EXPORT MessageLite { // Like SerializeToString(), but appends to the data to the string's // existing contents. All required fields must be set. bool AppendToString(std::string* output) const; + bool AppendToString(absl::Cord* output) const; // Like AppendToString(), but allows missing required fields. bool AppendPartialToString(std::string* output) const; + bool AppendPartialToString(absl::Cord* output) const; // Reads a protocol buffer from a Cord and merges it into this message. - bool MergeFromCord(const absl::Cord& cord); + PROTOBUF_DEPRECATE_AND_INLINE() bool MergeFromCord(const absl::Cord& data) { + return MergeFromString(data); + } // Like MergeFromCord(), but accepts messages that are missing // required fields. - bool MergePartialFromCord(const absl::Cord& cord); + PROTOBUF_DEPRECATE_AND_INLINE() + bool MergePartialFromCord(const absl::Cord& data) { + return MergePartialFromString(data); + } // Parse a protocol buffer contained in a Cord. - ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromCord(const absl::Cord& cord); + PROTOBUF_DEPRECATE_AND_INLINE() + ABSL_ATTRIBUTE_REINITIALIZES bool ParseFromCord(const absl::Cord& data) { + return ParseFromString(data); + } // Like ParseFromCord(), but accepts messages that are missing // required fields. - ABSL_ATTRIBUTE_REINITIALIZES bool ParsePartialFromCord( - const absl::Cord& cord); + PROTOBUF_DEPRECATE_AND_INLINE() + ABSL_ATTRIBUTE_REINITIALIZES + bool ParsePartialFromCord(const absl::Cord& data) { + return ParsePartialFromString(data); + } // Serialize the message and store it in the given Cord. All required // fields must be set. - bool SerializeToCord(absl::Cord* output) const; + PROTOBUF_DEPRECATE_AND_INLINE() + bool SerializeToCord(absl::Cord* output) const { + return SerializeToString(output); + } // Like SerializeToCord(), but allows missing required fields. - bool SerializePartialToCord(absl::Cord* output) const; + PROTOBUF_DEPRECATE_AND_INLINE() + bool SerializePartialToCord(absl::Cord* output) const { + return SerializePartialToString(output); + } // Make a Cord encoding the message. Is equivalent to calling // SerializeToCord() on a Cord and using that. Returns an empty @@ -776,9 +809,14 @@ class PROTOBUF_EXPORT MessageLite { // Like SerializeToCord(), but appends to the data to the Cord's existing // contents. All required fields must be set. - bool AppendToCord(absl::Cord* output) const; + PROTOBUF_DEPRECATE_AND_INLINE() bool AppendToCord(absl::Cord* output) const { + return AppendToString(output); + } // Like AppendToCord(), but allows missing required fields. - bool AppendPartialToCord(absl::Cord* output) const; + PROTOBUF_DEPRECATE_AND_INLINE() + bool AppendPartialToCord(absl::Cord* output) const { + return AppendPartialToString(output); + } // Computes the serialized size of the message. This recursively calls // ByteSizeLong() on all embedded messages.