murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <map> |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 20 | #include <set> |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 21 | #include <sstream> |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 22 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 23 | #include "src/compiler/config.h" |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 24 | #include "src/compiler/objective_c_generator.h" |
| 25 | #include "src/compiler/objective_c_generator_helpers.h" |
| 26 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 27 | #include <google/protobuf/compiler/objectivec/objectivec_helpers.h> |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 28 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 29 | using ::google::protobuf::compiler::objectivec::ClassName; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 30 | using ::grpc::protobuf::FileDescriptor; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 31 | using ::grpc::protobuf::MethodDescriptor; |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 32 | using ::grpc::protobuf::ServiceDescriptor; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 33 | using ::grpc::protobuf::io::Printer; |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 34 | using ::std::map; |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 35 | using ::std::set; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 36 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 37 | namespace grpc_objective_c_generator { |
| 38 | namespace { |
| 39 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 40 | void PrintProtoRpcDeclarationAsPragma( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 41 | Printer* printer, const MethodDescriptor* method, |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 42 | map< ::grpc::string, ::grpc::string> vars) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 43 | vars["client_stream"] = method->client_streaming() ? "stream " : ""; |
| 44 | vars["server_stream"] = method->server_streaming() ? "stream " : ""; |
| 45 | |
| 46 | printer->Print(vars, |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 47 | "#pragma mark $method_name$($client_stream$$request_type$)" |
| 48 | " returns ($server_stream$$response_type$)\n\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Makarand Dharmapurikar | cb79b29 | 2016-06-08 15:07:54 -0700 | [diff] [blame] | 51 | template <typename DescriptorType> |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 52 | static void PrintAllComments(const DescriptorType* desc, Printer* printer) { |
Makarand Dharmapurikar | cb79b29 | 2016-06-08 15:07:54 -0700 | [diff] [blame] | 53 | std::vector<grpc::string> comments; |
| 54 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, |
| 55 | &comments); |
| 56 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING, |
| 57 | &comments); |
| 58 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, |
| 59 | &comments); |
| 60 | if (comments.empty()) { |
| 61 | return; |
| 62 | } |
| 63 | printer->Print("/**\n"); |
| 64 | for (auto it = comments.begin(); it != comments.end(); ++it) { |
| 65 | printer->Print(" * "); |
| 66 | size_t start_pos = it->find_first_not_of(' '); |
| 67 | if (start_pos != grpc::string::npos) { |
| 68 | printer->Print(it->c_str() + start_pos); |
| 69 | } |
| 70 | printer->Print("\n"); |
| 71 | } |
| 72 | printer->Print(" */\n"); |
| 73 | } |
| 74 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 75 | void PrintMethodSignature(Printer* printer, const MethodDescriptor* method, |
| 76 | const map< ::grpc::string, ::grpc::string>& vars) { |
Makarand Dharmapurikar | cb79b29 | 2016-06-08 15:07:54 -0700 | [diff] [blame] | 77 | // Print comment |
| 78 | PrintAllComments(method, printer); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 79 | |
| 80 | printer->Print(vars, "- ($return_type$)$method_name$With"); |
| 81 | if (method->client_streaming()) { |
Jorge Canizales | 739c998 | 2015-07-16 22:07:54 -0700 | [diff] [blame] | 82 | printer->Print("RequestsWriter:(GRXWriter *)requestWriter"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 83 | } else { |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 84 | printer->Print(vars, "Request:($request_class$ *)request"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 85 | } |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 86 | |
| 87 | // TODO(jcanizales): Put this on a new line and align colons. |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 88 | if (method->server_streaming()) { |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 89 | printer->Print(vars, |
| 90 | " eventHandler:(void(^)(BOOL done, " |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 91 | "$response_class$ *_Nullable response, NSError *_Nullable " |
| 92 | "error))eventHandler"); |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 93 | } else { |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 94 | printer->Print(vars, |
Benjamin Herzog | 208795c | 2016-04-18 18:10:14 +0200 | [diff] [blame] | 95 | " handler:(void(^)($response_class$ *_Nullable response, " |
| 96 | "NSError *_Nullable error))handler"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 97 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 100 | void PrintSimpleSignature(Printer* printer, const MethodDescriptor* method, |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 101 | map< ::grpc::string, ::grpc::string> vars) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 102 | vars["method_name"] = |
| 103 | grpc_generator::LowercaseFirstLetter(vars["method_name"]); |
| 104 | vars["return_type"] = "void"; |
| 105 | PrintMethodSignature(printer, method, vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 108 | void PrintAdvancedSignature(Printer* printer, const MethodDescriptor* method, |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 109 | map< ::grpc::string, ::grpc::string> vars) { |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 110 | vars["method_name"] = "RPCTo" + vars["method_name"]; |
Makarand Dharmapurikar | 4f11ab1 | 2016-06-08 10:40:00 -0700 | [diff] [blame] | 111 | vars["return_type"] = "GRPCProtoCall *"; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 112 | PrintMethodSignature(printer, method, vars); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 115 | inline map< ::grpc::string, ::grpc::string> GetMethodVars( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 116 | const MethodDescriptor* method) { |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 117 | map< ::grpc::string, ::grpc::string> res; |
vjpai | c7eed74 | 2015-07-14 10:47:28 -0700 | [diff] [blame] | 118 | res["method_name"] = method->name(); |
| 119 | res["request_type"] = method->input_type()->name(); |
| 120 | res["response_type"] = method->output_type()->name(); |
| 121 | res["request_class"] = ClassName(method->input_type()); |
| 122 | res["response_class"] = ClassName(method->output_type()); |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 123 | return res; |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 126 | void PrintMethodDeclarations(Printer* printer, const MethodDescriptor* method) { |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 127 | map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 128 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 129 | PrintProtoRpcDeclarationAsPragma(printer, method, vars); |
| 130 | |
| 131 | PrintSimpleSignature(printer, method, vars); |
| 132 | printer->Print(";\n\n"); |
| 133 | PrintAdvancedSignature(printer, method, vars); |
| 134 | printer->Print(";\n\n\n"); |
| 135 | } |
| 136 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 137 | void PrintSimpleImplementation(Printer* printer, const MethodDescriptor* method, |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 138 | map< ::grpc::string, ::grpc::string> vars) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 139 | printer->Print("{\n"); |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 140 | printer->Print(vars, " [[self RPCTo$method_name$With"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 141 | if (method->client_streaming()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 142 | printer->Print("RequestsWriter:requestWriter"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 143 | } else { |
| 144 | printer->Print("Request:request"); |
| 145 | } |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 146 | if (method->server_streaming()) { |
| 147 | printer->Print(" eventHandler:eventHandler] start];\n"); |
| 148 | } else { |
| 149 | printer->Print(" handler:handler] start];\n"); |
| 150 | } |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 151 | printer->Print("}\n"); |
| 152 | } |
| 153 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 154 | void PrintAdvancedImplementation(Printer* printer, |
| 155 | const MethodDescriptor* method, |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 156 | map< ::grpc::string, ::grpc::string> vars) { |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 157 | printer->Print("{\n"); |
| 158 | printer->Print(vars, " return [self RPCToMethod:@\"$method_name$\"\n"); |
| 159 | |
| 160 | printer->Print(" requestsWriter:"); |
| 161 | if (method->client_streaming()) { |
murgatroid99 | 25a2661 | 2015-06-25 11:22:23 -0700 | [diff] [blame] | 162 | printer->Print("requestWriter\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 163 | } else { |
| 164 | printer->Print("[GRXWriter writerWithValue:request]\n"); |
| 165 | } |
| 166 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 167 | printer->Print(vars, " responseClass:[$response_class$ class]\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 168 | |
| 169 | printer->Print(" responsesWriteable:[GRXWriteable "); |
| 170 | if (method->server_streaming()) { |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 171 | printer->Print("writeableWithEventHandler:eventHandler]];\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 172 | } else { |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 173 | printer->Print("writeableWithSingleHandler:handler]];\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | printer->Print("}\n"); |
| 177 | } |
| 178 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 179 | void PrintMethodImplementations(Printer* printer, |
| 180 | const MethodDescriptor* method) { |
Jorge Canizales | 637c365 | 2015-08-13 19:04:22 -0700 | [diff] [blame] | 181 | map< ::grpc::string, ::grpc::string> vars = GetMethodVars(method); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 182 | |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 183 | PrintProtoRpcDeclarationAsPragma(printer, method, vars); |
| 184 | |
| 185 | // TODO(jcanizales): Print documentation from the method. |
| 186 | PrintSimpleSignature(printer, method, vars); |
| 187 | PrintSimpleImplementation(printer, method, vars); |
| 188 | |
| 189 | printer->Print("// Returns a not-yet-started RPC object.\n"); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 190 | PrintAdvancedSignature(printer, method, vars); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 191 | PrintAdvancedImplementation(printer, method, vars); |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 194 | } // namespace |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 195 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 196 | ::grpc::string GetAllMessageClasses(const FileDescriptor* file) { |
Muxi Yan | 75271d7 | 2017-09-18 17:30:20 -0700 | [diff] [blame] | 197 | ::grpc::string output; |
| 198 | set< ::grpc::string> classes; |
| 199 | for (int i = 0; i < file->service_count(); i++) { |
| 200 | const auto service = file->service(i); |
| 201 | for (int i = 0; i < service->method_count(); i++) { |
| 202 | const auto method = service->method(i); |
| 203 | classes.insert(ClassName(method->input_type())); |
| 204 | classes.insert(ClassName(method->output_type())); |
| 205 | } |
| 206 | } |
| 207 | for (auto one_class : classes) { |
| 208 | output += " @class " + one_class + ";\n"; |
| 209 | } |
| 210 | |
| 211 | return output; |
| 212 | } |
| 213 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 214 | ::grpc::string GetHeader(const ServiceDescriptor* service) { |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 215 | ::grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 216 | { |
| 217 | // Scope the output stream so it closes and finalizes output to the string. |
| 218 | grpc::protobuf::io::StringOutputStream output_stream(&output); |
| 219 | Printer printer(&output_stream, '$'); |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 220 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 221 | map< ::grpc::string, ::grpc::string> vars = { |
| 222 | {"service_class", ServiceClassName(service)}}; |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 223 | |
| 224 | printer.Print(vars, "@protocol $service_class$ <NSObject>\n\n"); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 225 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 226 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 227 | PrintMethodDeclarations(&printer, service->method(i)); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 228 | } |
| 229 | printer.Print("@end\n\n"); |
| 230 | |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 231 | printer.Print( |
Makarand Dharmapurikar | cb79b29 | 2016-06-08 15:07:54 -0700 | [diff] [blame] | 232 | "/**\n" |
| 233 | " * Basic service implementation, over gRPC, that only does\n" |
| 234 | " * marshalling and parsing.\n" |
| 235 | " */\n"); |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 236 | printer.Print(vars, |
| 237 | "@interface $service_class$ :" |
Makarand Dharmapurikar | 6f95010 | 2016-06-07 16:52:19 -0700 | [diff] [blame] | 238 | " GRPCProtoService<$service_class$>\n"); |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 239 | printer.Print( |
| 240 | "- (instancetype)initWithHost:(NSString *)host" |
| 241 | " NS_DESIGNATED_INITIALIZER;\n"); |
Nate Kibler | 6d19724 | 2015-09-25 10:02:20 -0700 | [diff] [blame] | 242 | printer.Print("+ (instancetype)serviceWithHost:(NSString *)host;\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 243 | printer.Print("@end\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 244 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 245 | return output; |
| 246 | } |
| 247 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame^] | 248 | ::grpc::string GetSource(const ServiceDescriptor* service) { |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 249 | ::grpc::string output; |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 250 | { |
| 251 | // Scope the output stream so it closes and finalizes output to the string. |
| 252 | grpc::protobuf::io::StringOutputStream output_stream(&output); |
| 253 | Printer printer(&output_stream, '$'); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 254 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 255 | map< ::grpc::string, ::grpc::string> vars = { |
| 256 | {"service_name", service->name()}, |
| 257 | {"service_class", ServiceClassName(service)}, |
| 258 | {"package", service->file()->package()}}; |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 259 | |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 260 | printer.Print(vars, "@implementation $service_class$\n\n"); |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 261 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 262 | printer.Print("// Designated initializer\n"); |
| 263 | printer.Print("- (instancetype)initWithHost:(NSString *)host {\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 264 | printer.Print( |
| 265 | vars, |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 266 | " return (self = [super initWithHost:host" |
Makarand Dharmapurikar | 857495a | 2016-06-09 14:25:31 -0700 | [diff] [blame] | 267 | " packageName:@\"$package$\" serviceName:@\"$service_name$\"]);\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 268 | printer.Print("}\n\n"); |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 269 | printer.Print( |
| 270 | "// Override superclass initializer to disallow different" |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 271 | " package and service names.\n"); |
| 272 | printer.Print("- (instancetype)initWithHost:(NSString *)host\n"); |
| 273 | printer.Print(" packageName:(NSString *)packageName\n"); |
| 274 | printer.Print(" serviceName:(NSString *)serviceName {\n"); |
| 275 | printer.Print(" return [self initWithHost:host];\n"); |
Nate Kibler | 6d19724 | 2015-09-25 10:02:20 -0700 | [diff] [blame] | 276 | printer.Print("}\n\n"); |
Nate Kibler | 6d19724 | 2015-09-25 10:02:20 -0700 | [diff] [blame] | 277 | printer.Print("+ (instancetype)serviceWithHost:(NSString *)host {\n"); |
| 278 | printer.Print(" return [[self alloc] initWithHost:host];\n"); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 279 | printer.Print("}\n\n\n"); |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 280 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 281 | for (int i = 0; i < service->method_count(); i++) { |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 282 | PrintMethodImplementations(&printer, service->method(i)); |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 283 | } |
Jorge Canizales | 472f0b0 | 2015-05-12 22:56:04 -0700 | [diff] [blame] | 284 | |
Jan Tattermusch | 5dcebd9 | 2015-05-27 15:30:59 -0700 | [diff] [blame] | 285 | printer.Print("@end\n"); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 286 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 287 | return output; |
| 288 | } |
| 289 | |
Vijay Pai | 181ef45 | 2015-07-14 13:52:48 -0700 | [diff] [blame] | 290 | } // namespace grpc_objective_c_generator |