aboutsummaryrefslogtreecommitdiff
path: root/src/google/protobuf/compiler/python
diff options
context:
space:
mode:
authorJisi Liu <jisi.liu@gmail.com>2017-07-18 15:38:30 -0700
committerJisi Liu <jisi.liu@gmail.com>2017-07-18 15:38:30 -0700
commit09354db1434859a31a3c81abebcc4018d42f2715 (patch)
treeb87c7cdc2255e6c8062ab92b4082665cd698d753 /src/google/protobuf/compiler/python
parent9053033a5076f82cf18b823c31f352e95e5bfd8d (diff)
downloadprotobuf-09354db1434859a31a3c81abebcc4018d42f2715.tar.gz
protobuf-09354db1434859a31a3c81abebcc4018d42f2715.tar.bz2
protobuf-09354db1434859a31a3c81abebcc4018d42f2715.zip
Merge from Google internal for 3.4 release
Diffstat (limited to 'src/google/protobuf/compiler/python')
-rw-r--r--src/google/protobuf/compiler/python/python_generator.cc44
-rw-r--r--src/google/protobuf/compiler/python/python_generator.h4
2 files changed, 42 insertions, 6 deletions
diff --git a/src/google/protobuf/compiler/python/python_generator.cc b/src/google/protobuf/compiler/python/python_generator.cc
index 21a7e158..97769835 100644
--- a/src/google/protobuf/compiler/python/python_generator.cc
+++ b/src/google/protobuf/compiler/python/python_generator.cc
@@ -63,11 +63,12 @@
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/stringprintf.h>
#include <google/protobuf/io/printer.h>
-#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/descriptor.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
+
namespace google {
namespace protobuf {
namespace compiler {
@@ -75,12 +76,21 @@ namespace python {
namespace {
+// Reimplemented here because we can't bring in
+// absl/strings/string_view_utils.h because it needs C++11.
+bool StrStartsWith(StringPiece sp, StringPiece x) {
+ return sp.size() >= x.size() && sp.substr(0, x.size()) == x;
+}
+bool StrEndsWith(StringPiece sp, StringPiece x) {
+ return sp.size() >= x.size() && sp.substr(sp.size() - x.size()) == x;
+}
+
// Returns a copy of |filename| with any trailing ".protodevel" or ".proto
// suffix stripped.
// TODO(robinson): Unify with copy in compiler/cpp/internal/helpers.cc.
string StripProto(const string& filename) {
- const char* suffix = HasSuffixString(filename, ".protodevel")
- ? ".protodevel" : ".proto";
+ const char* suffix =
+ StrEndsWith(filename, ".protodevel") ? ".protodevel" : ".proto";
return StripSuffixString(filename, suffix);
}
@@ -350,7 +360,9 @@ bool Generator::Generate(const FileDescriptor* file,
// can only be successfully parsed after we register corresponding
// extensions. Therefore we parse all options again here to recognize
// custom options that may be unknown when we define the descriptors.
+ // This does not apply to services because they are not used by extensions.
FixAllDescriptorOptions();
+ PrintServiceDescriptors();
if (HasGenericServices(file)) {
PrintServices();
}
@@ -562,9 +574,16 @@ void Generator::PrintMessageDescriptors() const {
}
}
-void Generator::PrintServices() const {
+void Generator::PrintServiceDescriptors() const {
for (int i = 0; i < file_->service_count(); ++i) {
PrintServiceDescriptor(*file_->service(i));
+ AddServiceToFileDescriptor(*file_->service(i));
+ printer_->Print("\n");
+ }
+}
+
+void Generator::PrintServices() const {
+ for (int i = 0; i < file_->service_count(); ++i) {
PrintServiceClass(*file_->service(i));
PrintServiceStub(*file_->service(i));
printer_->Print("\n");
@@ -628,7 +647,10 @@ void Generator::PrintServiceDescriptor(
}
printer_->Outdent();
- printer_->Print("])\n\n");
+ printer_->Print("])\n");
+ printer_->Print("_sym_db.RegisterServiceDescriptor($name$)\n", "name",
+ service_name);
+ printer_->Print("\n");
}
@@ -887,6 +909,18 @@ void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const {
printer_->Print(m, file_descriptor_template);
}
+void Generator::AddServiceToFileDescriptor(
+ const ServiceDescriptor& descriptor) const {
+ std::map<string, string> m;
+ m["descriptor_name"] = kDescriptorKey;
+ m["service_name"] = descriptor.name();
+ m["service_descriptor_name"] = ModuleLevelServiceDescriptorName(descriptor);
+ const char file_descriptor_template[] =
+ "$descriptor_name$.services_by_name['$service_name$'] = "
+ "$service_descriptor_name$\n";
+ printer_->Print(m, file_descriptor_template);
+}
+
void Generator::AddEnumToFileDescriptor(
const EnumDescriptor& descriptor) const {
std::map<string, string> m;
diff --git a/src/google/protobuf/compiler/python/python_generator.h b/src/google/protobuf/compiler/python/python_generator.h
index 594260af..2b5a028b 100644
--- a/src/google/protobuf/compiler/python/python_generator.h
+++ b/src/google/protobuf/compiler/python/python_generator.h
@@ -112,6 +112,7 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
void AddMessageToFileDescriptor(const Descriptor& descriptor) const;
void AddEnumToFileDescriptor(const EnumDescriptor& descriptor) const;
void AddExtensionToFileDescriptor(const FieldDescriptor& descriptor) const;
+ void AddServiceToFileDescriptor(const ServiceDescriptor& descriptor) const;
string FieldReferencingExpression(const Descriptor* containing_type,
const FieldDescriptor& field,
const string& python_dict_name) const;
@@ -126,11 +127,12 @@ class LIBPROTOC_EXPORT Generator : public CodeGenerator {
void FixForeignFieldsInNestedExtensions(const Descriptor& descriptor) const;
void PrintServices() const;
+ void PrintServiceDescriptors() const;
void PrintServiceDescriptor(const ServiceDescriptor& descriptor) const;
void PrintServiceClass(const ServiceDescriptor& descriptor) const;
void PrintServiceStub(const ServiceDescriptor& descriptor) const;
void PrintDescriptorKeyAndModuleName(
- const ServiceDescriptor& descriptor) const ;
+ const ServiceDescriptor& descriptor) const;
void PrintEnumValueDescriptor(const EnumValueDescriptor& descriptor) const;
string OptionsValue(const string& class_name,