forked from bittersweet/notifilter-receive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.go
81 lines (68 loc) · 1.38 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
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
}
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]
needed_val, _ := strconv.ParseBool(r.Value)
if val.(bool) != needed_val {
return false
}
return true
}
func metString(r *Rule, parsed map[string]interface{}) bool {
val := parsed[r.Key]
needed_val := r.Value
if val.(string) != needed_val {
return false
}
return true
}
func metNumber(r *Rule, parsed map[string]interface{}) bool {
val := parsed[r.Key].(float64)
needed_val, _ := strconv.ParseFloat(r.Value, 64)
switch r.Setting {
case "eq":
if val != needed_val {
return false
}
case "gt":
if val <= needed_val {
return false
}
case "lt":
if val >= needed_val {
return false
}
}
return true
}