blob: 2a74a3b34093ff67fecb368a37c06cde559ed794 [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,
Vijay Pai181ef452015-07-14 13:52:48 -070060 "#pragma mark $method_name$($client_stream$$request_type$)"
61 " returns ($server_stream$$response_type$)\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -070062}
63
Vijay Pai181ef452015-07-14 13:52:48 -070064void PrintMethodSignature(Printer *printer, const MethodDescriptor *method,
65 const map<string, string> &vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -070066 // TODO(jcanizales): Print method comments.
67
68 printer->Print(vars, "- ($return_type$)$method_name$With");
69 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -070070 printer->Print("RequestsWriter:(id<GRXWriter>)requestWriter");
murgatroid99d3efd0a2015-04-07 11:40:29 -070071 } else {
Jorge Canizales52592fc2015-05-26 11:53:31 -070072 printer->Print(vars, "Request:($request_class$ *)request");
murgatroid99d3efd0a2015-04-07 11:40:29 -070073 }
Jorge Canizales9a065d22015-04-27 00:05:01 -070074
75 // TODO(jcanizales): Put this on a new line and align colons.
Jorge Canizales9a065d22015-04-27 00:05:01 -070076 if (method->server_streaming()) {
Vijay Pai181ef452015-07-14 13:52:48 -070077 printer->Print(vars,
78 " eventHandler:(void(^)(BOOL done, "
79 "$response_class$ *response, NSError *error))eventHandler");
murgatroid9925a26612015-06-25 11:22:23 -070080 } else {
Vijay Pai181ef452015-07-14 13:52:48 -070081 printer->Print(vars,
82 " handler:(void(^)($response_class$ *response, "
83 "NSError *error))handler");
Jorge Canizales9a065d22015-04-27 00:05:01 -070084 }
murgatroid99d3efd0a2015-04-07 11:40:29 -070085}
86
Vijay Pai181ef452015-07-14 13:52:48 -070087void PrintSimpleSignature(Printer *printer, const MethodDescriptor *method,
Jorge Canizales9a065d22015-04-27 00:05:01 -070088 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
Vijay Pai181ef452015-07-14 13:52:48 -070095void PrintAdvancedSignature(Printer *printer, const MethodDescriptor *method,
Jorge Canizales9a065d22015-04-27 00:05:01 -070096 map<string, string> vars) {
97 vars["method_name"] = "RPCTo" + vars["method_name"];
98 vars["return_type"] = "ProtoRPC *";
99 PrintMethodSignature(printer, method, vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700100}
101
Vijay Pai181ef452015-07-14 13:52:48 -0700102inline map<string, string> GetMethodVars(const MethodDescriptor *method) {
103 map<string, string> res;
vjpaic7eed742015-07-14 10:47:28 -0700104 res["method_name"] = method->name();
105 res["request_type"] = method->input_type()->name();
106 res["response_type"] = method->output_type()->name();
107 res["request_class"] = ClassName(method->input_type());
108 res["response_class"] = ClassName(method->output_type());
Vijay Pai181ef452015-07-14 13:52:48 -0700109 return res;
Jorge Canizales52592fc2015-05-26 11:53:31 -0700110}
111
Vijay Pai181ef452015-07-14 13:52:48 -0700112void PrintMethodDeclarations(Printer *printer, const MethodDescriptor *method) {
Jorge Canizales52592fc2015-05-26 11:53:31 -0700113 map<string, string> vars = GetMethodVars(method);
murgatroid99ac0002a2015-04-07 12:49:14 -0700114
Jorge Canizales9a065d22015-04-27 00:05:01 -0700115 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
116
117 PrintSimpleSignature(printer, method, vars);
118 printer->Print(";\n\n");
119 PrintAdvancedSignature(printer, method, vars);
120 printer->Print(";\n\n\n");
121}
122
Vijay Pai181ef452015-07-14 13:52:48 -0700123void PrintSimpleImplementation(Printer *printer, const MethodDescriptor *method,
Jorge Canizales9a065d22015-04-27 00:05:01 -0700124 map<string, string> vars) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700125 printer->Print("{\n");
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700126 printer->Print(vars, " [[self RPCTo$method_name$With");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700127 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700128 printer->Print("RequestsWriter:requestWriter");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700129 } else {
130 printer->Print("Request:request");
131 }
murgatroid9925a26612015-06-25 11:22:23 -0700132 if (method->server_streaming()) {
133 printer->Print(" eventHandler:eventHandler] start];\n");
134 } else {
135 printer->Print(" handler:handler] start];\n");
136 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700137 printer->Print("}\n");
138}
139
140void PrintAdvancedImplementation(Printer *printer,
141 const MethodDescriptor *method,
142 map<string, string> vars) {
143 printer->Print("{\n");
144 printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n");
145
146 printer->Print(" requestsWriter:");
147 if (method->client_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700148 printer->Print("requestWriter\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700149 } else {
150 printer->Print("[GRXWriter writerWithValue:request]\n");
151 }
152
Jorge Canizales52592fc2015-05-26 11:53:31 -0700153 printer->Print(vars, " responseClass:[$response_class$ class]\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700154
155 printer->Print(" responsesWriteable:[GRXWriteable ");
156 if (method->server_streaming()) {
murgatroid9925a26612015-06-25 11:22:23 -0700157 printer->Print("writeableWithStreamHandler:eventHandler]];\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700158 } else {
159 printer->Print("writeableWithSingleValueHandler:handler]];\n");
160 }
161
162 printer->Print("}\n");
163}
164
165void PrintMethodImplementations(Printer *printer,
Jorge Canizales52592fc2015-05-26 11:53:31 -0700166 const MethodDescriptor *method) {
167 map<string, string> vars = GetMethodVars(method);
murgatroid99ac0002a2015-04-07 12:49:14 -0700168
Jorge Canizales472f0b02015-05-12 22:56:04 -0700169 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
170
171 // TODO(jcanizales): Print documentation from the method.
172 PrintSimpleSignature(printer, method, vars);
173 PrintSimpleImplementation(printer, method, vars);
174
175 printer->Print("// Returns a not-yet-started RPC object.\n");
murgatroid99ac0002a2015-04-07 12:49:14 -0700176 PrintAdvancedSignature(printer, method, vars);
Jorge Canizales472f0b02015-05-12 22:56:04 -0700177 PrintAdvancedImplementation(printer, method, vars);
murgatroid99ac0002a2015-04-07 12:49:14 -0700178}
179
Vijay Pai181ef452015-07-14 13:52:48 -0700180} // namespace
murgatroid99ac0002a2015-04-07 12:49:14 -0700181
Jorge Canizales52592fc2015-05-26 11:53:31 -0700182string GetHeader(const ServiceDescriptor *service) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700183 string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700184 {
185 // Scope the output stream so it closes and finalizes output to the string.
186 grpc::protobuf::io::StringOutputStream output_stream(&output);
187 Printer printer(&output_stream, '$');
Vijay Pai181ef452015-07-14 13:52:48 -0700188
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700189 printer.Print("@protocol GRXWriteable;\n");
190 printer.Print("@protocol GRXWriter;\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700191
Jorge Canizales52592fc2015-05-26 11:53:31 -0700192 map<string, string> vars = {{"service_class", ServiceClassName(service)}};
193
194 printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700195
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700196 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales52592fc2015-05-26 11:53:31 -0700197 PrintMethodDeclarations(&printer, service->method(i));
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700198 }
199 printer.Print("@end\n\n");
200
Vijay Pai181ef452015-07-14 13:52:48 -0700201 printer.Print(
202 "// Basic service implementation, over gRPC, that only does"
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700203 " marshalling and parsing.\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700204 printer.Print(vars,
205 "@interface $service_class$ :"
206 " ProtoService<$service_class$>\n");
207 printer.Print(
208 "- (instancetype)initWithHost:(NSString *)host"
209 " NS_DESIGNATED_INITIALIZER;\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700210 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,
Vijay Pai181ef452015-07-14 13:52:48 -0700227 "static NSString *const kPackageName = @\"$package$\";\n");
228 printer.Print(
229 vars, "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");
Vijay Pai181ef452015-07-14 13:52:48 -0700232
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700233 printer.Print("// Designated initializer\n");
234 printer.Print("- (instancetype)initWithHost:(NSString *)host {\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700235 printer.Print(
236 " return (self = [super initWithHost:host"
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700237 " packageName:kPackageName serviceName:kServiceName]);\n");
238 printer.Print("}\n\n");
Vijay Pai181ef452015-07-14 13:52:48 -0700239 printer.Print(
240 "// Override superclass initializer to disallow different"
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700241 " package and service names.\n");
242 printer.Print("- (instancetype)initWithHost:(NSString *)host\n");
243 printer.Print(" packageName:(NSString *)packageName\n");
244 printer.Print(" serviceName:(NSString *)serviceName {\n");
245 printer.Print(" return [self initWithHost:host];\n");
246 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