diff --git a/CMakeLists.txt b/CMakeLists.txt index c42349e..8990723 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,9 +5,9 @@ project(tars2node) set(CMAKE_VERBOSE_MAKEFILE off) # version -set(TARS_VERSION "1.2.0") +set(TARS_VERSION "2.4.5") set(PARSER_VERSION "${TARS_VERSION}") -set(GENERATOR_VERSION "20200315") +set(GENERATOR_VERSION "20200707") # namespace set(IDL_NAMESPACE Tars) @@ -79,4 +79,4 @@ aux_source_directory(${PROJECT_SOURCE_DIR}/third_partly/tarsgrammar DIR_SRCS) aux_source_directory(${PROJECT_SOURCE_DIR}/third_partly/tarsparse DIR_SRCS) aux_source_directory(${PROJECT_SOURCE_DIR}/third_partly/util/src DIR_SRCS) aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRCS) -add_executable(${PROJECT_NAME} ${DIR_SRCS}) \ No newline at end of file +add_executable(${PROJECT_NAME} ${DIR_SRCS}) diff --git a/bin/linux/tars2node b/bin/linux/tars2node index 4b84327..55d08b8 100755 Binary files a/bin/linux/tars2node and b/bin/linux/tars2node differ diff --git a/bin/mac/tars2node b/bin/mac/tars2node index 01f68fa..af134a3 100755 Binary files a/bin/mac/tars2node and b/bin/mac/tars2node differ diff --git a/src/code_generator.h b/src/code_generator.h index f0b0060..3d90486 100644 --- a/src/code_generator.h +++ b/src/code_generator.h @@ -60,6 +60,10 @@ #define PROTOCOL_COMPLEX PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "COMPLEX") #define PROTOCOL_VAR TO_LOWER_STRING(PROTOCOL_NAME) +#define PROTOCOL_VJ(space, protocol, type) \ + space + "Stream." + protocol + "." + type + "_VERSION" +#define PROTOCOL_JSON PROTOCOL_VJ(IDL_NAMESPACE_STR, PROTOCOL_NAME, "JSON") + #define DISABLE_ESLINT "/* eslint-disable */" #define DISABLE_TSLINT "/* tslint:disable */" @@ -85,17 +89,17 @@ public: _bDTS(false), _iOptimizeLevel(O0) {} - void createFile(const string& file, const bool bEntry = true); + void createFile(const string &file, const bool bEntry = true); - void setRpcPath(const string& sPath) { _sRpcPath = sPath; } + void setRpcPath(const string & sPath) { _sRpcPath = sPath; } - void setStreamPath(const string& sPath) { _sStreamPath = sPath; } + void setStreamPath(const string & sPath) { _sStreamPath = sPath; } void setEnableClient(bool bEnable) { _bClient = bEnable; } void setEnableServer(bool bEnable) { _bServer = bEnable; } - void setTargetPath(const string& sPath) { _sToPath = sPath + "/"; } + void setTargetPath(const string & sPath) { _sToPath = sPath + "/"; } void setRecursive(bool bEnable) { _bRecursive = bEnable; } @@ -131,7 +135,7 @@ private: string sName; }; - struct ImportFile + struct ImportFile { string sFile; string sModule; @@ -153,6 +157,8 @@ private: private: string toFunctionName(const TypeIdPtr & pPtr, const string &sAction); + string toObjectString(const TypeIdPtr &pPtr); + string getDataType(const TypePtr& pPtr, const bool &bCastEnumAsAny = false); string getClassName(const TypePtr& pPtr); @@ -281,7 +287,7 @@ private: string getRealFileInfo(const string & sPath); - string printHeaderRemark(const string &sTypeName); + string printHeaderRemark(const string & sTypeName); private: string _sRpcPath; @@ -317,4 +323,4 @@ private: int _iOptimizeLevel; }; -#endif +#endif \ No newline at end of file diff --git a/src/gen_server.cpp b/src/gen_server.cpp index 6825787..a1d296b 100644 --- a/src/gen_server.cpp +++ b/src/gen_server.cpp @@ -103,6 +103,30 @@ string CodeGenerator::generateAsync(const NamespacePtr &nPtr, const InterfacePtr str << endl; str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl; DEL_TAB; + + //// ========= 增加对 JSON_VERSION 支持 + str << TAB << "} else if (this.getRequestVersion() === " << PROTOCOL_JSON << ") {" << endl; + + INC_TAB; + str << TAB << "var _data_ = {};" << endl; + if (oPtr->getReturnPtr()->getTypePtr()) + { + str << TAB << "_data_[\"tars_ret\"] = _ret;" << endl; + } + for (size_t i = 0; i < vParamDecl.size(); i++) + { + if (!vParamDecl[i]->isOut()) + continue; + + str << TAB << "_data_[\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\"] = " << vParamDecl[i]->getTypeIdPtr()->getId() + << ".toObject ? " << vParamDecl[i]->getTypeIdPtr()->getId() << ".toObject() : " + << vParamDecl[i]->getTypeIdPtr()->getId() << ";" << endl; + } + str << endl; + str << TAB << " this.doResponse(new TarsStream.BinBuffer(Buffer.from(JSON.stringify(_data_))));" << endl; + DEL_TAB; + //// ========= + str << TAB << "} else {" << endl; INC_TAB; @@ -178,6 +202,30 @@ string CodeGenerator::generateDispatch(const NamespacePtr &nPtr, const Interface dstr << ");" << endl; } DEL_TAB; + + //// ========= 增加对 JSON_VERSION 支持 + dstr << TAB << "} else if (current.getRequestVersion() === " << PROTOCOL_JSON << ") {" << endl; + INC_TAB; + dstr << TAB << "var _data_ = JSON.parse(binBuffer.toNodeBuffer());" << endl; + + for (size_t i = 0; i < vParamDecl.size(); i++) + { + if (vParamDecl[i]->isOut()) + { + dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() + << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId() << " || " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true) + << ";" << endl; + } + else + { + dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() + << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId() + << ";" << endl; + } + } + DEL_TAB; + //// ========= + dstr << TAB << "} else {" << endl; INC_TAB; diff --git a/src/gen_server_ts.cpp b/src/gen_server_ts.cpp index 8709f3a..62d7bf6 100644 --- a/src/gen_server_ts.cpp +++ b/src/gen_server_ts.cpp @@ -1,4 +1,4 @@ -/** +/** * Tencent is pleased to support the open source community by making Tars available. * * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. @@ -21,21 +21,25 @@ string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const Inte ostringstream str; string sParams = ""; + int rspNum = 0; if (oPtr->getReturnPtr()->getTypePtr()) { sParams += "_ret: " + getTsType(oPtr->getReturnPtr()->getTypePtr()); // push the symbol into dependent list getDataType(oPtr->getReturnPtr()->getTypePtr()); + ++rspNum; } - vector & vParamDecl = oPtr->getAllParamDeclPtr(); + vector &vParamDecl = oPtr->getAllParamDeclPtr(); for (size_t i = 0; i < vParamDecl.size(); i++) { - if (!vParamDecl[i]->isOut()) continue; + if (!vParamDecl[i]->isOut()) + continue; - sParams += (sParams.empty() ? "": ", ") + vParamDecl[i]->getTypeIdPtr()->getId(); + sParams += (sParams.empty() ? "" : ", ") + vParamDecl[i]->getTypeIdPtr()->getId(); sParams += ": " + getTsType(vParamDecl[i]->getTypeIdPtr()->getTypePtr()); + ++rspNum; } str << TAB << "protected static __" << oPtr->getId() << "_responser(this: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent, " << sParams << ") {" << endl; @@ -62,7 +66,8 @@ string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const Inte } for (size_t i = 0; i < vParamDecl.size(); i++) { - if (!vParamDecl[i]->isOut()) continue; + if (!vParamDecl[i]->isOut()) + continue; str << TAB << PROTOCOL_VAR << "." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\", " << vParamDecl[i]->getTypeIdPtr()->getId() @@ -71,6 +76,42 @@ string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const Inte str << endl; str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl; DEL_TAB; + + //// ========= 增加对 JSON_VERSION 支持 + str << TAB << "} else if (this.getRequestVersion() === " << PROTOCOL_JSON << ") {" << endl; + + INC_TAB; + str << TAB << "const _data_ = {" << endl; + INC_TAB; + if (oPtr->getReturnPtr()->getTypePtr()) + { + str << TAB << "\"tars_ret\": _ret"; + --rspNum; + if (rspNum > 0) + { + str << ", " << endl; + } + } + for (size_t i = 0; i < vParamDecl.size(); i++) + { + if (!vParamDecl[i]->isOut()) + continue; + + str << TAB << "\"" << vParamDecl[i]->getTypeIdPtr()->getId() << "\": " << toObjectString(vParamDecl[i]->getTypeIdPtr()); + --rspNum; + if (rspNum > 0) + { + str << ", " << endl; + } + } + DEL_TAB; + str << endl; + str << TAB << "};" << endl; + str << endl; + str << TAB << "this.doResponse(new TarsStream.BinBuffer(Buffer.from(JSON.stringify(_data_))));" << endl; + DEL_TAB; + //// ========= + str << TAB << "} else {" << endl; INC_TAB; @@ -82,7 +123,8 @@ string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const Inte } for (size_t i = 0; i < vParamDecl.size(); i++) { - if (!vParamDecl[i]->isOut()) continue; + if (!vParamDecl[i]->isOut()) + continue; str << TAB << "os." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(" << (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId() @@ -148,6 +190,30 @@ string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const I dstr << ");" << endl; } DEL_TAB; + + //// ========= 增加对 JSON_VERSION 支持 + dstr << TAB << "} else if (current.getRequestVersion() === " << PROTOCOL_JSON << ") {" << endl; + INC_TAB; + dstr << TAB << "const _data_ = JSON.parse(binBuffer.toNodeBuffer().toString());" << endl; + + for (size_t i = 0; i < vParamDecl.size(); i++) + { + if (vParamDecl[i]->isOut()) + { + dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() + << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId() << " || " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true) + << ";" << endl; + } + else + { + dstr << TAB << vParamDecl[i]->getTypeIdPtr()->getId() + << " = _data_." << vParamDecl[i]->getTypeIdPtr()->getId() + << ";" << endl; + } + } + DEL_TAB; + //// ========= + dstr << TAB << "} else {" << endl; INC_TAB; diff --git a/src/idl_util.cpp b/src/idl_util.cpp index 4aaa033..b29905a 100644 --- a/src/idl_util.cpp +++ b/src/idl_util.cpp @@ -1,4 +1,4 @@ -/** +/** * Tencent is pleased to support the open source community by making Tars available. * * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. @@ -68,6 +68,30 @@ string CodeGenerator::toFunctionName(const TypeIdPtr& pPtr, const string& sActio return ""; } +string CodeGenerator::toObjectString(const TypeIdPtr &pPtr) +{ + string nameId = pPtr->getId(); + VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr()); + if (vPtr) + { + return nameId + ".toObject()"; + } + + StructPtr sPtr = StructPtr::dynamicCast(pPtr->getTypePtr()); + if (sPtr) + { + return nameId + ".toObject()"; + } + + MapPtr mPtr = MapPtr::dynamicCast(pPtr->getTypePtr()); + if (mPtr) + { + return nameId + ".toObject()"; + } + + return nameId; +} + string CodeGenerator::representArgument(const TypePtr& pPtr) const { BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr); @@ -452,4 +476,4 @@ string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, } return sDefault; -} +} \ No newline at end of file