diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
commit | b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch) | |
tree | d2caec2610d4ea887803ec9e9c3cd77136c448ba /pkg/types/notification_types.go | |
parent | Initial commit. (diff) | |
download | icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.tar.xz icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.zip |
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | pkg/types/notification_types.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/pkg/types/notification_types.go b/pkg/types/notification_types.go new file mode 100644 index 0000000..832a515 --- /dev/null +++ b/pkg/types/notification_types.go @@ -0,0 +1,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) +) |