-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation.go
101 lines (92 loc) · 2.23 KB
/
annotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
Routes are first written in the comment lines.
Then the library I prepared reads these comment lines and creates automatic route functions.
It provides a Symfony framework or Flask-style routing environment.
It does not cause performance problems because the route function is already created.
*/
package annotation
import (
_ "embed"
"errors"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"regexp"
"text/template"
)
// Configures the application.
type Config struct {
Directory string // Handler or Controller directory path.
Output string // Output file path; routes.go
}
// Parses the annotation and returns the route.
type Route struct {
Method string // HTTP method
Path string // Path
Handler string // Handler name
Name string // Route name
}
// App is the application.
type App struct {
Config
Routes []Route
}
// Sets the configuration.
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
}
//go:embed template/fiber.tmpl
var fiberTemplate string
// Generates the routes.go file.
func (app *App) Generate() {
tmpl, err := template.New("fiber").Parse(fiberTemplate)
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,
})
}