gen_server_dts.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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::generateDTSServer(const NamespacePtr &nPtr, const InterfacePtr &pPtr)
  18. {
  19. vector<OperationPtr> &vOperation = pPtr->getAllOperationPtr();
  20. ostringstream str;
  21. //class
  22. INC_TAB;
  23. str << TAB << "class " << pPtr->getId() << "Imp {" << endl;
  24. INC_TAB;
  25. str << TAB << "initialize(): PromiseLike<any> | void;" << endl;
  26. str << TAB << "protected onDispatch(current: " << IDL_NAMESPACE_STR << "Rpc." << IDL_TYPE << "Current, funcName: string, binBuffer: " << IDL_NAMESPACE_STR << "Stream.BinBuffer): number" << endl;
  27. for (size_t i = 0; i < vOperation.size(); i++)
  28. {
  29. OperationPtr &oPtr = vOperation[i];
  30. str << TAB << oPtr->getId() << "(current: " << pPtr->getId() << "Imp." << oPtr->getId() << "Current";
  31. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  32. for (size_t j = 0; j < vParamDecl.size(); j++)
  33. {
  34. str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr());
  35. }
  36. str << "): any;" << endl;
  37. }
  38. DEL_TAB;
  39. str << TAB << "}" << endl;
  40. // Additional namespace
  41. str << TAB << "namespace " << pPtr->getId() << "Imp {" << endl;
  42. INC_TAB;
  43. for (size_t i = 0; i < vOperation.size(); i++)
  44. {
  45. OperationPtr &oPtr = vOperation[i];
  46. str << TAB << "interface " << oPtr->getId() << "Current extends " << IDL_NAMESPACE_STR << "Rpc." << IDL_TYPE << "Current {" <<endl;
  47. INC_TAB;
  48. str << TAB;
  49. if (oPtr->getReturnPtr()->getTypePtr())
  50. {
  51. str << "sendResponse(ret: " << getTsType(oPtr->getReturnPtr()->getTypePtr());
  52. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  53. for (size_t j = 0; j < vParamDecl.size(); j++)
  54. {
  55. if(!vParamDecl[j]->isOut()) {
  56. continue;
  57. }
  58. str << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr()) ;
  59. }
  60. str << "): void;" << endl;
  61. }
  62. else
  63. {
  64. str << "sendResponse(): void;" << endl;
  65. }
  66. DEL_TAB;
  67. str << TAB << "}" << endl;
  68. }
  69. DEL_TAB;
  70. str << TAB << "}" << endl;
  71. DEL_TAB;
  72. return str.str();
  73. }
  74. string CodeGenerator::generateDTSServer(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedRpc)
  75. {
  76. vector<InterfacePtr> &is = pPtr->getAllInterfacePtr();
  77. ostringstream str;
  78. for (size_t i = 0; i < is.size(); i++)
  79. {
  80. str << generateDTSServer(pPtr, is[i]) << endl;
  81. }
  82. if (is.size() != 0)
  83. {
  84. bNeedRpc = true;
  85. bNeedStream = true;
  86. }
  87. return str.str();
  88. }
  89. void CodeGenerator::generateDTSServer(const ContextPtr &pPtr)
  90. {
  91. vector<NamespacePtr> namespaces = pPtr->getNamespaces();
  92. // generate server classes with encoders and decoders
  93. ostringstream estr;
  94. bool bNeedStream = false;
  95. bool bNeedRpc = false;
  96. for(size_t i = 0; i < namespaces.size(); i++)
  97. {
  98. ostringstream kstr;
  99. kstr << generateDTS(namespaces[i], bNeedStream);
  100. kstr << generateDTSServer(namespaces[i], bNeedStream, bNeedRpc);
  101. estr << generateDTS(namespaces[i], kstr.str());
  102. }
  103. if(estr.str().empty())
  104. {
  105. return;
  106. }
  107. // generate module imports
  108. ostringstream ostr;
  109. if (bNeedStream)
  110. {
  111. ostr << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl;
  112. }
  113. if (bNeedRpc)
  114. {
  115. ostr << "import * as " << IDL_NAMESPACE_STR << "Rpc from \"" << _sRpcPath << "\";" << endl;
  116. }
  117. for (map<string, ImportFile>::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++)
  118. {
  119. if (it->second.sModule.empty()) continue;
  120. if (estr.str().find(it->second.sModule + ".") == string::npos) continue;
  121. ostr << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl;
  122. }
  123. ostringstream str;
  124. str << printHeaderRemark("Server");
  125. str << DISABLE_TSLINT << endl;
  126. str << DISABLE_ESLINT << endl;
  127. str << endl;
  128. str << ostr.str() << endl;
  129. str << estr.str() << endl;
  130. string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(pPtr->getFileName())) + ".d.ts";
  131. TC_File::makeDirRecursive(_sToPath);
  132. makeUTF8File(sFileName, str.str());
  133. }