-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditions.go
123 lines (108 loc) · 2.94 KB
/
conditions.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
package hypermatch
import (
"encoding/json"
"strings"
)
// ConditionSet represents a rule and consists of one or more items of type Condition
type ConditionSet []Condition
// MarshalJSON marshals a ConditionSet into an easy readable JSON object
func (c ConditionSet) MarshalJSON() ([]byte, error) {
// CAUTION: this must not be a pointer-receiver!
data := make(map[string]Pattern, len(c))
for _, cc := range c {
data[cc.Path] = cc.Pattern
}
return json.Marshal(data)
}
// UnmarshalJSON unmarshal the JSON back to a ConditionSet
func (c *ConditionSet) UnmarshalJSON(data []byte) error {
// CAUTION: this must be a pointer-receiver!
var r map[string]Pattern
if err := json.Unmarshal(data, &r); err != nil {
return err
}
var cs ConditionSet
for k, v := range r {
cs = append(cs, Condition{
Path: k,
Pattern: v,
})
}
*c = cs
return nil
}
// Condition represents a single condition inside a ConditionSet. It defines a Path (=reference to property in an event) and a Pattern to check against the value.
type Condition struct {
Path string `json:"path"`
Pattern Pattern `json:"pattern"`
}
// MarshalJSON marshals a Condition into an easy readable JSON object
func (c Condition) MarshalJSON() ([]byte, error) {
// CAUTION: this must not be a pointer-receiver!
return json.Marshal(map[string]Pattern{
c.Path: c.Pattern,
})
}
// UnmarshalJSON unmarshal the JSON back to a Condition
func (c *Condition) UnmarshalJSON(data []byte) error {
// CAUTION: this must be a pointer-receiver!
var r map[string]Pattern
if err := json.Unmarshal(data, &r); err != nil {
return err
}
for k, v := range r {
c.Path = k
c.Pattern = v
break
}
return nil
}
// Pattern defines how a value should be compared. It consists of a Type and either a Value or Sub-patterns depending on the used Type.
type Pattern struct {
Type PatternType `json:"type"`
Value string `json:"value,omitempty"`
Sub []Pattern `json:"sub,omitempty"`
}
// MarshalJSON marshals a Pattern into an easy readable JSON object
func (p Pattern) MarshalJSON() ([]byte, error) {
// CAUTION: this must not be a pointer-receiver!
if len(p.Sub) > 0 {
return json.Marshal(map[string][]Pattern{
p.Type.String(): p.Sub,
})
} else {
return json.Marshal(map[string]string{
p.Type.String(): p.Value,
})
}
}
// UnmarshalJSON unmarshal the JSON back to a Pattern
func (p *Pattern) UnmarshalJSON(data []byte) error {
// CAUTION: this must be a pointer-receiver!
var r map[string]json.RawMessage
if err := json.Unmarshal(data, &r); err != nil {
return err
}
for k, v := range r {
p.Type = PatternTypeFromString(k)
switch p.Type {
case PatternAnythingBut, PatternAnyOf, PatternAllOf:
var ps []Pattern
if err := json.Unmarshal(v, &ps); err != nil {
return err
}
p.Sub = ps
default:
d := string(v)
if strings.HasPrefix(d, "\"") {
d = d[1:]
}
if strings.HasSuffix(d, "\"") {
d = d[:len(d)-1]
}
p.Value = d
}
break
}
return nil
}