Skip to content

Commit

Permalink
Add UnknownFieldSet::SerializeToCord()
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 495164471
  • Loading branch information
martijnvels authored and copybara-github committed Dec 14, 2022
1 parent c658e27 commit 8661e45
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/google/protobuf/unknown_field_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ bool UnknownFieldSet::SerializeToCodedStream(
google::protobuf::internal::WireFormat::SerializeUnknownFields(*this, output);
return !output->HadError();
}

bool UnknownFieldSet::SerializeToCord(absl::Cord* output) const {
const size_t size =
google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize(*this);
io::CordOutputStream cord_output_stream(size);
{
io::CodedOutputStream coded_output_stream(&cord_output_stream);
if (!SerializeToCodedStream(&coded_output_stream)) return false;
}
*output = cord_output_stream.Consume();
return true;
}

void UnknownField::Delete() {
switch (type()) {
case UnknownField::TYPE_LENGTH_DELIMITED:
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/unknown_field_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class PROTOBUF_EXPORT UnknownFieldSet {

// Serialization.
bool SerializeToString(std::string* output) const;
bool SerializeToCord(absl::Cord* output) const;
bool SerializeToCodedStream(io::CodedOutputStream* output) const;
static const UnknownFieldSet& default_instance();

Expand Down
75 changes: 75 additions & 0 deletions src/google/protobuf/unknown_field_set_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace google {
namespace protobuf {

using internal::WireFormat;
using ::testing::ElementsAre;

class UnknownFieldSetTest : public testing::Test {
protected:
Expand Down Expand Up @@ -678,6 +679,80 @@ TEST_F(UnknownFieldSetTest, DeleteByNumber) {
}
#undef MAKE_VECTOR

TEST_F(UnknownFieldSetTest, SerializeToString) {
UnknownFieldSet field_set;
field_set.AddVarint(3, 3);
field_set.AddVarint(4, 4);
field_set.AddVarint(1, -1);
field_set.AddVarint(2, -2);
field_set.AddLengthDelimited(44, "str");
field_set.AddLengthDelimited(44, "byv");
field_set.AddFixed32(7, 7);
field_set.AddFixed64(8, 8);

UnknownFieldSet* group_field_set = field_set.AddGroup(46);
group_field_set->AddVarint(47, 1024);
group_field_set = field_set.AddGroup(46);
group_field_set->AddVarint(47, 2048);

unittest::TestAllTypes message;
std::string serialized_message;
ASSERT_TRUE(field_set.SerializeToString(&serialized_message));
ASSERT_TRUE(message.ParseFromString(serialized_message));

EXPECT_EQ(message.optional_int32(), -1);
EXPECT_EQ(message.optional_int64(), -2);
EXPECT_EQ(message.optional_uint32(), 3);
EXPECT_EQ(message.optional_uint64(), 4);
EXPECT_EQ(message.optional_fixed32(), 7);
EXPECT_EQ(message.optional_fixed64(), 8);
EXPECT_EQ(message.repeated_string(0), "str");
EXPECT_EQ(message.repeated_string(1), "byv");
EXPECT_EQ(message.repeatedgroup(0).a(), 1024);
EXPECT_EQ(message.repeatedgroup(1).a(), 2048);
}

TEST_F(UnknownFieldSetTest, SerializeToCodedStream_TestPackedTypes) {
UnknownFieldSet field_set;
field_set.AddVarint(90, -1);
field_set.AddVarint(90, -2);
field_set.AddVarint(90, -3);
field_set.AddVarint(90, -4);
field_set.AddVarint(93, 5);
field_set.AddVarint(93, 6);
field_set.AddVarint(93, 7);

unittest::TestPackedTypes message;
std::string serialized_message;
{
io::StringOutputStream string_output(&serialized_message);
io::CodedOutputStream coded_output(&string_output);
ASSERT_TRUE(field_set.SerializeToCodedStream(&coded_output));
}
ASSERT_TRUE(message.ParseFromString(serialized_message));
EXPECT_THAT(message.packed_int32(), ElementsAre(-1, -2, -3, -4));
EXPECT_THAT(message.packed_uint64(), ElementsAre(5, 6, 7));
}

TEST_F(UnknownFieldSetTest, SerializeToCord_TestPackedTypes) {
UnknownFieldSet field_set;
field_set.AddVarint(90, -1);
field_set.AddVarint(90, -2);
field_set.AddVarint(90, -3);
field_set.AddVarint(90, -4);
field_set.AddVarint(93, 5);
field_set.AddVarint(93, 6);
field_set.AddVarint(93, 7);

absl::Cord cord;
ASSERT_TRUE(field_set.SerializeToCord(&cord));

unittest::TestPackedTypes message;
ASSERT_TRUE(message.ParseFromCord(cord));
EXPECT_THAT(message.packed_int32(), ElementsAre(-1, -2, -3, -4));
EXPECT_THAT(message.packed_uint64(), ElementsAre(5, 6, 7));
}

} // namespace

} // namespace protobuf
Expand Down

0 comments on commit 8661e45

Please sign in to comment.