Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * NOTE: The following implementation is a translation for the Swift-grpc |
| 19 | * generator since flatbuffers doesnt allow plugins for now. if an issue arises |
| 20 | * please open an issue in the flatbuffers repository. This file should always |
| 21 | * be maintained according to the Swift-grpc repository |
| 22 | */ |
| 23 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 24 | #include "src/compiler/ts_generator.h" |
| 25 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 26 | #include <map> |
| 27 | #include <sstream> |
| 28 | |
| 29 | #include "flatbuffers/util.h" |
| 30 | #include "src/compiler/schema_interface.h" |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 31 | |
| 32 | namespace grpc_ts_generator { |
| 33 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 34 | grpc::string GenerateNamespace(const std::vector<std::string> ns, |
| 35 | const std::string filename, |
| 36 | const bool include_separator) { |
| 37 | grpc::string path = ""; |
| 38 | if (include_separator) path += "."; |
| 39 | |
| 40 | for (auto it = ns.begin(); it < ns.end(); it++) { |
| 41 | if (include_separator) path += "/"; |
| 42 | path += include_separator |
| 43 | ? flatbuffers::ConvertCase(*it, flatbuffers::Case::kDasher, |
| 44 | flatbuffers::Case::kUpperCamel) |
| 45 | : *it + "_"; |
| 46 | } |
| 47 | |
| 48 | if (include_separator) path += "/"; |
| 49 | path += include_separator |
| 50 | ? flatbuffers::ConvertCase(filename, flatbuffers::Case::kDasher, |
| 51 | flatbuffers::Case::kUpperCamel) |
| 52 | : filename; |
| 53 | return path; |
| 54 | } |
| 55 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 56 | // MARK: - Shared code |
| 57 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 58 | void GenerateImports(const grpc_generator::Service *service, |
| 59 | grpc_generator::Printer *printer, |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 60 | std::map<grpc::string, grpc::string> *dictonary, |
| 61 | const bool grpc_var_import) { |
| 62 | auto vars = *dictonary; |
| 63 | printer->Print( |
| 64 | "// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***\n"); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 65 | printer->Print("import * as flatbuffers from 'flatbuffers';\n"); |
| 66 | |
| 67 | std::set<grpc::string> generated_imports; |
| 68 | |
| 69 | for (auto it = 0; it < service->method_count(); it++) { |
| 70 | auto method = service->method(it); |
| 71 | auto output = method->get_output_type_name(); |
| 72 | auto input = method->get_input_type_name(); |
| 73 | auto input_namespace = method->get_input_namespace_parts(); |
| 74 | |
| 75 | vars["OUTPUT"] = output; |
| 76 | vars["INPUT"] = input; |
| 77 | |
| 78 | if (generated_imports.find(output) == generated_imports.end()) { |
| 79 | generated_imports.insert(output); |
| 80 | vars["OUTPUT_DIR"] = |
| 81 | GenerateNamespace(method->get_output_namespace_parts(), output, true); |
| 82 | vars["Output_alias"] = GenerateNamespace( |
| 83 | method->get_output_namespace_parts(), output, false); |
| 84 | printer->Print( |
| 85 | vars, "import { $OUTPUT$ as $Output_alias$ } from '$OUTPUT_DIR$';\n"); |
| 86 | } |
| 87 | if (generated_imports.find(input) == generated_imports.end()) { |
| 88 | generated_imports.insert(input); |
| 89 | vars["INPUT_DIR"] = |
| 90 | GenerateNamespace(method->get_output_namespace_parts(), input, true); |
| 91 | vars["Input_alias"] = |
| 92 | GenerateNamespace(method->get_output_namespace_parts(), input, false); |
| 93 | printer->Print( |
| 94 | vars, "import { $INPUT$ as $Input_alias$ } from '$INPUT_DIR$';\n"); |
| 95 | } |
| 96 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 97 | printer->Print("\n"); |
| 98 | if (grpc_var_import) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 99 | printer->Print("var grpc = require('@grpc/grpc-js');\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 100 | else |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 101 | printer->Print("import * as grpc from '@grpc/grpc-js';\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 102 | printer->Print("\n"); |
| 103 | } |
| 104 | |
| 105 | // MARK: - Generate Main GRPC Code |
| 106 | |
| 107 | void GetStreamType(grpc_generator::Printer *printer, |
| 108 | const grpc_generator::Method *method, |
| 109 | std::map<grpc::string, grpc::string> *dictonary) { |
| 110 | auto vars = *dictonary; |
| 111 | auto client_streaming = method->ClientStreaming() || method->BidiStreaming(); |
| 112 | auto server_streaming = method->ServerStreaming() || method->BidiStreaming(); |
| 113 | vars["ClientStreaming"] = client_streaming ? "true" : "false"; |
| 114 | vars["ServerStreaming"] = server_streaming ? "true" : "false"; |
| 115 | printer->Print(vars, "requestStream: $ClientStreaming$,\n"); |
| 116 | printer->Print(vars, "responseStream: $ServerStreaming$,\n"); |
| 117 | } |
| 118 | |
| 119 | void GenerateSerializeMethod(grpc_generator::Printer *printer, |
| 120 | std::map<grpc::string, grpc::string> *dictonary) { |
| 121 | auto vars = *dictonary; |
| 122 | printer->Print(vars, "function serialize_$Type$(buffer_args) {\n"); |
| 123 | printer->Indent(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 124 | printer->Print(vars, "if (!(buffer_args instanceof $Type$)) {\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 125 | printer->Indent(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 126 | printer->Print(vars, |
| 127 | "throw new Error('Expected argument of type $VALUE$');\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 128 | printer->Outdent(); |
| 129 | printer->Print("}\n"); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 130 | printer->Print(vars, "return Buffer.from(buffer_args.serialize());\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 131 | printer->Outdent(); |
| 132 | printer->Print("}\n\n"); |
| 133 | } |
| 134 | |
| 135 | void GenerateDeserializeMethod( |
| 136 | grpc_generator::Printer *printer, |
| 137 | std::map<grpc::string, grpc::string> *dictonary) { |
| 138 | auto vars = *dictonary; |
| 139 | printer->Print(vars, "function deserialize_$Type$(buffer) {\n"); |
| 140 | printer->Indent(); |
| 141 | printer->Print(vars, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 142 | "return $Type$.getRootAs$VALUE$(new " |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 143 | "flatbuffers.ByteBuffer(buffer))\n"); |
| 144 | printer->Outdent(); |
| 145 | printer->Print("}\n\n"); |
| 146 | } |
| 147 | |
| 148 | void GenerateMethods(const grpc_generator::Service *service, |
| 149 | grpc_generator::Printer *printer, |
| 150 | std::map<grpc::string, grpc::string> *dictonary) { |
| 151 | auto vars = *dictonary; |
| 152 | |
| 153 | std::set<grpc::string> generated_functions; |
| 154 | |
| 155 | for (auto it = 0; it < service->method_count(); it++) { |
| 156 | auto method = service->method(it); |
| 157 | auto output = method->get_output_type_name(); |
| 158 | auto input = method->get_input_type_name(); |
| 159 | |
| 160 | if (generated_functions.find(output) == generated_functions.end()) { |
| 161 | generated_functions.insert(output); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 162 | vars["VALUE"] = output; |
| 163 | vars["Type"] = GenerateNamespace(method->get_output_namespace_parts(), |
| 164 | output, false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 165 | GenerateSerializeMethod(printer, &vars); |
| 166 | GenerateDeserializeMethod(printer, &vars); |
| 167 | } |
| 168 | printer->Print("\n"); |
| 169 | if (generated_functions.find(input) == generated_functions.end()) { |
| 170 | generated_functions.insert(input); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 171 | vars["VALUE"] = input; |
| 172 | vars["Type"] = |
| 173 | GenerateNamespace(method->get_input_namespace_parts(), input, false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 174 | GenerateSerializeMethod(printer, &vars); |
| 175 | GenerateDeserializeMethod(printer, &vars); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void GenerateService(const grpc_generator::Service *service, |
| 181 | grpc_generator::Printer *printer, |
| 182 | std::map<grpc::string, grpc::string> *dictonary) { |
| 183 | auto vars = *dictonary; |
| 184 | vars["NAME"] = service->name() + "Service"; |
| 185 | |
| 186 | printer->Print(vars, "var $NAME$ = exports.$NAME$ = {\n"); |
| 187 | printer->Indent(); |
| 188 | for (auto it = 0; it < service->method_count(); it++) { |
| 189 | auto method = service->method(it); |
| 190 | vars["MethodName"] = method->name(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 191 | vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(), |
| 192 | method->get_output_type_name(), false); |
| 193 | vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(), |
| 194 | method->get_input_type_name(), false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 195 | printer->Print(vars, "$MethodName$: {\n"); |
| 196 | printer->Indent(); |
| 197 | printer->Print(vars, "path: '/$PATH$$ServiceName$/$MethodName$',\n"); |
| 198 | GetStreamType(printer, &*method, &vars); |
| 199 | printer->Print(vars, "requestType: flatbuffers.ByteBuffer,\n"); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 200 | printer->Print(vars, "responseType: $OUTPUT$,\n"); |
| 201 | printer->Print(vars, "requestSerialize: serialize_$INPUT$,\n"); |
| 202 | printer->Print(vars, "requestDeserialize: deserialize_$INPUT$,\n"); |
| 203 | printer->Print(vars, "responseSerialize: serialize_$OUTPUT$,\n"); |
| 204 | printer->Print(vars, "responseDeserialize: deserialize_$OUTPUT$,\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 205 | printer->Outdent(); |
| 206 | printer->Print("},\n"); |
| 207 | } |
| 208 | printer->Outdent(); |
| 209 | printer->Print("};\n"); |
| 210 | printer->Print(vars, |
| 211 | "exports.$ServiceName$Client = " |
| 212 | "grpc.makeGenericClientConstructor($NAME$);"); |
| 213 | } |
| 214 | |
| 215 | grpc::string Generate(grpc_generator::File *file, |
| 216 | const grpc_generator::Service *service, |
| 217 | const grpc::string &filename) { |
| 218 | grpc::string output; |
| 219 | std::map<grpc::string, grpc::string> vars; |
| 220 | |
| 221 | vars["PATH"] = file->package(); |
| 222 | |
| 223 | if (!file->package().empty()) { vars["PATH"].append("."); } |
| 224 | |
| 225 | vars["ServiceName"] = service->name(); |
| 226 | vars["FBSFile"] = service->name() + "_fbs"; |
| 227 | vars["Filename"] = filename; |
| 228 | auto printer = file->CreatePrinter(&output); |
| 229 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 230 | GenerateImports(service, &*printer, &vars, true); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 231 | GenerateMethods(service, &*printer, &vars); |
| 232 | GenerateService(service, &*printer, &vars); |
| 233 | return output; |
| 234 | } |
| 235 | |
| 236 | // MARK: - Generate Interface |
| 237 | |
| 238 | void FillInterface(grpc_generator::Printer *printer, |
| 239 | std::map<grpc::string, grpc::string> *dictonary) { |
| 240 | auto vars = *dictonary; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 241 | printer->Print(vars, |
| 242 | "interface I$ServiceName$Service_I$MethodName$ extends " |
| 243 | "grpc.MethodDefinition<$INPUT$, $OUTPUT$> {\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 244 | printer->Indent(); |
| 245 | printer->Print(vars, "path: string; // /$PATH$$ServiceName$/$MethodName$\n"); |
| 246 | printer->Print(vars, "requestStream: boolean; // $ClientStreaming$\n"); |
| 247 | printer->Print(vars, "responseStream: boolean; // $ServerStreaming$\n"); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 248 | printer->Print(vars, "requestSerialize: grpc.serialize<$INPUT$>;\n"); |
| 249 | printer->Print(vars, "requestDeserialize: grpc.deserialize<$INPUT$>;\n"); |
| 250 | printer->Print(vars, "responseSerialize: grpc.serialize<$OUTPUT$>;\n"); |
| 251 | printer->Print(vars, "responseDeserialize: grpc.deserialize<$OUTPUT$>;\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 252 | printer->Outdent(); |
| 253 | printer->Print("}\n"); |
| 254 | } |
| 255 | |
| 256 | void GenerateInterfaces(const grpc_generator::Service *service, |
| 257 | grpc_generator::Printer *printer, |
| 258 | std::map<grpc::string, grpc::string> *dictonary) { |
| 259 | auto vars = *dictonary; |
| 260 | for (auto it = 0; it < service->method_count(); it++) { |
| 261 | auto method = service->method(it); |
| 262 | auto client_streaming = |
| 263 | method->ClientStreaming() || method->BidiStreaming(); |
| 264 | auto server_streaming = |
| 265 | method->ServerStreaming() || method->BidiStreaming(); |
| 266 | vars["ClientStreaming"] = client_streaming ? "true" : "false"; |
| 267 | vars["ServerStreaming"] = server_streaming ? "true" : "false"; |
| 268 | vars["MethodName"] = method->name(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 269 | vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(), |
| 270 | method->get_output_type_name(), false); |
| 271 | vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(), |
| 272 | method->get_input_type_name(), false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 273 | FillInterface(printer, &vars); |
| 274 | printer->Print("\n"); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void GenerateExportedInterface( |
| 279 | const grpc_generator::Service *service, grpc_generator::Printer *printer, |
| 280 | std::map<grpc::string, grpc::string> *dictonary) { |
| 281 | auto vars = *dictonary; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 282 | printer->Print(vars, |
| 283 | "export interface I$ServiceName$Server extends " |
| 284 | "grpc.UntypedServiceImplementation {\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 285 | printer->Indent(); |
| 286 | for (auto it = 0; it < service->method_count(); it++) { |
| 287 | auto method = service->method(it); |
| 288 | vars["Name"] = method->name(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 289 | vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(), |
| 290 | method->get_output_type_name(), false); |
| 291 | vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(), |
| 292 | method->get_input_type_name(), false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 293 | if (method->BidiStreaming()) { |
| 294 | printer->Print(vars, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 295 | "$Name$: grpc.handleBidiStreamingCall<$INPUT$, " |
| 296 | "$OUTPUT$>;\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 297 | continue; |
| 298 | } |
| 299 | if (method->NoStreaming()) { |
| 300 | printer->Print(vars, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 301 | "$Name$: grpc.handleUnaryCall<$INPUT$, " |
| 302 | "$OUTPUT$>;\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 303 | continue; |
| 304 | } |
| 305 | if (method->ClientStreaming()) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 306 | printer->Print(vars, |
| 307 | "$Name$: grpc.handleClientStreamingCall<$INPUT$, " |
| 308 | "$OUTPUT$>;\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 309 | continue; |
| 310 | } |
| 311 | if (method->ServerStreaming()) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 312 | printer->Print(vars, |
| 313 | "$Name$: grpc.handleServerStreamingCall<$INPUT$, " |
| 314 | "$OUTPUT$>;\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 315 | continue; |
| 316 | } |
| 317 | } |
| 318 | printer->Outdent(); |
| 319 | printer->Print("}\n"); |
| 320 | } |
| 321 | |
| 322 | void GenerateMainInterface(const grpc_generator::Service *service, |
| 323 | grpc_generator::Printer *printer, |
| 324 | std::map<grpc::string, grpc::string> *dictonary) { |
| 325 | auto vars = *dictonary; |
| 326 | printer->Print( |
| 327 | vars, |
| 328 | "interface I$ServiceName$Service extends " |
| 329 | "grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {\n"); |
| 330 | printer->Indent(); |
| 331 | for (auto it = 0; it < service->method_count(); it++) { |
| 332 | auto method = service->method(it); |
| 333 | vars["MethodName"] = method->name(); |
| 334 | printer->Print(vars, |
| 335 | "$MethodName$: I$ServiceName$Service_I$MethodName$;\n"); |
| 336 | } |
| 337 | printer->Outdent(); |
| 338 | printer->Print("}\n"); |
| 339 | GenerateInterfaces(service, printer, &vars); |
| 340 | printer->Print("\n"); |
| 341 | printer->Print(vars, |
| 342 | "export const $ServiceName$Service: I$ServiceName$Service;\n"); |
| 343 | printer->Print("\n"); |
| 344 | GenerateExportedInterface(service, printer, &vars); |
| 345 | } |
| 346 | |
| 347 | grpc::string GenerateMetaData() { return "metadata: grpc.Metadata"; } |
| 348 | |
| 349 | grpc::string GenerateOptions() { return "options: Partial<grpc.CallOptions>"; } |
| 350 | |
| 351 | void GenerateUnaryClientInterface( |
| 352 | grpc_generator::Printer *printer, |
| 353 | std::map<grpc::string, grpc::string> *dictonary) { |
| 354 | auto vars = *dictonary; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 355 | grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, "; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 356 | grpc::string callback = |
| 357 | "callback: (error: grpc.ServiceError | null, response: " |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 358 | "$OUTPUT$) => void): grpc.ClientUnaryCall;\n"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 359 | auto meta_data = GenerateMetaData() + ", "; |
| 360 | auto options = GenerateOptions() + ", "; |
| 361 | printer->Print(vars, (main + callback).c_str()); |
| 362 | printer->Print(vars, (main + meta_data + callback).c_str()); |
| 363 | printer->Print(vars, (main + meta_data + options + callback).c_str()); |
| 364 | } |
| 365 | |
| 366 | void GenerateClientWriteStreamInterface( |
| 367 | grpc_generator::Printer *printer, |
| 368 | std::map<grpc::string, grpc::string> *dictonary) { |
| 369 | auto vars = *dictonary; |
| 370 | grpc::string main = "$ISPUBLIC$$MethodName$("; |
| 371 | grpc::string callback = |
| 372 | "callback: (error: grpc.ServiceError | null, response: " |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 373 | "$INPUT$) => void): " |
| 374 | "grpc.ClientWritableStream<$OUTPUT$>;\n"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 375 | auto meta_data = GenerateMetaData() + ", "; |
| 376 | auto options = GenerateOptions() + ", "; |
| 377 | printer->Print(vars, (main + callback).c_str()); |
| 378 | printer->Print(vars, (main + meta_data + callback).c_str()); |
| 379 | printer->Print(vars, (main + options + callback).c_str()); |
| 380 | printer->Print(vars, (main + meta_data + options + callback).c_str()); |
| 381 | } |
| 382 | |
| 383 | void GenerateClientReadableStreamInterface( |
| 384 | grpc_generator::Printer *printer, |
| 385 | std::map<grpc::string, grpc::string> *dictonary) { |
| 386 | auto vars = *dictonary; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 387 | grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, "; |
| 388 | grpc::string end_function = "): grpc.ClientReadableStream<$OUTPUT$>;\n"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 389 | auto meta_data = GenerateMetaData(); |
| 390 | auto options = GenerateOptions(); |
| 391 | printer->Print(vars, (main + meta_data + end_function).c_str()); |
| 392 | printer->Print(vars, (main + options + end_function).c_str()); |
| 393 | } |
| 394 | |
| 395 | void GenerateDepluxStreamInterface( |
| 396 | grpc_generator::Printer *printer, |
| 397 | std::map<grpc::string, grpc::string> *dictonary) { |
| 398 | auto vars = *dictonary; |
| 399 | grpc::string main = "$ISPUBLIC$$MethodName$("; |
| 400 | grpc::string end_function = |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 401 | "): grpc.ClientDuplexStream<$INPUT$, $OUTPUT$>;\n"; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 402 | auto meta_data = GenerateMetaData(); |
| 403 | auto options = GenerateOptions(); |
| 404 | printer->Print(vars, (main + end_function).c_str()); |
| 405 | printer->Print(vars, (main + options + end_function).c_str()); |
| 406 | printer->Print(vars, (main + meta_data + |
| 407 | ", options?: Partial<grpc.CallOptions>" + end_function) |
| 408 | .c_str()); |
| 409 | } |
| 410 | |
| 411 | void GenerateClientInterface(const grpc_generator::Service *service, |
| 412 | grpc_generator::Printer *printer, |
| 413 | std::map<grpc::string, grpc::string> *dictonary) { |
| 414 | auto vars = *dictonary; |
| 415 | printer->Print(vars, "export interface I$ServiceName$Client {\n"); |
| 416 | printer->Indent(); |
| 417 | for (auto it = 0; it < service->method_count(); it++) { |
| 418 | auto method = service->method(it); |
| 419 | vars["MethodName"] = method->name(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 420 | vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(), |
| 421 | method->get_output_type_name(), false); |
| 422 | vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(), |
| 423 | method->get_input_type_name(), false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 424 | vars["ISPUBLIC"] = ""; |
| 425 | |
| 426 | if (method->NoStreaming()) { |
| 427 | GenerateUnaryClientInterface(printer, &vars); |
| 428 | continue; |
| 429 | } |
| 430 | if (method->BidiStreaming()) { |
| 431 | GenerateDepluxStreamInterface(printer, &vars); |
| 432 | continue; |
| 433 | } |
| 434 | |
| 435 | if (method->ClientStreaming()) { |
| 436 | GenerateClientWriteStreamInterface(printer, &vars); |
| 437 | continue; |
| 438 | } |
| 439 | |
| 440 | if (method->ServerStreaming()) { |
| 441 | GenerateClientReadableStreamInterface(printer, &vars); |
| 442 | continue; |
| 443 | } |
| 444 | } |
| 445 | printer->Outdent(); |
| 446 | printer->Print("}\n"); |
| 447 | } |
| 448 | |
| 449 | void GenerateClientClassInterface( |
| 450 | const grpc_generator::Service *service, grpc_generator::Printer *printer, |
| 451 | std::map<grpc::string, grpc::string> *dictonary) { |
| 452 | auto vars = *dictonary; |
| 453 | printer->Print(vars, |
| 454 | "export class $ServiceName$Client extends grpc.Client " |
| 455 | "implements I$ServiceName$Client {\n"); |
| 456 | printer->Indent(); |
| 457 | printer->Print( |
| 458 | "constructor(address: string, credentials: grpc.ChannelCredentials, " |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 459 | "options?: object);\n"); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 460 | for (auto it = 0; it < service->method_count(); it++) { |
| 461 | auto method = service->method(it); |
| 462 | vars["MethodName"] = method->name(); |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 463 | vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(), |
| 464 | method->get_output_type_name(), false); |
| 465 | vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(), |
| 466 | method->get_input_type_name(), false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 467 | vars["ISPUBLIC"] = "public "; |
| 468 | if (method->NoStreaming()) { |
| 469 | GenerateUnaryClientInterface(printer, &vars); |
| 470 | continue; |
| 471 | } |
| 472 | if (method->BidiStreaming()) { |
| 473 | GenerateDepluxStreamInterface(printer, &vars); |
| 474 | continue; |
| 475 | } |
| 476 | |
| 477 | if (method->ClientStreaming()) { |
| 478 | GenerateClientWriteStreamInterface(printer, &vars); |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | if (method->ServerStreaming()) { |
| 483 | GenerateClientReadableStreamInterface(printer, &vars); |
| 484 | continue; |
| 485 | } |
| 486 | } |
| 487 | printer->Outdent(); |
| 488 | printer->Print("}\n"); |
| 489 | } |
| 490 | |
| 491 | grpc::string GenerateInterface(grpc_generator::File *file, |
| 492 | const grpc_generator::Service *service, |
| 493 | const grpc::string &filename) { |
| 494 | grpc::string output; |
| 495 | |
| 496 | std::set<grpc::string> generated_functions; |
| 497 | std::map<grpc::string, grpc::string> vars; |
| 498 | |
| 499 | vars["PATH"] = file->package(); |
| 500 | |
| 501 | if (!file->package().empty()) { vars["PATH"].append("."); } |
| 502 | |
| 503 | vars["ServiceName"] = service->name(); |
| 504 | vars["FBSFile"] = service->name() + "_fbs"; |
| 505 | vars["Filename"] = filename; |
| 506 | auto printer = file->CreatePrinter(&output); |
| 507 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 508 | GenerateImports(service, &*printer, &vars, false); |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 509 | GenerateMainInterface(service, &*printer, &vars); |
| 510 | printer->Print("\n"); |
| 511 | GenerateClientInterface(service, &*printer, &vars); |
| 512 | printer->Print("\n"); |
| 513 | GenerateClientClassInterface(service, &*printer, &vars); |
| 514 | return output; |
| 515 | } |
| 516 | } // namespace grpc_ts_generator |