-
Notifications
You must be signed in to change notification settings - Fork 1
/
predicates.go
117 lines (100 loc) · 2.06 KB
/
predicates.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
package yit
import (
"strings"
"gopkg.in/yaml.v3"
)
var (
All = func(node *yaml.Node) bool {
return true
}
None = func(node *yaml.Node) bool {
return false
}
StringValue = Intersect(
WithKind(yaml.ScalarNode),
WithShortTag("!!str"),
)
)
func Intersect(ps ...Predicate) Predicate {
return func(node *yaml.Node) bool {
for _, p := range ps {
if !p(node) {
return false
}
}
return true
}
}
func Union(ps ...Predicate) Predicate {
return func(node *yaml.Node) bool {
for _, p := range ps {
if p(node) {
return true
}
}
return false
}
}
func Negate(p Predicate) Predicate {
return func(node *yaml.Node) bool {
return !p(node)
}
}
func WithStringValue(value string) Predicate {
return Intersect(
StringValue,
func(node *yaml.Node) bool {
return node.Value == value
},
)
}
func WithShortTag(tag string) Predicate {
return func(node *yaml.Node) bool {
return node.ShortTag() == tag
}
}
func WithValue(value string) Predicate {
return func(node *yaml.Node) bool {
return node.Value == value
}
}
func WithKind(kind yaml.Kind) Predicate {
return func(node *yaml.Node) bool {
return node.Kind == kind
}
}
func WithMapKey(key string) Predicate {
return func(node *yaml.Node) bool {
return FromNode(node).MapKeys().AnyMatch(WithValue(key))
}
}
func WithMapValue(value string) Predicate {
return func(node *yaml.Node) bool {
return FromNode(node).MapValues().AnyMatch(WithValue(value))
}
}
func WithMapKeyValue(keyPredicate, valuePredicate Predicate) Predicate {
return Intersect(
WithKind(yaml.MappingNode),
func(node *yaml.Node) bool {
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i]
value := node.Content[i+1]
if keyPredicate(key) && valuePredicate(value) {
return true
}
}
return false
},
)
}
func WithPrefix(prefix string) Predicate {
return func(node *yaml.Node) bool {
return strings.HasPrefix(node.Value, prefix)
}
}
func WithSuffix(suffix string) Predicate {
return func(node *yaml.Node) bool {
return strings.HasSuffix(node.Value, suffix)
}
}