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> |
| 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 Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 43 | using ::grpc::protobuf::io::Printer; |
| 44 | using ::grpc::protobuf::MethodDescriptor; |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 45 | using ::grpc::protobuf::ServiceDescriptor; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 46 | using ::std::map; |
| 47 | using ::grpc::string; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 48 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 49 | namespace grpc_objective_c_generator { |
| 50 | namespace { |
| 51 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 52 | void 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 | |
| 63 | void 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"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 71 | } else { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 72 | printer->Print(vars, "Request:($prefix$$request_type$ *)request"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 73 | } |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 74 | |
| 75 | // TODO(jcanizales): Put this on a new line and align colons. |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 76 | // TODO(jcanizales): eventHandler for server streaming? |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 77 | 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"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 85 | void 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); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 94 | void 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); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 102 | void 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(); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 108 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 109 | 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 Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 117 | void PrintSimpleImplementation(Printer *printer, |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 118 | const MethodDescriptor *method, |
| 119 | map<string, string> vars) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 120 | printer->Print("{\n"); |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame^] | 121 | printer->Print(vars, " [[self RPCTo$method_name$With"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 122 | if (method->client_streaming()) { |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame^] | 123 | printer->Print("RequestsWriter:request"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 124 | } else { |
| 125 | printer->Print("Request:request"); |
| 126 | } |
| 127 | printer->Print(" handler:handler] start];\n"); |
| 128 | printer->Print("}\n"); |
| 129 | } |
| 130 | |
| 131 | void 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 Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame^] | 139 | printer->Print("request\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 140 | } else { |
| 141 | printer->Print("[GRXWriter writerWithValue:request]\n"); |
| 142 | } |
| 143 | |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame^] | 144 | printer->Print(vars, |
| 145 | " responseClass:[$prefix$$response_type$ class]\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 146 | |
| 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 | |
| 157 | void PrintMethodImplementations(Printer *printer, |
| 158 | const MethodDescriptor *method, |
| 159 | map<string, string> vars) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 160 | vars["method_name"] = method->name(); |
| 161 | vars["request_type"] = method->input_type()->name(); |
| 162 | vars["response_type"] = method->output_type()->name(); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 163 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 164 | 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"); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 171 | PrintAdvancedSignature(printer, method, vars); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 172 | PrintAdvancedImplementation(printer, method, vars); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 175 | } // namespace |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 176 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 177 | string GetHeader(const ServiceDescriptor *service, const string prefix) { |
| 178 | string output; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 179 | grpc::protobuf::io::StringOutputStream output_stream(&output); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 180 | 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 | |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 189 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 190 | PrintMethodDeclarations(&printer, service->method(i), vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 191 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 192 | printer.Print("@end\n\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 193 | |
| 194 | printer.Print("// Basic service implementation, over gRPC, that only does" |
| 195 | " marshalling and parsing.\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 196 | printer.Print(vars, "@interface $prefix$$service_name$ :" |
| 197 | " ProtoService<$prefix$$service_name$>\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 198 | printer.Print("- (instancetype)initWithHost:(NSString *)host" |
| 199 | " NS_DESIGNATED_INITIALIZER;\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 200 | printer.Print("@end\n"); |
| 201 | return output; |
| 202 | } |
| 203 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 204 | string GetSource(const ServiceDescriptor *service, const string prefix) { |
| 205 | string output; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 206 | grpc::protobuf::io::StringOutputStream output_stream(&output); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 207 | Printer printer(&output_stream, '$'); |
| 208 | |
| 209 | map<string, string> vars = {{"service_name", service->name()}, |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 210 | {"package", service->file()->package()}, |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 211 | {"prefix", prefix}}; |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 212 | |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 213 | printer.Print(vars, |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 214 | "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"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 221 | printer.Print("- (instancetype)initWithHost:(NSString *)host {\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 222 | printer.Print(" return (self = [super initWithHost:host" |
| 223 | " packageName:kPackageName serviceName:kServiceName]);\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 224 | printer.Print("}\n\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 225 | 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 | |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 233 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 234 | PrintMethodImplementations(&printer, service->method(i), vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 235 | } |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 236 | |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 237 | printer.Print("@end\n"); |
| 238 | return output; |
| 239 | } |
| 240 | |
| 241 | } // namespace grpc_objective_c_generator |