util.go
1.69 KB
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
package docgen
import (
"go/parser"
"go/token"
"reflect"
"runtime"
)
func getCallerFrame(i interface{}) *runtime.Frame {
pc := reflect.ValueOf(i).Pointer()
frames := runtime.CallersFrames([]uintptr{pc})
if frames == nil {
return nil
}
frame, _ := frames.Next()
if frame.Entry == 0 {
return nil
}
return &frame
}
func getPkgName(file string) string {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, file, nil, parser.PackageClauseOnly)
if err != nil {
return ""
}
if astFile.Name == nil {
return ""
}
return astFile.Name.Name
}
func getFuncComment(file string, line int) string {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
if err != nil {
return ""
}
if len(astFile.Comments) == 0 {
return ""
}
for _, cmt := range astFile.Comments {
if fset.Position(cmt.End()).Line+1 == line {
return cmt.Text()
}
}
return ""
}
func copyDocRouter(dr DocRouter) DocRouter {
var cloneRouter func(dr DocRouter) DocRouter
var cloneRoutes func(drt DocRoutes) DocRoutes
cloneRoutes = func(drts DocRoutes) DocRoutes {
rts := DocRoutes{}
for pat, drt := range drts {
rt := DocRoute{Pattern: drt.Pattern}
if len(drt.Handlers) > 0 {
rt.Handlers = DocHandlers{}
for meth, dh := range drt.Handlers {
rt.Handlers[meth] = dh
}
}
if drt.Router != nil {
rr := cloneRouter(*drt.Router)
rt.Router = &rr
}
rts[pat] = rt
}
return rts
}
cloneRouter = func(dr DocRouter) DocRouter {
cr := DocRouter{}
cr.Middlewares = make([]DocMiddleware, len(dr.Middlewares))
copy(cr.Middlewares, dr.Middlewares)
cr.Routes = cloneRoutes(dr.Routes)
return cr
}
return cloneRouter(dr)
}