25 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
41 void PrintProtoRpcDeclarationAsPragma(
Printer* printer,
43 map< ::std::string, ::std::string> vars) {
44 vars[
"client_stream"] =
method->client_streaming() ?
"stream " :
"";
45 vars[
"server_stream"] =
method->server_streaming() ?
"stream " :
"";
48 "#pragma mark $method_name$($client_stream$$request_type$)"
49 " returns ($server_stream$$response_type$)\n\n");
52 template <
typename DescriptorType>
53 static void PrintAllComments(
const DescriptorType*
desc,
Printer* printer,
54 bool deprecated =
false) {
55 std::vector<std::string> comments;
62 if (comments.empty()) {
65 printer->Print(
"/**\n");
66 for (
auto it = comments.begin();
it != comments.end(); ++
it) {
67 printer->Print(
" * ");
68 size_t start_pos =
it->find_first_not_of(
' ');
69 if (start_pos != std::string::npos) {
70 printer->PrintRaw(
it->c_str() + start_pos);
75 printer->Print(
" *\n");
77 " * This method belongs to a set of APIs that have been deprecated. "
79 " the v2 API is recommended.\n");
81 printer->Print(
" */\n");
85 const map< ::std::string, ::std::string>& vars) {
87 PrintAllComments(
method, printer,
true);
89 printer->Print(vars,
"- ($return_type$)$method_name$With");
90 if (
method->client_streaming()) {
91 printer->Print(
"RequestsWriter:(GRXWriter *)requestWriter");
93 printer->Print(vars,
"Request:($request_class$ *)request");
97 if (
method->server_streaming()) {
99 " eventHandler:(void(^)(BOOL done, "
100 "$response_class$ *_Nullable response, NSError *_Nullable "
101 "error))eventHandler");
104 " handler:(void(^)($response_class$ *_Nullable response, "
105 "NSError *_Nullable error))handler");
110 map< ::std::string, ::std::string> vars) {
111 vars[
"method_name"] =
113 vars[
"return_type"] =
"void";
114 PrintMethodSignature(printer,
method, vars);
118 map< ::std::string, ::std::string> vars) {
119 vars[
"method_name"] =
"RPCTo" + vars[
"method_name"];
120 vars[
"return_type"] =
"GRPCProtoCall *";
121 PrintMethodSignature(printer,
method, vars);
125 map< ::std::string, ::std::string> vars) {
126 if (
method->client_streaming()) {
127 vars[
"return_type"] =
"GRPCStreamingProtoCall *";
129 vars[
"return_type"] =
"GRPCUnaryProtoCall *";
131 vars[
"method_name"] =
134 PrintAllComments(
method, printer);
136 printer->Print(vars,
"- ($return_type$)$method_name$With");
137 if (
method->client_streaming()) {
138 printer->Print(
"ResponseHandler:(id<GRPCProtoResponseHandler>)handler");
141 "Message:($request_class$ *)message "
142 "responseHandler:(id<GRPCProtoResponseHandler>)handler");
144 printer->Print(
" callOptions:(GRPCCallOptions *_Nullable)callOptions");
147 inline map< ::std::string, ::std::string> GetMethodVars(
149 map< ::std::string, ::std::string> res;
150 res[
"method_name"] =
method->name();
151 res[
"request_type"] =
method->input_type()->name();
152 res[
"response_type"] =
method->output_type()->name();
159 map< ::std::string, ::std::string> vars = GetMethodVars(
method);
161 PrintProtoRpcDeclarationAsPragma(printer,
method, vars);
163 PrintSimpleSignature(printer,
method, vars);
164 printer->Print(
";\n\n");
165 PrintAdvancedSignature(printer,
method, vars);
166 printer->Print(
";\n\n\n");
169 void PrintV2MethodDeclarations(
Printer* printer,
171 map< ::std::string, ::std::string> vars = GetMethodVars(
method);
173 PrintProtoRpcDeclarationAsPragma(printer,
method, vars);
175 PrintV2Signature(printer,
method, vars);
176 printer->Print(
";\n\n");
180 map< ::std::string, ::std::string> vars) {
181 printer->Print(
"{\n");
182 printer->Print(vars,
" [[self RPCTo$method_name$With");
183 if (
method->client_streaming()) {
184 printer->Print(
"RequestsWriter:requestWriter");
186 printer->Print(
"Request:request");
188 if (
method->server_streaming()) {
189 printer->Print(
" eventHandler:eventHandler] start];\n");
191 printer->Print(
" handler:handler] start];\n");
193 printer->Print(
"}\n");
196 void PrintAdvancedImplementation(
Printer* printer,
198 map< ::std::string, ::std::string> vars) {
199 printer->Print(
"{\n");
200 printer->Print(vars,
" return [self RPCToMethod:@\"$method_name$\"\n");
202 printer->Print(
" requestsWriter:");
203 if (
method->client_streaming()) {
204 printer->Print(
"requestWriter\n");
206 printer->Print(
"[GRXWriter writerWithValue:request]\n");
209 printer->Print(vars,
" responseClass:[$response_class$ class]\n");
211 printer->Print(
" responsesWriteable:[GRXWriteable ");
212 if (
method->server_streaming()) {
213 printer->Print(
"writeableWithEventHandler:eventHandler]];\n");
215 printer->Print(
"writeableWithSingleHandler:handler]];\n");
218 printer->Print(
"}\n");
222 map< ::std::string, ::std::string> vars) {
223 printer->Print(
" {\n");
224 if (
method->client_streaming()) {
225 printer->Print(vars,
" return [self RPCToMethod:@\"$method_name$\"\n");
226 printer->Print(
" responseHandler:handler\n");
227 printer->Print(
" callOptions:callOptions\n");
229 vars,
" responseClass:[$response_class$ class]];\n}\n\n");
231 printer->Print(vars,
" return [self RPCToMethod:@\"$method_name$\"\n");
232 printer->Print(
" message:message\n");
233 printer->Print(
" responseHandler:handler\n");
234 printer->Print(
" callOptions:callOptions\n");
236 vars,
" responseClass:[$response_class$ class]];\n}\n\n");
240 void PrintMethodImplementations(
Printer* printer,
243 map< ::std::string, ::std::string> vars = GetMethodVars(
method);
245 PrintProtoRpcDeclarationAsPragma(printer,
method, vars);
249 PrintSimpleSignature(printer,
method, vars);
250 PrintSimpleImplementation(printer,
method, vars);
252 printer->Print(
"// Returns a not-yet-started RPC object.\n");
253 PrintAdvancedSignature(printer,
method, vars);
254 PrintAdvancedImplementation(printer,
method, vars);
257 PrintV2Signature(printer,
method, vars);
258 PrintV2Implementation(printer,
method, vars);
266 for (
int i = 0;
i <
file->service_count();
i++) {
268 for (
int i = 0;
i <
service->method_count();
i++) {
274 for (
auto one_class :
classes) {
275 output +=
"@class " + one_class +
";\n";
289 Printer printer(&output_stream,
'$');
291 map< ::std::string, ::std::string> vars = {
296 " * The methods in this protocol belong to a set of old APIs "
297 "that have been deprecated. They do not\n"
298 " * recognize call options provided in the initializer. Using "
299 "the v2 protocol is recommended.\n"
301 printer.Print(vars,
"@protocol $service_class$ <NSObject>\n\n");
302 for (
int i = 0;
i <
service->method_count();
i++) {
303 PrintMethodDeclarations(&printer,
service->method(
i));
305 printer.Print(
"@end\n\n");
315 Printer printer(&output_stream,
'$');
317 map< ::std::string, ::std::string> vars = {
320 printer.Print(vars,
"@protocol $service_class$ <NSObject>\n\n");
321 for (
int i = 0;
i <
service->method_count();
i++) {
322 PrintV2MethodDeclarations(&printer,
service->method(
i));
324 printer.Print(
"@end\n\n");
335 Printer printer(&output_stream,
'$');
337 map< ::std::string, ::std::string> vars = {
342 " * Basic service implementation, over gRPC, that only does\n"
343 " * marshalling and parsing.\n"
346 "@interface $service_class$ :"
347 " GRPCProtoService<$service_class$2");
349 printer.Print(vars,
", $service_class$");
351 printer.Print(
">\n");
353 "- (instancetype)initWithHost:(NSString *)host "
354 "callOptions:(GRPCCallOptions "
355 "*_Nullable)callOptions"
356 " NS_DESIGNATED_INITIALIZER;\n");
358 "+ (instancetype)serviceWithHost:(NSString *)host "
359 "callOptions:(GRPCCallOptions *_Nullable)callOptions;\n");
362 "// The following methods belong to a set of old APIs that have been "
364 printer.Print(
"- (instancetype)initWithHost:(NSString *)host;\n");
365 printer.Print(
"+ (instancetype)serviceWithHost:(NSString *)host;\n");
367 printer.Print(
"@end\n");
378 Printer printer(&output_stream,
'$');
380 map< ::std::string, ::std::string> vars = {
381 {
"service_name",
service->name()},
383 {
"package",
service->file()->package()}};
386 "@implementation $service_class$\n\n"
387 "#pragma clang diagnostic push\n"
388 "#pragma clang diagnostic ignored "
389 "\"-Wobjc-designated-initializers\"\n\n"
390 "// Designated initializer\n"
391 "- (instancetype)initWithHost:(NSString *)host "
392 "callOptions:(GRPCCallOptions *_Nullable)callOptions {\n"
393 " return [super initWithHost:host\n"
394 " packageName:@\"$package$\"\n"
395 " serviceName:@\"$service_name$\"\n"
396 " callOptions:callOptions];\n"
400 "- (instancetype)initWithHost:(NSString *)host {\n"
401 " return [super initWithHost:host\n"
402 " packageName:@\"$package$\"\n"
403 " serviceName:@\"$service_name$\"];\n"
406 printer.Print(
"#pragma clang diagnostic pop\n\n");
410 "// Override superclass initializer to disallow different"
411 " package and service names.\n"
412 "- (instancetype)initWithHost:(NSString *)host\n"
413 " packageName:(NSString *)packageName\n"
414 " serviceName:(NSString *)serviceName {\n"
415 " return [self initWithHost:host];\n"
419 "- (instancetype)initWithHost:(NSString *)host\n"
420 " packageName:(NSString *)packageName\n"
421 " serviceName:(NSString *)serviceName\n"
422 " callOptions:(GRPCCallOptions *)callOptions {\n"
423 " return [self initWithHost:host callOptions:callOptions];\n"
426 printer.Print(
"#pragma mark - Class Methods\n\n");
429 "+ (instancetype)serviceWithHost:(NSString *)host {\n"
430 " return [[self alloc] initWithHost:host];\n"
434 "+ (instancetype)serviceWithHost:(NSString *)host "
435 "callOptions:(GRPCCallOptions *_Nullable)callOptions {\n"
436 " return [[self alloc] initWithHost:host callOptions:callOptions];\n"
439 printer.Print(
"#pragma mark - Method Implementations\n\n");
441 for (
int i = 0;
i <
service->method_count();
i++) {
442 PrintMethodImplementations(&printer,
service->method(
i),
446 printer.Print(
"@end\n");