2018-05-31 00:06:45 +00:00
|
|
|
#include "google/protobuf/compiler/code_generator.h"
|
|
|
|
#include "google/protobuf/io/zero_copy_stream.h"
|
|
|
|
#include "google/protobuf/io/printer.h"
|
|
|
|
#include "google/protobuf/descriptor.h"
|
|
|
|
#include "google/protobuf/descriptor.pb.h"
|
|
|
|
#include "schema_proto2_to_proto3_util.h"
|
|
|
|
|
|
|
|
#include "google/protobuf/compiler/plugin.h"
|
|
|
|
|
|
|
|
using google::protobuf::FileDescriptorProto;
|
|
|
|
using google::protobuf::FileDescriptor;
|
|
|
|
using google::protobuf::DescriptorPool;
|
|
|
|
using google::protobuf::io::Printer;
|
|
|
|
using google::protobuf::util::SchemaGroupStripper;
|
|
|
|
using google::protobuf::util::EnumScrubber;
|
|
|
|
using google::protobuf::util::ExtensionStripper;
|
|
|
|
using google::protobuf::util::FieldScrubber;
|
|
|
|
|
|
|
|
namespace google {
|
|
|
|
namespace protobuf {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
string StripProto(string filename) {
|
2018-06-04 17:16:40 +00:00
|
|
|
return filename.substr(0, filename.rfind(".proto"));
|
2018-05-31 00:06:45 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 17:16:40 +00:00
|
|
|
DescriptorPool* GetPool() {
|
|
|
|
static DescriptorPool *pool = new DescriptorPool();
|
|
|
|
return pool;
|
|
|
|
}
|
2018-05-31 00:06:45 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-06-04 17:16:40 +00:00
|
|
|
class Proto2ToProto3Generator final : public CodeGenerator {
|
2018-05-31 00:06:45 +00:00
|
|
|
public:
|
2018-06-04 17:16:40 +00:00
|
|
|
bool GenerateAll(const std::vector<const FileDescriptor*>& files,
|
2018-05-31 00:06:45 +00:00
|
|
|
const string& parameter,
|
|
|
|
GeneratorContext* context,
|
|
|
|
string* error) const {
|
|
|
|
for (int i = 0; i < files.size(); i++) {
|
|
|
|
for (auto file : files) {
|
2018-06-04 17:16:40 +00:00
|
|
|
if (CanGenerate(file)) {
|
2018-05-31 00:06:45 +00:00
|
|
|
Generate(file, parameter, context, error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-04 17:16:40 +00:00
|
|
|
bool Generate(const FileDescriptor* file,
|
2018-05-31 00:06:45 +00:00
|
|
|
const string& parameter,
|
|
|
|
GeneratorContext* context,
|
|
|
|
string* error) const {
|
|
|
|
FileDescriptorProto new_file;
|
|
|
|
file->CopyTo(&new_file);
|
|
|
|
SchemaGroupStripper::StripFile(file, &new_file);
|
|
|
|
|
|
|
|
EnumScrubber enum_scrubber;
|
|
|
|
enum_scrubber.ScrubFile(&new_file);
|
|
|
|
ExtensionStripper::StripFile(&new_file);
|
|
|
|
FieldScrubber::ScrubFile(&new_file);
|
|
|
|
new_file.set_syntax("proto3");
|
|
|
|
|
|
|
|
string filename = file->name();
|
|
|
|
string basename = StripProto(filename);
|
|
|
|
|
|
|
|
std::vector<std::pair<string,string>> option_pairs;
|
|
|
|
ParseGeneratorParameter(parameter, &option_pairs);
|
|
|
|
|
|
|
|
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
|
|
|
|
context->Open(basename + ".proto"));
|
2018-06-04 17:16:40 +00:00
|
|
|
string content = GetPool()->BuildFile(new_file)->DebugString();
|
2018-05-31 00:06:45 +00:00
|
|
|
Printer printer(output.get(), '$');
|
|
|
|
printer.WriteRaw(content.c_str(), content.size());
|
|
|
|
|
2018-06-04 17:16:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool CanGenerate(const FileDescriptor* file) const {
|
|
|
|
if (GetPool()->FindFileByName(file->name()) != nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < file->dependency_count(); j++) {
|
|
|
|
if (GetPool()->FindFileByName(file->dependency(j)->name()) == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int j = 0; j < file->public_dependency_count(); j++) {
|
|
|
|
if (GetPool()->FindFileByName(
|
|
|
|
file->public_dependency(j)->name()) == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int j = 0; j < file->weak_dependency_count(); j++) {
|
|
|
|
if (GetPool()->FindFileByName(
|
|
|
|
file->weak_dependency(j)->name()) == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 00:06:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace protobuf
|
|
|
|
} // namespace google
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
google::protobuf::compiler::Proto2ToProto3Generator generator;
|
|
|
|
return google::protobuf::compiler::PluginMain(argc, argv, &generator);
|
|
|
|
}
|