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