-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmatcher.go
48 lines (40 loc) · 1.13 KB
/
matcher.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
package rbac
// A Matcher is a function that returns a bool representing
// whether or not the target matches some pre-defined pattern.
type Matcher func(target string) (bool, error)
// MatchAny will convert a slice of Matchers into a single Matcher
// that returns true if and only if at least one of the specified matchers returns true.
func MatchAny(matchers ...Matcher) Matcher {
return func(target string) (bool, error) {
for _, matcher := range matchers {
match, err := matcher(target)
if err != nil {
return false, err
}
if match {
return true, nil
}
}
return false, nil
}
}
// MatchAll will convert a slice of Matchers into a single Matcher
// that returns true if and only if all of the specified matchers returns true.
func MatchAll(matchers ...Matcher) Matcher {
return func(target string) (bool, error) {
for _, matcher := range matchers {
match, err := matcher(target)
if err != nil {
return false, err
}
if !match {
return false, nil
}
}
return true, nil
}
}
// Anything is a Matcher that always returns true
func Anything(target string) (bool, error) {
return true, nil
}