-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching.go
113 lines (94 loc) · 2.56 KB
/
matching.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
package newsdoc
// BlockMatcher checks if a block matches a condition.
type BlockMatcher interface {
// Match returns true if the block matches the condition.
Match(block Block) bool
}
// BlockMatchFunc is a custom BlockMatcher function.
type BlockMatchFunc func(block Block) bool
// Implements BlockMatcher.
func (fn BlockMatchFunc) Match(block Block) bool {
return fn(block)
}
// BlockRole can be used to check that a block has a specific role.
type BlockRole string
// Implements BlockMatcher.
func (role BlockRole) Match(block Block) bool {
return block.Role == string(role)
}
// BlockMatchesAll returns a block matcher that returns true if a block matches
// all the conditions.
func BlockMatchesAll(matchers ...BlockMatcher) BlockMatcher {
return BlockMatchFunc(func(block Block) bool {
for _, m := range matchers {
if !m.Match(block) {
return false
}
}
return true
})
}
// BlockMatchesAny returns a block matcher that returns true if a block matches
// any of the conditions.
func BlockMatchesAny(matchers ...BlockMatcher) BlockMatcher {
return BlockMatchFunc(func(block Block) bool {
for _, m := range matchers {
if m.Match(block) {
return true
}
}
return false
})
}
// BlockDoesntMatch returns a block matcher that negates the selector.
func BlockDoesntMatch(selector BlockMatcher) BlockMatcher {
return BlockMatchFunc(func(block Block) bool {
return !selector.Match(block)
})
}
// BlocksWithType returns a BlockMatcher that matches blocks with the given
// type.
func BlocksWithType(blockType string) BlockMatcher {
return blockSelector{
bType: &blockType,
}
}
// BlocksWithRel returns a BlockMatcher that matches blocks with the given rel.
func BlocksWithRel(rel string) BlockMatcher {
return blockSelector{
rel: &rel,
}
}
// BlocksWithTypeAndRel returns a BlockMatcher that matches blocks with the
// given type and rel.
func BlocksWithTypeAndRel(blockType string, rel string) BlockMatcher {
return blockSelector{
bType: &blockType,
rel: &rel,
}
}
// BlocksWithTypeAndRole returns a BlockMatcher that matches blocks with the
// given type and role.
func BlocksWithTypeAndRole(blockType string, role string) BlockMatcher {
return blockSelector{
bType: &blockType,
role: &role,
}
}
type blockSelector struct {
bType *string
rel *string
role *string
}
func (sel blockSelector) Match(block Block) bool {
if sel.bType != nil && *sel.bType != block.Type {
return false
}
if sel.rel != nil && *sel.rel != block.Rel {
return false
}
if sel.role != nil && *sel.role != block.Role {
return false
}
return true
}