1ffe3c632Sopenharmony_ci#ifndef PROTOBUF_BENCHMARKS_UTIL_DATA_PROTO2_TO_PROTO3_UTIL_H_ 2ffe3c632Sopenharmony_ci#define PROTOBUF_BENCHMARKS_UTIL_DATA_PROTO2_TO_PROTO3_UTIL_H_ 3ffe3c632Sopenharmony_ci 4ffe3c632Sopenharmony_ci#include "google/protobuf/message.h" 5ffe3c632Sopenharmony_ci#include "google/protobuf/descriptor.h" 6ffe3c632Sopenharmony_ci 7ffe3c632Sopenharmony_ciusing google::protobuf::FieldDescriptor; 8ffe3c632Sopenharmony_ciusing google::protobuf::Message; 9ffe3c632Sopenharmony_ciusing google::protobuf::Reflection; 10ffe3c632Sopenharmony_ci 11ffe3c632Sopenharmony_cinamespace google { 12ffe3c632Sopenharmony_cinamespace protobuf { 13ffe3c632Sopenharmony_cinamespace util { 14ffe3c632Sopenharmony_ci 15ffe3c632Sopenharmony_ciclass DataStripper { 16ffe3c632Sopenharmony_ci public: 17ffe3c632Sopenharmony_ci void StripMessage(Message *message) { 18ffe3c632Sopenharmony_ci std::vector<const FieldDescriptor*> set_fields; 19ffe3c632Sopenharmony_ci const Reflection* reflection = message->GetReflection(); 20ffe3c632Sopenharmony_ci reflection->ListFields(*message, &set_fields); 21ffe3c632Sopenharmony_ci 22ffe3c632Sopenharmony_ci for (size_t i = 0; i < set_fields.size(); i++) { 23ffe3c632Sopenharmony_ci const FieldDescriptor* field = set_fields[i]; 24ffe3c632Sopenharmony_ci if (ShouldBeClear(field)) { 25ffe3c632Sopenharmony_ci reflection->ClearField(message, field); 26ffe3c632Sopenharmony_ci continue; 27ffe3c632Sopenharmony_ci } 28ffe3c632Sopenharmony_ci if (field->type() == FieldDescriptor::TYPE_MESSAGE) { 29ffe3c632Sopenharmony_ci if (field->is_repeated()) { 30ffe3c632Sopenharmony_ci for (int j = 0; j < reflection->FieldSize(*message, field); j++) { 31ffe3c632Sopenharmony_ci StripMessage(reflection->MutableRepeatedMessage(message, field, j)); 32ffe3c632Sopenharmony_ci } 33ffe3c632Sopenharmony_ci } else { 34ffe3c632Sopenharmony_ci StripMessage(reflection->MutableMessage(message, field)); 35ffe3c632Sopenharmony_ci } 36ffe3c632Sopenharmony_ci } 37ffe3c632Sopenharmony_ci } 38ffe3c632Sopenharmony_ci 39ffe3c632Sopenharmony_ci reflection->MutableUnknownFields(message)->Clear(); 40ffe3c632Sopenharmony_ci } 41ffe3c632Sopenharmony_ci private: 42ffe3c632Sopenharmony_ci virtual bool ShouldBeClear(const FieldDescriptor *field) = 0; 43ffe3c632Sopenharmony_ci}; 44ffe3c632Sopenharmony_ci 45ffe3c632Sopenharmony_ciclass GogoDataStripper : public DataStripper { 46ffe3c632Sopenharmony_ci private: 47ffe3c632Sopenharmony_ci virtual bool ShouldBeClear(const FieldDescriptor *field) { 48ffe3c632Sopenharmony_ci return field->type() == FieldDescriptor::TYPE_GROUP; 49ffe3c632Sopenharmony_ci } 50ffe3c632Sopenharmony_ci}; 51ffe3c632Sopenharmony_ci 52ffe3c632Sopenharmony_ciclass Proto3DataStripper : public DataStripper { 53ffe3c632Sopenharmony_ci private: 54ffe3c632Sopenharmony_ci virtual bool ShouldBeClear(const FieldDescriptor *field) { 55ffe3c632Sopenharmony_ci return field->type() == FieldDescriptor::TYPE_GROUP || 56ffe3c632Sopenharmony_ci field->is_extension(); 57ffe3c632Sopenharmony_ci } 58ffe3c632Sopenharmony_ci}; 59ffe3c632Sopenharmony_ci 60ffe3c632Sopenharmony_ci} // namespace util 61ffe3c632Sopenharmony_ci} // namespace protobuf 62ffe3c632Sopenharmony_ci} // namespace google 63ffe3c632Sopenharmony_ci 64ffe3c632Sopenharmony_ci#endif // PROTOBUF_BENCHMARKS_UTIL_DATA_PROTO2_TO_PROTO3_UTIL_H_ 65