blob: 8f35302beee791718ae5601385b28c9997e6d1c1 [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>
35
36#include "src/compiler/objective_c_generator.h"
37#include "src/compiler/objective_c_generator_helpers.h"
38
39#include "src/compiler/config.h"
40
41#include <sstream>
42
Jorge Canizales9a065d22015-04-27 00:05:01 -070043using ::grpc::protobuf::io::Printer;
44using ::grpc::protobuf::MethodDescriptor;
Jorge Canizales472f0b02015-05-12 22:56:04 -070045using ::grpc::protobuf::ServiceDescriptor;
Jorge Canizales9a065d22015-04-27 00:05:01 -070046using ::std::map;
47using ::grpc::string;
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,
54 map<string, string> vars) {
55 vars["client_stream"] = method->client_streaming() ? "stream " : "";
56 vars["server_stream"] = method->server_streaming() ? "stream " : "";
57
58 printer->Print(vars,
59 "#pragma mark $method_name$($client_stream$$request_type$)"
60 " returns ($server_stream$$response_type$)\n\n");
61}
62
63void PrintMethodSignature(Printer *printer,
64 const MethodDescriptor *method,
65 const map<string, string>& vars) {
66 // TODO(jcanizales): Print method comments.
67
68 printer->Print(vars, "- ($return_type$)$method_name$With");
69 if (method->client_streaming()) {
70 printer->Print("RequestsWriter:(id<GRXWriter>)request");
murgatroid99d3efd0a2015-04-07 11:40:29 -070071 } else {
Jorge Canizales9a065d22015-04-27 00:05:01 -070072 printer->Print(vars, "Request:($prefix$$request_type$ *)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 Canizales472f0b02015-05-12 22:56:04 -070076 // TODO(jcanizales): eventHandler for server streaming?
Jorge Canizales9a065d22015-04-27 00:05:01 -070077 printer->Print(" handler:(void(^)(");
78 if (method->server_streaming()) {
79 printer->Print("BOOL done, ");
80 }
81 printer->Print(vars,
82 "$prefix$$response_type$ *response, NSError *error))handler");
murgatroid99d3efd0a2015-04-07 11:40:29 -070083}
84
Jorge Canizales9a065d22015-04-27 00:05:01 -070085void PrintSimpleSignature(Printer *printer,
86 const MethodDescriptor *method,
87 map<string, string> vars) {
88 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
Jorge Canizales9a065d22015-04-27 00:05:01 -070094void PrintAdvancedSignature(Printer *printer,
95 const MethodDescriptor *method,
96 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
Jorge Canizales9a065d22015-04-27 00:05:01 -0700102void PrintMethodDeclarations(Printer *printer,
103 const MethodDescriptor *method,
104 map<string, string> vars) {
105 vars["method_name"] = method->name();
106 vars["request_type"] = method->input_type()->name();
107 vars["response_type"] = method->output_type()->name();
murgatroid99ac0002a2015-04-07 12:49:14 -0700108
Jorge Canizales9a065d22015-04-27 00:05:01 -0700109 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
110
111 PrintSimpleSignature(printer, method, vars);
112 printer->Print(";\n\n");
113 PrintAdvancedSignature(printer, method, vars);
114 printer->Print(";\n\n\n");
115}
116
Jorge Canizales472f0b02015-05-12 22:56:04 -0700117void PrintSimpleImplementation(Printer *printer,
Jorge Canizales9a065d22015-04-27 00:05:01 -0700118 const MethodDescriptor *method,
119 map<string, string> vars) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700120 printer->Print("{\n");
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700121 printer->Print(vars, " [[self RPCTo$method_name$With");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700122 if (method->client_streaming()) {
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700123 printer->Print("RequestsWriter:request");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700124 } else {
125 printer->Print("Request:request");
126 }
127 printer->Print(" handler:handler] start];\n");
128 printer->Print("}\n");
129}
130
131void PrintAdvancedImplementation(Printer *printer,
132 const MethodDescriptor *method,
133 map<string, string> vars) {
134 printer->Print("{\n");
135 printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n");
136
137 printer->Print(" requestsWriter:");
138 if (method->client_streaming()) {
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700139 printer->Print("request\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700140 } else {
141 printer->Print("[GRXWriter writerWithValue:request]\n");
142 }
143
Jorge Canizales1900dfc2015-05-15 09:44:04 -0700144 printer->Print(vars,
145 " responseClass:[$prefix$$response_type$ class]\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700146
147 printer->Print(" responsesWriteable:[GRXWriteable ");
148 if (method->server_streaming()) {
149 printer->Print("writeableWithStreamHandler:handler]];\n");
150 } else {
151 printer->Print("writeableWithSingleValueHandler:handler]];\n");
152 }
153
154 printer->Print("}\n");
155}
156
157void PrintMethodImplementations(Printer *printer,
158 const MethodDescriptor *method,
159 map<string, string> vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -0700160 vars["method_name"] = method->name();
161 vars["request_type"] = method->input_type()->name();
162 vars["response_type"] = method->output_type()->name();
murgatroid99ac0002a2015-04-07 12:49:14 -0700163
Jorge Canizales472f0b02015-05-12 22:56:04 -0700164 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
165
166 // TODO(jcanizales): Print documentation from the method.
167 PrintSimpleSignature(printer, method, vars);
168 PrintSimpleImplementation(printer, method, vars);
169
170 printer->Print("// Returns a not-yet-started RPC object.\n");
murgatroid99ac0002a2015-04-07 12:49:14 -0700171 PrintAdvancedSignature(printer, method, vars);
Jorge Canizales472f0b02015-05-12 22:56:04 -0700172 PrintAdvancedImplementation(printer, method, vars);
murgatroid99ac0002a2015-04-07 12:49:14 -0700173}
174
Jorge Canizales472f0b02015-05-12 22:56:04 -0700175} // namespace
murgatroid99ac0002a2015-04-07 12:49:14 -0700176
Jorge Canizales472f0b02015-05-12 22:56:04 -0700177string GetHeader(const ServiceDescriptor *service, const string prefix) {
178 string output;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700179 grpc::protobuf::io::StringOutputStream output_stream(&output);
Jorge Canizales9a065d22015-04-27 00:05:01 -0700180 Printer printer(&output_stream, '$');
181
182 printer.Print("@protocol GRXWriteable;\n");
183 printer.Print("@protocol GRXWriter;\n\n");
184
185 map<string, string> vars = {{"service_name", service->name()},
186 {"prefix", prefix}};
187 printer.Print(vars, "@protocol $prefix$$service_name$ <NSObject>\n\n");
188
murgatroid99d3efd0a2015-04-07 11:40:29 -0700189 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales9a065d22015-04-27 00:05:01 -0700190 PrintMethodDeclarations(&printer, service->method(i), vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700191 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700192 printer.Print("@end\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700193
194 printer.Print("// Basic service implementation, over gRPC, that only does"
195 " marshalling and parsing.\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700196 printer.Print(vars, "@interface $prefix$$service_name$ :"
197 " ProtoService<$prefix$$service_name$>\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700198 printer.Print("- (instancetype)initWithHost:(NSString *)host"
199 " NS_DESIGNATED_INITIALIZER;\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700200 printer.Print("@end\n");
201 return output;
202}
203
Jorge Canizales472f0b02015-05-12 22:56:04 -0700204string GetSource(const ServiceDescriptor *service, const string prefix) {
205 string output;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700206 grpc::protobuf::io::StringOutputStream output_stream(&output);
Jorge Canizales9a065d22015-04-27 00:05:01 -0700207 Printer printer(&output_stream, '$');
208
209 map<string, string> vars = {{"service_name", service->name()},
Jorge Canizales472f0b02015-05-12 22:56:04 -0700210 {"package", service->file()->package()},
Jorge Canizales9a065d22015-04-27 00:05:01 -0700211 {"prefix", prefix}};
Jorge Canizales472f0b02015-05-12 22:56:04 -0700212
murgatroid99ac0002a2015-04-07 12:49:14 -0700213 printer.Print(vars,
Jorge Canizales472f0b02015-05-12 22:56:04 -0700214 "static NSString *const kPackageName = @\"$package$\";\n");
215 printer.Print(vars,
216 "static NSString *const kServiceName = @\"$service_name$\";\n\n");
217
218 printer.Print(vars, "@implementation $prefix$$service_name$\n\n");
219
220 printer.Print("// Designated initializer\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700221 printer.Print("- (instancetype)initWithHost:(NSString *)host {\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700222 printer.Print(" return (self = [super initWithHost:host"
223 " packageName:kPackageName serviceName:kServiceName]);\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700224 printer.Print("}\n\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700225 printer.Print("// Override superclass initializer to disallow different"
226 " package and service names.\n");
227 printer.Print("- (instancetype)initWithHost:(NSString *)host\n");
228 printer.Print(" packageName:(NSString *)packageName\n");
229 printer.Print(" serviceName:(NSString *)serviceName {\n");
230 printer.Print(" return [self initWithHost:host];\n");
231 printer.Print("}\n\n\n");
232
murgatroid99d3efd0a2015-04-07 11:40:29 -0700233 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700234 PrintMethodImplementations(&printer, service->method(i), vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700235 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700236
murgatroid99d3efd0a2015-04-07 11:40:29 -0700237 printer.Print("@end\n");
238 return output;
239}
240
241} // namespace grpc_objective_c_generator