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

Hahwul dev #55

Merged
merged 2 commits into from
Aug 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

| Language | Framework | URL | Method | Param | Header | WS |
|----------|-----------|-----|--------|-------|--------|----|
| Go | Echo | ✅ | ✅ | X | X | X |
| Go | Echo | ✅ | ✅ | ✅ | X | X |
| Python | Django | ✅ | X | X | X | X |
| Python | Flask | ✅ | X | X | X | X |
| Ruby | Rails | ✅ | ✅ | ✅ | ✅ | X |
Expand Down
9 changes: 9 additions & 0 deletions spec/functional_test/fixtures/go_echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ func main() {
return c.String(http.StatusOK, "Hello, World!")
})
e.GET("/pet", func(c echo.Context) error {
_ = c.QueryParam("query")
return c.String(http.StatusOK, "Hello, Pet!")
})
e.POST("/pet", func(c echo.Context) error {
_ = c.Param("name")
return c.String(http.StatusOK, "Hello, Pet!")
})
e.POST("/pet_form", func(c echo.Context) error {
_ = c.FormValue("name")
return c.String(http.StatusOK, "Hello, Pet!")
})
e.Static("/public", "public")
Expand Down
12 changes: 10 additions & 2 deletions spec/functional_test/testers/go_echo_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ require "../func_spec.cr"

extected_endpoints = [
Endpoint.new("/", "GET"),
Endpoint.new("/pet", "GET"),
Endpoint.new("/pet", "GET", [
Param.new("query", "", "query"),
]),
Endpoint.new("/pet", "POST", [
Param.new("name", "", "json"),
]),
Endpoint.new("/pet_form", "POST", [
Param.new("name", "", "body"),
]),
Endpoint.new("/public/secret.html", "GET"),
]

FunctionalTester.new("fixtures/go_echo/", {
:techs => 1,
:endpoints => 3,
:endpoints => 5,
}, extected_endpoints).test_all
38 changes: 36 additions & 2 deletions src/analyzer/analyzers/analyzer_go_echo.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ class AnalyzerGoEcho < Analyzer
next if File.directory?(path)
if File.exists?(path) && File.extname(path) == ".go"
File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|
last_endpoint = Endpoint.new("", "")
file.each_line do |line|
if line.includes?(".GET(") || line.includes?(".POST(") || line.includes?(".PUT(") || line.includes?(".DELETE(")
get_route_path(line).tap do |route_path|
if route_path.size > 0
result << Endpoint.new("#{url}#{route_path}", line.split(".")[1].split("(")[0])
new_endpoint = Endpoint.new("#{url}#{route_path}", line.split(".")[1].split("(")[0])
result << new_endpoint
last_endpoint = new_endpoint
end
end
end

if line.includes?("Param(") || line.includes?("FormValue(")
get_param(line).tap do |param|
if param.name.size > 0 && last_endpoint.method != ""
last_endpoint.params << param
end
end
end
Expand All @@ -30,7 +41,7 @@ class AnalyzerGoEcho < Analyzer
end

public_dirs.each do |p_dir|
full_path = (base_path + "/" + p_dir["file_path"]).gsub("//","/")
full_path = (base_path + "/" + p_dir["file_path"]).gsub("//", "/")
Dir.glob("#{full_path}/**/*") do |path|
next if File.directory?(path)
if File.exists?(path)
Expand All @@ -48,6 +59,29 @@ class AnalyzerGoEcho < Analyzer
result
end

def get_param(line : String) : Param
param_type = "json"
if line.includes?("QueryParam")
param_type = "query"
end
if line.includes?("FormValue")
param_type = "body"
end

first = line.strip.split("(")
if first.size > 1
second = first[1].split(")")
if second.size > 1
param_name = second[0].gsub("\"", "")
rtn = Param.new(param_name, "", param_type)

return rtn
end
end

Param.new("", "", "")
end

def get_static_path(line : String) : Hash(String, String)
first = line.strip.split("(")
if first.size > 1
Expand Down
Loading