Drop superfluous {user,notifications},states,types filter mappings

This commit is contained in:
Yonas Habteab 2025-05-20 16:30:11 +02:00
parent c4a69d71b5
commit 40a4bccc51
4 changed files with 18 additions and 179 deletions

View file

@ -1,78 +0,0 @@
package types
import (
"database/sql/driver"
"encoding"
"encoding/json"
"github.com/icinga/icinga-go-library/types"
"github.com/pkg/errors"
)
// NotificationStates specifies the set of states a notification may be sent for.
type NotificationStates uint8
// UnmarshalJSON implements the json.Unmarshaler interface.
func (nst *NotificationStates) UnmarshalJSON(data []byte) error {
var states []string
if err := types.UnmarshalJSON(data, &states); err != nil {
return err
}
var n NotificationStates
for _, state := range states {
if v, ok := notificationStateNames[state]; ok {
n |= v
} else {
return badNotificationStates(states)
}
}
*nst = n
return nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (nst *NotificationStates) UnmarshalText(text []byte) error {
return nst.UnmarshalJSON(text)
}
// Value implements the driver.Valuer interface.
func (nst NotificationStates) Value() (driver.Value, error) {
if nst&^allNotificationStates == 0 {
return int64(nst), nil
} else {
return nil, badNotificationStates(nst)
}
}
// badNotificationStates returns an error about syntactically, but not semantically valid NotificationStates.
func badNotificationStates(s interface{}) error {
return errors.Errorf("bad notification states: %#v", s)
}
// notificationStateNames maps all valid NotificationStates values to their SQL representation.
var notificationStateNames = map[string]NotificationStates{
"OK": 1,
"Warning": 2,
"Critical": 4,
"Unknown": 8,
"Up": 16,
"Down": 32,
}
// allNotificationStates is the largest valid NotificationStates value.
var allNotificationStates = func() NotificationStates {
var nt NotificationStates
for _, v := range notificationStateNames {
nt |= v
}
return nt
}()
// Assert interface compliance.
var (
_ json.Unmarshaler = (*NotificationStates)(nil)
_ encoding.TextUnmarshaler = (*NotificationStates)(nil)
_ driver.Valuer = NotificationStates(0)
)

View file

@ -1,81 +0,0 @@
package types
import (
"database/sql/driver"
"encoding"
"encoding/json"
"github.com/icinga/icinga-go-library/types"
"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 names []string
if err := types.UnmarshalJSON(data, &names); err != nil {
return err
}
var v NotificationTypes
for _, name := range names {
if i, ok := notificationTypeMap[name]; ok {
v |= i
} else {
return badNotificationTypes(nt)
}
}
*nt = v
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)
}
// notificationTypeMap maps all valid NotificationTypes values to their SQL representation.
var notificationTypeMap = 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 all NotificationTypes
for _, i := range notificationTypeMap {
all |= i
}
return all
}()
// Assert interface compliance.
var (
_ json.Unmarshaler = (*NotificationTypes)(nil)
_ encoding.TextUnmarshaler = (*NotificationTypes)(nil)
_ driver.Valuer = NotificationTypes(0)
)

View file

@ -4,23 +4,22 @@ import (
"github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icingadb/pkg/contracts"
icingadbTypes "github.com/icinga/icingadb/pkg/icingadb/types"
)
type Notification struct {
EntityWithChecksum `json:",inline"`
EnvironmentMeta `json:",inline"`
NameCiMeta `json:",inline"`
HostId types.Binary `json:"host_id"`
ServiceId types.Binary `json:"service_id"`
NotificationcommandId types.Binary `json:"notificationcommand_id"`
TimesBegin types.Int `json:"times_begin"`
TimesEnd types.Int `json:"times_end"`
NotificationInterval uint32 `json:"notification_interval"`
TimeperiodId types.Binary `json:"timeperiod_id"`
States icingadbTypes.NotificationStates `json:"states"`
Types icingadbTypes.NotificationTypes `json:"types"`
ZoneId types.Binary `json:"zone_id"`
HostId types.Binary `json:"host_id"`
ServiceId types.Binary `json:"service_id"`
NotificationcommandId types.Binary `json:"notificationcommand_id"`
TimesBegin types.Int `json:"times_begin"`
TimesEnd types.Int `json:"times_end"`
NotificationInterval uint32 `json:"notification_interval"`
TimeperiodId types.Binary `json:"timeperiod_id"`
States uint8 `json:"states"`
Types uint16 `json:"types"`
ZoneId types.Binary `json:"zone_id"`
}
type NotificationUser struct {

View file

@ -4,21 +4,20 @@ import (
"github.com/icinga/icinga-go-library/database"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icingadb/pkg/contracts"
icingadbTypes "github.com/icinga/icingadb/pkg/icingadb/types"
)
type User struct {
EntityWithChecksum `json:",inline"`
EnvironmentMeta `json:",inline"`
NameCiMeta `json:",inline"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
Pager string `json:"pager"`
NotificationsEnabled types.Bool `json:"notifications_enabled"`
TimeperiodId types.Binary `json:"timeperiod_id"`
States icingadbTypes.NotificationStates `json:"states"`
Types icingadbTypes.NotificationTypes `json:"types"`
ZoneId types.Binary `json:"zone_id"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
Pager string `json:"pager"`
NotificationsEnabled types.Bool `json:"notifications_enabled"`
TimeperiodId types.Binary `json:"timeperiod_id"`
States uint8 `json:"states"`
Types uint16 `json:"types"`
ZoneId types.Binary `json:"zone_id"`
}
type UserCustomvar struct {