From 3e45ea9b769c3dbc8c8ceb41b1d1ebda109f3165 Mon Sep 17 00:00:00 2001 From: superzheng Date: Mon, 25 Mar 2019 19:11:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20TypeScript=20=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=BD=AC=E6=8D=A2=E9=94=99=E8=AF=AF=E5=9C=B0=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=20JavaScript=20=E6=96=87=E4=BB=B6=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20=E4=BF=AE=E5=A4=8D=E5=9C=A8?= =?UTF-8?q?=E6=B7=B7=E5=90=88=E4=BD=BF=E7=94=A8=20enum=20=E5=92=8C=20vecto?= =?UTF-8?q?r=20=E6=97=B6=EF=BC=8Cjs=20=E6=96=87=E4=BB=B6=E4=B8=AD=E4=BC=9A?= =?UTF-8?q?=E5=87=BA=E7=8E=B0=20`as=20any`=20=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/code_generator.h | 4 +--- src/gen_proxy_ts.cpp | 6 +++--- src/gen_server_ts.cpp | 4 ++-- src/gen_ts.cpp | 6 +++--- src/idl_util.cpp | 9 ++------- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/code_generator.h b/src/code_generator.h index a37d42d..f42097d 100644 --- a/src/code_generator.h +++ b/src/code_generator.h @@ -157,9 +157,7 @@ private: string getTsType(const TypePtr &pPtr, const bool bStream = true, const bool bBase = false); - string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace); - - string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool bGlobal); + string getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string & sNamespace, const bool &bGlobal = true, const bool &bCastEnumAsAny = false); private: string generateJS(const StructPtr &pPtr, const string &sNamespace, bool &bNeedAssert, bool &bQuickFunc); diff --git a/src/gen_proxy_ts.cpp b/src/gen_proxy_ts.cpp index 1d6af8a..049de9a 100644 --- a/src/gen_proxy_ts.cpp +++ b/src/gen_proxy_ts.cpp @@ -136,7 +136,7 @@ string CodeGenerator::generateTSProxy(const NamespacePtr &nPtr, const InterfaceP if (isSimple(oPtr->getReturnPtr()->getTypePtr())) { - str << getDefault(oPtr->getReturnPtr(), oPtr->getReturnPtr()->def(), nPtr->getId()) + str << getDefault(oPtr->getReturnPtr(), oPtr->getReturnPtr()->def(), nPtr->getId(), true, true) << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : ""); } else @@ -166,7 +166,7 @@ string CodeGenerator::generateTSProxy(const NamespacePtr &nPtr, const InterfaceP if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) { - str << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId()) + str << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId(), true, true) << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); } else @@ -257,7 +257,7 @@ string CodeGenerator::generateTSProxy(const NamespacePtr &nPtr, const InterfaceP str << ", " << getDataType(oPtr->getReturnPtr()->getTypePtr(), true); } - str << ", " << getDefault(oPtr->getReturnPtr(), "", nPtr->getId(), true) + str << ", " << getDefault(oPtr->getReturnPtr(), "", nPtr->getId(), true, true) << (isRawOrString(oPtr->getReturnPtr()->getTypePtr()) ? ", 1" : ""); str << ")," << endl; diff --git a/src/gen_server_ts.cpp b/src/gen_server_ts.cpp index 6032c49..b42a9b3 100644 --- a/src/gen_server_ts.cpp +++ b/src/gen_server_ts.cpp @@ -141,7 +141,7 @@ string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const I if (vParamDecl[i]->isOut()) { - dstr << ", " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true) + dstr << ", " << getDefault(vParamDecl[i]->getTypeIdPtr(), "", nPtr->getId(), true, true) << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); } @@ -163,7 +163,7 @@ string CodeGenerator::generateTSServerDispatch(const NamespacePtr &nPtr, const I if (isSimple(vParamDecl[i]->getTypeIdPtr()->getTypePtr())) { - dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId()) + dstr << getDefault(vParamDecl[i]->getTypeIdPtr(), vParamDecl[i]->getTypeIdPtr()->def(), nPtr->getId(), true, true) << (isRawOrString(vParamDecl[i]->getTypeIdPtr()->getTypePtr()) ? ", 1" : ""); } else diff --git a/src/gen_ts.cpp b/src/gen_ts.cpp index 67e7e68..63de237 100644 --- a/src/gen_ts.cpp +++ b/src/gen_ts.cpp @@ -77,7 +77,7 @@ string CodeGenerator::generateTS(const ConstPtr &pPtr, const string &sNamespace, INC_TAB; s << TAB << "export const " << pPtr->getTypeIdPtr()->getId() << ": " << getTsType(pPtr->getTypeIdPtr()->getTypePtr()) << " = " - << getDefault(pPtr->getTypeIdPtr(), GET_CONST_GRAMMAR_PTR(pPtr)->v, sNamespace, false) << ";" + << getDefault(pPtr->getTypeIdPtr(), GET_CONST_GRAMMAR_PTR(pPtr)->v, sNamespace, false, true) << ";" << endl; DEL_TAB; return s.str(); @@ -104,7 +104,7 @@ string CodeGenerator::generateTS(const StructPtr &pPtr, const string &sNamespace { s << TAB << (member[i]->getId()) << ": " << getTsType(member[i]->getTypePtr()) << " = " - << getDefault(member[i], member[i]->def(), sNamespace) << ";" << endl; + << getDefault(member[i], member[i]->def(), sNamespace, true, true) << ";" << endl; } if (member.size() > 0) { @@ -133,7 +133,7 @@ string CodeGenerator::generateTS(const StructPtr &pPtr, const string &sNamespace if (isSimple(member[i]->getTypePtr())) { - s << getDefault(member[i], member[i]->def(), sNamespace) + s << getDefault(member[i], member[i]->def(), sNamespace, true, true) << (isRawOrString(member[i]->getTypePtr()) ? ", 1" : ""); } else diff --git a/src/idl_util.cpp b/src/idl_util.cpp index ec25eb6..a46da10 100644 --- a/src/idl_util.cpp +++ b/src/idl_util.cpp @@ -323,12 +323,7 @@ bool CodeGenerator::isBinBuffer(const TypePtr & pPtr) const return false; } -string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace) -{ - return getDefault(pPtr, sDefault, sNamespace, true); -} - -string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace, const bool bGlobal) +string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, const string& sNamespace, const bool &bGlobal, const bool &bCastEnumAsAny) { BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr()); if (bPtr) @@ -438,7 +433,7 @@ string CodeGenerator::getDefault(const TypeIdPtr & pPtr, const string &sDefault, if (bGlobal) { - return "new " + getDataType(pPtr->getTypePtr(), true); + return "new " + getDataType(pPtr->getTypePtr(), bCastEnumAsAny); } return sDefault;