-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
status.go
74 lines (64 loc) · 1.35 KB
/
status.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
package notification
import (
"encoding/json"
"strings"
)
// StatusRule includes parametes of status rules.
type StatusRule struct {
CurrentLevel CheckLevel `json:"currentLevel"`
PreviousLevel *CheckLevel `json:"previousLevel"`
}
// CheckLevel is the enum value of status levels.
type CheckLevel int
// consts of CheckStatusLevel
const (
Unknown CheckLevel = iota
Ok
Info
Warn
Critical
Any
)
var checkLevels = []string{
"UNKNOWN",
"OK",
"INFO",
"WARN",
"CRIT",
"ANY",
}
var checkLevelMaps = map[string]CheckLevel{
"UNKNOWN": Unknown,
"OK": Ok,
"INFO": Info,
"WARN": Warn,
"CRIT": Critical,
"ANY": Any,
}
// MarshalJSON implements json.Marshaller.
func (cl CheckLevel) MarshalJSON() ([]byte, error) {
return json.Marshal(cl.String())
}
// UnmarshalJSON implements json.Unmarshaller.
func (cl *CheckLevel) UnmarshalJSON(b []byte) error {
var ss string
if err := json.Unmarshal(b, &ss); err != nil {
return err
}
*cl = ParseCheckLevel(strings.ToUpper(ss))
return nil
}
// String returns the string value, invalid CheckLevel will return Unknown.
func (cl CheckLevel) String() string {
if cl < Unknown || cl > Any {
cl = Unknown
}
return checkLevels[cl]
}
// ParseCheckLevel will parse the string to checkLevel
func ParseCheckLevel(s string) CheckLevel {
if cl, ok := checkLevelMaps[s]; ok {
return cl
}
return Unknown
}