-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
74 lines (59 loc) · 1.29 KB
/
info.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
package buildtag
import (
"go/build/constraint"
"go/token"
"go/types"
"regexp"
)
type Entry struct {
Package *types.Package
File string
Pos token.Pos
Constraint constraint.Expr
}
type Info struct {
Entries []Entry
}
func (info *Info) add(pkg *types.Package, file string, pos token.Pos, expr constraint.Expr) {
if expr == nil {
return
}
info.Entries = append(info.Entries, Entry{
Package: pkg,
File: file,
Pos: pos,
Constraint: expr,
})
}
func (info *Info) Matches(ok func(tag string) bool) []Entry {
var matches []Entry
if ok == nil {
matches = make([]Entry, len(info.Entries))
copy(matches, info.Entries)
return matches
}
for i := range info.Entries {
if info.Entries[i].Constraint.Eval(ok) {
matches = append(matches, info.Entries[i])
}
}
return matches
}
func (info *Info) Find(f func(constraint string) bool) []Entry {
var founds []Entry
for i := range info.Entries {
if f(info.Entries[i].Constraint.String()) {
founds = append(founds, info.Entries[i])
}
}
return founds
}
func (info *Info) FindByRegexp(reg *regexp.Regexp) []Entry {
var founds []Entry
for i := range info.Entries {
if reg.MatchString(info.Entries[i].Constraint.String()) {
founds = append(founds, info.Entries[i])
}
}
return founds
}