-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.go
197 lines (161 loc) · 3.37 KB
/
cond.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
package topic
type source struct {
key string
}
type condAnd struct {
source Expr
targets []Expr
}
type condOr struct {
source Expr
targets []Expr
}
type condNot struct {
source Expr
targets []Expr
}
// And returns Expr
func And(source Expr, targets ...Expr) Expr {
if len(targets) == 0 {
return source
}
var cond condAnd
switch s := source.(type) {
case condAnd:
cond = s
default:
cond = condAnd{
source: source,
}
}
for _, target := range targets {
switch t := target.(type) {
case condAnd:
cond.targets = append([]Expr{t.source}, t.targets...)
default:
cond.targets = append(cond.targets, t)
}
}
return cond
}
// Or returns Expr
func Or(source Expr, targets ...Expr) Expr {
if len(targets) == 0 {
return source
}
var cond condOr
switch s := source.(type) {
case condOr:
cond = s
default:
cond = condOr{
source: source,
}
}
for _, target := range targets {
switch t := target.(type) {
case condOr:
cond.targets = append([]Expr{t.source}, t.targets...)
default:
cond.targets = append(cond.targets, t)
}
}
return cond
}
// Not returns Expr
func Not(source Expr, targets ...Expr) Expr {
if len(targets) == 0 {
return source
}
var cond condNot
switch s := source.(type) {
case condNot:
cond = s
default:
cond = condNot{
source: source,
}
}
for _, target := range targets {
switch t := target.(type) {
case condNot:
cond.targets = append([]Expr{t.source}, t.targets...)
default:
cond.targets = append(cond.targets, t)
}
}
return cond
}
// Source returns Expr from topic.
func Source(key string) Expr {
return source{key}
}
func (e source) And(targets ...Expr) Expr {
return And(e, targets...)
}
func (e source) Or(targets ...Expr) Expr {
return Or(e, targets...)
}
func (e source) Not(targets ...Expr) Expr {
return Not(e, targets...)
}
func (e source) Exec(p Pipeline) string {
return p.Source(e.key)
}
func (e condAnd) And(targets ...Expr) Expr {
return And(e, targets...)
}
func (e condAnd) Or(targets ...Expr) Expr {
return Or(e, targets...)
}
func (e condAnd) Not(targets ...Expr) Expr {
return Not(e, targets...)
}
func (e condAnd) Exec(pipe Pipeline) string {
keys := make([]string, 0, len(e.targets)+1)
keys = append(keys, e.source.Exec(pipe))
for _, target := range e.targets {
keys = append(keys, target.Exec(pipe))
}
dest := pipe.Session()
pipe.Inter(dest, keys...)
return dest
}
func (e condOr) And(targets ...Expr) Expr {
return And(e, targets...)
}
func (e condOr) Or(targets ...Expr) Expr {
return Or(e, targets...)
}
func (e condOr) Not(targets ...Expr) Expr {
return Not(e, targets...)
}
func (e condOr) Exec(pipe Pipeline) string {
keys := make([]string, 0, len(e.targets)+1)
keys = append(keys, e.source.Exec(pipe))
for _, target := range e.targets {
keys = append(keys, target.Exec(pipe))
}
dest := pipe.Session()
pipe.Union(dest, keys...)
return dest
}
func (e condNot) And(targets ...Expr) Expr {
return And(e, targets...)
}
func (e condNot) Or(targets ...Expr) Expr {
return Or(e, targets...)
}
func (e condNot) Not(targets ...Expr) Expr {
return Not(e, targets...)
}
func (e condNot) Exec(pipe Pipeline) string {
keys := make([]string, 0, len(e.targets)+1)
keys = append(keys, e.source.Exec(pipe))
for _, target := range e.targets {
keys = append(keys, target.Exec(pipe))
}
dest := pipe.Session()
pipe.Diff(dest, keys...)
return dest
}