mirror of
https://github.com/tars-node/tars2node.git
synced 2024-03-22 13:10:56 +08:00
Support JSON_VERSION and update linux & mac bin file
This commit is contained in:
parent
bddca26bbd
commit
7a19a430b0
|
@ -5,9 +5,9 @@ project(tars2node)
|
||||||
set(CMAKE_VERBOSE_MAKEFILE off)
|
set(CMAKE_VERBOSE_MAKEFILE off)
|
||||||
|
|
||||||
# version
|
# version
|
||||||
set(TARS_VERSION "1.2.0")
|
set(TARS_VERSION "2.4.5")
|
||||||
set(PARSER_VERSION "${TARS_VERSION}")
|
set(PARSER_VERSION "${TARS_VERSION}")
|
||||||
set(GENERATOR_VERSION "20200315")
|
set(GENERATOR_VERSION "20200707")
|
||||||
|
|
||||||
# namespace
|
# namespace
|
||||||
set(IDL_NAMESPACE Tars)
|
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/tarsparse DIR_SRCS)
|
||||||
aux_source_directory(${PROJECT_SOURCE_DIR}/third_partly/util/src DIR_SRCS)
|
aux_source_directory(${PROJECT_SOURCE_DIR}/third_partly/util/src DIR_SRCS)
|
||||||
aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRCS)
|
aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRCS)
|
||||||
add_executable(${PROJECT_NAME} ${DIR_SRCS})
|
add_executable(${PROJECT_NAME} ${DIR_SRCS})
|
||||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -60,6 +60,10 @@
|
||||||
#define PROTOCOL_COMPLEX PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "COMPLEX")
|
#define PROTOCOL_COMPLEX PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "COMPLEX")
|
||||||
#define PROTOCOL_VAR TO_LOWER_STRING(PROTOCOL_NAME)
|
#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_ESLINT "/* eslint-disable */"
|
||||||
#define DISABLE_TSLINT "/* tslint:disable */"
|
#define DISABLE_TSLINT "/* tslint:disable */"
|
||||||
|
|
||||||
|
@ -85,17 +89,17 @@ public:
|
||||||
_bDTS(false),
|
_bDTS(false),
|
||||||
_iOptimizeLevel(O0) {}
|
_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 setEnableClient(bool bEnable) { _bClient = bEnable; }
|
||||||
|
|
||||||
void setEnableServer(bool bEnable) { _bServer = 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; }
|
void setRecursive(bool bEnable) { _bRecursive = bEnable; }
|
||||||
|
|
||||||
|
@ -131,7 +135,7 @@ private:
|
||||||
string sName;
|
string sName;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ImportFile
|
struct ImportFile
|
||||||
{
|
{
|
||||||
string sFile;
|
string sFile;
|
||||||
string sModule;
|
string sModule;
|
||||||
|
@ -153,6 +157,8 @@ private:
|
||||||
private:
|
private:
|
||||||
string toFunctionName(const TypeIdPtr & pPtr, const string &sAction);
|
string toFunctionName(const TypeIdPtr & pPtr, const string &sAction);
|
||||||
|
|
||||||
|
string toObjectString(const TypeIdPtr &pPtr);
|
||||||
|
|
||||||
string getDataType(const TypePtr& pPtr, const bool &bCastEnumAsAny = false);
|
string getDataType(const TypePtr& pPtr, const bool &bCastEnumAsAny = false);
|
||||||
|
|
||||||
string getClassName(const TypePtr& pPtr);
|
string getClassName(const TypePtr& pPtr);
|
||||||
|
@ -281,7 +287,7 @@ private:
|
||||||
|
|
||||||
string getRealFileInfo(const string & sPath);
|
string getRealFileInfo(const string & sPath);
|
||||||
|
|
||||||
string printHeaderRemark(const string &sTypeName);
|
string printHeaderRemark(const string & sTypeName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
string _sRpcPath;
|
string _sRpcPath;
|
||||||
|
@ -317,4 +323,4 @@ private:
|
||||||
int _iOptimizeLevel;
|
int _iOptimizeLevel;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -103,6 +103,30 @@ string CodeGenerator::generateAsync(const NamespacePtr &nPtr, const InterfacePtr
|
||||||
str << endl;
|
str << endl;
|
||||||
str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
|
str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
|
||||||
DEL_TAB;
|
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;
|
str << TAB << "} else {" << endl;
|
||||||
|
|
||||||
INC_TAB;
|
INC_TAB;
|
||||||
|
@ -178,6 +202,30 @@ string CodeGenerator::generateDispatch(const NamespacePtr &nPtr, const Interface
|
||||||
dstr << ");" << endl;
|
dstr << ");" << endl;
|
||||||
}
|
}
|
||||||
DEL_TAB;
|
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;
|
dstr << TAB << "} else {" << endl;
|
||||||
|
|
||||||
INC_TAB;
|
INC_TAB;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/**
|
/**
|
||||||
* Tencent is pleased to support the open source community by making Tars available.
|
* Tencent is pleased to support the open source community by making Tars available.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
|
* 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;
|
ostringstream str;
|
||||||
|
|
||||||
string sParams = "";
|
string sParams = "";
|
||||||
|
int rspNum = 0;
|
||||||
if (oPtr->getReturnPtr()->getTypePtr())
|
if (oPtr->getReturnPtr()->getTypePtr())
|
||||||
{
|
{
|
||||||
sParams += "_ret: " + getTsType(oPtr->getReturnPtr()->getTypePtr());
|
sParams += "_ret: " + getTsType(oPtr->getReturnPtr()->getTypePtr());
|
||||||
|
|
||||||
// push the symbol into dependent list
|
// push the symbol into dependent list
|
||||||
getDataType(oPtr->getReturnPtr()->getTypePtr());
|
getDataType(oPtr->getReturnPtr()->getTypePtr());
|
||||||
|
++rspNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<ParamDeclPtr> & vParamDecl = oPtr->getAllParamDeclPtr();
|
vector<ParamDeclPtr> &vParamDecl = oPtr->getAllParamDeclPtr();
|
||||||
for (size_t i = 0; i < vParamDecl.size(); i++)
|
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());
|
sParams += ": " + getTsType(vParamDecl[i]->getTypeIdPtr()->getTypePtr());
|
||||||
|
++rspNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
str << TAB << "protected static __" << oPtr->getId() << "_responser(this: " << IDL_NAMESPACE_STR << "Rpc.TarsCurrent, " << sParams << ") {" << endl;
|
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++)
|
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") << "(\""
|
str << TAB << PROTOCOL_VAR << "." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "(\""
|
||||||
<< vParamDecl[i]->getTypeIdPtr()->getId() << "\", " << vParamDecl[i]->getTypeIdPtr()->getId()
|
<< vParamDecl[i]->getTypeIdPtr()->getId() << "\", " << vParamDecl[i]->getTypeIdPtr()->getId()
|
||||||
|
@ -71,6 +76,42 @@ string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const Inte
|
||||||
str << endl;
|
str << endl;
|
||||||
str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
|
str << TAB << "this.doResponse(" << PROTOCOL_VAR << ".encode());" << endl;
|
||||||
DEL_TAB;
|
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;
|
str << TAB << "} else {" << endl;
|
||||||
|
|
||||||
INC_TAB;
|
INC_TAB;
|
||||||
|
@ -82,7 +123,8 @@ string CodeGenerator::generateTSServerAsync(const NamespacePtr &nPtr, const Inte
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < vParamDecl.size(); i++)
|
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") << "("
|
str << TAB << "os." << toFunctionName(vParamDecl[i]->getTypeIdPtr(), "write") << "("
|
||||||
<< (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId()
|
<< (i + 1) << ", " << vParamDecl[i]->getTypeIdPtr()->getId()
|
||||||
|
@ -148,6 +190,30 @@ string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const I
|
||||||
dstr << ");" << endl;
|
dstr << ");" << endl;
|
||||||
}
|
}
|
||||||
DEL_TAB;
|
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;
|
dstr << TAB << "} else {" << endl;
|
||||||
|
|
||||||
INC_TAB;
|
INC_TAB;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/**
|
/**
|
||||||
* Tencent is pleased to support the open source community by making Tars available.
|
* Tencent is pleased to support the open source community by making Tars available.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
|
* 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 "";
|
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
|
string CodeGenerator::representArgument(const TypePtr& pPtr) const
|
||||||
{
|
{
|
||||||
BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr);
|
BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr);
|
||||||
|
@ -452,4 +476,4 @@ string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault,
|
||||||
}
|
}
|
||||||
|
|
||||||
return sDefault;
|
return sDefault;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user