Adjusts generated header to new runtime and surface.

pull/1621/head
Jorge Canizales 10 years ago
parent 55e04e4e05
commit 9a065d2e2f
  1. 8
      src/compiler/generator_helpers.h
  2. 199
      src/compiler/objective_c_generator.cc
  3. 5
      src/compiler/objective_c_generator.h
  4. 32
      src/compiler/objective_c_plugin.cc

@ -103,6 +103,14 @@ inline grpc::string CapitalizeFirstLetter(grpc::string s) {
return s; return s;
} }
inline grpc::string LowercaseFirstLetter(grpc::string s) {
if (s.empty()) {
return s;
}
s[0] = ::tolower(s[0]);
return s;
}
inline grpc::string LowerUnderscoreToUpperCamel(grpc::string str) { inline grpc::string LowerUnderscoreToUpperCamel(grpc::string str) {
std::vector<grpc::string> tokens = tokenize(str, "_"); std::vector<grpc::string> tokens = tokenize(str, "_");
grpc::string result = ""; grpc::string result = "";

@ -43,51 +43,87 @@
namespace grpc_objective_c_generator { namespace grpc_objective_c_generator {
namespace { namespace {
void PrintSimpleBlockSignature(grpc::protobuf::io::Printer *printer, using ::grpc::protobuf::io::Printer;
const grpc::protobuf::MethodDescriptor *method, using ::grpc::protobuf::MethodDescriptor;
std::map<grpc::string, grpc::string> *vars) { using ::std::map;
(*vars)["method_name"] = method->name(); using ::grpc::string;
(*vars)["request_type"] = PrefixedName(method->input_type()->name());
(*vars)["response_type"] = PrefixedName(method->output_type()->name()); void PrintProtoRpcDeclarationAsPragma(Printer *printer,
const MethodDescriptor *method,
map<string, string> vars) {
vars["client_stream"] = method->client_streaming() ? "stream " : "";
vars["server_stream"] = method->server_streaming() ? "stream " : "";
printer->Print(vars,
"#pragma mark $method_name$($client_stream$$request_type$)"
" returns ($server_stream$$response_type$)\n\n");
}
void PrintMethodSignature(Printer *printer,
const MethodDescriptor *method,
const map<string, string>& vars) {
// TODO(jcanizales): Print method comments.
printer->Print(vars, "- ($return_type$)$method_name$With");
if (method->client_streaming()) {
printer->Print("RequestsWriter:(id<GRXWriter>)request");
} else {
printer->Print(vars, "Request:($prefix$$request_type$ *)request");
}
// TODO(jcanizales): Put this on a new line and align colons.
printer->Print(" handler:(void(^)(");
if (method->server_streaming()) { if (method->server_streaming()) {
printer->Print("// When the response stream finishes, the handler is " printer->Print("BOOL done, ");
"called with nil for both arguments.\n\n");
} else {
printer->Print("// The handler is only called once.\n\n");
} }
printer->Print(*vars, "- (id<GRXLiveSource>)$method_name$WithRequest:" printer->Print(vars,
"($request_type$)request completionHandler:(void(^)" "$prefix$$response_type$ *response, NSError *error))handler");
"($response_type$ *, NSError *))handler");
} }
void PrintSimpleDelegateSignature(grpc::protobuf::io::Printer *printer, void PrintSimpleSignature(Printer *printer,
const grpc::protobuf::MethodDescriptor *method, const MethodDescriptor *method,
std::map<grpc::string, grpc::string> *vars) { map<string, string> vars) {
(*vars)["method_name"] = method->name(); vars["method_name"] =
(*vars)["request_type"] = PrefixedName(method->input_type()->name()); grpc_generator::LowercaseFirstLetter(vars["method_name"]);
vars["return_type"] = "void";
PrintMethodSignature(printer, method, vars);
}
printer->Print(*vars, "- (id<GRXLiveSource>)$method_name$WithRequest:" void PrintAdvancedSignature(Printer *printer,
"($request_type$)request delegate:(id<GRXSink>)delegate"); const MethodDescriptor *method,
map<string, string> vars) {
vars["method_name"] = "RPCTo" + vars["method_name"];
vars["return_type"] = "ProtoRPC *";
PrintMethodSignature(printer, method, vars);
} }
void PrintAdvancedSignature(grpc::protobuf::io::Printer *printer, void PrintMethodDeclarations(Printer *printer,
const grpc::protobuf::MethodDescriptor *method, const MethodDescriptor *method,
std::map<grpc::string, grpc::string> *vars) { map<string, string> vars) {
(*vars)["method_name"] = method->name(); vars["method_name"] = method->name();
printer->Print(*vars, "- (GRXSource *)$method_name$WithRequest:" vars["request_type"] = method->input_type()->name();
"(id<GRXSource>)request"); vars["response_type"] = method->output_type()->name();
PrintProtoRpcDeclarationAsPragma(printer, method, vars);
PrintSimpleSignature(printer, method, vars);
printer->Print(";\n\n");
PrintAdvancedSignature(printer, method, vars);
printer->Print(";\n\n\n");
} }
void PrintSourceMethodSimpleBlock(grpc::protobuf::io::Printer *printer, void PrintSourceMethodSimpleBlock(Printer *printer,
const grpc::protobuf::MethodDescriptor *method, const MethodDescriptor *method,
std::map<grpc::string, grpc::string> *vars) { map<string, string> vars) {
PrintSimpleBlockSignature(printer, method, vars); vars["method_name"] = method->name();
vars["request_type"] = method->input_type()->name();
vars["response_type"] = method->output_type()->name();
PrintSimpleSignature(printer, method, vars);
(*vars)["method_name"] = method->name();
printer->Print(" {\n"); printer->Print(" {\n");
printer->Indent(); printer->Indent();
printer->Print(*vars, "return [[self $method_name$WithRequest:request] " printer->Print(vars, "return [[self $method_name$WithRequest:request] "
"connectHandler:^(id value, NSError *error) {\n"); "connectHandler:^(id value, NSError *error) {\n");
printer->Indent(); printer->Indent();
printer->Print("handler(value, error);\n"); printer->Print("handler(value, error);\n");
@ -97,36 +133,25 @@ void PrintSourceMethodSimpleBlock(grpc::protobuf::io::Printer *printer,
printer->Print("}\n"); printer->Print("}\n");
} }
void PrintSourceMethodSimpleDelegate(grpc::protobuf::io::Printer *printer, void PrintSourceMethodAdvanced(Printer *printer,
const grpc::protobuf::MethodDescriptor *method, const MethodDescriptor *method,
std::map<grpc::string, grpc::string> *vars) { map<string, string> vars) {
PrintSimpleDelegateSignature(printer, method, vars); vars["method_name"] = method->name();
vars["request_type"] = method->input_type()->name();
vars["response_type"] = method->output_type()->name();
(*vars)["method_name"] = method->name();
printer->Print(" {\n");
printer->Indent();
printer->Print(*vars, "return [[self $method_name$WithRequest:request]"
"connectToSink:delegate];\n");
printer->Outdent();
printer->Print("}\n");
}
void PrintSourceMethodAdvanced(grpc::protobuf::io::Printer *printer,
const grpc::protobuf::MethodDescriptor *method,
std::map<grpc::string, grpc::string> *vars) {
PrintAdvancedSignature(printer, method, vars); PrintAdvancedSignature(printer, method, vars);
(*vars)["method_name"] = method->name();
printer->Print(" {\n"); printer->Print(" {\n");
printer->Indent(); printer->Indent();
printer->Print(*vars, "return [self $method_name$WithRequest:request " printer->Print(vars, "return [self $method_name$WithRequest:request "
"client:[self newClient]];\n"); "client:[self newClient]];\n");
printer->Outdent(); printer->Outdent();
printer->Print("}\n"); printer->Print("}\n");
} }
void PrintSourceMethodHandler(grpc::protobuf::io::Printer *printer, void PrintSourceMethodHandler(Printer *printer,
const grpc::protobuf::MethodDescriptor *method, const MethodDescriptor *method,
std::map<grpc::string, grpc::string> *vars) { std::map<grpc::string, grpc::string> *vars) {
(*vars)["method_name"] = method->name(); (*vars)["method_name"] = method->name();
(*vars)["response_type"] = PrefixedName(method->output_type()->name()); (*vars)["response_type"] = PrefixedName(method->output_type()->name());
@ -148,53 +173,42 @@ void PrintSourceMethodHandler(grpc::protobuf::io::Printer *printer,
} }
grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service, grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service,
const grpc::string message_header) { const string prefix) {
grpc::string output; grpc::string output;
grpc::protobuf::io::StringOutputStream output_stream(&output); grpc::protobuf::io::StringOutputStream output_stream(&output);
grpc::protobuf::io::Printer printer(&output_stream, '$'); Printer printer(&output_stream, '$');
std::map<grpc::string, grpc::string> vars;
printer.Print("#import \"PBgRPCClient.h\"\n"); printer.Print("@protocol GRXWriteable;\n");
printer.Print("#import \"PBStub.h\"\n"); printer.Print("@protocol GRXWriter;\n\n");
vars["message_header"] = message_header;
printer.Print(vars, "#import \"$message_header$\"\n\n"); map<string, string> vars = {{"service_name", service->name()},
printer.Print("@protocol GRXSource\n"); {"prefix", prefix}};
printer.Print("@class GRXSource\n\n"); printer.Print(vars, "@protocol $prefix$$service_name$ <NSObject>\n\n");
vars["service_name"] = service->name();
printer.Print("@protocol $service_name$Stub <NSObject>\n\n");
printer.Print("#pragma mark Simple block handlers\n\n");
for (int i = 0; i < service->method_count(); i++) {
PrintSimpleBlockSignature(&printer, service->method(i), &vars);
printer.Print(";\n");
}
printer.Print("\n");
printer.Print("#pragma mark Simple delegate handlers.\n\n");
printer.Print("# TODO(jcanizales): Use high-level snippets to remove this duplication.");
for (int i = 0; i < service->method_count(); i++) {
PrintSimpleDelegateSignature(&printer, service->method(i), &vars);
printer.Print(";\n");
}
printer.Print("\n");
printer.Print("#pragma mark Advanced handlers.\n\n");
for (int i = 0; i < service->method_count(); i++) { for (int i = 0; i < service->method_count(); i++) {
PrintAdvancedSignature(&printer, service->method(i), &vars); PrintMethodDeclarations(&printer, service->method(i), vars);
printer.Print(";\n");
} }
printer.Print("\n");
printer.Print("@end\n\n"); printer.Print("@end\n\n");
printer.Print("// Basic stub that only does marshalling and parsing\n");
printer.Print(vars, "@interface $service_name$Stub :" printer.Print("// Basic service implementation, over gRPC, that only does"
" PBStub<$service_name$Stub>\n"); " marshalling and parsing.\n");
printer.Print("- (instancetype)initWithHost:(NSString *)host;\n"); // use prefix
printer.Print(vars, "@interface RMT$service_name$ :"
" ProtoService<RMT$service_name$>\n");
printer.Print("- (instancetype)initWithHost:(NSString *)host"
" NS_DESIGNATED_INITIALIZER;\n");
printer.Print("@end\n"); printer.Print("@end\n");
return output; return output;
} }
grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service) { grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service,
const string prefix) {
grpc::string output; grpc::string output;
grpc::protobuf::io::StringOutputStream output_stream(&output); grpc::protobuf::io::StringOutputStream output_stream(&output);
grpc::protobuf::io::Printer printer(&output_stream, '$'); Printer printer(&output_stream, '$');
std::map<grpc::string, grpc::string> vars;
vars["service_name"] = service->name(); map<string, string> vars = {{"service_name", service->name()},
{"prefix", prefix}};
printer.Print(vars, "#import \"$service_name$Stub.pb.h\"\n"); printer.Print(vars, "#import \"$service_name$Stub.pb.h\"\n");
printer.Print("#import \"PBGeneratedMessage+GRXSource.h\"\n\n"); printer.Print("#import \"PBGeneratedMessage+GRXSource.h\"\n\n");
vars["full_name"] = service->full_name(); vars["full_name"] = service->full_name();
@ -211,17 +225,12 @@ grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service) {
printer.Print("}\n\n"); printer.Print("}\n\n");
printer.Print("#pragma mark Simple block handlers.\n"); printer.Print("#pragma mark Simple block handlers.\n");
for (int i = 0; i < service->method_count(); i++) { for (int i = 0; i < service->method_count(); i++) {
PrintSourceMethodSimpleBlock(&printer, service->method(i), &vars); PrintSourceMethodSimpleBlock(&printer, service->method(i), vars);
}
printer.Print("\n");
printer.Print("#pragma mark Simple delegate handlers.\n");
for (int i = 0; i < service->method_count(); i++) {
PrintSourceMethodSimpleDelegate(&printer, service->method(i), &vars);
} }
printer.Print("\n"); printer.Print("\n");
printer.Print("#pragma mark Advanced handlers.\n"); printer.Print("#pragma mark Advanced handlers.\n");
for (int i = 0; i < service->method_count(); i++) { for (int i = 0; i < service->method_count(); i++) {
PrintSourceMethodAdvanced(&printer, service->method(i), &vars); PrintSourceMethodAdvanced(&printer, service->method(i), vars);
} }
printer.Print("\n"); printer.Print("\n");
printer.Print("#pragma mark Handlers for subclasses " printer.Print("#pragma mark Handlers for subclasses "

@ -39,9 +39,10 @@
namespace grpc_objective_c_generator { namespace grpc_objective_c_generator {
grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service, grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service,
const grpc::string message_header); const grpc::string prefix);
grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service); grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service,
const grpc::string prefix);
} // namespace grpc_objective_c_generator } // namespace grpc_objective_c_generator

@ -54,27 +54,29 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
return true; return true;
} }
grpc::string file_name = grpc_generator::FileNameInUpperCamel(file);
grpc::string prefix = "RMT"; // TODO
for (int i = 0; i < file->service_count(); i++) { for (int i = 0; i < file->service_count(); i++) {
const grpc::protobuf::ServiceDescriptor *service = file->service(i); const grpc::protobuf::ServiceDescriptor *service = file->service(i);
grpc::string file_name = grpc_objective_c_generator::StubFileName(
service->name());
// Generate .pb.h // Generate .pb.h
grpc::string header_code = grpc_objective_c_generator::GetHeader(
service, grpc_objective_c_generator::MessageHeaderName(file)); Insert(context, file_name + ".pb.h", "imports",
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output( "#import <gRPC/ProtoService.h>\n");
context->Open(file_name + ".pb.h"));
grpc::protobuf::io::CodedOutputStream header_coded_out( Insert(context, file_name + ".pb.h", "global_scope",
header_output.get()); grpc_objective_c_generator::GetHeader(service, prefix));
header_coded_out.WriteRaw(header_code.data(), header_code.size());
// Generate .pb.m // Generate .pb.m
grpc::string source_code = grpc_objective_c_generator::GetSource(service);
std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output( Insert(context, file_name + ".pb.m", "imports",
context->Open(file_name + ".pb.m")); "#import <gRPC/GRXWriteable.h>\n"
grpc::protobuf::io::CodedOutputStream source_coded_out( "#import <gRPC/GRXWriter+Immediate.h>\n"
source_output.get()); "#import <gRPC/ProtoRPC.h>\n");
source_coded_out.WriteRaw(source_code.data(), source_code.size());
Insert(context, file_name + ".pb.m", "global_scope",
grpc_objective_c_generator::GetSource(service, prefix));
} }
return true; return true;

Loading…
Cancel
Save