gen_server_ts_imp.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. void CodeGenerator::generateTSServerImp(const ContextPtr &cPtr)
  18. {
  19. string sFileName = TC_File::excludeFileExt(_sToPath + TC_File::extractFileName(cPtr->getFileName())) + "Imp.ts";
  20. if (TC_File::isFileExist(sFileName))
  21. {
  22. return ;
  23. }
  24. ostringstream str;
  25. str << printHeaderRemark("Imp");
  26. vector<NamespacePtr> namespaces = cPtr->getNamespaces();
  27. // generate the server implementation class
  28. ostringstream estr;
  29. set<string> setInterface;
  30. for(size_t i = 0; i < namespaces.size(); i++)
  31. {
  32. estr << "export namespace " << namespaces[i]->getId() << " {" << endl;
  33. INC_TAB;
  34. vector<InterfacePtr> & is = namespaces[i]->getAllInterfacePtr();
  35. for (size_t ii = 0; ii < is.size(); ii++)
  36. {
  37. if (setInterface.count(namespaces[i]->getId() + "::" + is[ii]->getId()) != 0)
  38. {
  39. continue;
  40. }
  41. setInterface.insert(namespaces[i]->getId() + "::" + is[ii]->getId());
  42. estr << TAB << "export class " << is[ii]->getId() << "Imp extends base." << namespaces[i]->getId() << "." << is[ii]->getId() << "Imp { " << endl;
  43. INC_TAB;
  44. estr << TAB << "async initialize() {" << endl;
  45. INC_TAB;
  46. estr << TAB << "// TODO: implement initialize" << endl;
  47. DEL_TAB;
  48. estr << TAB << "}" << endl << endl;
  49. vector<OperationPtr> & vOperation = is[ii]->getAllOperationPtr();
  50. for (size_t iii = 0; iii < vOperation.size(); iii++)
  51. {
  52. const OperationPtr &oPtr = vOperation[iii];
  53. // generate function entries
  54. estr << TAB << "async " << oPtr->getId() << "(current: base." << namespaces[i]->getId() << "." << is[ii]->getId() << "Imp." << oPtr->getId() << "Current";
  55. vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
  56. for (size_t j = 0; j < vParamDecl.size(); j++)
  57. {
  58. estr << ", " << vParamDecl[j]->getTypeIdPtr()->getId() << ": " << getTsType(vParamDecl[j]->getTypeIdPtr()->getTypePtr(), true, true);
  59. }
  60. estr << ") { " << endl;
  61. INC_TAB;
  62. estr << TAB << "// TODO: implement " << oPtr->getId() << "" << endl;
  63. DEL_TAB;
  64. estr << TAB << "}" << endl;
  65. if (iii != vOperation.size() - 1) estr << endl;
  66. }
  67. DEL_TAB;
  68. estr << TAB << "}" << endl;
  69. if (ii != is.size() - 1) estr << endl;
  70. }
  71. DEL_TAB;
  72. estr << "}" << endl;
  73. if (i != namespaces.size() - 1) estr << endl;
  74. }
  75. // generate module imports
  76. str << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl;
  77. str << "import * as base from \"./"
  78. << TC_File::excludeFileExt(TC_File::extractFileName(cPtr->getFileName())) << "\";" << endl;
  79. for (map<string, ImportFile>::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++)
  80. {
  81. if (it->second.sModule.empty()) continue;
  82. if (estr.str().find(it->second.sModule + ".") == string::npos) continue;
  83. str << "import * as " << it->second.sModule << " from \"" << TC_File::excludeFileExt(it->second.sFile) << "\";" << endl;
  84. }
  85. str << endl;
  86. str << estr.str();
  87. TC_File::makeDirRecursive(_sToPath);
  88. makeUTF8File(sFileName, str.str());
  89. }