Commit 61a50b9e authored by Jon Skeet's avatar Jon Skeet

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.
parent 64aa954d
...@@ -117,43 +117,19 @@ std::string GetFileNamespace(const FileDescriptor* descriptor) { ...@@ -117,43 +117,19 @@ std::string GetFileNamespace(const FileDescriptor* descriptor) {
return UnderscoresToCamelCase(descriptor->package(), true, true); return UnderscoresToCamelCase(descriptor->package(), true, true);
} }
std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) { // Returns the Pascal-cased last part of the proto file. For example,
// We manually rename Descriptor to DescriptorProtoFile to avoid collisions with // input of "google/protobuf/foo_bar.proto" would result in "FooBar".
// the static Descriptor property. It would be nice to be able to do this with an std::string GetFileNameBase(const FileDescriptor* descriptor) {
// 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(); std::string proto_file = descriptor->name();
int lastslash = proto_file.find_last_of("/"); int lastslash = proto_file.find_last_of("/");
std::string base = proto_file.substr(lastslash + 1); std::string base = proto_file.substr(lastslash + 1);
return UnderscoresToPascalCase(StripDotProto(base)); return UnderscoresToPascalCase(StripDotProto(base));
} }
std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor) { std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
// TODO(jtattermusch): reintroduce csharp_umbrella_namespace option // umbrella_classname can no longer be set using message option.
bool collision = false; // TODO: Detect collisions with existing messages, and append an underscore if necessary.
std::string umbrella_classname = GetUmbrellaClassUnqualifiedName(descriptor); return GetFileNameBase(descriptor) + "Reflection";
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" : "";
} }
// TODO(jtattermusch): can we reuse a utility function? // TODO(jtattermusch): can we reuse a utility function?
...@@ -223,10 +199,6 @@ std::string GetUmbrellaClassName(const FileDescriptor* descriptor) { ...@@ -223,10 +199,6 @@ std::string GetUmbrellaClassName(const FileDescriptor* descriptor) {
if (!result.empty()) { if (!result.empty()) {
result += '.'; result += '.';
} }
std::string umbrellaNamespace = GetUmbrellaClassNestedNamespace(descriptor);
if (!umbrellaNamespace.empty()) {
result += umbrellaNamespace + ".";
}
result += GetUmbrellaClassUnqualifiedName(descriptor); result += GetUmbrellaClassUnqualifiedName(descriptor);
return "global::" + result; return "global::" + result;
} }
...@@ -275,7 +247,7 @@ std::string GetOutputFile( ...@@ -275,7 +247,7 @@ std::string GetOutputFile(
const bool generate_directories, const bool generate_directories,
const std::string base_namespace, const std::string base_namespace,
string* error) { string* error) {
string relative_filename = GetUmbrellaClassUnqualifiedName(descriptor) + file_extension; string relative_filename = GetFileNameBase(descriptor) + file_extension;
if (!generate_directories) { if (!generate_directories) {
return relative_filename; return relative_filename;
} }
......
...@@ -55,7 +55,6 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file) ...@@ -55,7 +55,6 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file)
file_(file) { file_(file) {
namespace_ = GetFileNamespace(file); namespace_ = GetFileNamespace(file);
umbrellaClassname_ = GetUmbrellaClassUnqualifiedName(file); umbrellaClassname_ = GetUmbrellaClassUnqualifiedName(file);
umbrellaNamespace_ = GetUmbrellaClassNestedNamespace(file);
} }
UmbrellaClassGenerator::~UmbrellaClassGenerator() { UmbrellaClassGenerator::~UmbrellaClassGenerator() {
...@@ -69,12 +68,6 @@ void UmbrellaClassGenerator::Generate(io::Printer* printer) { ...@@ -69,12 +68,6 @@ void UmbrellaClassGenerator::Generate(io::Printer* printer) {
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
// Close the namespace around the umbrella class if defined
if (!umbrellaNamespace_.empty()) {
printer->Outdent();
printer->Print("}\n");
}
// write children: Enums // write children: Enums
if (file_->enum_type_count() > 0) { if (file_->enum_type_count() > 0) {
printer->Print("#region Enums\n"); printer->Print("#region Enums\n");
...@@ -126,14 +119,6 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) { ...@@ -126,14 +119,6 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) {
printer->Print("\n"); 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( printer->Print(
"/// <summary>Holder for reflection information generated from $file_name$</summary>\n" "/// <summary>Holder for reflection information generated from $file_name$</summary>\n"
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n", "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n",
......
...@@ -53,7 +53,6 @@ class UmbrellaClassGenerator : public SourceGeneratorBase { ...@@ -53,7 +53,6 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
std::string namespace_; std::string namespace_;
std::string umbrellaClassname_; std::string umbrellaClassname_;
std::string umbrellaNamespace_;
void WriteIntroduction(io::Printer* printer); void WriteIntroduction(io::Printer* printer);
void WriteDescriptor(io::Printer* printer); void WriteDescriptor(io::Printer* printer);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment