gen_server_ts.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "code_generator.h"
  17. string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr)
  18. {
  19. ostringstream str;
  20. string sParams = "";
  21. int rspNum = 0;
  22. if (oPtr->getReturnPtr()->getTypePtr())
  23. {
  24. sParams += "_ret: " + getTsType(oPtr->getReturnPtr()->getTypePtr());
  25. // push the symbol into dependent list
  26. getDataType(oPtr->getReturnPtr()->getTypePtr());
  27. ++rspNum;
  28. }
  29. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  30. for (size_t i = 0; i < vParamDecl.size(); i++)
  31. {
  32. if (!vParamDecl[i]->isOut())
  33. continue;
  34. sParams += (sParams.empty() ? "" : ", ") + vParamDecl[i]->getTypeIdPtr()->getId();
  35. sParams += ": " + getTsType(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
  36. ++rspNum;
  37. }
  38. str << TAB << "protected static __" << oPtr->getId() << "_responser(this: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent, " << sParams << ") {" << endl;
  39. INC_TAB;
  40. if (sParams.empty())
  41. {
  42. str << TAB << "this.doResponse(new " << IDL_NAMESPACE_STR << "Stream.BinBuffer());" << endl;
  43. DEL_TAB;
  44. str << TAB << "}" << endl;
  45. return str.str();
  46. }
  47. str << TAB << "if (this.getRequestVersion() === " << PROTOCOL_SIMPLE << " || this.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl;
  48. INC_TAB;
  49. str << TAB << "const " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl;
  50. str << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = this.getRequestVersion();" << endl;
  51. if (oPtr->getReturnPtr()->getTypePtr())
  52. {
  53. str << TAB << PROTOCOL_VAR << "." << toFunctionName(oPtr->getReturnPtr(), "write") << "(\"\", _ret"
  54. << representArgument(oPtr->getReturnPtr()->getTypePtr()) << ");" << endl;
  55. }
  56. for (size_t i = 0; i < vParamDecl.size(); i++)
  57. {
  58. if (!vParamDecl[i]->isOut())
  59. continue;
  60. str << TAB << PROTOCOL_VAR << "." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(\""
  61. << vParamDecl[i]->getTypeIdPtr()->getId() << "\", " << vParamDecl[i]->getTypeIdPtr()->getId()
  62. << representArgument(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << ");" << endl;
  63. }
  64. str << endl;
  65. str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
  66. DEL_TAB;
  67. //// ========= 增加对 JSON_VERSION 支持
  68. str << TAB << "} else if (this.getRequestVersion() === " << PROTOCOL_JSON << ") {" << endl;
  69. INC_TAB;
  70. str << TAB << "const _data_ = {" << endl;
  71. INC_TAB;
  72. if (oPtr->getReturnPtr()->getTypePtr())
  73. {
  74. str << TAB << "\"tars_ret\": _ret";
  75. --rspNum;
  76. if (rspNum > 0)
  77. {
  78. str << ", " << endl;
  79. }
  80. }
  81. for (size_t i = 0; i < vParamDecl.size(); i++)
  82. {
  83. if (!vParamDecl[i]->isOut())
  84. continue;
  85. str << TAB << "\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\": " << toObjectString(vParamDecl[i]->getTypeIdPtr());
  86. --rspNum;
  87. if (rspNum > 0)
  88. {
  89. str << ", " << endl;
  90. }
  91. }
  92. DEL_TAB;
  93. str << endl;
  94. str << TAB << "};" << endl;
  95. str << endl;
  96. str << TAB << "this.doResponse(new TarsStream.BinBuffer(Buffer.from(JSON.stringify(_data_))));" << endl;
  97. DEL_TAB;
  98. //// =========
  99. str << TAB << "} else {" << endl;
  100. INC_TAB;
  101. str << TAB << "const os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl;
  102. if (oPtr->getReturnPtr()->getTypePtr())
  103. {
  104. str << TAB << "os." << toFunctionName(oPtr->getReturnPtr(), "write") << "(0, _ret"
  105. << representArgument(oPtr->getReturnPtr()->getTypePtr()) << ");" << endl;
  106. }
  107. for (size_t i = 0; i < vParamDecl.size(); i++)
  108. {
  109. if (!vParamDecl[i]->isOut())
  110. continue;
  111. str << TAB << "os." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "("
  112. << (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId()
  113. << representArgument(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << ");" << endl;
  114. }
  115. str << endl;
  116. str << TAB << "this.doResponse(os.getBinBuffer());" << endl;
  117. DEL_TAB;
  118. str << TAB << "}" << endl;
  119. DEL_TAB;
  120. str << TAB << "}" << endl;
  121. return str.str();
  122. }
  123. string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const InterfacePtr &pPtr, const OperationPtr &oPtr)
  124. {
  125. ostringstream str;
  126. vector<ParamDeclPtr> & vParamDecl = oPtr->getAllParamDeclPtr();
  127. str << TAB << "protected __" << oPtr->getId() << "(current: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent";
  128. if (vParamDecl.size() != 0) str << ", binBuffer: " << IDL_NAMESPACE_STR << "Stream.BinBuffer";
  129. str << ") {" << endl;
  130. INC_TAB;
  131. ostringstream dstr;
  132. for (size_t i = 0; i < vParamDecl.size(); i++)
  133. {
  134. dstr << TAB << "let " << vParamDecl[i]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) << ";" << endl;
  135. }
  136. if (vParamDecl.size() != 0)
  137. {
  138. dstr << endl;
  139. }
  140. dstr << TAB << "if (current.getRequestVersion() === " << PROTOCOL_SIMPLE << " || current.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl;
  141. INC_TAB;
  142. dstr << TAB << "const " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl;
  143. dstr << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = current.getRequestVersion();" << endl;
  144. dstr << TAB << PROTOCOL_VAR << ".decode(binBuffer);" << endl;
  145. for (size_t i = 0; i < vParamDecl.size(); i++)
  146. {
  147. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  148. << " = " << PROTOCOL_VAR << "." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "read")
  149. << "(\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\"";
  150. if (!isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) && !isBinBuffer(vParamDecl[i]->getTypeIdPtr()->getTypePtr()))
  151. {
  152. dstr << ", " << getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr(), true);
  153. }
  154. if (vParamDecl[i]->isOut())
  155. {
  156. dstr << ", " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true, true)
  157. << representArgument(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
  158. }
  159. dstr << ");" << endl;
  160. }
  161. DEL_TAB;
  162. //// ========= 增加对 JSON_VERSION 支持
  163. dstr << TAB << "} else if (current.getRequestVersion() === " << PROTOCOL_JSON << ") {" << endl;
  164. INC_TAB;
  165. dstr << TAB << "const _data_ = JSON.parse(binBuffer.toNodeBuffer().toString());" << endl;
  166. for (size_t i = 0; i < vParamDecl.size(); i++)
  167. {
  168. if (vParamDecl[i]->isOut())
  169. {
  170. StructPtr sPtr = StructPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
  171. if (sPtr)
  172. {
  173. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  174. << " = " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true)
  175. << ";" << endl;
  176. dstr << TAB << "_data_." << vParamDecl[i]->getTypeIdPtr()->getId() << " ? " << vParamDecl[i]->getTypeIdPtr()->getId() << ".readFromObject("
  177. << "_data_." << vParamDecl[i]->getTypeIdPtr()->getId() << ") : " << vParamDecl[i]->getTypeIdPtr()->getId()
  178. << ";" << endl;
  179. }
  180. else
  181. {
  182. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  183. << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId() << " || " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true)
  184. << ";" << endl;
  185. }
  186. }
  187. else
  188. {
  189. StructPtr sPtr = StructPtr::dynamicCast(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
  190. if (sPtr)
  191. {
  192. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  193. << " = " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true)
  194. << ";" << endl;
  195. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() << ".readFromObject("
  196. << "_data_." << vParamDecl[i]->getTypeIdPtr()->getId() << ")"
  197. << ";" << endl;
  198. }
  199. else
  200. {
  201. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  202. << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId()
  203. << ";" << endl;
  204. }
  205. }
  206. }
  207. DEL_TAB;
  208. //// =========
  209. dstr << TAB << "} else {" << endl;
  210. INC_TAB;
  211. dstr << TAB << "const is = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "InputStream(binBuffer);" << endl;
  212. string sParams = "";
  213. for (size_t i = 0; i < vParamDecl.size(); i++)
  214. {
  215. sParams += ", " + vParamDecl[i]->getTypeIdPtr()->getId();
  216. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  217. << " = is." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "read") << "("
  218. << (i + 1) << ", " << (vParamDecl[i]->isOut() ? "false" : "true") << ", ";
  219. if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr()))
  220. {
  221. dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId(), true, true)
  222. << representArgument(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
  223. }
  224. else
  225. {
  226. dstr << getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr(), true);
  227. }
  228. dstr << ");" << endl;
  229. }
  230. DEL_TAB;
  231. dstr << TAB << "}" << endl << endl;
  232. if (!sParams.empty())
  233. {
  234. str << dstr.str();
  235. }
  236. str << TAB << "current.sendResponse = " << pPtr->getId() << "Imp.__" << oPtr->getId() << "_responser;" << endl << endl;
  237. str << TAB << "this." << oPtr->getId() << "(current" << sParams << ");" << endl << endl;
  238. str << TAB << "return " << IDL_NAMESPACE_STR << "Rpc.error.SUCCESS;" << endl;
  239. DEL_TAB;
  240. str << TAB << "}" << endl;
  241. return str.str();
  242. }
  243. string CodeGenerator::generateTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc, bool &bNeedAssert)
  244. {
  245. ostringstream str;
  246. vector<InterfacePtr> & is = pPtr->getAllInterfacePtr();
  247. for (size_t i = 0; i < is.size(); i++)
  248. {
  249. str << generateTSServer(is[i], pPtr) << endl;
  250. }
  251. if (is.size() != 0)
  252. {
  253. bNeedRpc = true;
  254. bNeedStream = true;
  255. bNeedAssert = true;
  256. }
  257. return str.str();
  258. }
  259. string CodeGenerator::generateTSServer(const InterfacePtr &pPtr, const NamespacePtr &nPtr)
  260. {
  261. ostringstream str;
  262. vector<OperationPtr> & vOperation = pPtr->getAllOperationPtr();
  263. // generate the implementation class
  264. str << TAB << "export abstract class " << pPtr->getId() << "Imp { " << endl;
  265. INC_TAB;
  266. str << TAB << "_name!: string" << endl;
  267. str << TAB << "_worker!: any" << endl << endl;
  268. // generate the initialize function
  269. str << TAB << "initialize(): PromiseLike<any> | void {}" << endl << endl;
  270. // generate the dispatch function
  271. str << TAB << "onDispatch(current: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent, funcName: string, binBuffer: " << IDL_NAMESPACE_STR << "Stream.BinBuffer) { " << endl;
  272. INC_TAB;
  273. str << TAB << "if (\"__\" + funcName in this) {" << endl;
  274. INC_TAB;
  275. str << TAB << "return (this as any)[\"__\" + funcName](current, binBuffer);" << endl;
  276. DEL_TAB;
  277. str << TAB << "} else {" << endl;
  278. INC_TAB;
  279. str << TAB << "return " << IDL_NAMESPACE_STR << "Rpc.error.SERVER.FUNC_NOT_FOUND;" << endl;
  280. DEL_TAB;
  281. str << TAB << "}" << endl;
  282. DEL_TAB;
  283. str << TAB << "}" << endl << endl;
  284. // generate the ping function
  285. str << TAB << "__" << TC_Common::lower(IDL_NAMESPACE_STR) << "_ping(current: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent) { " << endl;
  286. INC_TAB;
  287. str << TAB << "const _ret = 0;" << endl;
  288. str << TAB << "if (current.getRequestVersion() === " << PROTOCOL_SIMPLE << " || current.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl;
  289. INC_TAB;
  290. str << TAB << "const " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl;
  291. str << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = current.getRequestVersion();" << endl;
  292. str << TAB << PROTOCOL_VAR << ".writeInt32(\"\", _ret);" << endl << endl;
  293. str << TAB << "current.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
  294. DEL_TAB;
  295. str << TAB << "} else {" << endl;
  296. INC_TAB;
  297. str << TAB << "const os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl;
  298. str << TAB << "os.writeInt32(0, _ret);" << endl << endl;
  299. str << TAB << "current.doResponse(os.getBinBuffer());" << endl;
  300. DEL_TAB;
  301. str << TAB << "}" << endl << endl;
  302. str << TAB << "return " << IDL_NAMESPACE_STR << "Rpc.error.SUCCESS;" << endl;
  303. DEL_TAB;
  304. str << TAB << "}" << endl << endl;
  305. // generate functions
  306. for (size_t i = 0; i < vOperation.size(); i++)
  307. {
  308. const OperationPtr &oPtr = vOperation[i];
  309. // generate function definition
  310. str << TAB << oPtr->getId() << "(current: " << pPtr->getId() << "Imp." << oPtr->getId() << "Current";
  311. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  312. for (size_t j = 0; j < vParamDecl.size(); j++)
  313. {
  314. str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr());
  315. }
  316. str << "): any { " << endl;
  317. INC_TAB;
  318. str << TAB << "assert.fail(\"" << oPtr->getId() << " function not implemented\");" << endl;
  319. DEL_TAB;
  320. str << TAB << "}" << endl << endl;
  321. // generate encoder and decoder
  322. str << generateTSServerAsync(nPtr, pPtr, vOperation[i]) << endl;
  323. str << generateTSServerDispatch(nPtr, pPtr, vOperation[i]) << endl;
  324. }
  325. DEL_TAB;
  326. str << TAB << "}" << endl << endl; // end of class
  327. // generate additional namespaces
  328. str << TAB << "export namespace " << pPtr->getId() << "Imp {" << endl;
  329. INC_TAB;
  330. for (size_t i = 0; i < vOperation.size(); i++)
  331. {
  332. OperationPtr &oPtr = vOperation[i];
  333. str << TAB << "export interface " << oPtr->getId() << "Current extends " << IDL_NAMESPACE_STR << "Rpc." << IDL_TYPE << "Current {" <<endl;
  334. INC_TAB;
  335. str << TAB;
  336. if (oPtr->getReturnPtr()->getTypePtr())
  337. {
  338. str << "sendResponse(ret: " << getTsType(oPtr->getReturnPtr()->getTypePtr());
  339. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  340. for (size_t j = 0; j < vParamDecl.size(); j++)
  341. {
  342. if(!vParamDecl[j]->isOut()) {
  343. continue;
  344. }
  345. str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()) ;
  346. }
  347. str << "): void;" << endl;
  348. }
  349. else
  350. {
  351. str << "sendResponse(): void;" << endl;
  352. }
  353. DEL_TAB;
  354. str << TAB << "}" << endl;
  355. }
  356. DEL_TAB;
  357. str << TAB << "}" << endl;
  358. return str.str();
  359. }
  360. bool CodeGenerator::generateTSServer(const ContextPtr &pPtr)
  361. {
  362. vector<NamespacePtr> namespaces = pPtr->getNamespaces();
  363. // generate server classes with encoders and decoders
  364. ostringstream estr;
  365. bool bNeedStream = false;
  366. bool bNeedRpc = false;
  367. bool bNeedAssert = false;
  368. bool bQuickFunc = false;
  369. for(size_t i = 0; i < namespaces.size(); i++)
  370. {
  371. ostringstream kstr;
  372. kstr << generateTS(namespaces[i], bNeedStream, bNeedAssert, bQuickFunc);
  373. INC_TAB;
  374. kstr << generateTSServer(namespaces[i], bNeedStream, bNeedRpc, bNeedAssert);
  375. DEL_TAB;
  376. estr << generateTS(namespaces[i], kstr.str());
  377. }
  378. if (estr.str().empty())
  379. {
  380. return false;
  381. }
  382. ostringstream str;
  383. // generate the source file
  384. str << printHeaderRemark("Server");
  385. str << DISABLE_TSLINT << endl;
  386. str << DISABLE_ESLINT << endl;
  387. str << endl;
  388. str << "/// <reference types=\"node\" />" << endl;
  389. if (bNeedAssert)
  390. {
  391. str << TAB << "import assert = require(\"assert\");" << endl;
  392. }
  393. if (bNeedStream)
  394. {
  395. str << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl;
  396. }
  397. if (bNeedRpc)
  398. {
  399. str << "import * as " << IDL_NAMESPACE_STR << "Rpc from \"" << _sRpcPath << "\";" << endl;
  400. }
  401. for (map<string, ImportFile>::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++)
  402. {
  403. if (it->second.sModule.empty()) continue;
  404. if (estr.str().find(it->second.sModule + ".") == string::npos) continue;
  405. str << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl;
  406. }
  407. if (bQuickFunc)
  408. {
  409. str << endl;
  410. str << TAB << "const _hasOwnProperty = Object.prototype.hasOwnProperty;" << endl;
  411. }
  412. str << endl << estr.str() << endl;
  413. string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".ts";
  414. TC_File::makeDirRecursive(_sToPath);
  415. makeUTF8File(sFileName, str.str());
  416. return true;
  417. }