protoc-gen-gogoproto.cc
Go to the documentation of this file.
7 
9 
10 using google::protobuf::FileDescriptorProto;
16 
17 namespace google {
18 namespace protobuf {
19 namespace compiler {
20 
21 namespace {
22 
23 string StripProto(string filename) {
24  if (filename.substr(filename.size() - 11) == ".protodevel") {
25  // .protodevel
26  return filename.substr(0, filename.size() - 11);
27  } else {
28  // .proto
29  return filename.substr(0, filename.size() - 6);
30  }
31 }
32 
33 DescriptorPool new_pool_;
34 
35 } // namespace
36 
38  public:
39  virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
40  const string& parameter,
41  GeneratorContext* context,
42  string* error) const {
43  for (int i = 0; i < files.size(); i++) {
44  for (auto file : files) {
45  bool can_generate =
46  (new_pool_.FindFileByName(file->name()) == nullptr);
47  for (int j = 0; j < file->dependency_count(); j++) {
48  can_generate &= (new_pool_.FindFileByName(
49  file->dependency(j)->name()) != nullptr);
50  }
51  for (int j = 0; j < file->public_dependency_count(); j++) {
52  can_generate &= (new_pool_.FindFileByName(
53  file->public_dependency(j)->name()) != nullptr);
54  }
55  for (int j = 0; j < file->weak_dependency_count(); j++) {
56  can_generate &= (new_pool_.FindFileByName(
57  file->weak_dependency(j)->name()) != nullptr);
58  }
59  if (can_generate) {
60  Generate(file, parameter, context, error);
61  break;
62  }
63  }
64  }
65 
66  return true;
67  }
68 
69  virtual bool Generate(const FileDescriptor* file,
70  const string& parameter,
71  GeneratorContext* context,
72  string* error) const {
73  FileDescriptorProto new_file;
74  file->CopyTo(&new_file);
75  SchemaGroupStripper::StripFile(file, &new_file);
76 
77  EnumScrubber enum_scrubber;
78  enum_scrubber.ScrubFile(&new_file);
79 
80  string filename = file->name();
81  string basename = StripProto(filename);
82 
83  std::vector<std::pair<string,string>> option_pairs;
84  ParseGeneratorParameter(parameter, &option_pairs);
85 
86  std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
87  context->Open(basename + ".proto"));
88  string content = new_pool_.BuildFile(new_file)->DebugString();
89  Printer printer(output.get(), '$');
90  printer.WriteRaw(content.c_str(), content.size());
91 
92  return true;
93  }
94 };
95 
96 } // namespace compiler
97 } // namespace protobuf
98 } // namespace google
99 
100 int main(int argc, char* argv[]) {
102  return google::protobuf::compiler::PluginMain(argc, argv, &generator);
103 }
zero_copy_stream.h
schema_proto2_to_proto3_util.h
google::protobuf::io::Printer::WriteRaw
void WriteRaw(const char *data, int size)
Definition: printer.cc:205
google::protobuf::compiler::CodeGenerator
Definition: code_generator.h:68
google::protobuf::compiler::cpp::StripProto
std::string StripProto(const std::string &filename)
Definition: cpp_helpers.cc:474
google::protobuf::compiler::GeneratorContext::Open
virtual io::ZeroCopyOutputStream * Open(const std::string &filename)=0
main
int main(int argc, char *argv[])
Definition: protoc-gen-gogoproto.cc:100
plugin.h
google::protobuf::util::EnumScrubber
Definition: schema_proto2_to_proto3_util.h:77
google::protobuf::util::SchemaGroupStripper
Definition: schema_proto2_to_proto3_util.h:22
google::protobuf::compiler::PluginMain
int PluginMain(int argc, char *argv[], const CodeGenerator *generator)
Definition: plugin.cc:147
error
Definition: cJSON.c:88
FileDescriptor
struct FileDescriptor FileDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:56
google::protobuf::util::EnumScrubber::ScrubFile
void ScrubFile(FileDescriptorProto *file)
Definition: schema_proto2_to_proto3_util.h:84
google::protobuf::DescriptorPool
Definition: src/google/protobuf/descriptor.h:1539
printer.h
code_generator.h
FileDescriptorProto
Definition: descriptor.pb.h:501
google::protobuf::FileDescriptor::CopyTo
void CopyTo(FileDescriptorProto *proto) const
Definition: src/google/protobuf/descriptor.cc:2010
google::protobuf::compiler::GoGoProtoGenerator::Generate
virtual bool Generate(const FileDescriptor *file, const string &parameter, GeneratorContext *context, string *error) const
Definition: protoc-gen-gogoproto.cc:69
google::protobuf::io::Printer
Definition: printer.h:181
i
int i
Definition: gmock-matchers_test.cc:764
google::protobuf::FileDescriptor::name
const std::string & name() const
descriptor.h
google::protobuf::compiler::ParseGeneratorParameter
void ParseGeneratorParameter(const std::string &text, std::vector< std::pair< std::string, std::string > > *output)
Definition: code_generator.cc:101
google::protobuf::compiler::GoGoProtoGenerator
Definition: protoc-gen-gogoproto.cc:37
google::protobuf::FileDescriptor
Definition: src/google/protobuf/descriptor.h:1320
google::protobuf::compiler::GoGoProtoGenerator::GenerateAll
virtual bool GenerateAll(const std::vector< const FileDescriptor * > &files, const string &parameter, GeneratorContext *context, string *error) const
Definition: protoc-gen-gogoproto.cc:39
descriptor.pb.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::compiler::GeneratorContext
Definition: code_generator.h:119
DescriptorPool
struct DescriptorPool DescriptorPool
Definition: php/ext/google/protobuf/protobuf.h:629
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


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