-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipient.go
47 lines (41 loc) · 1.2 KB
/
recipient.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
package recipient
import (
"fmt"
"github.com/icinga/icinga-notifications/internal/utils"
"github.com/icinga/icingadb/pkg/types"
"time"
)
type Recipient interface {
fmt.Stringer
GetContactsAt(t time.Time) []*Contact
}
type Key struct {
// Only one of the members is allowed to be a non-zero value.
ContactID types.Int `db:"contact_id"`
GroupID types.Int `db:"contactgroup_id"`
ScheduleID types.Int `db:"schedule_id"`
}
// MarshalText implements the encoding.TextMarshaler interface to allow JSON marshaling of map[Key]T.
func (r Key) MarshalText() (text []byte, err error) {
if r.ContactID.Valid {
return []byte(fmt.Sprintf("contact_id=%d", r.ContactID.Int64)), nil
} else if r.GroupID.Valid {
return []byte(fmt.Sprintf("group_id=%d", r.GroupID.Int64)), nil
} else if r.ScheduleID.Valid {
return []byte(fmt.Sprintf("schedule_id=%d", r.ScheduleID.Int64)), nil
} else {
return nil, nil
}
}
func ToKey(r Recipient) Key {
switch v := r.(type) {
case *Contact:
return Key{ContactID: utils.ToDBInt(v.ID)}
case *Group:
return Key{GroupID: utils.ToDBInt(v.ID)}
case *Schedule:
return Key{ScheduleID: utils.ToDBInt(v.ID)}
default:
panic(fmt.Sprintf("unexpected recipient type: %T", r))
}
}