/
status_err_scanner.go
157 lines (129 loc) · 4.29 KB
/
status_err_scanner.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package scanner
import (
"fmt"
"github.com/profzone/eden-framework/pkg/courier/status_error"
"github.com/profzone/eden-framework/pkg/packagex"
"go/ast"
"go/types"
"sort"
"strings"
)
func NewStatusErrScanner(pkg *packagex.Package) *StatusErrScanner {
statusErrorScanner := &StatusErrScanner{
pkg: pkg,
statusErrorTypes: map[*types.Named][]*status_error.StatusError{},
errorsUsed: map[*types.Func][]*status_error.StatusError{},
}
statusErrorScanner.init()
return statusErrorScanner
}
type StatusErrScanner struct {
StatusErrType *types.Named
pkg *packagex.Package
statusErrorTypes map[*types.Named][]*status_error.StatusError
errorsUsed map[*types.Func][]*status_error.StatusError
}
func (scanner *StatusErrScanner) StatusErrorsInFunc(typeFunc *types.Func) []*status_error.StatusError {
if typeFunc == nil {
return nil
}
if statusErrList, ok := scanner.errorsUsed[typeFunc]; ok {
return statusErrList
}
scanner.errorsUsed[typeFunc] = []*status_error.StatusError{}
pkg := packagex.NewPackage(scanner.pkg.Pkg(typeFunc.Pkg().Path()))
funcDecl := pkg.FuncDeclOf(typeFunc)
if funcDecl != nil {
ast.Inspect(funcDecl, func(node ast.Node) bool {
switch node.(type) {
case *ast.CallExpr:
identList := packagex.GetIdentChainOfCallFunc(node.(*ast.CallExpr).Fun)
if len(identList) > 0 {
callIdent := identList[len(identList)-1]
obj := pkg.TypesInfo.ObjectOf(callIdent)
if obj != nil && obj.Pkg() != nil && obj.Pkg().Path() == scanner.StatusErrType.Obj().Pkg().Path() {
for i := range identList {
scanner.mayAddStateErrorByObject(typeFunc, pkg.TypesInfo.ObjectOf(identList[i]))
}
return false
}
if nextTypeFunc, ok := obj.(*types.Func); ok && nextTypeFunc != typeFunc && nextTypeFunc.Pkg() != nil {
scanner.appendStateErrs(typeFunc, scanner.StatusErrorsInFunc(nextTypeFunc)...)
}
}
case *ast.Ident:
scanner.mayAddStateErrorByObject(typeFunc, pkg.TypesInfo.ObjectOf(node.(*ast.Ident)))
}
return true
})
doc := packagex.StringifyCommentGroup(funcDecl.Doc)
scanner.appendStateErrs(typeFunc, pickStatusErrorsFromDoc(doc)...)
}
return scanner.errorsUsed[typeFunc]
}
func (scanner *StatusErrScanner) mayAddStateErrorByObject(typeFunc *types.Func, obj types.Object) {
if obj == nil {
return
}
if typeConst, ok := obj.(*types.Const); ok {
if named, ok := typeConst.Type().(*types.Named); ok {
if errs, ok := scanner.statusErrorTypes[named]; ok {
for i := range errs {
if errs[i].Key == typeConst.Name() {
scanner.appendStateErrs(typeFunc, errs[i])
}
}
}
}
}
}
func (scanner *StatusErrScanner) appendStateErrs(typeFunc *types.Func, statusErrs ...*status_error.StatusError) {
m := map[string]*status_error.StatusError{}
errs := append(scanner.errorsUsed[typeFunc], statusErrs...)
for i := range errs {
s := errs[i]
m[fmt.Sprintf("%s%d", s.Key, s.Code)] = s
}
next := make([]*status_error.StatusError, 0)
for k := range m {
next = append(next, m[k])
}
sort.Slice(next, func(i, j int) bool {
return next[i].Code < next[j].Code
})
scanner.errorsUsed[typeFunc] = next
}
func (scanner *StatusErrScanner) init() {
pkg := scanner.pkg.Pkg("github.com/profzone/eden-framework/pkg/courier/status_error")
if pkg == nil {
return
}
scanner.StatusErrType = packagex.NewPackage(pkg).TypeName("StatusError").Type().(*types.Named)
//ttypeStatusError := packagex.NewPackage(pkg).TypeName("StatusError").Type().Underlying().(*types.Interface)
//
//isStatusError := func(typ *types.TypeName) bool {
// return types.Implements(typ.Type(), ttypeStatusError)
//}
//s := NewStatusErrorScanner(scanner.pkg)
for _, pkgInfo := range scanner.pkg.AllPackages {
for _, obj := range pkgInfo.TypesInfo.Defs {
if _, ok := obj.(*types.TypeName); ok {
//if isStatusError(typName) {
// scanner.statusErrorTypes[typName.Type().(*types.Named)] = s.StatusError(typName)
//}
}
}
}
}
func pickStatusErrorsFromDoc(doc string) []*status_error.StatusError {
statusErrorList := make([]*status_error.StatusError, 0)
lines := strings.Split(doc, "\n")
for _, line := range lines {
if line != "" {
if statusErr := status_error.ParseString(line); statusErr != nil {
statusErrorList = append(statusErrorList, statusErr)
}
}
}
return statusErrorList
}