-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification_state.go
69 lines (56 loc) · 1.37 KB
/
notification_state.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
package incident
import (
"database/sql/driver"
"fmt"
)
type NotificationState int
const (
NotificationStateNull NotificationState = iota
NotificationStatePending
NotificationStateSent
NotificationStateFailed
)
var notificationStatTypeByName = map[string]NotificationState{
"pending": NotificationStatePending,
"sent": NotificationStateSent,
"failed": NotificationStateFailed,
}
var notificationStateTypeToName = func() map[NotificationState]string {
stateTypes := make(map[NotificationState]string)
for name, eventType := range notificationStatTypeByName {
stateTypes[eventType] = name
}
return stateTypes
}()
// Scan implements the sql.Scanner interface.
// Supports SQL NULL.
func (n *NotificationState) Scan(src any) error {
if src == nil {
*n = NotificationStateNull
return nil
}
var name string
switch val := src.(type) {
case string:
name = val
case []byte:
name = string(val)
default:
return fmt.Errorf("unable to scan type %T into NotificationState", src)
}
historyType, ok := notificationStatTypeByName[name]
if !ok {
return fmt.Errorf("unknown notification state type %q", name)
}
*n = historyType
return nil
}
func (n NotificationState) Value() (driver.Value, error) {
if n == NotificationStateNull {
return nil, nil
}
return n.String(), nil
}
func (n *NotificationState) String() string {
return notificationStateTypeToName[*n]
}