gen_server_ts.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  171. << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId() << " || " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true)
  172. << ";" << endl;
  173. }
  174. else
  175. {
  176. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  177. << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId()
  178. << ";" << endl;
  179. }
  180. }
  181. DEL_TAB;
  182. //// =========
  183. dstr << TAB << "} else {" << endl;
  184. INC_TAB;
  185. dstr << TAB << "const is = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "InputStream(binBuffer);" << endl;
  186. string sParams = "";
  187. for (size_t i = 0; i < vParamDecl.size(); i++)
  188. {
  189. sParams += ", " + vParamDecl[i]->getTypeIdPtr()->getId();
  190. dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId()
  191. << " = is." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "read") << "("
  192. << (i + 1) << ", " << (vParamDecl[i]->isOut() ? "false" : "true") << ", ";
  193. if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr()))
  194. {
  195. dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId(), true, true)
  196. << representArgument(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
  197. }
  198. else
  199. {
  200. dstr << getDataType(vParamDecl[i]->getTypeIdPtr()->getTypePtr(), true);
  201. }
  202. dstr << ");" << endl;
  203. }
  204. DEL_TAB;
  205. dstr << TAB << "}" << endl << endl;
  206. if (!sParams.empty())
  207. {
  208. str << dstr.str();
  209. }
  210. str << TAB << "current.sendResponse = " << pPtr->getId() << "Imp.__" << oPtr->getId() << "_responser;" << endl << endl;
  211. str << TAB << "this." << oPtr->getId() << "(current" << sParams << ");" << endl << endl;
  212. str << TAB << "return " << IDL_NAMESPACE_STR << "Rpc.error.SUCCESS;" << endl;
  213. DEL_TAB;
  214. str << TAB << "}" << endl;
  215. return str.str();
  216. }
  217. string CodeGenerator::generateTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc, bool &bNeedAssert)
  218. {
  219. ostringstream str;
  220. vector<InterfacePtr> & is = pPtr->getAllInterfacePtr();
  221. for (size_t i = 0; i < is.size(); i++)
  222. {
  223. str << generateTSServer(is[i], pPtr) << endl;
  224. }
  225. if (is.size() != 0)
  226. {
  227. bNeedRpc = true;
  228. bNeedStream = true;
  229. bNeedAssert = true;
  230. }
  231. return str.str();
  232. }
  233. string CodeGenerator::generateTSServer(const InterfacePtr &pPtr, const NamespacePtr &nPtr)
  234. {
  235. ostringstream str;
  236. vector<OperationPtr> & vOperation = pPtr->getAllOperationPtr();
  237. // generate the implementation class
  238. str << TAB << "export abstract class " << pPtr->getId() << "Imp { " << endl;
  239. INC_TAB;
  240. str << TAB << "_name!: string" << endl;
  241. str << TAB << "_worker!: any" << endl << endl;
  242. // generate the initialize function
  243. str << TAB << "initialize(): PromiseLike<any> | void {}" << endl << endl;
  244. // generate the dispatch function
  245. str << TAB << "onDispatch(current: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent, funcName: string, binBuffer: " << IDL_NAMESPACE_STR << "Stream.BinBuffer) { " << endl;
  246. INC_TAB;
  247. str << TAB << "if (\"__\" + funcName in this) {" << endl;
  248. INC_TAB;
  249. str << TAB << "return (this as any)[\"__\" + funcName](current, binBuffer);" << endl;
  250. DEL_TAB;
  251. str << TAB << "} else {" << endl;
  252. INC_TAB;
  253. str << TAB << "return " << IDL_NAMESPACE_STR << "Rpc.error.SERVER.FUNC_NOT_FOUND;" << endl;
  254. DEL_TAB;
  255. str << TAB << "}" << endl;
  256. DEL_TAB;
  257. str << TAB << "}" << endl << endl;
  258. // generate the ping function
  259. str << TAB << "__" << TC_Common::lower(IDL_NAMESPACE_STR) << "_ping(current: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent) { " << endl;
  260. INC_TAB;
  261. str << TAB << "const _ret = 0;" << endl;
  262. str << TAB << "if (current.getRequestVersion() === " << PROTOCOL_SIMPLE << " || current.getRequestVersion() === " << PROTOCOL_COMPLEX << ") {" << endl;
  263. INC_TAB;
  264. str << TAB << "const " << PROTOCOL_VAR << " = new " << IDL_NAMESPACE_STR << "Stream.UniAttribute();" << endl;
  265. str << TAB << PROTOCOL_VAR << "." << PROTOCOL_VAR << "Version = current.getRequestVersion();" << endl;
  266. str << TAB << PROTOCOL_VAR << ".writeInt32(\"\", _ret);" << endl << endl;
  267. str << TAB << "current.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
  268. DEL_TAB;
  269. str << TAB << "} else {" << endl;
  270. INC_TAB;
  271. str << TAB << "const os = new " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "OutputStream();" << endl;
  272. str << TAB << "os.writeInt32(0, _ret);" << endl << endl;
  273. str << TAB << "current.doResponse(os.getBinBuffer());" << endl;
  274. DEL_TAB;
  275. str << TAB << "}" << endl << endl;
  276. str << TAB << "return " << IDL_NAMESPACE_STR << "Rpc.error.SUCCESS;" << endl;
  277. DEL_TAB;
  278. str << TAB << "}" << endl << endl;
  279. // generate functions
  280. for (size_t i = 0; i < vOperation.size(); i++)
  281. {
  282. const OperationPtr &oPtr = vOperation[i];
  283. // generate function definition
  284. str << TAB << oPtr->getId() << "(current: " << pPtr->getId() << "Imp." << oPtr->getId() << "Current";
  285. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  286. for (size_t j = 0; j < vParamDecl.size(); j++)
  287. {
  288. str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr());
  289. }
  290. str << "): any { " << endl;
  291. INC_TAB;
  292. str << TAB << "assert.fail(\"" << oPtr->getId() << " function not implemented\");" << endl;
  293. DEL_TAB;
  294. str << TAB << "}" << endl << endl;
  295. // generate encoder and decoder
  296. str << generateTSServerAsync(nPtr, pPtr, vOperation[i]) << endl;
  297. str << generateTSServerDispatch(nPtr, pPtr, vOperation[i]) << endl;
  298. }
  299. DEL_TAB;
  300. str << TAB << "}" << endl << endl; // end of class
  301. // generate additional namespaces
  302. str << TAB << "export namespace " << pPtr->getId() << "Imp {" << endl;
  303. INC_TAB;
  304. for (size_t i = 0; i < vOperation.size(); i++)
  305. {
  306. OperationPtr &oPtr = vOperation[i];
  307. str << TAB << "export interface " << oPtr->getId() << "Current extends " << IDL_NAMESPACE_STR << "Rpc." << IDL_TYPE << "Current {" <<endl;
  308. INC_TAB;
  309. str << TAB;
  310. if (oPtr->getReturnPtr()->getTypePtr())
  311. {
  312. str << "sendResponse(ret: " << getTsType(oPtr->getReturnPtr()->getTypePtr());
  313. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  314. for (size_t j = 0; j < vParamDecl.size(); j++)
  315. {
  316. if(!vParamDecl[j]->isOut()) {
  317. continue;
  318. }
  319. str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()) ;
  320. }
  321. str << "): void;" << endl;
  322. }
  323. else
  324. {
  325. str << "sendResponse(): void;" << endl;
  326. }
  327. DEL_TAB;
  328. str << TAB << "}" << endl;
  329. }
  330. DEL_TAB;
  331. str << TAB << "}" << endl;
  332. return str.str();
  333. }
  334. bool CodeGenerator::generateTSServer(const ContextPtr &pPtr)
  335. {
  336. vector<NamespacePtr> namespaces = pPtr->getNamespaces();
  337. // generate server classes with encoders and decoders
  338. ostringstream estr;
  339. bool bNeedStream = false;
  340. bool bNeedRpc = false;
  341. bool bNeedAssert = false;
  342. bool bQuickFunc = false;
  343. for(size_t i = 0; i < namespaces.size(); i++)
  344. {
  345. ostringstream kstr;
  346. kstr << generateTS(namespaces[i], bNeedStream, bNeedAssert, bQuickFunc);
  347. INC_TAB;
  348. kstr << generateTSServer(namespaces[i], bNeedStream, bNeedRpc, bNeedAssert);
  349. DEL_TAB;
  350. estr << generateTS(namespaces[i], kstr.str());
  351. }
  352. if (estr.str().empty())
  353. {
  354. return false;
  355. }
  356. ostringstream str;
  357. // generate the source file
  358. str << printHeaderRemark("Server");
  359. str << DISABLE_TSLINT << endl;
  360. str << DISABLE_ESLINT << endl;
  361. str << endl;
  362. str << "/// <reference types=\"node\" />" << endl;
  363. if (bNeedAssert)
  364. {
  365. str << TAB << "import assert = require(\"assert\");" << endl;
  366. }
  367. if (bNeedStream)
  368. {
  369. str << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl;
  370. }
  371. if (bNeedRpc)
  372. {
  373. str << "import * as " << IDL_NAMESPACE_STR << "Rpc from \"" << _sRpcPath << "\";" << endl;
  374. }
  375. for (map<string, ImportFile>::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++)
  376. {
  377. if (it->second.sModule.empty()) continue;
  378. if (estr.str().find(it->second.sModule + ".") == string::npos) continue;
  379. str << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl;
  380. }
  381. if (bQuickFunc)
  382. {
  383. str << endl;
  384. str << TAB << "const _hasOwnProperty = Object.prototype.hasOwnProperty;" << endl;
  385. }
  386. str << endl << estr.str() << endl;
  387. string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".ts";
  388. TC_File::makeDirRecursive(_sToPath);
  389. makeUTF8File(sFileName, str.str());
  390. return true;
  391. }