-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathainsp.go
194 lines (161 loc) · 4.48 KB
/
ainsp.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// aahframework.org/ainsp source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// Package ainsp is a Go ast library for aah framework, it does inspect and
// discovers the Go `struct` which embeds particular type `struct`.
//
// For e.g.: `aahframework.org/{aah.Context, ws.Context}`, etc.
package ainsp
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"os"
"path/filepath"
"reflect"
"aahframework.org/essentials.v0"
)
var (
buildImportCache = map[string]string{}
// Reference: https://golang.org/pkg/builtin/
builtInDataTypes = map[string]bool{
"bool": true,
"byte": true,
"complex128": true,
"complex64": true,
"error": true,
"float32": true,
"float64": true,
"int": true,
"int16": true,
"int32": true,
"int64": true,
"int8": true,
"rune": true,
"string": true,
"uint": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uint8": true,
"uintptr": true,
}
errInvalidActionParam = errors.New("aah: invalid action parameter")
errInterfaceActionParam = errors.New("aah: 'interface{}' is not supported in the action parameter")
errMapActionParam = errors.New("aah: 'map' is not supported in the action parameter")
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package methods
//______________________________________________________________________________
// Inspect method processes the Go source code for the given directory and its
// sub-directories.
func Inspect(path string, excludes ess.Excludes, registeredActions map[string]map[string]uint8) (*Program, []error) {
prg := &Program{
Path: path,
Packages: []*packageInfo{},
RegisteredActions: registeredActions,
}
if err := validateInput(path); err != nil {
return prg, append([]error{}, err)
}
var (
pkgs map[string]*ast.Package
errs []error
)
err := ess.Walk(path, func(srcPath string, info os.FileInfo, err error) error {
if err != nil {
errs = append(errs, err)
}
// Excludes
if excludes.Match(filepath.Base(srcPath)) {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
if !info.IsDir() {
return nil
}
if info.IsDir() && ess.IsDirEmpty(srcPath) {
// skip directory if it's empty
return filepath.SkipDir
}
pfset := token.NewFileSet()
pkgs, err = parser.ParseDir(pfset, srcPath, func(f os.FileInfo) bool {
return !f.IsDir() && !excludes.Match(f.Name())
}, 0)
if err != nil {
if errList, ok := err.(scanner.ErrorList); ok {
// TODO parsing error list
fmt.Println(errList)
}
errs = append(errs, fmt.Errorf("error parsing dir[%s]: %s", srcPath, err))
return nil
}
pkg, err := validateAndGetPkg(pkgs, srcPath)
if err != nil {
errs = append(errs, err)
return nil
}
if pkg != nil {
pkg.Fset = pfset
pkg.FilePath = srcPath
pkg.ImportPath = stripGoPath(srcPath)
prg.Packages = append(prg.Packages, pkg)
}
return nil
})
if err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
prg.process()
}
return prg, errs
}
// FindFieldIndexes method does breadth-first search on struct
// anonymous field to find given type `struct` discover index positions.
//
// For e.g.: `aah.Context`, `ws.Context`, etc.
func FindFieldIndexes(targetTyp reflect.Type, searchTyp reflect.Type) [][]int {
var indexes [][]int
type nodeType struct {
val reflect.Value
index []int
}
queue := []nodeType{{reflect.New(targetTyp), []int{}}}
for len(queue) > 0 {
var (
node = queue[0]
elem = node.val
elemType = elem.Type()
)
if elemType.Kind() == reflect.Ptr {
elem = elem.Elem()
elemType = elem.Type()
}
queue = queue[1:]
if elemType.Kind() != reflect.Struct {
continue
}
for i := 0; i < elem.NumField(); i++ {
// skip non-anonymous fields
field := elemType.Field(i)
if !field.Anonymous {
continue
}
// If it's a search type then record the field index and move on
if field.Type == searchTyp {
indexes = append(indexes, append(node.index, i))
continue
}
fieldValue := elem.Field(i)
queue = append(queue,
nodeType{fieldValue, append(append([]int{}, node.index...), i)})
}
}
return indexes
}