From d74bf607685b9155de8900a14c82325cf72e0251 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Thu, 4 Apr 2024 15:51:03 -0700 Subject: [PATCH] Create a CopyHeadingTo method for message descriptors. This will allow users to export any metadata attached to this descriptor without also constructing every nested descriptor. This is consistent with FileDescriptor::CopyHeadingTo, which serves a similar purpose at a different scope. PiperOrigin-RevId: 621995153 --- src/google/protobuf/descriptor.cc | 7 ++++++- src/google/protobuf/descriptor.h | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 8d8cff8393..a63a1d46b9 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -2791,7 +2791,7 @@ void FileDescriptor::CopySourceCodeInfoTo(FileDescriptorProto* proto) const { } void Descriptor::CopyTo(DescriptorProto* proto) const { - proto->set_name(name()); + CopyHeadingTo(proto); for (int i = 0; i < field_count(); i++) { field(i)->CopyTo(proto->add_field()); @@ -2811,6 +2811,11 @@ void Descriptor::CopyTo(DescriptorProto* proto) const { for (int i = 0; i < extension_count(); i++) { extension(i)->CopyTo(proto->add_extension()); } +} + +void Descriptor::CopyHeadingTo(DescriptorProto* proto) const { + proto->set_name(name()); + for (int i = 0; i < reserved_range_count(); i++) { DescriptorProto::ReservedRange* range = proto->add_reserved_range(); range->set_start(reserved_range(i)->start); diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 13446c7d91..2cd35c2331 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -339,6 +339,12 @@ class PROTOBUF_EXPORT Descriptor : private internal::SymbolBase { // isn't, the result may be garbage. void CopyTo(DescriptorProto* proto) const; + // Fills in the message-level settings of this message (e.g. name, reserved + // fields, message options) to `proto`. This is essentially all of the + // metadata owned exclusively by this descriptor, and not any nested + // descriptors. + void CopyHeadingTo(DescriptorProto* proto) const; + // Write the contents of this descriptor in a human-readable form. Output // will be suitable for re-parsing. std::string DebugString() const;