-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
217 lines (176 loc) · 5.07 KB
/
filter.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package pkgdmp
import (
"fmt"
"regexp"
"sort"
"strings"
)
// FilterAction configures a [SymbolFilterFn].
type FilterAction int
const (
Exclude FilterAction = iota // Exclude symbols.
Include // Include symbols.
)
// String returns a string representation of a filter action.
func (fa FilterAction) String() string {
return [...]string{
"Exclude",
"Include",
}[fa]
}
func (fa FilterAction) GoString() string {
return "pkgdmp." + fa.String()
}
// SymbolType represents a type of package symbol.
type SymbolType int
const (
SymbolUnknown SymbolType = iota
SymbolPackage // `package mypackage`
SymbolConst // `const myConst = ...`
SymbolIdentType // `type MyInt int`
SymbolFuncType // `type MyFunc func(...)`
SymbolStructType // `type MyStruct { ... }`
SymbolInterfaceType // `type MyInterface { ... }`
SymbolMapType // `type MyMap map[...]...`
SymbolChanType // `type MyChan chan ...`
SymbolArrayType // `type MyArray []string`
SymbolFunc // `func MyFunc(...) { ... }`
SymbolMethod // `func (...) MyMethod(...) { ... }`
SymbolStructField // Struct field.
SymbolParamField // Function parameter field.
SymbolResultField // Function result field.
SymbolReceiverField // Function Receiver field.
)
// unfilterableMap contains symbol types that filter functions should always
// return true for.
var unfilterableMap = map[SymbolType]struct{}{
SymbolPackage: {},
SymbolParamField: {},
SymbolResultField: {},
SymbolReceiverField: {},
}
// String returns a string representation of a symbol type.
func (st SymbolType) String() string {
return [...]string{
"SymbolUnknown",
"SymbolPackage",
"SymbolConst",
"SymbolIdentType",
"SymbolFunctionType",
"SymbolStructType",
"SymbolInterfaceType",
"SymbolMapType",
"SymbolChanType",
"SymbolArrayType",
"SymbolFunc",
"SymbolMethod",
"SymbolStructField",
"SymbolParamField",
"SymbolResultField",
"SymbolReceiverField",
}[st]
}
func (st SymbolType) GoString() string {
return "pkgdmp." + st.String()
}
// Symbol represents a symbol such as a const, type definition, or function.
type Symbol interface {
Ident() string
IsExported() bool
SymbolType() SymbolType
}
// SymbolFilter filters symbols by different conditions.
type SymbolFilter interface {
// Include should return true if symbol should be included according to
// the filter's logic and configuration.
Include(Symbol) bool
// String should return a string representation of the filter.
//
// This method is mainly intended for testing purposes.
String() string
}
// FilterUnexported creates a filter that determines whether to include or
// exclude unexported symbols.
func FilterUnexported(action FilterAction) SymbolFilter {
return &filterUnexported{action: action}
}
type filterUnexported struct {
action FilterAction
}
func (f *filterUnexported) Include(s Symbol) bool {
if isUnfilterable(s) {
return true
}
return f.action == Include || s.IsExported()
}
func (f *filterUnexported) String() string {
return fmt.Sprintf("filterUnexported(action=%s)", f.action)
}
// FilterSymbolTypes creates a filter function that determines whether to
// include or exclude symbols of different types.
func FilterSymbolTypes(action FilterAction, types ...SymbolType) SymbolFilter {
stMap := make(map[SymbolType]struct{}, len(types))
for _, t := range types {
stMap[t] = struct{}{}
}
return &filterSymbolTypes{
stMap: stMap,
action: action,
}
}
type filterSymbolTypes struct {
stMap map[SymbolType]struct{}
action FilterAction
}
func (f *filterSymbolTypes) Include(s Symbol) bool {
if isUnfilterable(s) {
return true
}
if s.SymbolType() == SymbolStructField {
return true
}
_, ok := f.stMap[s.SymbolType()]
if f.action == Include {
return ok
}
return !ok
}
func (f *filterSymbolTypes) String() string {
sts := make([]string, 0, len(f.stMap))
for st := range f.stMap {
sts = append(sts, st.String())
}
sort.Strings(sts)
return fmt.Sprintf("filterSymbolTypes(action=%s,symbolTypes=%s)", f.action, strings.Join(sts, ","))
}
// FilterSymbolTypes creates a filter function that determines whether to
// include or exclude symbols with matching idents.
func FilterMatchingIdents(action FilterAction, p *regexp.Regexp) SymbolFilter {
return &filterMatchingIdents{action: action, pattern: p}
}
type filterMatchingIdents struct {
pattern *regexp.Regexp
action FilterAction
}
func (f *filterMatchingIdents) Include(s Symbol) bool {
if isUnfilterable(s) {
return true
}
if s.SymbolType() == SymbolStructField {
return true
}
match := f.pattern.MatchString(s.Ident())
if f.action == Include {
return match
}
return !match
}
func (f *filterMatchingIdents) String() string {
return fmt.Sprintf("filterMatchingIdents(action=%s,pattern=%s)", f.action, f.pattern)
}
func isUnfilterable(s Symbol) bool {
if _, ok := unfilterableMap[s.SymbolType()]; ok {
return true
}
return false
}