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

Add support for Swagger #923

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions drogon_ctl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ set(ctl_sources
create_plugin.cc
create_project.cc
create_view.cc
HandlerParser.cc
create_swagger.cc
help.cc
main.cc
press.cc
Expand Down Expand Up @@ -78,6 +80,17 @@ else(WIN32)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/dg_ctl"
DESTINATION ${INSTALL_BIN_DIR})
endif(WIN32)
#find_package(Clang REQUIRED)
#include_directories(${LLVM_INCLUDE_DIRS})
#separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
#add_definitions(${LLVM_DEFINITIONS_LIST})
## Find the libraries that correspond to the LLVM components
## that we wish to use
#llvm_map_components_to_libnames(llvm_libs support core irreader)
#
## Link against LLVM libraries
#target_link_libraries(drogon_ctl PRIVATE ${llvm_libs})

set(ctl_targets _drogon_ctl drogon_ctl)
set_property(TARGET ${ctl_targets} PROPERTY CXX_STANDARD ${DROGON_CXX_STANDARD})
set_property(TARGET ${ctl_targets} PROPERTY CXX_STANDARD_REQUIRED ON)
Expand Down
168 changes: 168 additions & 0 deletions drogon_ctl/HandlerParser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include "HandlerParser.h"
#include <iostream>
#include <regex>

using namespace drogon::internal;

std::pair<std::string, std::string> StructNode::findContentOfClassOrNameSpace(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have checks for comments here, it will break if the class or namespace includes commented uneven brackets

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right, thanks.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want I can help you with that so you can focus on the main dev

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you very much, I look forward to your PR :)

const std::string &content,
std::string::size_type start)
{
int braces = 0;
std::string::size_type pos1{start};
std::string::size_type pos2{content.size() - 1};
for (auto i = start; i < content.size(); i++)
{
if (content[i] == '{')
{
braces++;
if (braces == 1)
{
pos1 = i + 1;
}
}
else if (content[i] == '}')
{
braces--;
if (braces == 0)
{
pos2 = i;
break;
}
}
}
return std::pair<std::string, std::string>(content.substr(pos1,
pos2 - pos1),
content.substr(pos2 + 1));
}
std::pair<StructNodePtr, std::string> StructNode::findClass(
const std::string &content)
{
LOG_DEBUG << "findClass: " << content;
if (content.empty())
return std::pair<StructNodePtr, std::string>(nullptr, "");
std::regex rx(R"(class[ \r\n]+([^ \r\n\{]+)[ \r\n\{:]+)");
std::smatch results;
if (std::regex_search(content, results, rx))
{
assert(results.size() > 1);
auto nextPart =
findContentOfClassOrNameSpace(content, results.position());
return std::pair<StructNodePtr, std::string>(
std::make_shared<StructNode>(nextPart.first,
results[1].str(),
kClass),
nextPart.second);
}
return std::pair<StructNodePtr, std::string>(nullptr, "");
}
std::tuple<std::string, StructNodePtr, std::string> StructNode::findNameSpace(
const std::string &content)
{
LOG_DEBUG << "findNameSpace";
if (content.empty())
return std::tuple<std::string, StructNodePtr, std::string>("",
nullptr,
"");
std::regex rx(R"(namespace[ \r\n]+([^ \r\n]+)[ \r\n]*\{)");
std::smatch results;
if (std::regex_search(content, results, rx))
{
assert(results.size() > 1);
auto pos = results.position();
auto first = content.substr(0, pos);
auto nextPart = findContentOfClassOrNameSpace(content, pos);
auto npNodePtr = std::make_shared<StructNode>(nextPart.first,
results[1].str(),
kNameSpace);

return std::tuple<std::string, StructNodePtr, std::string>(
first, npNodePtr, nextPart.second);
}
else
{
return std::tuple<std::string, StructNodePtr, std::string>("",
nullptr,
"");
}
}
std::vector<StructNodePtr> StructNode::parse(const std::string &content)
{
std::vector<StructNodePtr> res;
auto t = findNameSpace(content);
if (std::get<1>(t))
{
res.emplace_back(std::get<1>(t));
auto firstPart = std::get<0>(t);
while (1)
{
auto p = findClass(firstPart);
if (p.first)
{
res.emplace_back(p.first);
firstPart = p.second;
}
else
{
break;
}
}
auto subsequentNode = parse(std::get<2>(t));
for (auto &node : subsequentNode)
{
res.emplace_back(node);
}
return res;
}
std::string classPart = content;
while (1)
{
auto p = findClass(classPart);
if (p.first)
{
res.emplace_back(p.first);
classPart = p.second;
}
else
{
break;
}
}
return res;
}
void StructNode::print(int indent) const
{
std::string ind(indent, ' ');
std::cout << ind;
switch (type_)
{
case kRoot:
{
std::cout << "Root\n" << ind << "{\n";
break;
}
case kClass:
{
std::cout << "class " << name_ << "\n" << ind << "{\n";
break;
}
case kNameSpace:
{
std::cout << "namespace " << name_ << "\n" << ind << "{\n";
break;
}
}

for (auto child : children_)
{
child->print(indent + 2);
}
if (type_ == kClass)
{
std::cout << content_ << "\n";
}
std::cout << ind << "}";
if (type_ == kClass)
std::cout << ";";
std::cout << "\n";
}
100 changes: 100 additions & 0 deletions drogon_ctl/HandlerParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#pragma once
#include <drogon/HttpTypes.h>
#include <trantor/utils/Logger.h>
#include <vector>
#include <memory>
#include <string>
#include <tuple>

