schema_proto2_to_proto3_util.h
Go to the documentation of this file.
1 #ifndef PROTOBUF_BENCHMARKS_UTIL_SCHEMA_PROTO2_TO_PROTO3_UTIL_H_
2 #define PROTOBUF_BENCHMARKS_UTIL_SCHEMA_PROTO2_TO_PROTO3_UTIL_H_
3 
7 
8 #include <sstream>
9 #include <algorithm>
10 
12 using google::protobuf::DescriptorProto;
13 using google::protobuf::FileDescriptorProto;
14 using google::protobuf::FieldDescriptorProto;
16 using google::protobuf::EnumValueDescriptorProto;
17 
18 namespace google {
19 namespace protobuf {
20 namespace util {
21 
23 
24  public:
25  static void StripFile(const FileDescriptor* old_file,
26  FileDescriptorProto *file) {
27  for (int i = file->mutable_message_type()->size() - 1; i >= 0; i--) {
28  if (IsMessageSet(old_file->message_type(i))) {
29  file->mutable_message_type()->DeleteSubrange(i, 1);
30  continue;
31  }
32  StripMessage(old_file->message_type(i), file->mutable_message_type(i));
33  }
34  for (int i = file->mutable_extension()->size() - 1; i >= 0; i--) {
35  auto field = old_file->extension(i);
36  if (field->type() == FieldDescriptor::TYPE_GROUP ||
37  IsMessageSet(field->message_type()) ||
38  IsMessageSet(field->containing_type())) {
39  file->mutable_extension()->DeleteSubrange(i, 1);
40  }
41  }
42  }
43 
44  private:
45  static bool IsMessageSet(const Descriptor *descriptor) {
46  if (descriptor != nullptr
47  && descriptor->options().message_set_wire_format()) {
48  return true;
49  }
50  return false;
51  }
52 
53  static void StripMessage(const Descriptor *old_message,
54  DescriptorProto *new_message) {
55  for (int i = new_message->mutable_field()->size() - 1; i >= 0; i--) {
56  if (old_message->field(i)->type() == FieldDescriptor::TYPE_GROUP ||
57  IsMessageSet(old_message->field(i)->message_type())) {
58  new_message->mutable_field()->DeleteSubrange(i, 1);
59  }
60  }
61  for (int i = new_message->mutable_extension()->size() - 1; i >= 0; i--) {
62  auto field_type_name = new_message->mutable_extension(i)->type_name();
63  if (old_message->extension(i)->type() == FieldDescriptor::TYPE_GROUP ||
64  IsMessageSet(old_message->extension(i)->containing_type()) ||
65  IsMessageSet(old_message->extension(i)->message_type())) {
66  new_message->mutable_extension()->DeleteSubrange(i, 1);
67  }
68  }
69  for (int i = 0; i < new_message->mutable_nested_type()->size(); i++) {
70  StripMessage(old_message->nested_type(i),
71  new_message->mutable_nested_type(i));
72  }
73  }
74 
75 };
76 
77 class EnumScrubber {
78 
79  public:
81  : total_added_(0) {
82  }
83 
85  for (int i = 0; i < file->enum_type_size(); i++) {
87  }
88  for (int i = 0; i < file->mutable_message_type()->size(); i++) {
90  }
91  }
92 
93  private:
95  if (enum_type->value(0).number() != 0) {
96  bool has_zero = false;
97  for (int j = 0; j < enum_type->value().size(); j++) {
98  if (enum_type->value(j).number() == 0) {
99  EnumValueDescriptorProto temp_enum_value;
100  temp_enum_value.CopyFrom(enum_type->value(j));
101  enum_type->mutable_value(j)->CopyFrom(enum_type->value(0));
102  enum_type->mutable_value(0)->CopyFrom(temp_enum_value);
103  has_zero = true;
104  break;
105  }
106  }
107  if (!has_zero) {
108  enum_type->mutable_value()->Add();
109  for (int i = enum_type->mutable_value()->size() - 1; i > 0; i--) {
110  enum_type->mutable_value(i)->CopyFrom(
111  *enum_type->mutable_value(i - 1));
112  }
113  enum_type->mutable_value(0)->set_number(0);
114  enum_type->mutable_value(0)->set_name("ADDED_ZERO_VALUE_" +
115  std::to_string(total_added_++));
116  }
117  }
118 
119  }
120 
122  for (int i = 0; i < message_type->mutable_enum_type()->size(); i++) {
123  ScrubEnum(message_type->mutable_enum_type(i));
124  }
125  for (int i = 0; i < message_type->mutable_nested_type()->size(); i++) {
126  ScrubMessage(message_type->mutable_nested_type(i));
127  }
128  }
129 
131 };
132 
134  public:
135  static void StripFile(FileDescriptorProto *file) {
136  for (int i = 0; i < file->mutable_message_type()->size(); i++) {
138  }
139  file->mutable_extension()->Clear();
140  }
141  private:
143  message_type->mutable_extension()->Clear();
144  message_type->clear_extension_range();
145  for (int i = 0; i < message_type->mutable_nested_type()->size(); i++) {
146  StripMessage(message_type->mutable_nested_type(i));
147  }
148  }
149 };
150 
151 
153  public:
154  static void ScrubFile(FileDescriptorProto *file) {
155  for (int i = 0; i < file->mutable_message_type()->size(); i++) {
157  }
158  for (int i = 0; i < file->mutable_extension()->size(); i++) {
159  file->mutable_extension(i)->clear_default_value();
160  if (ShouldClearLabel(file->mutable_extension(i))) {
161  file->mutable_extension(i)->clear_label();
162  }
163  }
164  }
165  private:
167  return field->label() == FieldDescriptorProto::LABEL_REQUIRED;
168  }
169 
171  message_type->mutable_extension()->Clear();
172  for (int i = 0; i < message_type->mutable_extension()->size(); i++) {
173  message_type->mutable_extension(i)->clear_default_value();
174  if (ShouldClearLabel(message_type->mutable_extension(i))) {
175  message_type->mutable_extension(i)->clear_label();
176  }
177  }
178  for (int i = 0; i < message_type->mutable_field()->size(); i++) {
179  message_type->mutable_field(i)->clear_default_value();
180  if (ShouldClearLabel(message_type->mutable_field(i))) {
181  message_type->mutable_field(i)->clear_label();
182  }
183  }
184  for (int i = 0; i < message_type->mutable_nested_type()->size(); i++) {
185  ScrubMessage(message_type->mutable_nested_type(i));
186  }
187  }
188 };
189 
190 } // namespace util
191 } // namespace protobuf
192 } // namespace google
193 
194 #endif // PROTOBUF_BENCHMARKS_UTIL_SCHEMA_PROTO2_TO_PROTO3_UTIL_H_
google::protobuf::util::EnumScrubber::ScrubMessage
void ScrubMessage(DescriptorProto *message_type)
Definition: schema_proto2_to_proto3_util.h:121
FieldDescriptorProto
Definition: descriptor.pb.h:1678
google::protobuf::FieldDescriptor::containing_type
const Descriptor * containing_type() const
google::protobuf::util::ExtensionStripper
Definition: schema_proto2_to_proto3_util.h:133
google::protobuf::util::SchemaGroupStripper::StripFile
static void StripFile(const FileDescriptor *old_file, FileDescriptorProto *file)
Definition: schema_proto2_to_proto3_util.h:25
google::protobuf::util::FieldScrubber::ScrubMessage
static void ScrubMessage(DescriptorProto *message_type)
Definition: schema_proto2_to_proto3_util.h:170
google::protobuf::util::ExtensionStripper::StripMessage
static void StripMessage(DescriptorProto *message_type)
Definition: schema_proto2_to_proto3_util.h:142
google::protobuf::util::EnumScrubber
Definition: schema_proto2_to_proto3_util.h:77
enum_type
zend_class_entry * enum_type
Definition: php/ext/google/protobuf/message.c:1904
descriptor
Descriptor * descriptor
Definition: php/ext/google/protobuf/protobuf.h:936
google::protobuf::util::FieldScrubber::ShouldClearLabel
static bool ShouldClearLabel(const FieldDescriptorProto *field)
Definition: schema_proto2_to_proto3_util.h:166
FileDescriptorProto::mutable_extension
PROTOBUF_NAMESPACE_ID::FieldDescriptorProto * mutable_extension(int index)
Definition: descriptor.pb.h:6950
DescriptorProto::mutable_nested_type
PROTOBUF_NAMESPACE_ID::DescriptorProto * mutable_nested_type(int index)
Definition: descriptor.pb.h:7464
google::protobuf::util::SchemaGroupStripper
Definition: schema_proto2_to_proto3_util.h:22
EnumValueDescriptorProto
Definition: descriptor.pb.h:2687
DescriptorProto::mutable_extension
PROTOBUF_NAMESPACE_ID::FieldDescriptorProto * mutable_extension(int index)
Definition: descriptor.pb.h:7434
message_type
zend_class_entry * message_type
Definition: php/ext/google/protobuf/message.c:45
google::protobuf::Descriptor::field
const FieldDescriptor * field(int index) const
google::protobuf::util::ExtensionStripper::StripFile
static void StripFile(FileDescriptorProto *file)
Definition: schema_proto2_to_proto3_util.h:135
google::protobuf::util::EnumScrubber::ScrubFile
void ScrubFile(FileDescriptorProto *file)
Definition: schema_proto2_to_proto3_util.h:84
EnumValueDescriptorProto::CopyFrom
void CopyFrom(const ::PROTOBUF_NAMESPACE_ID::Message &from) final
Definition: descriptor.pb.cc:7129
FileDescriptorProto::mutable_enum_type
PROTOBUF_NAMESPACE_ID::EnumDescriptorProto * mutable_enum_type(int index)
Definition: descriptor.pb.h:6890
message.h
size
#define size
Definition: glcorearb.h:2944
google::protobuf::Descriptor::extension
const FieldDescriptor * extension(int index) const
FileDescriptorProto
Definition: descriptor.pb.h:501
google::protobuf::util::FieldScrubber
Definition: schema_proto2_to_proto3_util.h:152
Descriptor
struct Descriptor Descriptor
Definition: php/ext/google/protobuf/protobuf.h:628
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
FileDescriptorProto::mutable_message_type
PROTOBUF_NAMESPACE_ID::DescriptorProto * mutable_message_type(int index)
Definition: descriptor.pb.h:6860
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::Descriptor::nested_type
const Descriptor * nested_type(int index) const
google::protobuf::util::EnumScrubber::EnumScrubber
EnumScrubber()
Definition: schema_proto2_to_proto3_util.h:80
google::protobuf::Message
Definition: src/google/protobuf/message.h:205
google::protobuf::FieldDescriptor::type
Type type() const
Definition: src/google/protobuf/descriptor.h:2052
google::protobuf::FileDescriptor::message_type
const Descriptor * message_type(int index) const
EnumDescriptorProto
Definition: descriptor.pb.h:2449
google::protobuf::FileDescriptor::extension
const FieldDescriptor * extension(int index) const
google::protobuf::util::FieldScrubber::ScrubFile
static void ScrubFile(FileDescriptorProto *file)
Definition: schema_proto2_to_proto3_util.h:154
DescriptorProto
Definition: descriptor.pb.h:1203
google::protobuf::Descriptor
Definition: src/google/protobuf/descriptor.h:231
google::protobuf::util::SchemaGroupStripper::StripMessage
static void StripMessage(const Descriptor *old_message, DescriptorProto *new_message)
Definition: schema_proto2_to_proto3_util.h:53
descriptor.h
google::protobuf::FieldDescriptor::TYPE_GROUP
@ TYPE_GROUP
Definition: src/google/protobuf/descriptor.h:535
google::protobuf::util::EnumScrubber::total_added_
int total_added_
Definition: schema_proto2_to_proto3_util.h:130
FieldDescriptorProto::LABEL_REQUIRED
static constexpr Label LABEL_REQUIRED
Definition: descriptor.pb.h:1880
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type() const
Definition: src/google/protobuf/descriptor.cc:7228
google::protobuf::util::EnumScrubber::ScrubEnum
void ScrubEnum(EnumDescriptorProto *enum_type)
Definition: schema_proto2_to_proto3_util.h:94
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
descriptor.pb.h
DescriptorProto::mutable_field
PROTOBUF_NAMESPACE_ID::FieldDescriptorProto * mutable_field(int index)
Definition: descriptor.pb.h:7404
FileDescriptorProto::enum_type_size
int enum_type_size() const
Definition: descriptor.pb.h:6884
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::util::SchemaGroupStripper::IsMessageSet
static bool IsMessageSet(const Descriptor *descriptor)
Definition: schema_proto2_to_proto3_util.h:45


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58