/
schema.go
96 lines (82 loc) · 2.05 KB
/
schema.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
package schema
import (
"go/ast"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/jiaxinwang/go-idiocy/logger"
"github.com/jiaxinwang/err2"
"golang.org/x/mod/modfile"
)
func init() {
}
var ProjSchema *Schema
type Schema struct {
ModuleFilePath string
ModulePath string
cacheStore *sync.Map
SourceFile []SourceFile
GinIdentifiers []*GinIdentifier
GinAPIs []*GinAPI
}
func NewSchema(modFilePath string) *Schema {
var err error
defer err2.Handle(&err, func() {
logger.S.Errorw("can't create Schema", "err", err)
})
ret := new(Schema)
ret.GinIdentifiers = make([]*GinIdentifier, 0)
ret.GinAPIs = make([]*GinAPI, 0)
err2.Try(ret.ParseModules(modFilePath))
return ret
}
func (schema *Schema) ParseModules(filename string) (err error) {
defer err2.Return(&err)
content := err2.Bytes.Try(ioutil.ReadFile(filename))
schema.ModuleFilePath = filename
schema.ModulePath = modfile.ModulePath(content)
return nil
}
var skips = []string{
".git",
"vendor",
}
func (schema *Schema) LoadSourceFiles() (err error) {
sourceRoot := path.Dir(schema.ModuleFilePath)
err = filepath.Walk(sourceRoot, func(filename string, info os.FileInfo, err error) error {
if info.IsDir() {
for _, v := range skips {
if strings.EqualFold(filename, path.Join(sourceRoot, v)) {
return filepath.SkipDir
}
}
} else {
if strings.EqualFold(filepath.Ext(filename), ".go") {
schema.SourceFile = append(schema.SourceFile, SourceFile{FullPath: filename, Path: filepath.Dir(filename)})
}
}
return nil
})
return nil
}
func (s *Schema) GinIdentifierWithFileIdent(source *SourceFile, callExpr *ast.CallExpr) *GinIdentifier {
another := GinIdentifier{Source: source, InstancingCall: callExpr}
for _, v := range s.GinIdentifiers {
if v.Equal(&another) {
return v
}
}
return nil
}
func (s *Schema) AddGinIdentifier(source *SourceFile, ident *GinIdentifier) bool {
for _, v := range s.GinIdentifiers {
if ident.Equal(v) {
return false
}
}
s.GinIdentifiers = append(s.GinIdentifiers, ident)
return true
}