-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchain_matcher.go
More file actions
38 lines (31 loc) · 923 Bytes
/
chain_matcher.go
File metadata and controls
38 lines (31 loc) · 923 Bytes
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
package gomatch
import "errors"
var errMatcherNotFound = errors.New("none of matchers could be used")
// A ChainMatcher allows to chain multiple value matchers
type ChainMatcher struct {
matchers []ValueMatcher
}
// CanMatch returns true if pattern p can be handled by any of internal matchers
func (m *ChainMatcher) CanMatch(p interface{}) bool {
for _, m := range m.matchers {
if m.CanMatch(p) {
return true
}
}
return false
}
// Match performs value matching against given pattern.
// It iterates through internal matchers and uses first which can handle given pattern.
func (m *ChainMatcher) Match(p, v interface{}) (bool, error) {
for _, m := range m.matchers {
if !m.CanMatch(p) {
continue
}
return m.Match(p, v)
}
return false, errMatcherNotFound
}
// NewChainMatcher creates ChainMatcher.
func NewChainMatcher(matchers []ValueMatcher) *ChainMatcher {
return &ChainMatcher{matchers}
}