blob: 08b321152d3fdf6cc3c44988e0940045c59b620d [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 Canizales9a065d22015-04-27 00:05:01 -070047using ::grpc::string;
Jorge Canizales52592fc2015-05-26 11:53:31 -070048using ::std::map;
murgatroid99d3efd0a2015-04-07 11:40:29 -070049
Jorge Canizales472f0b02015-05-12 22:56:04 -070050namespace grpc_objective_c_generator {
51namespace {
52
Jorge Canizales9a065d22015-04-27 00:05:01 -070053void PrintProtoRpcDeclarationAsPragma(Printer *printer,
54 const MethodDescriptor *method,
55 map<string, string> vars) {
56 vars["client_stream"] = method->client_streaming() ? "stream " : "";
57 vars["server_stream"] = method->server_streaming() ? "stream " : "";
58
59 printer->Print(vars,
60 "#pragma mark $method_name$($client_stream$$request_type$)"
61 " returns ($server_stream$$response_type$)\n\n");
62}
63
64void PrintMethodSignature(Printer *printer,
65 const MethodDescriptor *method,
66 const map<string, string>& vars) {
67 // TODO(jcanizales): Print method comments.
68
69 printer->Print(vars, "- ($return_type$)$method_name$With");
70 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -070071 printer->Print("RequestsWriter:(id<GRXWriter>)requestWriter");
murgatroid99d3efd0a2015-04-07 11:40:29 -070072 } else {
Jorge Canizales52592fc2015-05-26 11:53:31 -070073 printer->Print(vars, "Request:($request_class$ *)request");
murgatroid99d3efd0a2015-04-07 11:40:29 -070074 }
Jorge Canizales9a065d22015-04-27 00:05:01 -070075
76 // TODO(jcanizales): Put this on a new line and align colons.
Jorge Canizales9a065d22015-04-27 00:05:01 -070077 if (method->server_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -070078 printer->Print(vars, " eventHandler:(void(^)(BOOL done, "
79 "$response_class$ *response, NSError *error))eventHandler");
80 } else {
81 printer->Print(vars, " 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
Jorge Canizales9a065d22015-04-27 00:05:01 -070086void PrintSimpleSignature(Printer *printer,
87 const MethodDescriptor *method,
88 map<string, string> vars) {
89 vars["method_name"] =
90 grpc_generator::LowercaseFirstLetter(vars["method_name"]);
91 vars["return_type"] = "void";
92 PrintMethodSignature(printer, method, vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -070093}
94
Jorge Canizales9a065d22015-04-27 00:05:01 -070095void PrintAdvancedSignature(Printer *printer,
96 const MethodDescriptor *method,
97 map<string, string> vars) {
98 vars["method_name"] = "RPCTo" + vars["method_name"];
99 vars["return_type"] = "ProtoRPC *";
100 PrintMethodSignature(printer, method, vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700101}
102
Jorge Canizales52592fc2015-05-26 11:53:31 -0700103inline map<string, string> GetMethodVars(const MethodDescriptor *method) {
vjpaic7eed742015-07-14 10:47:28 -0700104 map<string,string> res;
105 res["method_name"] = method->name();
106 res["request_type"] = method->input_type()->name();
107 res["response_type"] = method->output_type()->name();
108 res["request_class"] = ClassName(method->input_type());
109 res["response_class"] = ClassName(method->output_type());
110 return res;
Jorge Canizales52592fc2015-05-26 11:53:31 -0700111}
112
Jorge Canizales9a065d22015-04-27 00:05:01 -0700113void PrintMethodDeclarations(Printer *printer,
Jorge Canizales52592fc2015-05-26 11:53:31 -0700114 const MethodDescriptor *method) {
115 map<string, string> vars = GetMethodVars(method);
murgatroid99ac0002a2015-04-07 12:49:14 -0700116
Jorge Canizales9a065d22015-04-27 00:05:01 -0700117 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
118
119 PrintSimpleSignature(printer, method, vars);
120 printer->Print(";\n\n");
121 PrintAdvancedSignature(printer, method, vars);
122 printer->Print(";\n\n\n");
123}
124
Jorge Canizales472f0b02015-05-12 22:56:04 -0700125void PrintSimpleImplementation(Printer *printer,
Jorge Canizales9a065d22015-04-27 00:05:01 -0700126 const MethodDescriptor *method,
127 map<string, string> vars) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700128 printer->Print("{\n");
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700129 printer->Print(vars, " [[self RPCTo$method_name$With");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700130 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700131 printer->Print("RequestsWriter:requestWriter");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700132 } else {
133 printer->Print("Request:request");
134 }
murgatroid9925a26612015-06-25 11:22:23 -0700135 if (method->server_streaming()) {
136 printer->Print(" eventHandler:eventHandler] start];\n");
137 } else {
138 printer->Print(" handler:handler] start];\n");
139 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700140 printer->Print("}\n");
141}
142
143void PrintAdvancedImplementation(Printer *printer,
144 const MethodDescriptor *method,
145 map<string, string> vars) {
146 printer->Print("{\n");
147 printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n");
148
149 printer->Print(" requestsWriter:");
150 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700151 printer->Print("requestWriter\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700152 } else {
153 printer->Print("[GRXWriter writerWithValue:request]\n");
154 }
155
Jorge Canizales52592fc2015-05-26 11:53:31 -0700156 printer->Print(vars, " responseClass:[$response_class$ class]\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700157
158 printer->Print(" responsesWriteable:[GRXWriteable ");
159 if (method->server_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700160 printer->Print("writeableWithStreamHandler:eventHandler]];\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700161 } else {
162 printer->Print("writeableWithSingleValueHandler:handler]];\n");
163 }
164
165 printer->Print("}\n");
166}
167
168void PrintMethodImplementations(Printer *printer,
Jorge Canizales52592fc2015-05-26 11:53:31 -0700169 const MethodDescriptor *method) {
170 map<string, string> vars = GetMethodVars(method);
murgatroid99ac0002a2015-04-07 12:49:14 -0700171
Jorge Canizales472f0b02015-05-12 22:56:04 -0700172 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
173
174 // TODO(jcanizales): Print documentation from the method.
175 PrintSimpleSignature(printer, method, vars);
176 PrintSimpleImplementation(printer, method, vars);
177
178 printer->Print("// Returns a not-yet-started RPC object.\n");
murgatroid99ac0002a2015-04-07 12:49:14 -0700179 PrintAdvancedSignature(printer, method, vars);
Jorge Canizales472f0b02015-05-12 22:56:04 -0700180 PrintAdvancedImplementation(printer, method, vars);
murgatroid99ac0002a2015-04-07 12:49:14 -0700181}
182
Jorge Canizales472f0b02015-05-12 22:56:04 -0700183} // namespace
murgatroid99ac0002a2015-04-07 12:49:14 -0700184
Jorge Canizales52592fc2015-05-26 11:53:31 -0700185string GetHeader(const ServiceDescriptor *service) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700186 string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700187 {
188 // Scope the output stream so it closes and finalizes output to the string.
189 grpc::protobuf::io::StringOutputStream output_stream(&output);
190 Printer printer(&output_stream, '$');
Jorge Canizales9a065d22015-04-27 00:05:01 -0700191
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700192 printer.Print("@protocol GRXWriteable;\n");
193 printer.Print("@protocol GRXWriter;\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700194
Jorge Canizales52592fc2015-05-26 11:53:31 -0700195 map<string, string> vars = {{"service_class", ServiceClassName(service)}};
196
197 printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700198
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700199 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales52592fc2015-05-26 11:53:31 -0700200 PrintMethodDeclarations(&printer, service->method(i));
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700201 }
202 printer.Print("@end\n\n");
203
204 printer.Print("// Basic service implementation, over gRPC, that only does"
205 " marshalling and parsing.\n");
Jorge Canizales52592fc2015-05-26 11:53:31 -0700206 printer.Print(vars, "@interface $service_class$ :"
207 " ProtoService<$service_class$>\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700208 printer.Print("- (instancetype)initWithHost:(NSString *)host"
209 " NS_DESIGNATED_INITIALIZER;\n");
210 printer.Print("@end\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700211 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700212 return output;
213}
214
Jorge Canizales52592fc2015-05-26 11:53:31 -0700215string GetSource(const ServiceDescriptor *service) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700216 string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700217 {
218 // Scope the output stream so it closes and finalizes output to the string.
219 grpc::protobuf::io::StringOutputStream output_stream(&output);
220 Printer printer(&output_stream, '$');
Jorge Canizales9a065d22015-04-27 00:05:01 -0700221
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700222 map<string, string> vars = {{"service_name", service->name()},
Jorge Canizales52592fc2015-05-26 11:53:31 -0700223 {"service_class", ServiceClassName(service)},
224 {"package", service->file()->package()}};
Jorge Canizales472f0b02015-05-12 22:56:04 -0700225
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700226 printer.Print(vars,
227 "static NSString *const kPackageName = @\"$package$\";\n");
228 printer.Print(vars,
229 "static NSString *const kServiceName = @\"$service_name$\";\n\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700230
Jorge Canizales52592fc2015-05-26 11:53:31 -0700231 printer.Print(vars, "@implementation $service_class$\n\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700232
233 printer.Print("// Designated initializer\n");
234 printer.Print("- (instancetype)initWithHost:(NSString *)host {\n");
235 printer.Print(" return (self = [super initWithHost:host"
236 " packageName:kPackageName serviceName:kServiceName]);\n");
237 printer.Print("}\n\n");
238 printer.Print("// Override superclass initializer to disallow different"
239 " package and service names.\n");
240 printer.Print("- (instancetype)initWithHost:(NSString *)host\n");
241 printer.Print(" packageName:(NSString *)packageName\n");
242 printer.Print(" serviceName:(NSString *)serviceName {\n");
243 printer.Print(" return [self initWithHost:host];\n");
244 printer.Print("}\n\n\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700245
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700246 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales52592fc2015-05-26 11:53:31 -0700247 PrintMethodImplementations(&printer, service->method(i));
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700248 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700249
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700250 printer.Print("@end\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700251 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700252 return output;
253}
254
255} // namespace grpc_objective_c_generator