Skip to content

Commit

Permalink
Add absl::Cord overloads to FromString and ToString methods on Messag…
Browse files Browse the repository at this point in the history
…eLite.

This removes an impedance gap when migrating string fields to ctype=CORD

PiperOrigin-RevId: 703524999
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Dec 6, 2024
1 parent 0b185ad commit f10826e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
24 changes: 12 additions & 12 deletions src/google/protobuf/message_lite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,20 @@ struct SourceWrapper<absl::Cord> {

} // namespace internal

bool MessageLite::MergeFromCord(const absl::Cord& cord) {
return ParseFrom<kMerge>(internal::SourceWrapper<absl::Cord>(&cord));
bool MessageLite::MergeFromString(const absl::Cord& data) {
return ParseFrom<kMerge>(internal::SourceWrapper<absl::Cord>(&data));
}

bool MessageLite::MergePartialFromCord(const absl::Cord& cord) {
return ParseFrom<kMergePartial>(internal::SourceWrapper<absl::Cord>(&cord));
bool MessageLite::MergePartialFromString(const absl::Cord& data) {
return ParseFrom<kMergePartial>(internal::SourceWrapper<absl::Cord>(&data));
}

bool MessageLite::ParseFromCord(const absl::Cord& cord) {
return ParseFrom<kParse>(internal::SourceWrapper<absl::Cord>(&cord));
bool MessageLite::ParseFromString(const absl::Cord& data) {
return ParseFrom<kParse>(internal::SourceWrapper<absl::Cord>(&data));
}

bool MessageLite::ParsePartialFromCord(const absl::Cord& cord) {
return ParseFrom<kParsePartial>(internal::SourceWrapper<absl::Cord>(&cord));
bool MessageLite::ParsePartialFromString(const absl::Cord& data) {
return ParseFrom<kParsePartial>(internal::SourceWrapper<absl::Cord>(&data));
}

// ===================================================================
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
66 changes: 52 additions & 14 deletions src/google/protobuf/message_lite.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ---------------------------------------------------
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit f10826e

Please sign in to comment.