-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add support for Swagger #923
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
Draft
an-tao
wants to merge
18
commits into
master
Choose a base branch
from
swagger
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5c88630
Add support for swagger
an-tao de39fa2
Add create_swagger.cc
an-tao dc67f6e
update
an-tao f6aa119
Find .h
an-tao 304bce4
Update create_sagger
an-tao 624553a
libclang
an-tao ec3f079
Remove the llvm dependency
an-tao 7346ea4
use regex to find namespace
an-tao 66878d2
Parsing header works
an-tao c467c99
fix
an-tao c35c1f2
Fix for old compiler
an-tao 31832ac
Add some class definations for swagger
an-tao 77303e4
Update
an-tao d9d1990
sys log
an-tao acde804
flag
an-tao 745346f
Update
an-tao 3ce5e28
Lint
an-tao 3c76e68
Cpplint
an-tao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,3 +48,4 @@ install | |
| trace.json | ||
| .cache/ | ||
| build_examples/ | ||
| .kiro | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| #include "HandlerParser.h" | ||
| #include <iostream> | ||
| #include <regex> | ||
|
|
||
| using namespace drogon::internal; | ||
|
|
||
| std::pair<std::string, std::string> StructNode::findContentOfClassOrNameSpace( | ||
| 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"; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)