Commit 43a2dee7 authored by Jan Tattermusch's avatar Jan Tattermusch

refactor umbrella class helpers

parent 12febd0a
...@@ -50,7 +50,7 @@ namespace csharp { ...@@ -50,7 +50,7 @@ namespace csharp {
std::string GetOutputFile(const google::protobuf::FileDescriptor* file, const std::string file_extension) std::string GetOutputFile(const google::protobuf::FileDescriptor* file, const std::string file_extension)
{ {
return GetFileUmbrellaClassname(file) + file_extension; return GetUmbrellaClassUnqualifiedName(file) + file_extension;
} }
void GenerateFile(const google::protobuf::FileDescriptor* file, void GenerateFile(const google::protobuf::FileDescriptor* file,
......
...@@ -117,21 +117,18 @@ std::string GetFileNamespace(const FileDescriptor* descriptor) { ...@@ -117,21 +117,18 @@ std::string GetFileNamespace(const FileDescriptor* descriptor) {
return UnderscoresToCamelCase(descriptor->package(), true, true); return UnderscoresToCamelCase(descriptor->package(), true, true);
} }
std::string GetUmbrellaClassNameInternal(const std::string& proto_file) { std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
// umbrella_classname can no longer be set using message option.
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 GetFileUmbrellaClassname(const FileDescriptor* descriptor) { std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor) {
// umbrella_classname can no longer be set using message option.
return GetUmbrellaClassNameInternal(descriptor->name());
}
std::string GetFileUmbrellaNamespace(const FileDescriptor* descriptor) {
// TODO(jtattermusch): reintroduce csharp_umbrella_namespace option // TODO(jtattermusch): reintroduce csharp_umbrella_namespace option
bool collision = false; bool collision = false;
std::string umbrella_classname = GetFileUmbrellaClassname(descriptor); std::string umbrella_classname = GetUmbrellaClassUnqualifiedName(descriptor);
for(int i = 0; i < descriptor->message_type_count(); i++) { for(int i = 0; i < descriptor->message_type_count(); i++) {
if (descriptor->message_type(i)->name() == umbrella_classname) { if (descriptor->message_type(i)->name() == umbrella_classname) {
collision = true; collision = true;
...@@ -215,26 +212,17 @@ std::string ToCSharpName(const std::string& name, const FileDescriptor* file) { ...@@ -215,26 +212,17 @@ std::string ToCSharpName(const std::string& name, const FileDescriptor* file) {
return "global::" + result; return "global::" + result;
} }
std::string GetUmbrellaClassName(const FileDescriptor* descriptor) {
std::string GetFullUmbrellaClassName(const FileDescriptor* descriptor) {
std::string result = GetFileNamespace(descriptor); std::string result = GetFileNamespace(descriptor);
if (!result.empty()) { if (!result.empty()) {
result += '.'; result += '.';
} }
result += GetQualifiedUmbrellaClassName(descriptor); std::string umbrellaNamespace = GetUmbrellaClassNestedNamespace(descriptor);
return "global::" + result;
}
std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor) {
std::string umbrellaNamespace = GetFileUmbrellaNamespace(descriptor);
std::string umbrellaClassname = GetFileUmbrellaClassname(descriptor);
std::string fullName = umbrellaClassname;
if (!umbrellaNamespace.empty()) { if (!umbrellaNamespace.empty()) {
fullName = umbrellaNamespace + "." + umbrellaClassname; result += umbrellaNamespace + ".";
} }
return fullName; result += GetUmbrellaClassUnqualifiedName(descriptor);
return "global::" + result;
} }
std::string GetClassName(const Descriptor* descriptor) { std::string GetClassName(const Descriptor* descriptor) {
......
...@@ -69,13 +69,12 @@ CSharpType GetCSharpType(FieldDescriptor::Type type); ...@@ -69,13 +69,12 @@ CSharpType GetCSharpType(FieldDescriptor::Type type);
std::string StripDotProto(const std::string& proto_file); std::string StripDotProto(const std::string& proto_file);
std::string GetFileUmbrellaClassname(const FileDescriptor* descriptor); // Gets unqualified name of the umbrella class
std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor);
std::string GetFileUmbrellaNamespace(const FileDescriptor* descriptor); // Gets name of the nested for umbrella class (just the nested part,
// not including the GetFileNamespace part).
std::string GetFullUmbrellaClassName(const FileDescriptor* descriptor); std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor);
std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor);
std::string GetClassName(const Descriptor* descriptor); std::string GetClassName(const Descriptor* descriptor);
......
...@@ -100,7 +100,6 @@ void MessageGenerator::Generate(io::Printer* printer) { ...@@ -100,7 +100,6 @@ void MessageGenerator::Generate(io::Printer* printer) {
map<string, string> vars; map<string, string> vars;
vars["class_name"] = class_name(); vars["class_name"] = class_name();
vars["access_level"] = class_access_level(); vars["access_level"] = class_access_level();
vars["umbrella_class_name"] = GetFullUmbrellaClassName(descriptor_->file());
printer->Print( printer->Print(
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n"); "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n");
...@@ -118,7 +117,7 @@ void MessageGenerator::Generate(io::Printer* printer) { ...@@ -118,7 +117,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
// Access the message descriptor via the relevant file descriptor or containing message descriptor. // Access the message descriptor via the relevant file descriptor or containing message descriptor.
if (!descriptor_->containing_type()) { if (!descriptor_->containing_type()) {
vars["descriptor_accessor"] = GetFullUmbrellaClassName(descriptor_->file()) vars["descriptor_accessor"] = GetUmbrellaClassName(descriptor_->file())
+ ".Descriptor.MessageTypes[" + SimpleItoa(descriptor_->index()) + "]"; + ".Descriptor.MessageTypes[" + SimpleItoa(descriptor_->index()) + "]";
} else { } else {
vars["descriptor_accessor"] = GetClassName(descriptor_->containing_type()) vars["descriptor_accessor"] = GetClassName(descriptor_->containing_type())
......
...@@ -65,6 +65,14 @@ string GetFileNamespace(const FileDescriptor* descriptor); ...@@ -65,6 +65,14 @@ string GetFileNamespace(const FileDescriptor* descriptor);
// The fully-qualified C# class name. // The fully-qualified C# class name.
string GetClassName(const Descriptor* descriptor); string GetClassName(const Descriptor* descriptor);
// Requires:
// descriptor != NULL
//
// Returns:
// The fully-qualified name of the C# class that provides
// access to the file descriptor. Proto compiler generates
// such class for each .proto file processed.
std::string GetUmbrellaClassName(const FileDescriptor* descriptor);
} // namespace csharp } // namespace csharp
} // namespace compiler } // namespace compiler
......
...@@ -54,8 +54,8 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file) ...@@ -54,8 +54,8 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file)
: SourceGeneratorBase(file), : SourceGeneratorBase(file),
file_(file) { file_(file) {
namespace_ = GetFileNamespace(file); namespace_ = GetFileNamespace(file);
umbrellaClassname_ = GetFileUmbrellaClassname(file); umbrellaClassname_ = GetUmbrellaClassUnqualifiedName(file);
umbrellaNamespace_ = GetFileUmbrellaNamespace(file); umbrellaNamespace_ = GetUmbrellaClassNestedNamespace(file);
} }
UmbrellaClassGenerator::~UmbrellaClassGenerator() { UmbrellaClassGenerator::~UmbrellaClassGenerator() {
...@@ -183,7 +183,7 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) { ...@@ -183,7 +183,7 @@ void UmbrellaClassGenerator::WriteDescriptor(io::Printer* printer) {
printer->Print( printer->Print(
"$full_umbrella_class_name$.Descriptor, ", "$full_umbrella_class_name$.Descriptor, ",
"full_umbrella_class_name", "full_umbrella_class_name",
GetFullUmbrellaClassName(file_->dependency(i))); GetUmbrellaClassName(file_->dependency(i)));
} }
printer->Print("},\n" printer->Print("},\n"
" new pbr::GeneratedCodeInfo("); " new pbr::GeneratedCodeInfo(");
......
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