gen_proxy_dts.cpp 4.9 KB

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