Support JSON_VERSION and update linux & mac bin file

This commit is contained in:
kivenchen 2020-07-07 14:10:53 +08:00
parent bddca26bbd
commit 7a19a430b0
7 changed files with 162 additions and 18 deletions

View File

@ -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)

Binary file not shown.

Binary file not shown.

View File

@ -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 */"
@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);