forked from micheartin/docgen-yes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
102 lines (84 loc) · 1.9 KB
/
builder.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
102
package docgen
import (
"errors"
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
)
func BuildDoc(r chi.Routes) (Doc, error) {
d := Doc{}
if getGoPath() == "" {
return d, errors.New("docgen: unable to determine your $GOPATH")
}
// Walk and generate the router docs
d.Router = BuildDocRouter(r)
return d, nil
}
func BuildDocRouter(r chi.Routes) DocRouter {
if r == nil {
return DocRouter{}
}
rts := r
dr := DocRouter{
Middlewares: []DocMiddleware{},
Routes: map[string]DocRoute{},
}
dr.Routes = DocRoutes{}
for _, mw := range rts.Middlewares() {
dmw := DocMiddleware{
FuncInfo: GetFuncInfo(mw),
}
dr.Middlewares = append(dr.Middlewares, dmw)
}
for _, rt := range rts.Routes() {
drt := DocRoute{
Pattern: rt.Pattern,
Handlers: DocHandlers{},
Router: &DocRouter{
Middlewares: []DocMiddleware{},
Routes: map[string]DocRoute{},
},
}
if rt.SubRoutes != nil {
subRoutes := rt.SubRoutes
subDrts := BuildDocRouter(subRoutes)
drt.Router = &subDrts
} else {
hall := rt.Handlers["*"]
for method, h := range rt.Handlers {
if method != "*" && hall != nil && fmt.Sprint(hall) == fmt.Sprint(h) {
continue
}
dh := DocHandler{
Middlewares: []DocMiddleware{},
Method: method,
FuncInfo: FuncInfo{
Pkg: "",
Func: "",
Comment: "",
File: "",
Line: 0,
Anonymous: false,
Unresolvable: false,
},
}
var endpoint http.Handler
chain, _ := h.(*chi.ChainHandler)
if chain != nil {
for _, mw := range chain.Middlewares {
dh.Middlewares = append(dh.Middlewares, DocMiddleware{
FuncInfo: GetFuncInfo(mw),
})
}
endpoint = chain.Endpoint
} else {
endpoint = h
}
dh.FuncInfo = GetFuncInfo(endpoint)
drt.Handlers[method] = dh
}
}
dr.Routes[rt.Pattern] = drt
}
return dr
}