namespace drogon
{
namespace internal
{
class StructNode;
using StructNodePtr = std::shared_ptr<StructNode>;
class StructNode
{
public:
enum NodeType
{
kRoot = 0,
kClass,
kNameSpace
};

StructNode(const std::string &content,
const std::string &name,
NodeType type = kRoot)
: type_(type), name_(name), content_(content)
{
LOG_DEBUG << "new node:" << name << "-" << type;
if (type != kClass)
children_ = parse(content);
}
const std::string &content() const
{
return content_;
}
const std::string &name() const
{
return name_;
}
NodeType type() const
{
return type_;
}
void print() const
{
print(0);
}

private:
std::vector<StructNodePtr> children_;
std::string content_;
NodeType type_;
std::string name_;
std::pair<std::string, std::string> findContentOfClassOrNameSpace(
const std::string &content,
std::string::size_type start);
std::pair<StructNodePtr, std::string> findClass(const std::string &content);
std::tuple<std::string, StructNodePtr, std::string> findNameSpace(
const std::string &content);
std::vector<StructNodePtr> parse(const std::string &content);
void print(int indent) const;
};

class ParametersInfo
{
public:
std::string getName() const;
std::string getType() const;
std::string getConstraint() const;
};
class RoutingInfo
{
public:
std::string getPath() const;
std::vector<std::string> getFilters() const;
std::vector<drogon::HttpMethod> getHttpMethods() const;
std::string getScripts() const;
std::string getContentType() const;
std::string getReturnContentType() const;
std::vector<ParametersInfo> getParameterInfo() const;
std::vector<ParametersInfo> getReturnParameterInfo() const;
};

class HandlerInfo
{
public:
std::string getClassName() const;
std::string getNamespace() const;
std::vector<RoutingInfo> getRoutingInfo() const;
};
class HandlerParser
{
public:
std::vector<HandlerInfo> parse();
HandlerParser(const StructNodePtr root);
};
} // namespace internal
} // namespace drogon
4 changes: 3 additions & 1 deletion drogon_ctl/create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ std::string create::detail()
"drogon_ctl create project <project_name> //"
"create a project named project_name\n\n"
"drogon_ctl create model <model_path> [--table=<table_name>] [-f]//"
"create model classes in model_path\n";
"create model classes in model_path\n\n"
"drogon_ctl create swagger <swagger_path> //"
"create swagger controller in swagger_path\n\n";
}

void create::handleCommand(std::vector<std::string> &parameters)
Expand Down
8 changes: 8 additions & 0 deletions drogon_ctl/create_project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ static void newModelConfigFile(std::ofstream &configFile)
auto templ = DrTemplateBase::newTemplate("model_json");
configFile << templ->genText();
}
static void newSwaggerConfigFile(std::ofstream &configFile)
{
auto templ = DrTemplateBase::newTemplate("swagger_json");
configFile << templ->genText();
}
static void newTestMainFile(std::ofstream &mainFile)
{
auto templ = DrTemplateBase::newTemplate("test_main");
Expand Down Expand Up @@ -114,13 +119,16 @@ void create_project::createProject(const std::string &projectName)
drogon::utils::createPath("build");
drogon::utils::createPath("models");
drogon::utils::createPath("test");
drogon::utils::createPath("swagger");

std::ofstream gitFile(".gitignore", std::ofstream::out);
newGitIgFile(gitFile);
std::ofstream configFile("config.json", std::ofstream::out);
newConfigFile(configFile);
std::ofstream modelConfigFile("models/model.json", std::ofstream::out);
newModelConfigFile(modelConfigFile);
std::ofstream swaggerConfigFile("swagger/swagger.json", std::ofstream::out);
newSwaggerConfigFile(swaggerConfigFile);
std::ofstream testMainFile("test/test_main.cc", std::ofstream::out);
newTestMainFile(testMainFile);
std::ofstream testCmakeFile("test/CMakeLists.txt", std::ofstream::out);
Expand Down
Loading