Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source code implementation of IDL tool for generating Dart(Flutter) code through tars file #194

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CMakeFiles/
*.cmake
bin/
tmp/
src/
Makefile
CMakeCache.txt
download/
*.a
install_manifest.txt
servant/servant/AdminF.h
servant/servant/AuthF.h
servant/servant/BaseF.h
servant/servant/ConfigF.h
servant/servant/EndpointF.h
servant/servant/LogF.h
servant/servant/NodeF.h
servant/servant/NotifyF.h
servant/servant/PropertyF.h
servant/servant/QueryF.h
servant/servant/StatF.h
servant/tup/RequestF.h
tools/tarsgrammar/tars.lex.cpp
tools/tarsgrammar/tars.tab.hpp
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_subdirectory(tars2php)
add_subdirectory(tars2android)
add_subdirectory(tars2node)
add_subdirectory(tars2case)
add_subdirectory(tars2dart)

IF(TARS_PROTOBUF)
add_subdirectory(pb2tarscpp)
Expand Down
1 change: 1 addition & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ tars2php | Source code implementation of IDL tool for generating PHP code t
tars2python | Source code implementation of IDL tool for generating Python code through tars file
tars2node | Source code implementation of IDL tool for generating Node.js code through tars file
tars2android | Source code implementation of IDL tool for generating Android code through tars file
tars2dart | Source code implementation of IDL tool for generating Dart(Flutter) code through tars file,[Dependences](https://github.com/brooklet/dart_tars_protocol)
tars2case | Generate test case for tars server
pb2tarscpp | Generate protoc plugin source code for tars C++ code via proto file
1 change: 1 addition & 0 deletions tools/README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ tars2python | 通过tars文件生成 Python 代码的IDL工具的源码实现
tars2node | 通过tars文件生成 Node.js 代码的IDL工具的源码实现
tars2android | 通过tars文件生成 Android 代码的IDL工具的源码实现
tars2case | 通过tars文件生成 tars 服务生成测试用例
tars2dart | 通过tars文件生成 Dart(Flutter) 代码的IDL工具的源码实现,[依赖库](https://github.com/brooklet/dart_tars_protocol)
pb2tarscpp | 通过proto文件生成tars C++ 代码的protoc插件源码实现
1 change: 1 addition & 0 deletions tools/tars2dart/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tars2language("tars2dart")
100 changes: 100 additions & 0 deletions tools/tars2dart/interface_analysis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "interface_analysis.h"

#include <vector>

InterfaceAnalysis::InterfaceAnalysis() {
}

void InterfaceAnalysis::analysis(const vector<InterfacePtr>& interfacePtrs) {
for (size_t index = 0; index < interfacePtrs.size(); ++index) {
analysis(interfacePtrs[index]);
}
}

void InterfaceAnalysis::analysis(const InterfacePtr& interfacePtr) {
const vector<OperationPtr>& operations = interfacePtr->getAllOperationPtr();

for (size_t operationIndex = 0; operationIndex < operations.size(); ++operationIndex) {
const OperationPtr& operation = operations[operationIndex];

analysis(operation->getReturnPtr()->getTypePtr());

const vector<ParamDeclPtr>& paramDecls = operation->getAllParamDeclPtr();

for (size_t paramIndex = 0; paramIndex < paramDecls.size(); paramIndex++) {
const ParamDeclPtr& paramDeclPtr = paramDecls[paramIndex];
analysis(paramDeclPtr->getTypeIdPtr()->getTypePtr());
}
}
}

void InterfaceAnalysis::analysis(const StructPtr& structPtr) {
const vector<TypeIdPtr>& allMembersPtr = structPtr->getAllMemberPtr();
for (size_t index = 0; index < allMembersPtr.size(); ++index) {
analysis(allMembersPtr[index]->getTypePtr());
}
}

void InterfaceAnalysis::analysis(const TypePtr& typePtr) {
VectorPtr vPtr = VectorPtr::dynamicCast(typePtr);
if (vPtr) {
analysis(vPtr->getTypePtr());
return ;
}

MapPtr mPtr = MapPtr::dynamicCast(typePtr);
if (mPtr) {
analysis(mPtr->getLeftTypePtr());
analysis(mPtr->getRightTypePtr());
return ;
}

StructPtr sPtr = StructPtr::dynamicCast(typePtr);
if (sPtr) {
// 说明已经分析过,并且找到了
if (mAllStructs.find(sPtr->getSid()) != mAllStructs.end()) {
return ;
}

mAllStructs.insert(std::pair<std::string, StructPtr>(sPtr->getSid(), sPtr));
analysis(sPtr);
}

EnumPtr ePtr = EnumPtr::dynamicCast(typePtr);
if (ePtr) {
if (mAllEnums.find(ePtr->getSid()) != mAllEnums.end()) {
return ;
}

mAllEnums.insert(std::pair<std::string, EnumPtr>(ePtr->getSid(), ePtr));
}
}

const std::map<std::string, StructPtr>& InterfaceAnalysis::getAllStructs() const {
return mAllStructs;
}

const std::map<std::string, EnumPtr>& InterfaceAnalysis::getAllEnums() const {
return mAllEnums;
}

const std::map<std::string, ConstPtr>& InterfaceAnalysis::getAllConsts() const {
return mAllConsts;
}


47 changes: 47 additions & 0 deletions tools/tars2dart/interface_analysis.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

#ifndef INTERFACE_ANALYSIS_H_
#define INTERFACE_ANALYSIS_H_

#include "parse.h"
#include <map>
#include <vector>

class InterfaceAnalysis {
public:
InterfaceAnalysis();

const std::map<std::string, StructPtr>& getAllStructs() const;
const std::map<std::string, EnumPtr>& getAllEnums() const;
const std::map<std::string, ConstPtr>& getAllConsts() const;

void analysis(const InterfacePtr& interfacePtr);
void analysis(const vector<InterfacePtr>& interfacePtrs);

private:
void analysis(const StructPtr& structPtr);
void analysis(const TypePtr& typePtr);

private:
std::map<std::string, StructPtr> mAllStructs;
std::map<std::string, EnumPtr> mAllEnums;
std::map<std::string, ConstPtr> mAllConsts;
};



#endif /* INTERFACE_ANALYSIS_H_ */
135 changes: 135 additions & 0 deletions tools/tars2dart/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "util/tc_option.h"
#include "util/tc_file.h"
#include "tars2dart.h"

void usage()
{
cout << "Usage : tars2dart [OPTION] tarsfile" << endl;
cout << " tars2dart support type: bool byte short int long float double vector map" << endl;
cout << "supported [OPTION]:" << endl;
cout << " --help help,print this" << endl;
cout << " --dir=DIRECTORY generate dart file to DIRECTORY(default to current)" << endl;
cout << " --base-package=NAME package prefix, default(tars_idl)" << endl;
cout << " --extends-package=NAME set the extends package name, default(/tars/)"<< endl;
cout << " --with-charset set charset, default UTF8" << endl;


cout << endl;
exit(0);
}

void check(vector<string> &vTars)
{
for(size_t i = 0; i < vTars.size(); i++)
{
string ext = tars::TC_File::extractFileExt(vTars[i]);
if(ext == "tars")
{
if(!tars::TC_File::isFileExist(vTars[i]))
{
cerr << "file '" << vTars[i] << "' not exists" << endl;
usage();
exit(0);
}
}
else
{
cerr << "only support tars file." << endl;
exit(0);
}
}
}

int main(int argc, char* argv[])
{
if(argc < 2)
{
usage();
}

tars::TC_Option option;
option.decode(argc, argv);
vector<string> vTars = option.getSingle();

check(vTars);

if(option.hasParam("help"))
{
usage();
}

Tars2Dart t2d;

g_parse->setTars(option.hasParam("with-tars"));

if(option.getValue("dir") != "")
{
t2d.setBaseDir(option.getValue("dir"));
}
else
{
t2d.setBaseDir(".");
}

if(option.hasParam("base-package"))
{
t2d.setBasePackage(option.getValue("base-package"));
}
else
{
t2d.setBasePackage("tars_idl.");
}

if (option.hasParam("extends-package"))
{
t2d.setTafPacket(option.getValue("extends-package"));
}

if(option.hasParam("with-JavaBeanRule"))
{
t2d.setWithJbr(true);
}
else
{
t2d.setWithJbr(false);
}

t2d.setWithCompact(false);
t2d.setEnumCompact(true);

t2d.setWithGenerateInterfaceDependencies(true);
t2d.setWithFilterRomTars(true);

try
{
//增加include搜索路径
g_parse->addIncludePath(option.getValue("include"));

for(size_t i = 0; i < vTars.size(); i++)
{
g_parse->parse(vTars[i]);
t2d.createFile(vTars[i]);
}
}catch(exception& e)
{
cerr<<e.what()<<endl;
}

return 0;
}

Loading