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 types
import (
"database/sql/driver"
"encoding"
"encoding/json"
"github.com/icinga/icingadb/internal"
"github.com/pkg/errors"
)
// NotificationTypes specifies the set of reasons a notification may be sent for.
type NotificationTypes uint16
// UnmarshalJSON implements the json.Unmarshaler interface.
func (nt *NotificationTypes) UnmarshalJSON(data []byte) error {
var types []string
if err := internal.UnmarshalJSON(data, &types); err != nil {
return err
}
var n NotificationTypes
for _, typ := range types {
if v, ok := notificationTypeNames[typ]; ok {
n |= v
} else {
return badNotificationTypes(nt)
}
}
*nt = n
return nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (nt *NotificationTypes) UnmarshalText(text []byte) error {
return nt.UnmarshalJSON(text)
}
// Value implements the driver.Valuer interface.
func (nt NotificationTypes) Value() (driver.Value, error) {
if nt&^allNotificationTypes == 0 {
return int64(nt), nil
} else {
return nil, badNotificationTypes(nt)
}
}
// badNotificationTypes returns an error about syntactically, but not semantically valid NotificationTypes.
func badNotificationTypes(t interface{}) error {
return errors.Errorf("bad notification types: %#v", t)
}
// notificationTypeNames maps all valid NotificationTypes values to their SQL representation.
var notificationTypeNames = map[string]NotificationTypes{
"DowntimeStart": 1,
"DowntimeEnd": 2,
"DowntimeRemoved": 4,
"Custom": 8,
"Acknowledgement": 16,
"Problem": 32,
"Recovery": 64,
"FlappingStart": 128,
"FlappingEnd": 256,
}
// allNotificationTypes is the largest valid NotificationTypes value.
var allNotificationTypes = func() NotificationTypes {
var nt NotificationTypes
for _, v := range notificationTypeNames {
nt |= v
}
return nt
}()
// Assert interface compliance.
var (
_ json.Unmarshaler = (*NotificationTypes)(nil)
_ encoding.TextUnmarshaler = (*NotificationTypes)(nil)
_ driver.Valuer = NotificationTypes(0)
)
|