From 1900dfc70b04545fe7a0f6a7ad9441ab3ba1fdda Mon Sep 17 00:00:00 2001
From: Jorge Canizales <jcanizales@google.com>
Date: Fri, 15 May 2015 09:44:04 -0700
Subject: [PATCH] Generate imports for .proto dependencies, and read prefix
 from file.

The prefix has still to be applied per-message, and we could
do forward-declarations in the generated header.
---
 src/compiler/objective_c_generator.cc        |  9 ++-
 src/compiler/objective_c_generator_helpers.h | 12 +--
 src/compiler/objective_c_plugin.cc           | 78 +++++++++-----------
 3 files changed, 39 insertions(+), 60 deletions(-)

diff --git a/src/compiler/objective_c_generator.cc b/src/compiler/objective_c_generator.cc
index 6898b4dda3..8f35302bee 100644
--- a/src/compiler/objective_c_generator.cc
+++ b/src/compiler/objective_c_generator.cc
@@ -118,9 +118,9 @@ void PrintSimpleImplementation(Printer *printer,
                                const MethodDescriptor *method,
                                map<string, string> vars) {
   printer->Print("{\n");
-  printer->Print(vars, "[[self RPCTo$method_name$With");
+  printer->Print(vars, "  [[self RPCTo$method_name$With");
   if (method->client_streaming()) {
-    printer->Print("RequestsWriter:requestsWriter"); //TODO(jcanizales):request?
+    printer->Print("RequestsWriter:request");
   } else {
     printer->Print("Request:request");
   }
@@ -136,12 +136,13 @@ void PrintAdvancedImplementation(Printer *printer,
 
   printer->Print("            requestsWriter:");
   if (method->client_streaming()) {
-    printer->Print("requestsWriter\n");
+    printer->Print("request\n");
   } else {
     printer->Print("[GRXWriter writerWithValue:request]\n");
   }
 
-  printer->Print(vars, "             responseClass:[$response_type$ class]\n");
+  printer->Print(vars,
+      "             responseClass:[$prefix$$response_type$ class]\n");
 
   printer->Print("        responsesWriteable:[GRXWriteable ");
   if (method->server_streaming()) {
diff --git a/src/compiler/objective_c_generator_helpers.h b/src/compiler/objective_c_generator_helpers.h
index 6a7c13991f..d92a2b5e9a 100644
--- a/src/compiler/objective_c_generator_helpers.h
+++ b/src/compiler/objective_c_generator_helpers.h
@@ -40,18 +40,8 @@
 
 namespace grpc_objective_c_generator {
 
-const grpc::string prefix = "PBG";
-
 inline grpc::string MessageHeaderName(const grpc::protobuf::FileDescriptor *file) {
-  return grpc_generator::FileNameInUpperCamel(file) + ".pb.h";
-}
-
-inline grpc::string StubFileName(grpc::string service_name) {
-  return prefix + service_name + "Stub";
-}
-
-inline grpc::string PrefixedName(grpc::string name) {
-  return prefix + name;
+  return grpc_generator::FileNameInUpperCamel(file) + ".pbobjc.h";
 }
 
 }
diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc
index 1fe382cfdb..3cb170e95c 100644
--- a/src/compiler/objective_c_plugin.cc
+++ b/src/compiler/objective_c_plugin.cc
@@ -57,66 +57,54 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
     }
 
     string file_name = grpc_generator::FileNameInUpperCamel(file);
-    string prefix = "RMT"; // TODO
+    string prefix = file->options().objc_class_prefix();
 
-    for (int i = 0; i < file->service_count(); i++) {
-      const grpc::protobuf::ServiceDescriptor *service = file->service(i);
+    {
+      // Generate .pbrpc.h
 
-      {
-        // Generate .pbrpc.h
+      string imports = string("#import \"") + file_name + ".pbobjc.h\"\n"
+        "#import <gRPC/ProtoService.h>\n";
 
-        string imports = string("#import \"") + file_name + ".pbobjc.h\"\n"
-          "#import <gRPC/ProtoService.h>\n";
-        //Append(context, file_name + ".pbobjc.h",/* "imports",*/ imports);
-
-        string declarations =
-            grpc_objective_c_generator::GetHeader(service, prefix);
-        //Append(context, file_name + ".pbobjc.h",/* "global_scope",*/
-        //    declarations);
+      // TODO(jcanizales): Instead forward-declare the input and output types
+      // and import the files in the .pbrpc.m
+      string proto_imports;
+      for (int i = 0; i < file->dependency_count(); i++) {
+        string header = grpc_objective_c_generator::MessageHeaderName(
+            file->dependency(i));
+        proto_imports += string("#import \"") + header + "\"\n";
+      }
 
-        Write(context, file_name + ".pbrpc.h", imports + declarations);
+      string declarations;
+      for (int i = 0; i < file->service_count(); i++) {
+        const grpc::protobuf::ServiceDescriptor *service = file->service(i);
+        declarations += grpc_objective_c_generator::GetHeader(service, prefix);
       }
 
-      {
-        // Generate .pbrpc.m
+      Write(context, file_name + ".pbrpc.h",
+          imports + '\n' + proto_imports + '\n' + declarations);
+    }
+
+    {
+      // Generate .pbrpc.m
 
-        string imports = string("#import \"") + file_name + ".pbrpc.h\"\n"
-          "#import <gRPC/GRXWriteable.h>\n"
-          "#import <gRPC/GRXWriter+Immediate.h>\n"
-          "#import <gRPC/ProtoRPC.h>\n";
-        //Append(context, file_name + ".pbobjc.m",/* "imports",*/ imports);
+      string imports = string("#import \"") + file_name + ".pbrpc.h\"\n"
+        "#import <gRPC/GRXWriteable.h>\n"
+        "#import <gRPC/GRXWriter+Immediate.h>\n"
+        "#import <gRPC/ProtoRPC.h>\n";
 
-        string definitions =
-            grpc_objective_c_generator::GetSource(service, prefix);
-        //Append(context, file_name + ".pbobjc.m",/* "global_scope",*/
-        //    definitions);        
-        Write(context, file_name + ".pbrpc.m", imports + definitions);
+      string definitions;
+      for (int i = 0; i < file->service_count(); i++) {
+        const grpc::protobuf::ServiceDescriptor *service = file->service(i);
+        definitions += grpc_objective_c_generator::GetSource(service, prefix);
       }
+
+      Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions);
     }
 
     return true;
   }
 
  private:
-  // Insert the given code into the given file at the given insertion point.
-  void Insert(grpc::protobuf::compiler::GeneratorContext *context,
-              const string &filename, const string &insertion_point,
-              const string &code) const {
-    std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
-        context->OpenForInsert(filename, insertion_point));
-    grpc::protobuf::io::CodedOutputStream coded_out(output.get());
-    coded_out.WriteRaw(code.data(), code.size());
-  }
-
-  // Append the given code into the given file.
-  void Append(grpc::protobuf::compiler::GeneratorContext *context,
-              const string &filename, const string &code) const {
-    std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
-        context->OpenForAppend(filename));
-    grpc::protobuf::io::CodedOutputStream coded_out(output.get());
-    coded_out.WriteRaw(code.data(), code.size());
-  }
-
   // Write the given code into the given file.
   void Write(grpc::protobuf::compiler::GeneratorContext *context,
               const string &filename, const string &code) const {
-- 
GitLab