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

beta #2

Merged
merged 4 commits into from
Jul 21, 2022
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
65 changes: 65 additions & 0 deletions annotation.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package annotation

import (
"errors"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"regexp"
"text/template"
)

type Config struct {
Directory string // Handler or Controller directory path.
Output string // Output file path; routes.go
Expand All @@ -19,3 +30,57 @@ type App struct {
func (app *App) SetConfig(config Config) {
app.Config = config
}

// Parses the handler directory.
func (app *App) ParseDirectory() {
fileSet := token.NewFileSet()
node, err := parser.ParseDir(fileSet, app.Directory, nil, parser.ParseComments)
if err != nil {
panic(err)
}
for _, v := range node {
for _, f := range v.Files {
for _, d := range f.Decls {
switch d.(type) {
case *ast.FuncDecl:
route, err := app.ParseAnnotationRoute(d.(*ast.FuncDecl).Doc.Text())
if err != nil {
log.Fatal(err)
}
route.Handler = d.(*ast.FuncDecl).Name.Name
app.Routes = append(app.Routes, route)
}
}
}
}
}
func (app *App) ParseAnnotationRoute(annotation string) (Route, error) {
regex := regexp.MustCompile(`@Route\(\"(.*?)\", name=\"(.*?)\", methods=\{\"(.*?)\"\}\)`)
match := regex.FindStringSubmatch(annotation)
if len(match) == 0 {
err := errors.New("Invalid annotation")
return Route{}, err
}
return Route{
Method: match[3],
Path: match[1],
Name: match[2],
}, nil
}

// Generates the routes.go file.
func (app *App) Generate() {
tmpl, err := template.ParseFiles("template/fiber.tmpl")
if err != nil {
panic(err)
}
file, err := os.Create(app.Output)
if err != nil {
panic(err)
}
defer file.Close()
tmpl.Execute(file, map[string]interface{}{
"Routes": app.Routes,
})

}
42 changes: 42 additions & 0 deletions annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,45 @@ func TestSetConfig(t *testing.T) {
t.Error("SetConfig() failed")
}
}
func TestParseDirectory(t *testing.T) {
app := new(annotation.App)
app.SetConfig(annotation.Config{
Directory: "./example",
Output: "routes.go",
})
app.ParseDirectory()
if len(app.Routes) == 0 {
t.Error("ParseDirectory() failed")
}
}
func TestParseAnnotationRoute(t *testing.T) {
app := new(annotation.App)
app.SetConfig(annotation.Config{
Directory: ".",
Output: "routes.go",
})
annotation := `@Route("/foo", name="foo", methods={"GET"})`
route, err := app.ParseAnnotationRoute(annotation)
if err != nil {
t.Error("ParseAnnotationRoute() failed")
}
if route.Method != "GET" {
t.Error("ParseAnnotationRoute() failed")
}
if route.Path != "/foo" {
t.Error("ParseAnnotationRoute() failed")
}
if route.Name != "foo" {
t.Error("ParseAnnotationRoute() failed")
}
}
func TestGenerate(t *testing.T) {
app := new(annotation.App)
app.SetConfig(annotation.Config{
Directory: "./handler",
Output: "routes.go",
})
app.ParseDirectory()
app.Generate()

}
5 changes: 5 additions & 0 deletions handler/Handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package handler

//@Route("/foo", name="foo", methods={"GET"})
func foo() {
}
10 changes: 10 additions & 0 deletions template/fiber.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main
import (
"github.com/gofiber/fiber/v2"
"app/handler"
)
func routeLoad(http *fiber.App) {
{{range .Routes}}
http.Add("{{.Method}}", "{{.Path}}", handler.{{.Handler}})
{{end}}
}