-
Notifications
You must be signed in to change notification settings - Fork 326
/
durationmatcher.go
323 lines (277 loc) · 6.86 KB
/
durationmatcher.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package durationmatcher
import (
json "encoding/json"
fmt "fmt"
strings "strings"
time "time"
durationpb "google.golang.org/protobuf/types/known/durationpb"
yaml "sigs.k8s.io/yaml"
)
type Duration struct {
time.Duration
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var str string
err := yaml.UnmarshalStrict(b, &str)
if err != nil {
return err
}
dur, err := time.ParseDuration(str)
if err != nil {
return err
}
d.Duration = dur
return nil
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.Duration.String())
}
type durationBetween struct {
Lower *Duration `json:"lower"`
Upper *Duration `json:"upper"`
}
type durationValue interface {
// *Duration
// *durationBetween
}
func (m *DurationMatcher) checkFull(duration *time.Duration) error {
value, ok := m.Value.(*Duration)
if !ok {
return fmt.Errorf("value is not a duration")
}
if *duration != value.Duration {
return fmt.Errorf("%s is not equal to expected %s", *duration, value.Duration)
}
return nil
}
func (m *DurationMatcher) checkLess(duration *time.Duration) error {
value, ok := m.Value.(*Duration)
if !ok {
return fmt.Errorf("value is not a duration")
}
if !(*duration <= value.Duration) {
return fmt.Errorf("%s is not less than %s", *duration, value.Duration)
}
return nil
}
func (m *DurationMatcher) checkGreater(duration *time.Duration) error {
value, ok := m.Value.(*Duration)
if !ok {
return fmt.Errorf("value is not a duration")
}
if !(*duration >= value.Duration) {
return fmt.Errorf("%s is not greater than %s", *duration, value.Duration)
}
return nil
}
func (m *DurationMatcher) checkBetween(duration *time.Duration) error {
value, ok := m.Value.(*durationBetween)
if !ok {
return fmt.Errorf("value is not a duration")
}
if value.Upper == nil || value.Lower == nil {
return fmt.Errorf("value is nil")
}
if !(*duration <= value.Upper.Duration) {
return fmt.Errorf("%s is not less than %s", *duration, value.Upper.Duration)
}
if !(*duration >= value.Lower.Duration) {
return fmt.Errorf("%s is not greater than %s", *duration, value.Lower.Duration)
}
return nil
}
// DurationMatcher matches a *durationpb.Duration based on an operator and a value
type DurationMatcher struct {
Operator Operator `json:"operator"`
Value durationValue `json:"value"`
}
// Operator is en enum over types of DurationMatcher
type Operator struct {
slug string
}
// String implements fmt.Stringer
func (o Operator) String() string {
return o.slug
}
// operatorFromString converts a string into Operator
func operatorFromString(str string) (Operator, error) {
switch str {
case opBetween.slug:
return opBetween, nil
case opFull.slug:
return opFull, nil
case opGreater.slug:
return opGreater, nil
case opLess.slug:
return opLess, nil
default:
return opUnknown, fmt.Errorf("Invalid value for DurationMatcher operator: %s", str)
}
}
// UnmarshalJSON implements json.Unmarshaler interface
func (o *Operator) UnmarshalJSON(b []byte) error {
var str string
err := yaml.UnmarshalStrict(b, &str)
if err != nil {
return err
}
str = strings.ToLower(str)
operator, err := operatorFromString(str)
if err != nil {
return err
}
*o = operator
return nil
}
// MarshalJSON implements json.Marshaler interface
func (o Operator) MarshalJSON() ([]byte, error) {
return json.Marshal(o.slug)
}
var (
opUnknown = Operator{"unknown"}
opBetween = Operator{"between"}
opFull = Operator{"full"}
opGreater = Operator{"greater"}
opLess = Operator{"less"}
)
// Match attempts to match a *durationpb.Duration based on the DurationMatcher
func (m *DurationMatcher) Match(value *durationpb.Duration) error {
switch m.Operator {
case opBetween:
{
if m.Value == nil {
return fmt.Errorf("matcher value is nil")
}
if value == nil {
return fmt.Errorf("duration is nil")
}
dur := value.AsDuration()
return m.checkBetween(&dur)
}
case opFull:
{
if m.Value == nil {
return fmt.Errorf("matcher value is nil")
}
if value == nil {
return fmt.Errorf("duration is nil")
}
dur := value.AsDuration()
return m.checkFull(&dur)
}
case opGreater:
{
if m.Value == nil {
return fmt.Errorf("matcher value is nil")
}
if value == nil {
return fmt.Errorf("duration is nil")
}
dur := value.AsDuration()
return m.checkGreater(&dur)
}
case opLess:
{
if m.Value == nil {
return fmt.Errorf("matcher value is nil")
}
if value == nil {
return fmt.Errorf("duration is nil")
}
dur := value.AsDuration()
return m.checkLess(&dur)
}
default:
return fmt.Errorf("Unhandled DurationMatcher operator %s", m.Operator)
}
}
// Marshal implements json.Marshaler
func (m DurationMatcher) MarshalJSON() ([]byte, error) {
type Alias DurationMatcher
switch valueType := m.Value.(type) {
case *Duration:
return json.Marshal(&struct {
Value *Duration `json:"value"`
*Alias
}{
Value: valueType,
Alias: (*Alias)(&m),
})
case *durationBetween:
return json.Marshal(&struct {
Value *durationBetween `json:"value"`
*Alias
}{
Value: valueType,
Alias: (*Alias)(&m),
})
default:
return nil, fmt.Errorf("Marshal DurationMatcher: Invalid match value")
}
}
// Unmarshal implements json.Unmarshaler
func (m *DurationMatcher) UnmarshalJSON(b []byte) error {
// User just provides a plain durationValue, so default to Full
var rawVal Duration
err := yaml.UnmarshalStrict(b, &rawVal)
if err == nil {
m.Operator = opFull
m.Value = (durationValue)(&rawVal)
return nil
}
type Alias DurationMatcher
{
temp := struct {
Value *Duration `json:"value"`
*Alias
}{Alias: (*Alias)(m)}
if err := yaml.UnmarshalStrict(b, &temp); err == nil {
m.Value = temp.Value
return nil
}
}
{
temp := struct {
Value *durationBetween `json:"value"`
*Alias
}{Alias: (*Alias)(m)}
if err := yaml.UnmarshalStrict(b, &temp); err == nil {
m.Value = temp.Value
return nil
}
}
return fmt.Errorf("Unmarshal DurationMatcher: Failed to unmarshal")
}
// Between constructs a new DurationMatcher that matches using the Between operator
func Between(lower *Duration, upper *Duration) *DurationMatcher {
return &DurationMatcher{
Operator: opBetween,
Value: &durationBetween{
Lower: lower,
Upper: upper,
},
}
}
// Full constructs a new DurationMatcher that matches using the Full operator
func Full(value *Duration) *DurationMatcher {
return &DurationMatcher{
Operator: opFull,
Value: value,
}
}
// Greater constructs a new DurationMatcher that matches using the Greater operator
func Greater(value *Duration) *DurationMatcher {
return &DurationMatcher{
Operator: opGreater,
Value: value,
}
}
// Less constructs a new DurationMatcher that matches using the Less operator
func Less(value *Duration) *DurationMatcher {
return &DurationMatcher{
Operator: opLess,
Value: value,
}
}