Browse Source

Change how the reflection descriptor class is generated.

Instead of having a Proto nested namespace to avoid conflicts between the descriptor-holding static class and message classes, just append "Reflection" to the name.
Generated code changes (and corresponding manual changes) in following commit.
Jon Skeet 10 năm trước cách đây
mục cha
commit
61a50b9e6b

+ 12 - 40
src/google/protobuf/compiler/csharp/csharp_helpers.cc

@@ -117,43 +117,19 @@ std::string GetFileNamespace(const FileDescriptor* descriptor) {
   return UnderscoresToCamelCase(descriptor->package(), true, true);
 }
 
-std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
-  // We manually rename Descriptor to DescriptorProtoFile to avoid collisions with
-  // the static Descriptor property. It would be nice to be able to do this with an
-  // option, but it would be rarely used.
-  if (IsDescriptorProto(descriptor)) {
-    return "DescriptorProtoFile";
-  }
-  // umbrella_classname can no longer be set using message option.
-  std::string proto_file = descriptor->name();
-  int lastslash = proto_file.find_last_of("/");
-  std::string base = proto_file.substr(lastslash + 1);
-  return UnderscoresToPascalCase(StripDotProto(base));
+// Returns the Pascal-cased last part of the proto file. For example,
+// input of "google/protobuf/foo_bar.proto" would result in "FooBar".
+std::string GetFileNameBase(const FileDescriptor* descriptor) {
+    std::string proto_file = descriptor->name();
+    int lastslash = proto_file.find_last_of("/");
+    std::string base = proto_file.substr(lastslash + 1);
+    return UnderscoresToPascalCase(StripDotProto(base));
 }
 
-std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor) {
-  // TODO(jtattermusch): reintroduce csharp_umbrella_namespace option
-  bool collision = false;
-  std::string umbrella_classname = GetUmbrellaClassUnqualifiedName(descriptor);
-  for(int i = 0; i < descriptor->message_type_count(); i++) {
-    if (descriptor->message_type(i)->name() == umbrella_classname) {
-      collision = true;
-      break;
-    }
-  }
-  for (int i = 0; i < descriptor->service_count(); i++) {
-    if (descriptor->service(i)->name() == umbrella_classname) {
-      collision = true;
-      break;
-    }
-  }
-  for (int i = 0; i < descriptor->enum_type_count(); i++) {
-    if (descriptor->enum_type(i)->name() == umbrella_classname) {
-      collision = true;
-      break;
-    }
-  }
-  return collision ? "Proto" : "";
+std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
+  // umbrella_classname can no longer be set using message option.
+  // TODO: Detect collisions with existing messages, and append an underscore if necessary.
+  return GetFileNameBase(descriptor) + "Reflection";
 }
 
 // TODO(jtattermusch): can we reuse a utility function?
@@ -223,10 +199,6 @@ std::string GetUmbrellaClassName(const FileDescriptor* descriptor) {
   if (!result.empty()) {
     result += '.';
   }
-  std::string umbrellaNamespace = GetUmbrellaClassNestedNamespace(descriptor);
-  if (!umbrellaNamespace.empty()) {
-      result += umbrellaNamespace + ".";
-  }
   result += GetUmbrellaClassUnqualifiedName(descriptor);
   return "global::" + result;
 }
@@ -275,7 +247,7 @@ std::string GetOutputFile(
     const bool generate_directories,
     const std::string base_namespace,
     string* error) {
-  string relative_filename = GetUmbrellaClassUnqualifiedName(descriptor) + file_extension;
+  string relative_filename = GetFileNameBase(descriptor) + file_extension;
   if (!generate_directories) {
     return relative_filename;
   }

+ 0 - 15
src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc

@@ -55,7 +55,6 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file)
       file_(file) {
   namespace_ = GetFileNamespace(file);
   umbrellaClassname_ = GetUmbrellaClassUnqualifiedName(file);
-  umbrellaNamespace_ = GetUmbrellaClassNestedNamespace(file);
 }
 
 UmbrellaClassGenerator::~UmbrellaClassGenerator() {
@@ -69,12 +68,6 @@ void UmbrellaClassGenerator::Generate(io::Printer* printer) {
   printer->Outdent();
   printer->Print("}\n");
 
-  // Close the namespace around the umbrella class if defined
-  if (!umbrellaNamespace_.empty()) {
-    printer->Outdent();
-    printer->Print("}\n");
-  }
-
   // write children: Enums
   if (file_->enum_type_count() > 0) {
     printer->Print("#region Enums\n");
@@ -126,14 +119,6 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) {
     printer->Print("\n");
   }
 
-  // Add the namespace around the umbrella class if defined
-  if (!umbrellaNamespace_.empty()) {
-    printer->Print("namespace $umbrella_namespace$ {\n",
-                   "umbrella_namespace", umbrellaNamespace_);
-    printer->Indent();
-    printer->Print("\n");
-  }
-
   printer->Print(
     "/// <summary>Holder for reflection information generated from $file_name$</summary>\n"
     "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n",

+ 0 - 1
src/google/protobuf/compiler/csharp/csharp_umbrella_class.h

@@ -53,7 +53,6 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
 
   std::string namespace_;
   std::string umbrellaClassname_;
-  std::string umbrellaNamespace_;
 
   void WriteIntroduction(io::Printer* printer);
   void WriteDescriptor(io::Printer* printer);