-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregators.go
74 lines (64 loc) · 1.41 KB
/
aggregators.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 filters
import vocab "github.com/go-ap/activitypub"
type notCrit []Check
func (n notCrit) Match(it vocab.Item) bool {
if len(n) == 0 {
return false
}
f := n[0]
if f == nil {
return false
}
return !f.Match(it)
}
// Not negates the result of a Check function.
// It is equivalent to a unary NOT operator.
func Not(fn Check) Check {
return notCrit([]Check{fn})
}
type checkAny []Check
func (a checkAny) Match(it vocab.Item) bool {
for _, fn := range a {
if fn == nil {
continue
}
if fn.Match(it) {
return true
}
}
return false
}
// Any aggregates a list of individual Check functions into a single Check
// which resolves to false if all the individual members resolve as false,
// and true if any of them resolves as true.
// It is equivalent to a sequence of OR operators.
func Any(fns ...Check) Check {
if len(fns) == 1 {
return fns[0]
}
return checkAny(fns)
}
type checkAll []Check
func (a checkAll) Match(it vocab.Item) bool {
if len(a) == 0 {
return true
}
for _, fn := range a {
if fn == nil {
continue
}
if !fn.Match(it) {
return false
}
}
return true
}
// All aggregates a list of individual Check functions into a single Check
// which resolves true if all individual members resolve as true, and false otherwise.
// It is equivalent to a sequence of AND operators.
func All(fns ...Check) Check {
if len(fns) == 1 {
return fns[0]
}
return checkAll(fns)
}