blob: 6898b4dda3a05524c9b7fbd05c08949e41b7616a [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");
121 printer->Print(vars, "[[self RPCTo$method_name$With");
122 if (method->client_streaming()) {
123 printer->Print("RequestsWriter:requestsWriter"); //TODO(jcanizales):request?
124 } 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()) {
139 printer->Print("requestsWriter\n");
140 } else {
141 printer->Print("[GRXWriter writerWithValue:request]\n");
142 }
143
144 printer->Print(vars, " responseClass:[$response_type$ class]\n");
145
146 printer->Print(" responsesWriteable:[GRXWriteable ");
147 if (method->server_streaming()) {
148 printer->Print("writeableWithStreamHandler:handler]];\n");
149 } else {
150 printer->Print("writeableWithSingleValueHandler:handler]];\n");
151 }
152
153 printer->Print("}\n");
154}
155
156void PrintMethodImplementations(Printer *printer,
157 const MethodDescriptor *method,
158 map<string, string> vars) {
Jorge Canizales9a065d22015-04-27 00:05:01 -0700159 vars["method_name"] = method->name();
160 vars["request_type"] = method->input_type()->name();
161 vars["response_type"] = method->output_type()->name();
murgatroid99ac0002a2015-04-07 12:49:14 -0700162
Jorge Canizales472f0b02015-05-12 22:56:04 -0700163 PrintProtoRpcDeclarationAsPragma(printer, method, vars);
164
165 // TODO(jcanizales): Print documentation from the method.
166 PrintSimpleSignature(printer, method, vars);
167 PrintSimpleImplementation(printer, method, vars);
168
169 printer->Print("// Returns a not-yet-started RPC object.\n");
murgatroid99ac0002a2015-04-07 12:49:14 -0700170 PrintAdvancedSignature(printer, method, vars);
Jorge Canizales472f0b02015-05-12 22:56:04 -0700171 PrintAdvancedImplementation(printer, method, vars);
murgatroid99ac0002a2015-04-07 12:49:14 -0700172}
173
Jorge Canizales472f0b02015-05-12 22:56:04 -0700174} // namespace
murgatroid99ac0002a2015-04-07 12:49:14 -0700175
Jorge Canizales472f0b02015-05-12 22:56:04 -0700176string GetHeader(const ServiceDescriptor *service, const string prefix) {
177 string output;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700178 grpc::protobuf::io::StringOutputStream output_stream(&output);
Jorge Canizales9a065d22015-04-27 00:05:01 -0700179 Printer printer(&output_stream, '$');
180
181 printer.Print("@protocol GRXWriteable;\n");
182 printer.Print("@protocol GRXWriter;\n\n");
183
184 map<string, string> vars = {{"service_name", service->name()},
185 {"prefix", prefix}};
186 printer.Print(vars, "@protocol $prefix$$service_name$ <NSObject>\n\n");
187
murgatroid99d3efd0a2015-04-07 11:40:29 -0700188 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales9a065d22015-04-27 00:05:01 -0700189 PrintMethodDeclarations(&printer, service->method(i), vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700190 }
murgatroid99d3efd0a2015-04-07 11:40:29 -0700191 printer.Print("@end\n\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700192
193 printer.Print("// Basic service implementation, over gRPC, that only does"
194 " marshalling and parsing.\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700195 printer.Print(vars, "@interface $prefix$$service_name$ :"
196 " ProtoService<$prefix$$service_name$>\n");
Jorge Canizales9a065d22015-04-27 00:05:01 -0700197 printer.Print("- (instancetype)initWithHost:(NSString *)host"
198 " NS_DESIGNATED_INITIALIZER;\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700199 printer.Print("@end\n");
200 return output;
201}
202
Jorge Canizales472f0b02015-05-12 22:56:04 -0700203string GetSource(const ServiceDescriptor *service, const string prefix) {
204 string output;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700205 grpc::protobuf::io::StringOutputStream output_stream(&output);
Jorge Canizales9a065d22015-04-27 00:05:01 -0700206 Printer printer(&output_stream, '$');
207
208 map<string, string> vars = {{"service_name", service->name()},
Jorge Canizales472f0b02015-05-12 22:56:04 -0700209 {"package", service->file()->package()},
Jorge Canizales9a065d22015-04-27 00:05:01 -0700210 {"prefix", prefix}};
Jorge Canizales472f0b02015-05-12 22:56:04 -0700211
murgatroid99ac0002a2015-04-07 12:49:14 -0700212 printer.Print(vars,
Jorge Canizales472f0b02015-05-12 22:56:04 -0700213 "static NSString *const kPackageName = @\"$package$\";\n");
214 printer.Print(vars,
215 "static NSString *const kServiceName = @\"$service_name$\";\n\n");
216
217 printer.Print(vars, "@implementation $prefix$$service_name$\n\n");
218
219 printer.Print("// Designated initializer\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700220 printer.Print("- (instancetype)initWithHost:(NSString *)host {\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700221 printer.Print(" return (self = [super initWithHost:host"
222 " packageName:kPackageName serviceName:kServiceName]);\n");
murgatroid99d3efd0a2015-04-07 11:40:29 -0700223 printer.Print("}\n\n");
Jorge Canizales472f0b02015-05-12 22:56:04 -0700224 printer.Print("// Override superclass initializer to disallow different"
225 " package and service names.\n");
226 printer.Print("- (instancetype)initWithHost:(NSString *)host\n");
227 printer.Print(" packageName:(NSString *)packageName\n");
228 printer.Print(" serviceName:(NSString *)serviceName {\n");
229 printer.Print(" return [self initWithHost:host];\n");
230 printer.Print("}\n\n\n");
231
murgatroid99d3efd0a2015-04-07 11:40:29 -0700232 for (int i = 0; i < service->method_count(); i++) {
Jorge Canizales472f0b02015-05-12 22:56:04 -0700233 PrintMethodImplementations(&printer, service->method(i), vars);
murgatroid99d3efd0a2015-04-07 11:40:29 -0700234 }
Jorge Canizales472f0b02015-05-12 22:56:04 -0700235
murgatroid99d3efd0a2015-04-07 11:40:29 -0700236 printer.Print("@end\n");
237 return output;
238}
239
240} // namespace grpc_objective_c_generator