-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (78 loc) · 1.93 KB
/
main.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
package main
import (
"fmt"
"github.com/hopeio/cherry/utils/io/fs"
"go/ast"
"go/parser"
"go/token"
"os"
)
func main() {
packageName := "D:/code/hopeio/cherry/initialize"
fs.RangeDir(packageName, func(dir string, entries []os.DirEntry) ([]os.DirEntry, error) {
var recursion []os.DirEntry
for _, entry := range entries {
if entry.IsDir() {
recursion = append(recursion, entry)
getConfigStructs(dir + fs.PathSeparator + entry.Name())
}
}
return recursion, nil
})
}
// go递归读取某个包下的所有struct
func getStructs(packageName string) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, packageName, nil, parser.ParseComments)
if err != nil {
return
}
for _, pkg := range pkgs {
for _, file := range pkg.Files {
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok || genDecl.Tok != token.TYPE {
continue
}
for _, spec := range genDecl.Specs {
typeSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
_, ok = typeSpec.Type.(*ast.StructType)
if !ok {
continue
}
fmt.Printf("Struct %s found in file %s\n", typeSpec.Name.Name, fset.File(file.Pos()).Name())
}
}
}
}
}
func getConfigStructs(packageName string) {
var targetMethod = "Init"
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, packageName, nil, parser.ParseComments)
if err != nil {
return
}
for _, pkg := range pkgs {
for _, file := range pkg.Files {
ast.Inspect(file, func(n ast.Node) bool {
if x, ok := n.(*ast.FuncDecl); ok {
if x.Recv != nil && len(x.Recv.List) > 0 {
recvType := x.Recv.List[0].Type
if star, ok := recvType.(*ast.StarExpr); ok {
if ident, ok := star.X.(*ast.Ident); ok {
if x.Name.Name == targetMethod {
fmt.Printf("Struct %s implements method %s\n", ident.Name, targetMethod)
}
}
}
}
}
return true
})
}
}
}