murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 35 | #include <sstream> |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 36 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 37 | #include "src/compiler/config.h" |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 38 | #include "src/compiler/objective_c_generator.h" |
| 39 | #include "src/compiler/objective_c_generator_helpers.h" |
| 40 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 41 | #include <google/protobuf/compiler/objectivec/objectivec_helpers.h> |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 42 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 43 | using ::google::protobuf::compiler::objectivec::ClassName; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 44 | using ::grpc::protobuf::io::Printer; |
| 45 | using ::grpc::protobuf::MethodDescriptor; |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 46 | using ::grpc::protobuf::ServiceDescriptor; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 47 | using ::grpc::string; |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 48 | using ::std::map; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 49 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 50 | namespace grpc_objective_c_generator { |
| 51 | namespace { |
| 52 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 53 | void 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 | |
| 64 | void 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()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 71 | printer->Print("RequestsWriter:(id<GRXWriter>)requestWriter"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 72 | } else { |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 73 | printer->Print(vars, "Request:($request_class$ *)request"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 74 | } |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 75 | |
| 76 | // TODO(jcanizales): Put this on a new line and align colons. |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 77 | if (method->server_streaming()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 78 | 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 Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 83 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 86 | void 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); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 95 | void 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); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 103 | inline map<string, string> GetMethodVars(const MethodDescriptor *method) { |
vjpai | c7eed74 | 2015-07-14 10:47:28 -0700 | [diff] [blame^] | 104 | 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 Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 113 | void PrintMethodDeclarations(Printer *printer, |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 114 | const MethodDescriptor *method) { |
| 115 | map<string, string> vars = GetMethodVars(method); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 116 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 117 | 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 Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 125 | void PrintSimpleImplementation(Printer *printer, |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 126 | const MethodDescriptor *method, |
| 127 | map<string, string> vars) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 128 | printer->Print("{\n"); |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 129 | printer->Print(vars, " [[self RPCTo$method_name$With"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 130 | if (method->client_streaming()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 131 | printer->Print("RequestsWriter:requestWriter"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 132 | } else { |
| 133 | printer->Print("Request:request"); |
| 134 | } |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 135 | if (method->server_streaming()) { |
| 136 | printer->Print(" eventHandler:eventHandler] start];\n"); |
| 137 | } else { |
| 138 | printer->Print(" handler:handler] start];\n"); |
| 139 | } |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 140 | printer->Print("}\n"); |
| 141 | } |
| 142 | |
| 143 | void 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()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 151 | printer->Print("requestWriter\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 152 | } else { |
| 153 | printer->Print("[GRXWriter writerWithValue:request]\n"); |
| 154 | } |
| 155 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 156 | printer->Print(vars, " responseClass:[$response_class$ class]\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 157 | |
| 158 | printer->Print(" responsesWriteable:[GRXWriteable "); |
| 159 | if (method->server_streaming()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 160 | printer->Print("writeableWithStreamHandler:eventHandler]];\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 161 | } else { |
| 162 | printer->Print("writeableWithSingleValueHandler:handler]];\n"); |
| 163 | } |
| 164 | |
| 165 | printer->Print("}\n"); |
| 166 | } |
| 167 | |
| 168 | void PrintMethodImplementations(Printer *printer, |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 169 | const MethodDescriptor *method) { |
| 170 | map<string, string> vars = GetMethodVars(method); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 171 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 172 | 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"); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 179 | PrintAdvancedSignature(printer, method, vars); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 180 | PrintAdvancedImplementation(printer, method, vars); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 183 | } // namespace |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 184 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 185 | string GetHeader(const ServiceDescriptor *service) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 186 | string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 187 | { |
| 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 Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 191 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 192 | printer.Print("@protocol GRXWriteable;\n"); |
| 193 | printer.Print("@protocol GRXWriter;\n\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 194 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 195 | map<string, string> vars = {{"service_class", ServiceClassName(service)}}; |
| 196 | |
| 197 | printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 198 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 199 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 200 | PrintMethodDeclarations(&printer, service->method(i)); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 201 | } |
| 202 | printer.Print("@end\n\n"); |
| 203 | |
| 204 | printer.Print("// Basic service implementation, over gRPC, that only does" |
| 205 | " marshalling and parsing.\n"); |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 206 | printer.Print(vars, "@interface $service_class$ :" |
| 207 | " ProtoService<$service_class$>\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 208 | printer.Print("- (instancetype)initWithHost:(NSString *)host" |
| 209 | " NS_DESIGNATED_INITIALIZER;\n"); |
| 210 | printer.Print("@end\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 211 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 212 | return output; |
| 213 | } |
| 214 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 215 | string GetSource(const ServiceDescriptor *service) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 216 | string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 217 | { |
| 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 Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 221 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 222 | map<string, string> vars = {{"service_name", service->name()}, |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 223 | {"service_class", ServiceClassName(service)}, |
| 224 | {"package", service->file()->package()}}; |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 225 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 226 | printer.Print(vars, |
| 227 | "static NSString *const kPackageName = @\"$package$\";\n"); |
| 228 | printer.Print(vars, |
| 229 | "static NSString *const kServiceName = @\"$service_name$\";\n\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 230 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 231 | printer.Print(vars, "@implementation $service_class$\n\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 232 | |
| 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 Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 245 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 246 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 247 | PrintMethodImplementations(&printer, service->method(i)); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 248 | } |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 249 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 250 | printer.Print("@end\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 251 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 252 | return output; |
| 253 | } |
| 254 | |
| 255 | } // namespace grpc_objective_c_generator |