Add UnknownFieldSet::SerializeToCord()

PiperOrigin-RevId: 495164471
pull/10966/head^2
Martijn Vels 2 years ago committed by Copybara-Service
parent c658e27529
commit 8661e45075
  1. 13
      src/google/protobuf/unknown_field_set.cc
  2. 1
      src/google/protobuf/unknown_field_set.h
  3. 75
      src/google/protobuf/unknown_field_set_unittest.cc

@ -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:

@ -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();

@ -64,6 +64,7 @@ namespace google {
namespace protobuf {
using internal::WireFormat;
using ::testing::ElementsAre;
class UnknownFieldSetTest : public testing::Test {
protected:
@ -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

Loading…
Cancel
Save