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 | |
| 43 | namespace grpc_objective_c_generator { |
| 44 | namespace { |
| 45 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 46 | using ::grpc::protobuf::io::Printer; |
| 47 | using ::grpc::protobuf::MethodDescriptor; |
| 48 | using ::std::map; |
| 49 | using ::grpc::string; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 50 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 51 | void PrintProtoRpcDeclarationAsPragma(Printer *printer, |
| 52 | const MethodDescriptor *method, |
| 53 | map<string, string> vars) { |
| 54 | vars["client_stream"] = method->client_streaming() ? "stream " : ""; |
| 55 | vars["server_stream"] = method->server_streaming() ? "stream " : ""; |
| 56 | |
| 57 | printer->Print(vars, |
| 58 | "#pragma mark $method_name$($client_stream$$request_type$)" |
| 59 | " returns ($server_stream$$response_type$)\n\n"); |
| 60 | } |
| 61 | |
| 62 | void PrintMethodSignature(Printer *printer, |
| 63 | const MethodDescriptor *method, |
| 64 | const map<string, string>& vars) { |
| 65 | // TODO(jcanizales): Print method comments. |
| 66 | |
| 67 | printer->Print(vars, "- ($return_type$)$method_name$With"); |
| 68 | if (method->client_streaming()) { |
| 69 | printer->Print("RequestsWriter:(id<GRXWriter>)request"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 70 | } else { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 71 | printer->Print(vars, "Request:($prefix$$request_type$ *)request"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 72 | } |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 73 | |
| 74 | // TODO(jcanizales): Put this on a new line and align colons. |
| 75 | printer->Print(" handler:(void(^)("); |
| 76 | if (method->server_streaming()) { |
| 77 | printer->Print("BOOL done, "); |
| 78 | } |
| 79 | printer->Print(vars, |
| 80 | "$prefix$$response_type$ *response, NSError *error))handler"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 83 | void PrintSimpleSignature(Printer *printer, |
| 84 | const MethodDescriptor *method, |
| 85 | map<string, string> vars) { |
| 86 | vars["method_name"] = |
| 87 | grpc_generator::LowercaseFirstLetter(vars["method_name"]); |
| 88 | vars["return_type"] = "void"; |
| 89 | PrintMethodSignature(printer, method, vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 92 | void PrintAdvancedSignature(Printer *printer, |
| 93 | const MethodDescriptor *method, |
| 94 | map<string, string> vars) { |
| 95 | vars["method_name"] = "RPCTo" + vars["method_name"]; |
| 96 | vars["return_type"] = "ProtoRPC *"; |
| 97 | PrintMethodSignature(printer, method, vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 100 | void PrintMethodDeclarations(Printer *printer, |
| 101 | const MethodDescriptor *method, |
| 102 | map<string, string> vars) { |
| 103 | vars["method_name"] = method->name(); |
| 104 | vars["request_type"] = method->input_type()->name(); |
| 105 | vars["response_type"] = method->output_type()->name(); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 106 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 107 | PrintProtoRpcDeclarationAsPragma(printer, method, vars); |
| 108 | |
| 109 | PrintSimpleSignature(printer, method, vars); |
| 110 | printer->Print(";\n\n"); |
| 111 | PrintAdvancedSignature(printer, method, vars); |
| 112 | printer->Print(";\n\n\n"); |
| 113 | } |
| 114 | |
| 115 | void PrintSourceMethodSimpleBlock(Printer *printer, |
| 116 | const MethodDescriptor *method, |
| 117 | map<string, string> vars) { |
| 118 | vars["method_name"] = method->name(); |
| 119 | vars["request_type"] = method->input_type()->name(); |
| 120 | vars["response_type"] = method->output_type()->name(); |
| 121 | |
| 122 | PrintSimpleSignature(printer, method, vars); |
| 123 | |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 124 | printer->Print(" {\n"); |
| 125 | printer->Indent(); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 126 | printer->Print(vars, "return [[self $method_name$WithRequest:request] " |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 127 | "connectHandler:^(id value, NSError *error) {\n"); |
| 128 | printer->Indent(); |
| 129 | printer->Print("handler(value, error);\n"); |
| 130 | printer->Outdent(); |
| 131 | printer->Print("}];\n"); |
| 132 | printer->Outdent(); |
| 133 | printer->Print("}\n"); |
| 134 | } |
| 135 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 136 | void PrintSourceMethodAdvanced(Printer *printer, |
| 137 | const MethodDescriptor *method, |
| 138 | map<string, string> vars) { |
| 139 | vars["method_name"] = method->name(); |
| 140 | vars["request_type"] = method->input_type()->name(); |
| 141 | vars["response_type"] = method->output_type()->name(); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 142 | |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 143 | PrintAdvancedSignature(printer, method, vars); |
| 144 | |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 145 | printer->Print(" {\n"); |
| 146 | printer->Indent(); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 147 | printer->Print(vars, "return [self $method_name$WithRequest:request " |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 148 | "client:[self newClient]];\n"); |
| 149 | printer->Outdent(); |
| 150 | printer->Print("}\n"); |
| 151 | } |
| 152 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 153 | void PrintSourceMethodHandler(Printer *printer, |
| 154 | const MethodDescriptor *method, |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 155 | std::map<grpc::string, grpc::string> *vars) { |
| 156 | (*vars)["method_name"] = method->name(); |
| 157 | (*vars)["response_type"] = PrefixedName(method->output_type()->name()); |
| 158 | (*vars)["caps_name"] = grpc_generator::CapitalizeFirstLetter(method->name()); |
| 159 | |
| 160 | printer->Print(*vars, "- (GRXSource *)$method_name$WithRequest:" |
| 161 | "(id<GRXSource>)request client:(PBgRPCClient *)client {\n"); |
| 162 | printer->Indent(); |
| 163 | printer->Print(*vars, |
| 164 | "return [self responseWithMethod:$@\"$caps_name\"\n"); |
| 165 | printer->Print(*vars, |
| 166 | " class:[$response_type$ class]\n"); |
| 167 | printer->Print(" request:request\n"); |
| 168 | printer->Print(" client:client];\n"); |
| 169 | printer->Outdent(); |
| 170 | printer->Print("}\n"); |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | grpc::string GetHeader(const grpc::protobuf::ServiceDescriptor *service, |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 176 | const string prefix) { |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 177 | grpc::string output; |
| 178 | grpc::protobuf::io::StringOutputStream output_stream(&output); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 179 | 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 | |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 188 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 189 | PrintMethodDeclarations(&printer, service->method(i), vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 190 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 191 | printer.Print("@end\n\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 192 | |
| 193 | printer.Print("// Basic service implementation, over gRPC, that only does" |
| 194 | " marshalling and parsing.\n"); |
| 195 | // use prefix |
| 196 | printer.Print(vars, "@interface RMT$service_name$ :" |
| 197 | " ProtoService<RMT$service_name$>\n"); |
| 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 | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 204 | grpc::string GetSource(const grpc::protobuf::ServiceDescriptor *service, |
| 205 | const string prefix) { |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 206 | grpc::string output; |
| 207 | grpc::protobuf::io::StringOutputStream output_stream(&output); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 208 | Printer printer(&output_stream, '$'); |
| 209 | |
| 210 | map<string, string> vars = {{"service_name", service->name()}, |
| 211 | {"prefix", prefix}}; |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 212 | printer.Print(vars, "#import \"$service_name$Stub.pb.h\"\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 213 | printer.Print("#import \"PBGeneratedMessage+GRXSource.h\"\n\n"); |
| 214 | vars["full_name"] = service->full_name(); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 215 | printer.Print(vars, |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 216 | "static NSString *const kInterface = @\"$full_name$\";\n"); |
| 217 | printer.Print("@implementation $service_name$Stub\n\n"); |
| 218 | printer.Print("- (instancetype)initWithHost:(NSString *)host {\n"); |
| 219 | printer.Indent(); |
| 220 | printer.Print("if ((self = [super initWithHost:host " |
| 221 | "interface:kInterface])) {\n"); |
| 222 | printer.Print("}\n"); |
| 223 | printer.Print("return self;\n"); |
| 224 | printer.Outdent(); |
| 225 | printer.Print("}\n\n"); |
| 226 | printer.Print("#pragma mark Simple block handlers.\n"); |
| 227 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 228 | PrintSourceMethodSimpleBlock(&printer, service->method(i), vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 229 | } |
| 230 | printer.Print("\n"); |
| 231 | printer.Print("#pragma mark Advanced handlers.\n"); |
| 232 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame^] | 233 | PrintSourceMethodAdvanced(&printer, service->method(i), vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 234 | } |
| 235 | printer.Print("\n"); |
| 236 | printer.Print("#pragma mark Handlers for subclasses " |
| 237 | "(stub wrappers) to override.\n"); |
| 238 | for (int i = 0; i < service->method_count(); i++) { |
| 239 | PrintSourceMethodHandler(&printer, service->method(i), &vars); |
| 240 | } |
| 241 | printer.Print("@end\n"); |
| 242 | return output; |
| 243 | } |
| 244 | |
| 245 | } // namespace grpc_objective_c_generator |