-
Notifications
You must be signed in to change notification settings - Fork 234
/
selector.go
237 lines (195 loc) · 4.8 KB
/
selector.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package selector
import (
"go/ast"
"go/token"
"regexp"
"sort"
"strings"
"golang.org/x/exp/slices"
meta "encr.dev/proto/encore/parser/meta/v1"
"encr.dev/v2/internals/perr"
)
type Type string
// NOTE: New types added should be also added to meta.proto and est/selectors.go.
const (
All Type = "all"
Tag Type = "tag"
)
type Selector struct {
// Ensure this type is not comparable,
// since it contains position information.
_ [0]func()
Type Type
Value string
startPos token.Pos
endPos token.Pos
}
// Key is a suitable key for a [Selector],
// since selectors are not otherwise comparable.
type Key struct {
Type Type
Value string
}
var _ ast.Node = Selector{}
func (s Selector) Pos() token.Pos {
return s.startPos
}
func (s Selector) End() token.Pos {
return s.endPos
}
func (s Selector) String() string {
if s.Type == All {
return "all"
}
return string(s.Type) + ":" + s.Value
}
// Key returns a Key for s.
func (s Selector) Key() Key {
return Key{Type: s.Type, Value: s.Value}
}
// Equals reports whether s and o are equal on type and value.
//
// It does not compare the start and end positions.
func (s Selector) Equals(o Selector) bool {
return s.Key() == o.Key()
}
func (s Selector) ToProto() *meta.Selector {
pb := &meta.Selector{Type: meta.Selector_UNKNOWN, Value: s.Value}
switch s.Type {
case All:
pb.Type = meta.Selector_ALL
case Tag:
pb.Type = meta.Selector_TAG
}
return pb
}
func Parse(errs *perr.List, startPos token.Pos, s string) (Selector, bool) {
if s == "all" {
return Selector{Type: All, Value: ""}, true
}
typ, val, ok := strings.Cut(s, ":")
if !ok {
errs.Add(errMissingSelectorType.AtGoPos(startPos, startPos+token.Pos(len([]byte(s)))))
return Selector{}, false
}
sel := Selector{
Type: Type(typ),
Value: val,
startPos: startPos,
endPos: startPos + token.Pos(len([]byte(s))),
}
var re *regexp.Regexp
switch sel.Type {
case Tag:
re = tagRegexp
default:
errs.Add(errUnknownSelectorType(typ).AtGoNode(sel))
return Selector{}, false
}
if !re.MatchString(val) {
errs.Add(errInvalidSelectorValue(val).AtGoNode(sel))
return Selector{}, false
}
return sel, true
}
var (
tagRegexp = regexp.MustCompile(`^[a-z]([-_a-z0-9]*[a-z0-9])?$`)
)
type Set struct {
vals []Selector
}
func NewSet(sels ...Selector) Set {
var s Set
for _, sel := range sels {
s.Add(sel)
}
return s
}
// Add adds a selector to the set. It reports whether the selector was added,
// meaning it reports false iff the set already contained that selector.
//
// Add ensures that the set is sorted.
func (s *Set) Add(sel Selector) (added bool) {
idx := sort.Search(len(s.vals), func(i int) bool {
v := &s.vals[i]
return v.Type >= sel.Type && v.Value >= sel.Value
})
if idx < len(s.vals) && (s.vals)[idx].Equals(sel) {
return false
}
// Insert at the end
if idx == len(s.vals) {
s.vals = append(s.vals, sel)
return true
}
// Make space for the new selector by shifting the rest of the slice
s.vals = append(s.vals[:idx+1], s.vals[idx:]...)
// Insert the new selector
s.vals[idx] = sel
return true
}
// Merge adds all the selectors from other to s.
//
// It is equivalent to calling Add for each selector in other.
func (s *Set) Merge(other Set) {
for _, sel := range other.vals {
s.Add(sel)
}
}
// Contains reports whether the set contains the given selector.
func (s *Set) Contains(sel Selector) bool {
idx := sort.Search(len(s.vals), func(i int) bool {
v := &s.vals[i]
return v.Type >= sel.Type && v.Value >= sel.Value
})
return idx < len(s.vals) && (s.vals)[idx].Equals(sel)
}
// ContainsAny reports whether the set contains any of the selectors in other.
//
// It compares in linear time O(N) where N is the number of selectors in the
// larger set. It is faster than calling Contains for each selector in other.
func (s *Set) ContainsAny(other Set) bool {
// If this set contains the "all" selector it always matches.
if slices.IndexFunc(s.vals, func(sel Selector) bool { return sel.Type == All }) != -1 {
return true
}
i, j := 0, 0
for i < len(s.vals) && j < len(other.vals) {
iv, jv := &s.vals[i], &other.vals[j]
switch {
case iv.Type == All:
return true
case iv.Type == jv.Type:
if s.vals[i].Equals(other.vals[j]) {
return true
} else if iv.Value < jv.Value {
i++
} else {
j++
}
case iv.Type < jv.Type:
i++
case iv.Type > jv.Type:
j++
}
}
return false
}
// ForEach calls fn for each selector in the set.
func (s *Set) ForEach(fn func(Selector)) {
for _, sel := range s.vals {
fn(sel)
}
}
// Len returns the number of selectors in the set.
func (s *Set) Len() int {
return len(s.vals)
}
// ToProto returns the set as a slice of proto selectors.
func (s *Set) ToProto() []*meta.Selector {
pbs := make([]*meta.Selector, len(s.vals))
for i, sel := range s.vals {
pbs[i] = sel.ToProto()
}
return pbs
}