mirror of
https://github.com/tars-node/tars2node.git
synced 2024-03-22 13:10:56 +08:00
version: 20190108
This commit is contained in:
parent
82c75b3fe7
commit
59b01b3d3c
|
@ -7,7 +7,7 @@ set(CMAKE_VERBOSE_MAKEFILE off)
|
|||
# version
|
||||
set(TARS_VERSION "1.1.0")
|
||||
set(PARSER_VERSION "${TARS_VERSION}")
|
||||
set(GENERATOR_VERSION "20180713")
|
||||
set(GENERATOR_VERSION "20190109")
|
||||
|
||||
# namespace
|
||||
set(IDL_NAMESPACE Tars)
|
||||
|
|
|
@ -30,6 +30,7 @@ tars2node [OPTIONS] tarsfile
|
|||
| --dts | 生成时附加 TypeScript 描述文件(.d.ts)。|
|
||||
| --use-string-represent | 当需要保持 <long> 类型的精度时,打开此选项使用 \<string\> 存储 \<long\>。|
|
||||
| --string-binary-encoding | 遇到字符编码问题或需对原始数据进行存取时,打开此选项使用 \<buffer\> 存储 \<string\>。|
|
||||
| --enum-reverse-mappings | 输出代码 \<enum\> 支持枚举值到枚举名的反向映射 。|
|
||||
| --optimize=[0\|s] | 优化输出代码大小,默认为 0(也就是不优化)。|
|
||||
|
||||
## 例子
|
||||
|
|
BIN
build/tars2node
BIN
build/tars2node
Binary file not shown.
|
@ -60,6 +60,9 @@
|
|||
#define PROTOCOL_COMPLEX PROTOCOL_V(IDL_NAMESPACE_STR, PROTOCOL_NAME, "COMPLEX")
|
||||
#define PROTOCOL_VAR TO_LOWER_STRING(PROTOCOL_NAME)
|
||||
|
||||
#define DISABLE_ESLINT "/* eslint-disable */"
|
||||
#define DISABLE_TSLINT "/* tslint:disable */"
|
||||
|
||||
using namespace TC_NAMESPACE;
|
||||
|
||||
class CodeGenerator
|
||||
|
@ -76,6 +79,7 @@ public:
|
|||
_bUseSpecialPath(false),
|
||||
_bUseStringRepresent(false),
|
||||
_bStringBinaryEncoding(false),
|
||||
_bEnumReverseMappings(false),
|
||||
_bMinimalMembers(false),
|
||||
_bDTS(false),
|
||||
_iOptimizeLevel(O0) {}
|
||||
|
@ -100,6 +104,8 @@ public:
|
|||
|
||||
void setStringBinaryEncoding(bool bEnable) { _bStringBinaryEncoding = bEnable; }
|
||||
|
||||
void setEnumReverseMappings(bool bEnable) { _bEnumReverseMappings = bEnable; }
|
||||
|
||||
void setMinimalMembers(bool bEnable) { _bMinimalMembers = bEnable; }
|
||||
|
||||
void setDependent(set<string>& deps) { _depMembers = deps; }
|
||||
|
@ -153,13 +159,13 @@ private:
|
|||
string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool bGlobal);
|
||||
|
||||
private:
|
||||
string generateJS(const StructPtr & pPtr, const string &sNamespace, bool &bNeedAssert);
|
||||
string generateJS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc);
|
||||
|
||||
string generateJS(const ConstPtr &pPtr, const string &sNamespace, bool &bNeedStream);
|
||||
|
||||
string generateJS(const EnumPtr &pPtr, const string &sNamespace);
|
||||
|
||||
string generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert);
|
||||
string generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert, bool &bQuickFunc);
|
||||
|
||||
bool generateJS(const ContextPtr &pPtr);
|
||||
|
||||
|
@ -238,7 +244,9 @@ private:
|
|||
|
||||
string getRealFileInfo(const string & sPath);
|
||||
|
||||
string printHeaderRemark(const string & sTypeName);
|
||||
string printHeaderRemark(const string &sTypeName);
|
||||
|
||||
string printHeaderRemark(const string &sTypeName, const string &sSuffix);
|
||||
|
||||
private:
|
||||
string _sRpcPath;
|
||||
|
@ -259,6 +267,8 @@ private:
|
|||
|
||||
bool _bStringBinaryEncoding;
|
||||
|
||||
bool _bEnumReverseMappings;
|
||||
|
||||
bool _bMinimalMembers;
|
||||
|
||||
bool _bEntry;
|
||||
|
|
|
@ -27,7 +27,14 @@ string CodeGenerator::printHeaderRemark(const string &sTypeName)
|
|||
s << "// **********************************************************************" << endl;
|
||||
s << endl;
|
||||
|
||||
s << "/* eslint-disable */" << endl;
|
||||
return s.str();
|
||||
}
|
||||
|
||||
string CodeGenerator::printHeaderRemark(const string &sTypeName, const string &sSuffix)
|
||||
{
|
||||
ostringstream s;
|
||||
s << printHeaderRemark(sTypeName);
|
||||
s << sSuffix << endl;
|
||||
s << endl;
|
||||
|
||||
return s.str();
|
||||
|
|
112
src/gen_js.cpp
112
src/gen_js.cpp
|
@ -19,33 +19,62 @@
|
|||
string CodeGenerator::generateJS(const EnumPtr &pPtr, const string &sNamespace)
|
||||
{
|
||||
ostringstream s;
|
||||
s << TAB << sNamespace << "." << pPtr->getId() << " = {" << endl;
|
||||
INC_TAB;
|
||||
|
||||
//成员变量
|
||||
int nenum = -1;
|
||||
bool bDependent = false;
|
||||
vector<TypeIdPtr>& member = pPtr->getAllMemberPtr();
|
||||
for (size_t i = 0; i < member.size(); i++)
|
||||
|
||||
if (_bEnumReverseMappings)
|
||||
{
|
||||
bDependent |= isDependent(sNamespace, member[i]->getId());
|
||||
s << TAB << "(function(" << pPtr->getId() << ") {" << endl;
|
||||
INC_TAB;
|
||||
|
||||
if (member[i]->hasDefault())
|
||||
{
|
||||
nenum = TC_Common::strto<int>(member[i]->def());
|
||||
}
|
||||
else
|
||||
{
|
||||
nenum++;
|
||||
}
|
||||
int nenum = -1;
|
||||
vector<TypeIdPtr>& member = pPtr->getAllMemberPtr();
|
||||
for (size_t i = 0; i < member.size(); i++)
|
||||
{
|
||||
bDependent |= isDependent(sNamespace, member[i]->getId());
|
||||
|
||||
s << TAB << "\"" << member[i]->getId() << "\" : " << TC_Common::tostr(nenum) << "," << endl;
|
||||
if (member[i]->hasDefault())
|
||||
{
|
||||
nenum = TC_Common::strto<int>(member[i]->def());
|
||||
}
|
||||
else
|
||||
{
|
||||
nenum++;
|
||||
}
|
||||
|
||||
s << TAB << pPtr->getId() << "[" << pPtr->getId() << "[\"" << member[i]->getId() << "\"] = " << TC_Common::tostr(nenum) << "] = "
|
||||
<< "\"" << member[i]->getId() << "\";" << endl;
|
||||
}
|
||||
DEL_TAB;
|
||||
s << TAB << "}(" << sNamespace << "." << pPtr->getId() << " = {}));" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
s << TAB << sNamespace << "." << pPtr->getId() << " = {" << endl;
|
||||
INC_TAB;
|
||||
|
||||
int nenum = -1;
|
||||
vector<TypeIdPtr>& member = pPtr->getAllMemberPtr();
|
||||
for (size_t i = 0; i < member.size(); i++)
|
||||
{
|
||||
bDependent |= isDependent(sNamespace, member[i]->getId());
|
||||
|
||||
if (member[i]->hasDefault())
|
||||
{
|
||||
nenum = TC_Common::strto<int>(member[i]->def());
|
||||
}
|
||||
else
|
||||
{
|
||||
nenum++;
|
||||
}
|
||||
|
||||
s << TAB << "\"" << member[i]->getId() << "\" : " << TC_Common::tostr(nenum) << (i < member.size() - 1 ? "," : "") << endl;
|
||||
}
|
||||
DEL_TAB;
|
||||
s << TAB << "};" << endl;
|
||||
}
|
||||
s << TAB << "\"_classname\" : \"" << sNamespace << "." << pPtr->getId() << "\"" << endl;
|
||||
DEL_TAB;
|
||||
s << TAB << "};" << endl;
|
||||
|
||||
//函数
|
||||
s << TAB << sNamespace << "." << pPtr->getId() << "._classname = \"" << sNamespace << "." << pPtr->getId() << "\";" << endl;
|
||||
s << TAB << sNamespace << "." << pPtr->getId() << "._write = function(os, tag, val) { return os.writeInt32(tag, val); };" << endl;
|
||||
s << TAB << sNamespace << "." << pPtr->getId() << "._read = function(is, tag, def) { return is.readInt32(tag, true, def); };" << endl;
|
||||
|
||||
|
@ -75,7 +104,7 @@ string CodeGenerator::generateJS(const ConstPtr &pPtr, const string &sNamespace,
|
|||
return s.str();
|
||||
}
|
||||
|
||||
string CodeGenerator::generateJS(const StructPtr & pPtr, const string &sNamespace, bool &bNeedAssert)
|
||||
string CodeGenerator::generateJS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc)
|
||||
{
|
||||
if (_bMinimalMembers && !_bEntry && !isDependent(sNamespace, pPtr->getId()))
|
||||
{
|
||||
|
@ -243,19 +272,22 @@ string CodeGenerator::generateJS(const StructPtr & pPtr, const string &sNamespac
|
|||
DEL_TAB;
|
||||
s << TAB << "};" << endl;
|
||||
|
||||
//readFromJson
|
||||
//readFromObject
|
||||
s << TAB << sNamespace << "." << pPtr->getId() << ".prototype.readFromObject = function(json) { "<< endl;
|
||||
INC_TAB;
|
||||
|
||||
for (size_t i = 0; i < member.size(); i++)
|
||||
{
|
||||
if (isSimple(member[i]->getTypePtr())) {
|
||||
s << TAB << "json.hasOwnProperty(\"" << member[i]->getId() << "\") && (this." << member[i]->getId() << " = json." << member[i]->getId() << ");" << endl;
|
||||
s << TAB << "_hasOwnProperty.call(json, \"" << member[i]->getId() << "\") && (this." << member[i]->getId() << " = json." << member[i]->getId() << ");" << endl;
|
||||
} else {
|
||||
s << TAB << "json.hasOwnProperty(\"" << member[i]->getId() << "\") && (this." << member[i]->getId() << ".readFromObject(json." << member[i]->getId() << "));" << endl;
|
||||
s << TAB << "_hasOwnProperty.call(json, \"" << member[i]->getId() << "\") && (this." << member[i]->getId() << ".readFromObject(json." << member[i]->getId() << "));" << endl;
|
||||
}
|
||||
bQuickFunc = true;
|
||||
}
|
||||
|
||||
s << TAB << "return this;" << endl;
|
||||
|
||||
DEL_TAB;
|
||||
s << TAB << "};" << endl;
|
||||
|
||||
|
@ -285,7 +317,7 @@ string CodeGenerator::generateJS(const StructPtr & pPtr, const string &sNamespac
|
|||
return s.str();
|
||||
}
|
||||
|
||||
string CodeGenerator::generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert)
|
||||
string CodeGenerator::generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bool &bNeedAssert, bool &bQuickFunc)
|
||||
{
|
||||
ostringstream sstr;
|
||||
vector<StructPtr> ss(pPtr->getAllStructPtr());
|
||||
|
@ -295,7 +327,7 @@ string CodeGenerator::generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bo
|
|||
|
||||
for (vector<StructPtr>::iterator iter=ss.begin(); iter!=ss.end();)
|
||||
{
|
||||
string str = generateJS(*iter, pPtr->getId(), bNeedAssert);
|
||||
string str = generateJS(*iter, pPtr->getId(), bNeedAssert, bQuickFunc);
|
||||
|
||||
if (!str.empty()) {
|
||||
sstr << str << endl;
|
||||
|
@ -311,23 +343,31 @@ string CodeGenerator::generateJS(const NamespacePtr &pPtr, bool &bNeedStream, bo
|
|||
vector<ConstPtr> &cs = pPtr->getAllConstPtr();
|
||||
for (size_t i = 0; i < cs.size(); i++)
|
||||
{
|
||||
cstr << generateJS(cs[i], pPtr->getId(), bNeedStream);
|
||||
string str = generateJS(cs[i], pPtr->getId(), bNeedStream);
|
||||
|
||||
if (!str.empty()) {
|
||||
cstr << str << endl;
|
||||
}
|
||||
}
|
||||
|
||||
ostringstream estr;
|
||||
vector<EnumPtr> & es = pPtr->getAllEnumPtr();
|
||||
for (size_t i = 0; i < es.size(); i++)
|
||||
{
|
||||
estr << generateJS(es[i], pPtr->getId());
|
||||
string str = generateJS(es[i], pPtr->getId());
|
||||
|
||||
if (!str.empty()) {
|
||||
estr << str << endl;
|
||||
}
|
||||
}
|
||||
|
||||
ostringstream str;
|
||||
if (!estr.str().empty()) str << estr.str() << endl;
|
||||
if (!cstr.str().empty()) str << cstr.str() << endl;
|
||||
if (!estr.str().empty()) str << estr.str();
|
||||
if (!cstr.str().empty()) str << cstr.str();
|
||||
if (!sstr.str().empty())
|
||||
{
|
||||
bNeedStream = true;
|
||||
str << sstr.str() << endl;
|
||||
str << sstr.str();
|
||||
}
|
||||
|
||||
return str.str();
|
||||
|
@ -354,9 +394,10 @@ bool CodeGenerator::generateJS(const ContextPtr &pPtr)
|
|||
ostringstream estr;
|
||||
bool bNeedAssert = false;
|
||||
bool bNeedStream = false;
|
||||
bool bQuickFunc = false;
|
||||
for(size_t i = 0; i < namespaces.size(); i++)
|
||||
{
|
||||
estr << generateJS(namespaces[i], bNeedStream, bNeedAssert);
|
||||
estr << generateJS(namespaces[i], bNeedStream, bNeedAssert, bQuickFunc);
|
||||
}
|
||||
if (estr.str().empty())
|
||||
{
|
||||
|
@ -376,7 +417,7 @@ bool CodeGenerator::generateJS(const ContextPtr &pPtr)
|
|||
|
||||
//生成文件内容
|
||||
ostringstream sstr;
|
||||
sstr << printHeaderRemark("Structure");
|
||||
sstr << printHeaderRemark("Structure", DISABLE_ESLINT);
|
||||
sstr << "\"use strict\";" << endl << endl;
|
||||
if (bNeedAssert)
|
||||
{
|
||||
|
@ -386,6 +427,11 @@ bool CodeGenerator::generateJS(const ContextPtr &pPtr)
|
|||
{
|
||||
sstr << "var " << IDL_NAMESPACE_STR << "Stream = require(\"" << _sStreamPath << "\");" << endl;
|
||||
}
|
||||
if (bQuickFunc)
|
||||
{
|
||||
sstr << endl;
|
||||
sstr << "var _hasOwnProperty = Object.prototype.hasOwnProperty;" << endl;
|
||||
}
|
||||
sstr << ostr.str() << endl;
|
||||
sstr << istr.str();
|
||||
sstr << estr.str() << endl;
|
||||
|
|
|
@ -21,7 +21,7 @@ string CodeGenerator::generateDTS(const EnumPtr &pPtr, const string &sNamespace)
|
|||
ostringstream s;
|
||||
|
||||
INC_TAB;
|
||||
s << TAB << "enum " << pPtr->getId() << " {" << endl;
|
||||
s << TAB << (_bEnumReverseMappings ? "enum " : "const enum ") << pPtr->getId() << " {" << endl;
|
||||
|
||||
INC_TAB;
|
||||
//成员变量
|
||||
|
@ -83,9 +83,29 @@ string CodeGenerator::generateDTS(const StructPtr &pPtr, const string &sNamespac
|
|||
|
||||
string sStructName = pPtr->getId() + "$OBJ";
|
||||
vector<TypeIdPtr> &member = pPtr->getAllMemberPtr();
|
||||
ostringstream s;
|
||||
|
||||
INC_TAB;
|
||||
|
||||
ostringstream istr;
|
||||
bool bNever = false;
|
||||
istr << TAB << "interface " << sStructName << " {" << endl;
|
||||
INC_TAB;
|
||||
for (size_t i = 0; i < member.size(); i++)
|
||||
{
|
||||
const string &sType = getDtsType(member[i]->getTypePtr(), false);
|
||||
if (!sType.empty())
|
||||
{
|
||||
istr << TAB << (member[i]->getId()) << (member[i]->isRequire()?": ":"?: ") << sType << ";" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
bNever = true;
|
||||
}
|
||||
}
|
||||
DEL_TAB;
|
||||
istr << TAB << "}" << endl;
|
||||
|
||||
|
||||
ostringstream s;
|
||||
s << TAB << "class " << pPtr->getId() << " {" << endl;
|
||||
INC_TAB;
|
||||
|
||||
|
@ -105,8 +125,8 @@ string CodeGenerator::generateDTS(const StructPtr &pPtr, const string &sNamespac
|
|||
*/
|
||||
if (_iOptimizeLevel != Os)
|
||||
{
|
||||
s << TAB << "toObject(): " << sStructName << ";" << endl;
|
||||
s << TAB << "readFromObject(json: " << sStructName << "): void;" << endl;
|
||||
s << TAB << "toObject(): " << (bNever ? "never" : sStructName) << ";" << endl;
|
||||
s << TAB << "readFromObject(json: " << sStructName << "): " << pPtr->getId() << ";" << endl;
|
||||
s << TAB << "toBinBuffer(): " << IDL_NAMESPACE_STR << "Stream.BinBuffer;" << endl;
|
||||
s << TAB << "static new(): " << pPtr->getId() << ";" << endl;
|
||||
s << TAB << "static create(is: " << IDL_NAMESPACE_STR << "Stream." << IDL_TYPE << "InputStream): " << pPtr->getId() << ";" << endl;
|
||||
|
@ -114,20 +134,10 @@ string CodeGenerator::generateDTS(const StructPtr &pPtr, const string &sNamespac
|
|||
DEL_TAB;
|
||||
s << TAB << "}" << endl;
|
||||
|
||||
s << TAB << "interface " << sStructName << " {" << endl;
|
||||
INC_TAB;
|
||||
s << istr.str();
|
||||
|
||||
for (size_t i = 0; i < member.size(); i++)
|
||||
{
|
||||
const string &sType = getDtsType(member[i]->getTypePtr(), false);
|
||||
if (!sType.empty())
|
||||
{
|
||||
s << TAB << (member[i]->getId()) << (member[i]->isRequire()?": ":"?: ") << sType << ";" << endl;
|
||||
}
|
||||
}
|
||||
DEL_TAB;
|
||||
s << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
@ -187,7 +197,7 @@ string CodeGenerator::generateDTS(const NamespacePtr &pPtr, const string &sConte
|
|||
ostringstream str;
|
||||
if (!sContent.empty())
|
||||
{
|
||||
str << "export declare namespace " << pPtr->getId() << " {" << endl;
|
||||
str << "export namespace " << pPtr->getId() << " {" << endl;
|
||||
str << sContent;
|
||||
str << "}" << endl << endl;
|
||||
}
|
||||
|
@ -228,7 +238,7 @@ void CodeGenerator::generateDTS(const ContextPtr &pPtr)
|
|||
|
||||
//生成文件内容
|
||||
ostringstream sstr;
|
||||
sstr << printHeaderRemark("Structure");
|
||||
sstr << printHeaderRemark("Structure", DISABLE_TSLINT);
|
||||
sstr << ostr.str() << endl;
|
||||
sstr << estr.str() << endl;
|
||||
|
||||
|
|
|
@ -199,22 +199,7 @@ string CodeGenerator::generateJSProxy(const NamespacePtr &nPtr, const InterfaceP
|
|||
DEL_TAB;
|
||||
str << TAB << "} catch (e) {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "throw {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"request\" : data.request," << endl;
|
||||
str << TAB << "\"response\" : {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"costtime\" : data.request.costtime," << endl;
|
||||
str << TAB << "\"error\" : {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"code\" : " << IDL_NAMESPACE_STR << "Error.CLIENT.DECODE_ERROR," << endl;
|
||||
str << TAB << "\"message\" : e.message" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "};" << endl;
|
||||
str << TAB << "throw _makeError(data, e.message, " << IDL_NAMESPACE_STR << "Error.CLIENT.DECODE_ERROR);" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
|
@ -322,22 +307,7 @@ string CodeGenerator::generateJSProxy(const NamespacePtr &nPtr, const InterfaceP
|
|||
DEL_TAB;
|
||||
str << TAB << "} catch (e) {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "throw {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"request\" : data.request," << endl;
|
||||
str << TAB << "\"response\" : {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"costtime\" : data.request.costtime," << endl;
|
||||
str << TAB << "\"error\" : {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"code\" : " << IDL_NAMESPACE_STR << "Error.CLIENT.DECODE_ERROR," << endl;
|
||||
str << TAB << "\"message\" : e.message" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "};" << endl;
|
||||
str << TAB << "throw _makeError(data, e.message, " << IDL_NAMESPACE_STR << "Error.CLIENT.DECODE_ERROR);" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
|
@ -347,17 +317,7 @@ string CodeGenerator::generateJSProxy(const NamespacePtr &nPtr, const InterfaceP
|
|||
//STEP03 生成框架调用错误处理函数(Error Response = ER)
|
||||
str << TAB << "var __" << nPtr->getId() << "_" << pPtr->getId() << "$" << oPtr->getId() << "$ER = function (data) {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "throw {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"request\" : data.request," << endl;
|
||||
str << TAB << "\"response\" : {" << endl;
|
||||
INC_TAB;
|
||||
str << TAB << "\"costtime\" : data.request.costtime," << endl;
|
||||
str << TAB << "\"error\" : data.error" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "}" << endl;
|
||||
str << TAB << "throw _makeError(data, \"Call " << pPtr->getId() << "::" << oPtr->getId() << " failed\");" << endl;
|
||||
DEL_TAB;
|
||||
str << TAB << "};" << endl << endl;
|
||||
|
||||
|
@ -481,7 +441,7 @@ bool CodeGenerator::generateJSProxy(const ContextPtr &cPtr)
|
|||
INC_TAB;
|
||||
istr << TAB << "return this._worker.version;" << endl;
|
||||
DEL_TAB;
|
||||
istr << TAB << "};" << endl << endl;
|
||||
istr << TAB << "};" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -489,9 +449,10 @@ bool CodeGenerator::generateJSProxy(const ContextPtr &cPtr)
|
|||
ostringstream estr;
|
||||
bool bNeedAssert = false;
|
||||
bool bNeedStream = false;
|
||||
bool bQuickFunc = false;
|
||||
for(size_t i = 0; i < namespaces.size(); i++)
|
||||
{
|
||||
estr << generateJS(namespaces[i], bNeedStream, bNeedAssert);
|
||||
estr << generateJS(namespaces[i], bNeedStream, bNeedAssert, bQuickFunc);
|
||||
}
|
||||
|
||||
bool bNeedRpc = false;
|
||||
|
@ -518,7 +479,7 @@ bool CodeGenerator::generateJSProxy(const ContextPtr &cPtr)
|
|||
|
||||
//生成文件内容
|
||||
ostringstream sstr;
|
||||
sstr << printHeaderRemark("Client");
|
||||
sstr << printHeaderRemark("Client", DISABLE_ESLINT);
|
||||
sstr << "\"use strict\";" << endl << endl;
|
||||
if (bNeedAssert)
|
||||
{
|
||||
|
@ -534,6 +495,48 @@ bool CodeGenerator::generateJSProxy(const ContextPtr &cPtr)
|
|||
}
|
||||
|
||||
sstr << ostr.str() << endl;
|
||||
|
||||
//生成帮助函数
|
||||
if (bQuickFunc)
|
||||
{
|
||||
sstr << "var _hasOwnProperty = Object.prototype.hasOwnProperty;" << endl;
|
||||
}
|
||||
if (bNeedRpc)
|
||||
{
|
||||
sstr << TAB << "var _makeError = function (data, message, type) {" << endl;
|
||||
INC_TAB;
|
||||
sstr << TAB << "var error = new Error(message || \"\");" << endl;
|
||||
sstr << TAB << "error.request = data.request;" << endl;
|
||||
sstr << TAB << "error.response = {" << endl;
|
||||
INC_TAB;
|
||||
sstr << TAB << "\"costtime\" : data.request.costtime" << endl;
|
||||
DEL_TAB;
|
||||
sstr << TAB << "};" << endl;
|
||||
sstr << TAB << "if (type === " << IDL_NAMESPACE_STR << "Error.CLIENT.DECODE_ERROR) {" << endl;
|
||||
INC_TAB;
|
||||
sstr << TAB << "error.name = \"DECODE_ERROR\";" << endl;
|
||||
sstr << TAB << "error.response.error = {" << endl;
|
||||
INC_TAB;
|
||||
sstr << TAB << "\"code\" : type," << endl;
|
||||
sstr << TAB << "\"message\" : message" << endl;
|
||||
DEL_TAB;
|
||||
sstr << TAB << "};" << endl;
|
||||
DEL_TAB;
|
||||
sstr << TAB << "} else {" << endl;
|
||||
INC_TAB;
|
||||
sstr << TAB << "error.name = \"RPC_ERROR\";" << endl;
|
||||
sstr << TAB << "error.response.error = data.error;" << endl;
|
||||
DEL_TAB;
|
||||
sstr << TAB << "}" << endl;
|
||||
sstr << TAB << "return error;" << endl;
|
||||
DEL_TAB;
|
||||
sstr << TAB << "};" << endl;
|
||||
}
|
||||
if (bQuickFunc || bNeedRpc)
|
||||
{
|
||||
sstr << endl;
|
||||
}
|
||||
|
||||
sstr << istr.str() << endl;
|
||||
sstr << estr.str() << endl;
|
||||
|
||||
|
|
|
@ -189,7 +189,7 @@ void CodeGenerator::generateDTSProxy(const ContextPtr &cPtr)
|
|||
|
||||
//生成文件内容
|
||||
ostringstream sstr;
|
||||
sstr << printHeaderRemark("Client");
|
||||
sstr << printHeaderRemark("Client", DISABLE_TSLINT);
|
||||
if (bNeedStream)
|
||||
{
|
||||
sstr << "import * as " << IDL_NAMESPACE_STR << "Stream from \"" << _sStreamPath << "\";" << endl;
|
||||
|
|
|
@ -325,9 +325,10 @@ bool CodeGenerator::generateJSServer(const ContextPtr &pPtr)
|
|||
ostringstream estr;
|
||||
bool bNeedAssert = false;
|
||||
bool bNeedStream = false;
|
||||
bool bQuickFunc = false;
|
||||
for(size_t i = 0; i < namespaces.size(); i++)
|
||||
{
|
||||
estr << generateJS(namespaces[i], bNeedStream, bNeedAssert);
|
||||
estr << generateJS(namespaces[i], bNeedStream, bNeedAssert, bQuickFunc);
|
||||
}
|
||||
|
||||
bool bNeedRpc = false;
|
||||
|
@ -363,10 +364,15 @@ bool CodeGenerator::generateJSServer(const ContextPtr &pPtr)
|
|||
|
||||
ostr << TAB << "var " << it->second.sModule << " = require(\"" << it->second.sFile << "\");" << endl;
|
||||
}
|
||||
if (bQuickFunc)
|
||||
{
|
||||
ostr << endl;
|
||||
ostr << TAB << "var _hasOwnProperty = Object.prototype.hasOwnProperty;" << endl;
|
||||
}
|
||||
|
||||
ostringstream str;
|
||||
|
||||
str << printHeaderRemark("Server");
|
||||
str << printHeaderRemark("Server", DISABLE_ESLINT);
|
||||
str << "\"use strict\";" << endl << endl;
|
||||
str << ostr.str() << endl;
|
||||
str << istr.str();
|
||||
|
|
|
@ -138,7 +138,7 @@ void CodeGenerator::generateDTSServer(const ContextPtr &pPtr)
|
|||
}
|
||||
|
||||
ostringstream str;
|
||||
str << printHeaderRemark("Server");
|
||||
str << printHeaderRemark("Server", DISABLE_TSLINT);
|
||||
str << ostr.str() << endl;
|
||||
str << estr.str() << endl;
|
||||
|
||||
|
|
|
@ -109,7 +109,7 @@ string CodeGenerator::getClassName(const TypePtr& pPtr)
|
|||
VectorPtr vPtr = VectorPtr::dynamicCast(pPtr);
|
||||
if (vPtr)
|
||||
{
|
||||
return "list(" + getDataType(vPtr->getTypePtr()) + ")";
|
||||
return "list(" + getClassName(vPtr->getTypePtr()) + ")";
|
||||
}
|
||||
|
||||
StructPtr sPtr = StructPtr::dynamicCast(pPtr);
|
||||
|
@ -118,7 +118,7 @@ string CodeGenerator::getClassName(const TypePtr& pPtr)
|
|||
vector<string> vecNames = TC_Common::sepstr<string>(sPtr->getSid(), "::");
|
||||
assert(vecNames.size() == 2);
|
||||
|
||||
return findName(vecNames[0], vecNames[1]);
|
||||
return vecNames[0] + "." + vecNames[1];
|
||||
}
|
||||
|
||||
MapPtr mPtr = MapPtr::dynamicCast(pPtr);
|
||||
|
|
|
@ -27,8 +27,8 @@ void usage()
|
|||
cout << "Version : " << GENERATOR_VERSION << endl;
|
||||
cout << "Usage : " << EXECUTE_FILENAME << " [OPTION] " << "*." << TC_Common::lower(IDL_TYPE) << " file" << endl;
|
||||
cout << " --version print " << EXECUTE_FILENAME << " version" << endl;
|
||||
cout << " --rpc-path=[DIRECTORY] specify the path of rpc module." << endl;
|
||||
cout << " --stream-path=[DIRECTORY] specify the path of stream module." << endl;
|
||||
cout << " --rpc-path=[DIRECTORY] specify the path of rpc module." << endl;
|
||||
cout << " --stream-path=[DIRECTORY] specify the path of stream module." << endl;
|
||||
cout << " --allow-reserved-namespace allow you to use reserved word as a namespace." << endl;
|
||||
cout << " --dir=DIRECTORY generate source file to DIRECTORY." << endl;
|
||||
cout << " --relative use current path." << endl;
|
||||
|
@ -39,8 +39,9 @@ void usage()
|
|||
cout << " --client just for client side source file." << endl;
|
||||
cout << " --server just for server side source file." << endl;
|
||||
cout << " --dts generate d.ts file." << endl;
|
||||
cout << " --use-string-represent use string represent long type." << endl;
|
||||
cout << " --use-string-represent use <string> represent <long> type." << endl;
|
||||
cout << " --string-binary-encoding get string raw bytes <BinBuffer>." << endl;
|
||||
cout << " --enum-reverse-mappings reverse mapping from enum values to enum names." << endl;
|
||||
cout << " --optimize=[0|s] support \"s\" to reduce code size, default is 0." << endl;
|
||||
cout << endl;
|
||||
cout << EXECUTE_FILENAME << " support type: boolean char short int long float double list map" << endl;
|
||||
|
@ -130,6 +131,7 @@ int main(int argc, char* argv[])
|
|||
generator.setUseSpecialPath(option.hasParam("relative"));
|
||||
generator.setUseStringRepresent(option.hasParam("use-string-represent"));
|
||||
generator.setStringBinaryEncoding(option.hasParam("string-binary-encoding"));
|
||||
generator.setEnumReverseMappings(option.hasParam("enum-reverse-mappings"));
|
||||
generator.setEnableDTS(option.hasParam("dts"));
|
||||
|
||||
if (option.hasParam("optimize"))
|
||||
|
|
Loading…
Reference in New Issue
Block a user