-
Notifications
You must be signed in to change notification settings - Fork 2
/
rules.go
102 lines (84 loc) · 1.68 KB
/
rules.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
package main
import (
"encoding/json"
"log"
"strconv"
)
type rule struct {
Key string `json:"key"`
Type string `json:"type"`
Setting string `json:"setting"`
Value string `json:"value"`
}
func (r *rule) Met(e *Event) bool {
var parsed map[string]interface{}
err := json.Unmarshal([]byte(e.Data), &parsed)
if err != nil {
log.Fatal("json.Unmarshal", err)
}
// check if key is in the map
// first value is actual value of key in the map
if _, ok := parsed[r.Key]; !ok {
return false
}
// if key is present but nil
if parsed[r.Key] == nil {
if r.Setting == "noteq" {
return true
}
return false
}
switch r.Type {
case "boolean":
return metBool(r, parsed)
case "string":
return metString(r, parsed)
case "number":
return metNumber(r, parsed)
}
return true
}
func metBool(r *rule, parsed map[string]interface{}) bool {
val := parsed[r.Key]
neededVal, _ := strconv.ParseBool(r.Value)
if val.(bool) != neededVal {
return false
}
return true
}
func metString(r *rule, parsed map[string]interface{}) bool {
val := parsed[r.Key]
neededVal := r.Value
str := val.(string)
if r.Setting == "noteq" {
if str == neededVal {
// We need not equal and string is equal
return false
}
// We need not equal and string is not equal
return true
}
if str != neededVal {
return false
}
return true
}
func metNumber(r *rule, parsed map[string]interface{}) bool {
val := parsed[r.Key].(float64)
neededVal, _ := strconv.ParseFloat(r.Value, 64)
switch r.Setting {
case "eq":
if val != neededVal {
return false
}
case "gt":
if val <= neededVal {
return false
}
case "lt":
if val >= neededVal {
return false
}
}
return true
}