3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 #include "src/compiler/config.h"
24 #include "src/compiler/objective_c_generator.h"
25 #include "src/compiler/objective_c_generator_helpers.h"
27 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
29 using ::google::protobuf::compiler::objectivec::ClassName;
30 using ::grpc::protobuf::FileDescriptor;
31 using ::grpc::protobuf::FileDescriptor;
32 using ::grpc::protobuf::MethodDescriptor;
33 using ::grpc::protobuf::ServiceDescriptor;
34 using ::grpc::protobuf::io::Printer;
38 namespace grpc_objective_c_generator {
41 void PrintProtoRpcDeclarationAsPragma(
42 Printer* printer, const MethodDescriptor* method,
43 map< ::grpc::string, ::grpc::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<grpc::string> comments;
56 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED,
58 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING,
60 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING,
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 != grpc::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");
84 void PrintMethodSignature(Printer* printer, const MethodDescriptor* method,
85 const map< ::grpc::string, ::grpc::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");
96 // TODO(jcanizales): Put this on a new line and align colons.
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");
109 void PrintSimpleSignature(Printer* printer, const MethodDescriptor* method,
110 map< ::grpc::string, ::grpc::string> vars) {
111 vars["method_name"] =
112 grpc_generator::LowercaseFirstLetter(vars["method_name"]);
113 vars["return_type"] = "void";
114 PrintMethodSignature(printer, method, vars);
117 void PrintAdvancedSignature(Printer* printer, const MethodDescriptor* method,
118 map< ::grpc::string, ::grpc::string> vars) {
119 vars["method_name"] = "RPCTo" + vars["method_name"];
120 vars["return_type"] = "GRPCProtoCall *";
121 PrintMethodSignature(printer, method, vars);
124 void PrintV2Signature(Printer* printer, const MethodDescriptor* method,
125 map< ::grpc::string, ::grpc::string> vars) {
126 if (method->client_streaming()) {
127 vars["return_type"] = "GRPCStreamingProtoCall *";
129 vars["return_type"] = "GRPCUnaryProtoCall *";
131 vars["method_name"] =
132 grpc_generator::LowercaseFirstLetter(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< ::grpc::string, ::grpc::string> GetMethodVars(
148 const MethodDescriptor* method) {
149 map< ::grpc::string, ::grpc::string> res;
150 res["method_name"] = method->name();
151 res["request_type"] = method->input_type()->name();
152 res["response_type"] = method->output_type()->name();
153 res["request_class"] = ClassName(method->input_type());
154 res["response_class"] = ClassName(method->output_type());
158 void PrintMethodDeclarations(Printer* printer, const MethodDescriptor* method) {
159 map< ::grpc::string, ::grpc::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,
170 const MethodDescriptor* method) {
171 map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method);
173 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
175 PrintV2Signature(printer, method, vars);
176 printer->Print(";\n\n");
179 void PrintSimpleImplementation(Printer* printer, const MethodDescriptor* method,
180 map< ::grpc::string, ::grpc::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,
197 const MethodDescriptor* method,
198 map< ::grpc::string, ::grpc::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");
221 void PrintV2Implementation(Printer* printer, const MethodDescriptor* method,
222 map< ::grpc::string, ::grpc::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,
241 const MethodDescriptor* method) {
242 map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method);
244 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
246 // TODO(jcanizales): Print documentation from the method.
247 printer->Print("// Deprecated methods.\n");
248 PrintSimpleSignature(printer, method, vars);
249 PrintSimpleImplementation(printer, method, vars);
251 printer->Print("// Returns a not-yet-started RPC object.\n");
252 PrintAdvancedSignature(printer, method, vars);
253 PrintAdvancedImplementation(printer, method, vars);
255 PrintV2Signature(printer, method, vars);
256 PrintV2Implementation(printer, method, vars);
261 ::grpc::string GetAllMessageClasses(const FileDescriptor* file) {
262 ::grpc::string output;
263 set< ::grpc::string> classes;
264 for (int i = 0; i < file->service_count(); i++) {
265 const auto service = file->service(i);
266 for (int i = 0; i < service->method_count(); i++) {
267 const auto method = service->method(i);
268 classes.insert(ClassName(method->input_type()));
269 classes.insert(ClassName(method->output_type()));
272 for (auto one_class : classes) {
273 output += "@class " + one_class + ";\n";
279 ::grpc::string GetProtocol(const ServiceDescriptor* service) {
280 ::grpc::string output;
282 // Scope the output stream so it closes and finalizes output to the string.
283 grpc::protobuf::io::StringOutputStream output_stream(&output);
284 Printer printer(&output_stream, '$');
286 map< ::grpc::string, ::grpc::string> vars = {
287 {"service_class", ServiceClassName(service)}};
291 " * The methods in this protocol belong to a set of old APIs "
292 "that have been deprecated. They do not\n"
293 " * recognize call options provided in the initializer. Using "
294 "the v2 protocol is recommended.\n"
296 printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n");
297 for (int i = 0; i < service->method_count(); i++) {
298 PrintMethodDeclarations(&printer, service->method(i));
300 printer.Print("@end\n\n");
305 ::grpc::string GetV2Protocol(const ServiceDescriptor* service) {
306 ::grpc::string output;
308 // Scope the output stream so it closes and finalizes output to the string.
309 grpc::protobuf::io::StringOutputStream output_stream(&output);
310 Printer printer(&output_stream, '$');
312 map< ::grpc::string, ::grpc::string> vars = {
313 {"service_class", ServiceClassName(service) + "2"}};
315 printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n");
316 for (int i = 0; i < service->method_count(); i++) {
317 PrintV2MethodDeclarations(&printer, service->method(i));
319 printer.Print("@end\n\n");
324 ::grpc::string GetInterface(const ServiceDescriptor* service) {
325 ::grpc::string output;
327 // Scope the output stream so it closes and finalizes output to the string.
328 grpc::protobuf::io::StringOutputStream output_stream(&output);
329 Printer printer(&output_stream, '$');
331 map< ::grpc::string, ::grpc::string> vars = {
332 {"service_class", ServiceClassName(service)}};
336 " * Basic service implementation, over gRPC, that only does\n"
337 " * marshalling and parsing.\n"
340 "@interface $service_class$ :"
341 " GRPCProtoService<$service_class$, $service_class$2>\n");
343 "- (instancetype)initWithHost:(NSString *)host "
344 "callOptions:(GRPCCallOptions "
345 "*_Nullable)callOptions"
346 " NS_DESIGNATED_INITIALIZER;\n");
348 "+ (instancetype)serviceWithHost:(NSString *)host "
349 "callOptions:(GRPCCallOptions *_Nullable)callOptions;\n");
351 "// The following methods belong to a set of old APIs that have been "
353 printer.Print("- (instancetype)initWithHost:(NSString *)host;\n");
354 printer.Print("+ (instancetype)serviceWithHost:(NSString *)host;\n");
355 printer.Print("@end\n");
360 ::grpc::string GetSource(const ServiceDescriptor* service) {
361 ::grpc::string output;
363 // Scope the output stream so it closes and finalizes output to the string.
364 grpc::protobuf::io::StringOutputStream output_stream(&output);
365 Printer printer(&output_stream, '$');
367 map< ::grpc::string, ::grpc::string> vars = {
368 {"service_name", service->name()},
369 {"service_class", ServiceClassName(service)},
370 {"package", service->file()->package()}};
373 "@implementation $service_class$\n\n"
374 "#pragma clang diagnostic push\n"
375 "#pragma clang diagnostic ignored "
376 "\"-Wobjc-designated-initializers\"\n\n"
377 "// Designated initializer\n"
378 "- (instancetype)initWithHost:(NSString *)host "
379 "callOptions:(GRPCCallOptions *_Nullable)callOptions {\n"
380 " return [super initWithHost:host\n"
381 " packageName:@\"$package$\"\n"
382 " serviceName:@\"$service_name$\"\n"
383 " callOptions:callOptions];\n"
385 "- (instancetype)initWithHost:(NSString *)host {\n"
386 " return [super initWithHost:host\n"
387 " packageName:@\"$package$\"\n"
388 " serviceName:@\"$service_name$\"];\n"
390 "#pragma clang diagnostic pop\n\n");
393 "// Override superclass initializer to disallow different"
394 " package and service names.\n"
395 "- (instancetype)initWithHost:(NSString *)host\n"
396 " packageName:(NSString *)packageName\n"
397 " serviceName:(NSString *)serviceName {\n"
398 " return [self initWithHost:host];\n"
400 "- (instancetype)initWithHost:(NSString *)host\n"
401 " packageName:(NSString *)packageName\n"
402 " serviceName:(NSString *)serviceName\n"
403 " callOptions:(GRPCCallOptions *)callOptions {\n"
404 " return [self initWithHost:host callOptions:callOptions];\n"
408 "#pragma mark - Class Methods\n\n"
409 "+ (instancetype)serviceWithHost:(NSString *)host {\n"
410 " return [[self alloc] initWithHost:host];\n"
412 "+ (instancetype)serviceWithHost:(NSString *)host "
413 "callOptions:(GRPCCallOptions *_Nullable)callOptions {\n"
414 " return [[self alloc] initWithHost:host callOptions:callOptions];\n"
417 printer.Print("#pragma mark - Method Implementations\n\n");
419 for (int i = 0; i < service->method_count(); i++) {
420 PrintMethodImplementations(&printer, service->method(i));
423 printer.Print("@end\n");
428 } // namespace grpc_objective_c_generator