This repository has been archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrule.go
122 lines (106 loc) · 2.92 KB
/
rule.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
118
119
120
121
122
package ruler
/*
This struct is the main format for rules or conditions in ruler-compatable libraries.
Here's a sample in JSON format:
{
"comparator": "eq",
"path": "person.name",
"value": "James"
}
Valid comparators are: eq, neq, lt, lte, gt, gte, contains (regex), ncontains (!regex)
This struct is exported here so that you can include it in your own JSON encoding/decoding,
but go-ruler has a facility to help decode your rules from JSON into its own structs.
*/
type Rule struct {
Comparator string `json:"comparator"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
/*
A RulerRule combines a single rule and a whole set of rules and is used
when building rules programmatically through Ruler's Rule() function.
It's not meant to be created directly.
*/
type RulerRule struct {
*Ruler
*Rule
}
// adds an equals condition
func (rf *RulerRule) Eq(value interface{}) *RulerRule {
return rf.compare(eq, value)
}
// adds a not equals condition
func (rf *RulerRule) Neq(value interface{}) *RulerRule {
return rf.compare(neq, value)
}
// adds a less than condition
func (rf *RulerRule) Lt(value interface{}) *RulerRule {
return rf.compare(lt, value)
}
// adds a less than or equal condition
func (rf *RulerRule) Lte(value interface{}) *RulerRule {
return rf.compare(lte, value)
}
// adds a greater than condition
func (rf *RulerRule) Gt(value interface{}) *RulerRule {
return rf.compare(gt, value)
}
// adds a greater than or equal to condition
func (rf *RulerRule) Gte(value interface{}) *RulerRule {
return rf.compare(gte, value)
}
// adds a matches (regex) condition
func (rf *RulerRule) Matches(value interface{}) *RulerRule {
return rf.compare(matches, value)
}
// adds a not matches condition (ncontains, in the way this thinks of it)
func (rf *RulerRule) NotMatches(value interface{}) *RulerRule {
return rf.compare(ncontains, value)
}
// Stops chaining for the current rule, allowing you
// to add rules for other properties
func (rf *RulerRule) End() *Ruler {
return rf.Ruler
}
// comparator will either create a new ruler filter and add its filter
func (rf *RulerRule) compare(comp int, value interface{}) *RulerRule {
var comparator string
switch comp {
case eq:
comparator = "eq"
case neq:
comparator = "neq"
case lt:
comparator = "lt"
case lte:
comparator = "lte"
case gt:
comparator = "gt"
case gte:
comparator = "gte"
case contains:
comparator = "contains"
case matches:
comparator = "matches"
case ncontains:
comparator = "ncontains"
}
// if this thing has a comparator already, we need to make a new ruler filter
if rf.Comparator != "" {
rf = &RulerRule{
rf.Ruler,
&Rule{
comparator,
rf.Path,
value,
},
}
// attach the new filter to the ruler
rf.Ruler.rules = append(rf.Ruler.rules, rf.Rule)
} else {
//if there is no comparator, we can just set things on the current filter
rf.Comparator = comparator
rf.Value = value
}
return rf
}