OLD | NEW |
(Empty) | |
| 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 #include <sstream> |
| 36 |
| 37 #include "src/compiler/config.h" |
| 38 #include "src/compiler/objective_c_generator.h" |
| 39 #include "src/compiler/objective_c_generator_helpers.h" |
| 40 |
| 41 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h> |
| 42 |
| 43 using ::google::protobuf::compiler::objectivec::ClassName; |
| 44 using ::grpc::protobuf::io::Printer; |
| 45 using ::grpc::protobuf::MethodDescriptor; |
| 46 using ::grpc::protobuf::ServiceDescriptor; |
| 47 using ::std::map; |
| 48 |
| 49 namespace grpc_objective_c_generator { |
| 50 namespace { |
| 51 |
| 52 void PrintProtoRpcDeclarationAsPragma(Printer *printer, |
| 53 const MethodDescriptor *method, |
| 54 map< ::grpc::string, ::grpc::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, const MethodDescriptor *method, |
| 64 const map< ::grpc::string, ::grpc::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:(GRXWriter *)requestWriter"); |
| 70 } else { |
| 71 printer->Print(vars, "Request:($request_class$ *)request"); |
| 72 } |
| 73 |
| 74 // TODO(jcanizales): Put this on a new line and align colons. |
| 75 if (method->server_streaming()) { |
| 76 printer->Print(vars, |
| 77 " eventHandler:(void(^)(BOOL done, " |
| 78 "$response_class$ *response, NSError *error))eventHandler"); |
| 79 } else { |
| 80 printer->Print(vars, |
| 81 " handler:(void(^)($response_class$ *response, " |
| 82 "NSError *error))handler"); |
| 83 } |
| 84 } |
| 85 |
| 86 void PrintSimpleSignature(Printer *printer, const MethodDescriptor *method, |
| 87 map< ::grpc::string, ::grpc::string> vars) { |
| 88 vars["method_name"] = |
| 89 grpc_generator::LowercaseFirstLetter(vars["method_name"]); |
| 90 vars["return_type"] = "void"; |
| 91 PrintMethodSignature(printer, method, vars); |
| 92 } |
| 93 |
| 94 void PrintAdvancedSignature(Printer *printer, const MethodDescriptor *method, |
| 95 map< ::grpc::string, ::grpc::string> vars) { |
| 96 vars["method_name"] = "RPCTo" + vars["method_name"]; |
| 97 vars["return_type"] = "ProtoRPC *"; |
| 98 PrintMethodSignature(printer, method, vars); |
| 99 } |
| 100 |
| 101 inline map< ::grpc::string, ::grpc::string> GetMethodVars(const MethodDescriptor
*method) { |
| 102 map< ::grpc::string, ::grpc::string> res; |
| 103 res["method_name"] = method->name(); |
| 104 res["request_type"] = method->input_type()->name(); |
| 105 res["response_type"] = method->output_type()->name(); |
| 106 res["request_class"] = ClassName(method->input_type()); |
| 107 res["response_class"] = ClassName(method->output_type()); |
| 108 return res; |
| 109 } |
| 110 |
| 111 void PrintMethodDeclarations(Printer *printer, const MethodDescriptor *method) { |
| 112 map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method); |
| 113 |
| 114 PrintProtoRpcDeclarationAsPragma(printer, method, vars); |
| 115 |
| 116 PrintSimpleSignature(printer, method, vars); |
| 117 printer->Print(";\n\n"); |
| 118 PrintAdvancedSignature(printer, method, vars); |
| 119 printer->Print(";\n\n\n"); |
| 120 } |
| 121 |
| 122 void PrintSimpleImplementation(Printer *printer, const MethodDescriptor *method, |
| 123 map< ::grpc::string, ::grpc::string> vars) { |
| 124 printer->Print("{\n"); |
| 125 printer->Print(vars, " [[self RPCTo$method_name$With"); |
| 126 if (method->client_streaming()) { |
| 127 printer->Print("RequestsWriter:requestWriter"); |
| 128 } else { |
| 129 printer->Print("Request:request"); |
| 130 } |
| 131 if (method->server_streaming()) { |
| 132 printer->Print(" eventHandler:eventHandler] start];\n"); |
| 133 } else { |
| 134 printer->Print(" handler:handler] start];\n"); |
| 135 } |
| 136 printer->Print("}\n"); |
| 137 } |
| 138 |
| 139 void PrintAdvancedImplementation(Printer *printer, |
| 140 const MethodDescriptor *method, |
| 141 map< ::grpc::string, ::grpc::string> vars) { |
| 142 printer->Print("{\n"); |
| 143 printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n"); |
| 144 |
| 145 printer->Print(" requestsWriter:"); |
| 146 if (method->client_streaming()) { |
| 147 printer->Print("requestWriter\n"); |
| 148 } else { |
| 149 printer->Print("[GRXWriter writerWithValue:request]\n"); |
| 150 } |
| 151 |
| 152 printer->Print(vars, " responseClass:[$response_class$ class]\n"); |
| 153 |
| 154 printer->Print(" responsesWriteable:[GRXWriteable "); |
| 155 if (method->server_streaming()) { |
| 156 printer->Print("writeableWithEventHandler:eventHandler]];\n"); |
| 157 } else { |
| 158 printer->Print("writeableWithSingleHandler:handler]];\n"); |
| 159 } |
| 160 |
| 161 printer->Print("}\n"); |
| 162 } |
| 163 |
| 164 void PrintMethodImplementations(Printer *printer, |
| 165 const MethodDescriptor *method) { |
| 166 map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method); |
| 167 |
| 168 PrintProtoRpcDeclarationAsPragma(printer, method, vars); |
| 169 |
| 170 // TODO(jcanizales): Print documentation from the method. |
| 171 PrintSimpleSignature(printer, method, vars); |
| 172 PrintSimpleImplementation(printer, method, vars); |
| 173 |
| 174 printer->Print("// Returns a not-yet-started RPC object.\n"); |
| 175 PrintAdvancedSignature(printer, method, vars); |
| 176 PrintAdvancedImplementation(printer, method, vars); |
| 177 } |
| 178 |
| 179 } // namespace |
| 180 |
| 181 ::grpc::string GetHeader(const ServiceDescriptor *service) { |
| 182 ::grpc::string output; |
| 183 { |
| 184 // Scope the output stream so it closes and finalizes output to the string. |
| 185 grpc::protobuf::io::StringOutputStream output_stream(&output); |
| 186 Printer printer(&output_stream, '$'); |
| 187 |
| 188 map< ::grpc::string, ::grpc::string> vars = {{"service_class", ServiceClassN
ame(service)}}; |
| 189 |
| 190 printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n"); |
| 191 |
| 192 for (int i = 0; i < service->method_count(); i++) { |
| 193 PrintMethodDeclarations(&printer, service->method(i)); |
| 194 } |
| 195 printer.Print("@end\n\n"); |
| 196 |
| 197 printer.Print( |
| 198 "// Basic service implementation, over gRPC, that only does" |
| 199 " marshalling and parsing.\n"); |
| 200 printer.Print(vars, |
| 201 "@interface $service_class$ :" |
| 202 " ProtoService<$service_class$>\n"); |
| 203 printer.Print( |
| 204 "- (instancetype)initWithHost:(NSString *)host" |
| 205 " NS_DESIGNATED_INITIALIZER;\n"); |
| 206 printer.Print("+ (instancetype)serviceWithHost:(NSString *)host;\n"); |
| 207 printer.Print("@end\n"); |
| 208 } |
| 209 return output; |
| 210 } |
| 211 |
| 212 ::grpc::string GetSource(const ServiceDescriptor *service) { |
| 213 ::grpc::string output; |
| 214 { |
| 215 // Scope the output stream so it closes and finalizes output to the string. |
| 216 grpc::protobuf::io::StringOutputStream output_stream(&output); |
| 217 Printer printer(&output_stream, '$'); |
| 218 |
| 219 map< ::grpc::string,::grpc::string> vars = {{"service_name", service->name()
}, |
| 220 {"service_class", ServiceClassName(service)}, |
| 221 {"package", service->file()->package()}}; |
| 222 |
| 223 printer.Print(vars, |
| 224 "static NSString *const kPackageName = @\"$package$\";\n"); |
| 225 printer.Print( |
| 226 vars, "static NSString *const kServiceName = @\"$service_name$\";\n\n"); |
| 227 |
| 228 printer.Print(vars, "@implementation $service_class$\n\n"); |
| 229 |
| 230 printer.Print("// Designated initializer\n"); |
| 231 printer.Print("- (instancetype)initWithHost:(NSString *)host {\n"); |
| 232 printer.Print( |
| 233 " return (self = [super initWithHost:host" |
| 234 " packageName:kPackageName serviceName:kServiceName]);\n"); |
| 235 printer.Print("}\n\n"); |
| 236 printer.Print( |
| 237 "// Override superclass initializer to disallow different" |
| 238 " package and service names.\n"); |
| 239 printer.Print("- (instancetype)initWithHost:(NSString *)host\n"); |
| 240 printer.Print(" packageName:(NSString *)packageName\n"); |
| 241 printer.Print(" serviceName:(NSString *)serviceName {\n"); |
| 242 printer.Print(" return [self initWithHost:host];\n"); |
| 243 printer.Print("}\n\n"); |
| 244 printer.Print("+ (instancetype)serviceWithHost:(NSString *)host {\n"); |
| 245 printer.Print(" return [[self alloc] initWithHost:host];\n"); |
| 246 printer.Print("}\n\n\n"); |
| 247 |
| 248 for (int i = 0; i < service->method_count(); i++) { |
| 249 PrintMethodImplementations(&printer, service->method(i)); |
| 250 } |
| 251 |
| 252 printer.Print("@end\n"); |
| 253 } |
| 254 return output; |
| 255 } |
| 256 |
| 257 } // namespace grpc_objective_c_generator |
OLD | NEW |