blob: ff092053ad09386f695387a3cdd232b81e96da7d [file] [log] [blame]
murgatroid99d3efd0a2015-04-07 11:40:29 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <map>
Jorge Canizales52592fc2015-05-26 11:53:31 -070035#include <sstream>
murgatroid99d3efd0a2015-04-07 11:40:29 -070036
Jorge Canizales52592fc2015-05-26 11:53:31 -070037#include "src/compiler/config.h"
murgatroid99d3efd0a2015-04-07 11:40:29 -070038#include "src/compiler/objective_c_generator.h"
39#include "src/compiler/objective_c_generator_helpers.h"
40
Jorge Canizales52592fc2015-05-26 11:53:31 -070041#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
murgatroid99d3efd0a2015-04-07 11:40:29 -070042
Jorge Canizales52592fc2015-05-26 11:53:31 -070043using ::google::protobuf::compiler::objectivec::ClassName;
Jorge Canizales9a065d22015-04-27 00:05:01 -070044using ::grpc::protobuf::io::Printer;
45using ::grpc::protobuf::MethodDescriptor;
Jorge Canizales472f0b02015-05-12 22:56:04 -070046using ::grpc::protobuf::ServiceDescriptor;
Jorge Canizales52592fc2015-05-26 11:53:31 -070047using ::std::map;
murgatroid99d3efd0a2015-04-07 11:40:29 -070048
Jorge Canizales472f0b02015-05-12 22:56:04 -070049namespace grpc_objective_c_generator {
50namespace {
51
Jorge Canizales9a065d22015-04-27 00:05:01 -070052void PrintProtoRpcDeclarationAsPragma(Printer *printer,
53 const MethodDescriptor *method,
Jorge Canizales637c3652015-08-13 19:04:22 -070054 map< ::grpc::string, ::grpc::string> vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -070055 vars["client_stream"] = method->client_streaming() ? "stream " : "";
56 vars["server_stream"] = method->server_streaming() ? "stream " : "";
57
58 printer->Print(vars,
Vijay Pai181ef452015-07-14 13:52:48 -070059 "#pragma mark $method_name$($client_stream$$request_type$)"
60 " returns ($server_stream$$response_type$)\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -070061}
62
Vijay Pai181ef452015-07-14 13:52:48 -070063void PrintMethodSignature(Printer *printer, const MethodDescriptor *method,
Jorge Canizales637c3652015-08-13 19:04:22 -070064 const map< ::grpc::string, ::grpc::string> &vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -070065 // TODO(jcanizales): Print method comments.
66
67 printer->Print(vars, "- ($return_type$)$method_name$With");
68 if (method->client_streaming()) {
Jorge Canizales739c9982015-07-16 22:07:54 -070069 printer->Print("RequestsWriter:(GRXWriter *)requestWriter");
murgatroid99d3efd0a2015-04-07 11:40:29 -070070 } else {
Jorge Canizales52592fc2015-05-26 11:53:31 -070071 printer->Print(vars, "Request:($request_class$ *)request");
murgatroid99d3efd0a2015-04-07 11:40:29 -070072 }
Jorge Canizales9a065d22015-04-27 00:05:01 -070073
74 // TODO(jcanizales): Put this on a new line and align colons.
Jorge Canizales9a065d22015-04-27 00:05:01 -070075 if (method->server_streaming()) {
Vijay Pai181ef452015-07-14 13:52:48 -070076 printer->Print(vars,
77 " eventHandler:(void(^)(BOOL done, "
78 "$response_class$ *response, NSError *error))eventHandler");
murgatroid9925a26612015-06-25 11:22:23 -070079 } else {
Vijay Pai181ef452015-07-14 13:52:48 -070080 printer->Print(vars,
81 " handler:(void(^)($response_class$ *response, "
82 "NSError *error))handler");
Jorge Canizales9a065d22015-04-27 00:05:01 -070083 }
murgatroid99d3efd0a2015-04-07 11:40:29 -070084}
85
Vijay Pai181ef452015-07-14 13:52:48 -070086void PrintSimpleSignature(Printer *printer, const MethodDescriptor *method,
Jorge Canizales637c3652015-08-13 19:04:22 -070087 map< ::grpc::string, ::grpc::string> vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -070088 vars["method_name"] =
89 grpc_generator::LowercaseFirstLetter(vars["method_name"]);
90 vars["return_type"] = "void";
91 PrintMethodSignature(printer, method, vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -070092}
93
Vijay Pai181ef452015-07-14 13:52:48 -070094void PrintAdvancedSignature(Printer *printer, const MethodDescriptor *method,
Jorge Canizales637c3652015-08-13 19:04:22 -070095 map< ::grpc::string, ::grpc::string> vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -070096 vars["method_name"] = "RPCTo" + vars["method_name"];
97 vars["return_type"] = "ProtoRPC *";
98 PrintMethodSignature(printer, method, vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -070099}
100
Jorge Canizales637c3652015-08-13 19:04:22 -0700101inline map< ::grpc::string, ::grpc::string> GetMethodVars(const MethodDescriptor *method) {
102 map< ::grpc::string, ::grpc::string> res;
vjpaic7eed742015-07-14 10:47:28 -0700103 res["method_name"] = method->name();
104 res["request_type"] = method->input_type()->name();
105 res["response_type"] = method->output_type()->name();
106 res["request_class"] = ClassName(method->input_type());
107 res["response_class"] = ClassName(method->output_type());
Vijay Pai181ef452015-07-14 13:52:48 -0700108 return res;
Jorge Canizales52592fc2015-05-26 11:53:31 -0700109}
110
Vijay Pai181ef452015-07-14 13:52:48 -0700111void PrintMethodDeclarations(Printer *printer, const MethodDescriptor *method) {
Jorge Canizales637c3652015-08-13 19:04:22 -0700112 map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method);
murgatroid99ac0002a2015-04-07 12:49:14 -0700113
Jorge Canizales9a065d22015-04-27 00:05:01 -0700114 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
115
116 PrintSimpleSignature(printer, method, vars);
117 printer->Print(";\n\n");
118 PrintAdvancedSignature(printer, method, vars);
119 printer->Print(";\n\n\n");
120}
121
Vijay Pai181ef452015-07-14 13:52:48 -0700122void PrintSimpleImplementation(Printer *printer, const MethodDescriptor *method,
Jorge Canizales637c3652015-08-13 19:04:22 -0700123 map< ::grpc::string, ::grpc::string> vars) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700124 printer->Print("{\n");
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700125 printer->Print(vars, " [[self RPCTo$method_name$With");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700126 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700127 printer->Print("RequestsWriter:requestWriter");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700128 } else {
129 printer->Print("Request:request");
130 }
murgatroid9925a26612015-06-25 11:22:23 -0700131 if (method->server_streaming()) {
132 printer->Print(" eventHandler:eventHandler] start];\n");
133 } else {
134 printer->Print(" handler:handler] start];\n");
135 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700136 printer->Print("}\n");
137}
138
139void PrintAdvancedImplementation(Printer *printer,
140 const MethodDescriptor *method,
Jorge Canizales637c3652015-08-13 19:04:22 -0700141 map< ::grpc::string, ::grpc::string> vars) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700142 printer->Print("{\n");
143 printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n");
144
145 printer->Print(" requestsWriter:");
146 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700147 printer->Print("requestWriter\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700148 } else {
149 printer->Print("[GRXWriter writerWithValue:request]\n");
150 }
151
Jorge Canizales52592fc2015-05-26 11:53:31 -0700152 printer->Print(vars, " responseClass:[$response_class$ class]\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700153
154 printer->Print(" responsesWriteable:[GRXWriteable ");
155 if (method->server_streaming()) {
Jorge Canizalesf95ddba2015-08-12 10:51:56 -0700156 printer->Print("writeableWithEventHandler:eventHandler]];\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700157 } else {
Jorge Canizalesf95ddba2015-08-12 10:51:56 -0700158 printer->Print("writeableWithSingleHandler:handler]];\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700159 }
160
161 printer->Print("}\n");
162}
163
164void PrintMethodImplementations(Printer *printer,
Jorge Canizales52592fc2015-05-26 11:53:31 -0700165 const MethodDescriptor *method) {
Jorge Canizales637c3652015-08-13 19:04:22 -0700166 map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method);
murgatroid99ac0002a2015-04-07 12:49:14 -0700167
Jorge Canizales472f0b02015-05-12 22:56:04 -0700168 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
169
170 // TODO(jcanizales): Print documentation from the method.
171 PrintSimpleSignature(printer, method, vars);
172 PrintSimpleImplementation(printer, method, vars);
173
174 printer->Print("// Returns a not-yet-started RPC object.\n");
murgatroid99ac0002a2015-04-07 12:49:14 -0700175 PrintAdvancedSignature(printer, method, vars);
Jorge Canizales472f0b02015-05-12 22:56:04 -0700176 PrintAdvancedImplementation(printer, method, vars);
murgatroid99ac0002a2015-04-07 12:49:14 -0700177}
178
Vijay Pai181ef452015-07-14 13:52:48 -0700179} // namespace
murgatroid99ac0002a2015-04-07 12:49:14 -0700180
Jorge Canizalesb015dfb2015-08-13 18:41:29 -0700181::grpc::string GetHeader(const ServiceDescriptor *service) {
182 ::grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700183 {
184 // Scope the output stream so it closes and finalizes output to the string.
185 grpc::protobuf::io::StringOutputStream output_stream(&output);
186 Printer printer(&output_stream, '$');
Vijay Pai181ef452015-07-14 13:52:48 -0700187
Jorge Canizales637c3652015-08-13 19:04:22 -0700188 map< ::grpc::string, ::grpc::string> vars = {{"service_class", ServiceClassName(service)}};
Jorge Canizales52592fc2015-05-26 11:53:31 -0700189
190 printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700191
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700192 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales52592fc2015-05-26 11:53:31 -0700193 PrintMethodDeclarations(&printer, service->method(i));
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700194 }
195 printer.Print("@end\n\n");
196
Vijay Pai181ef452015-07-14 13:52:48 -0700197 printer.Print(
198 "// Basic service implementation, over gRPC, that only does"
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700199 " marshalling and parsing.\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700200 printer.Print(vars,
201 "@interface $service_class$ :"
202 " ProtoService<$service_class$>\n");
203 printer.Print(
204 "- (instancetype)initWithHost:(NSString *)host"
205 " NS_DESIGNATED_INITIALIZER;\n");
Nate Kibler6d197242015-09-25 10:02:20 -0700206 printer.Print("+ (instancetype)serviceWithHost:(NSString *)host;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700207 printer.Print("@end\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700208 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700209 return output;
210}
211
Jorge Canizalesb015dfb2015-08-13 18:41:29 -0700212::grpc::string GetSource(const ServiceDescriptor *service) {
213 ::grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700214 {
215 // Scope the output stream so it closes and finalizes output to the string.
216 grpc::protobuf::io::StringOutputStream output_stream(&output);
217 Printer printer(&output_stream, '$');
Jorge Canizales9a065d22015-04-27 00:05:01 -0700218
Jorge Canizales637c3652015-08-13 19:04:22 -0700219 map< ::grpc::string,::grpc::string> vars = {{"service_name", service->name()},
Jorge Canizales52592fc2015-05-26 11:53:31 -0700220 {"service_class", ServiceClassName(service)},
221 {"package", service->file()->package()}};
Jorge Canizales472f0b02015-05-12 22:56:04 -0700222
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700223 printer.Print(vars,
Vijay Pai181ef452015-07-14 13:52:48 -0700224 "static NSString *const kPackageName = @\"$package$\";\n");
225 printer.Print(
226 vars, "static NSString *const kServiceName = @\"$service_name$\";\n\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700227
Jorge Canizales52592fc2015-05-26 11:53:31 -0700228 printer.Print(vars, "@implementation $service_class$\n\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700229
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700230 printer.Print("// Designated initializer\n");
231 printer.Print("- (instancetype)initWithHost:(NSString *)host {\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700232 printer.Print(
233 " return (self = [super initWithHost:host"
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700234 " packageName:kPackageName serviceName:kServiceName]);\n");
235 printer.Print("}\n\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700236 printer.Print(
237 "// Override superclass initializer to disallow different"
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700238 " package and service names.\n");
239 printer.Print("- (instancetype)initWithHost:(NSString *)host\n");
240 printer.Print(" packageName:(NSString *)packageName\n");
241 printer.Print(" serviceName:(NSString *)serviceName {\n");
242 printer.Print(" return [self initWithHost:host];\n");
Nate Kibler6d197242015-09-25 10:02:20 -0700243 printer.Print("}\n\n");
Nate Kibler6d197242015-09-25 10:02:20 -0700244 printer.Print("+ (instancetype)serviceWithHost:(NSString *)host {\n");
245 printer.Print(" return [[self alloc] initWithHost:host];\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700246 printer.Print("}\n\n\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700247
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700248 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales52592fc2015-05-26 11:53:31 -0700249 PrintMethodImplementations(&printer, service->method(i));
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700250 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700251
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700252 printer.Print("@end\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700253 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700254 return output;
255}
256
Vijay Pai181ef452015-07-14 13:52:48 -0700257} // namespace grpc_objective_c_generator