-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
85 lines (79 loc) · 1.81 KB
/
process.go
File metadata and controls
85 lines (79 loc) · 1.81 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
package gingen
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
)
var pkgName string
//ProcessDir ...
func ProcessDir() (rs RouteList, err error) {
return processDir("./")
}
func processDir(dir string) (rs RouteList, err error) {
var tempFuncs FunctionList
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
for _, file := range files {
fInfo, err := file.Info()
if err != nil {
return nil, err
}
if !fInfo.IsDir() && strings.HasSuffix(fInfo.Name(), ".go") && (!strings.HasSuffix(fInfo.Name(), suffix)) {
cmap, err := cmapgen(dir + "/" + fInfo.Name())
if err != nil {
return nil, err
}
rt, fs, err := processFile(cmap, fInfo.Name())
if err != nil {
return nil, err
}
//assign function
for i, v := range rt {
in, ex := fs.splitByRecv(v.Name)
in2, tempFuncs := tempFuncs.splitByRecv(v.Name)
tempFuncs = append(tempFuncs, ex...)
rt[i].FunctionList = append(rt[i].FunctionList, in...)
rt[i].FunctionList = append(rt[i].FunctionList, in2...)
}
rs = append(rs, rt...)
}
}
return
}
func cmapgen(fileName string) (ast.CommentMap, error) {
fset := token.NewFileSet()
path, _ := filepath.Abs(fileName)
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
return nil, err
}
if pkgName == "" {
pkgName = f.Name.Name
}
return ast.NewCommentMap(fset, f, f.Comments), nil
}
func processFile(cmap ast.CommentMap, fileName string) (rs RouteList, fs FunctionList, err error) {
for k, v := range cmap {
var fInfo *FunctionInfo
fInfo, err = checkFunc(k, v)
if err != nil {
err = fmt.Errorf("%v: %v", fileName, err.Error())
return
}
if fInfo != nil {
fs = append(fs, fInfo)
continue
}
rInfo, ok := checkStruct(k, v)
if ok {
rs = append(rs, rInfo)
}
}
return
}