/
desttype.go
89 lines (72 loc) · 2.29 KB
/
desttype.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package notification
import (
"fmt"
"github.com/target/goalert/notificationchannel"
"github.com/target/goalert/user/contactmethod"
)
//go:generate go run golang.org/x/tools/cmd/stringer -type DestType
type Dest struct {
ID string
Type DestType
Value string
}
// DestType represents the type of destination, it is a combination of available contact methods and notification channels.
type DestType int
const (
DestTypeUnknown DestType = iota
DestTypeVoice
DestTypeSMS
DestTypeSlackChannel
DestTypeUserEmail
DestTypeUserWebhook
)
func (d Dest) String() string { return fmt.Sprintf("%s(%s)", d.Type.String(), d.ID) }
// IsUserCM returns true if the DestType represents a user contact method.
func (t DestType) IsUserCM() bool { return t.CMType() != contactmethod.TypeUnknown }
// ScannableDestType allows scanning a DestType from separate columns for user contact methods and notification channels.
type ScannableDestType struct {
// CM is the contactmethod.Type and should be scanned from the `type` column from `user_contact_methods`.
CM contactmethod.Type
// NC is the notificationchannel.Type and should be scanned from the `type` column from `notification_channels`.
NC notificationchannel.Type
}
// DestType returns a DestType from the scanned values.
func (t ScannableDestType) DestType() DestType {
switch t.CM {
case contactmethod.TypeSMS:
return DestTypeSMS
case contactmethod.TypeVoice:
return DestTypeVoice
case contactmethod.TypeEmail:
return DestTypeUserEmail
case contactmethod.TypeWebhook:
return DestTypeUserWebhook
}
switch t.NC {
case notificationchannel.TypeSlack:
return DestTypeSlackChannel
}
return DestTypeUnknown
}
// NCType returns the notificationchannel.Type associated with the DestType.
func (t DestType) NCType() notificationchannel.Type {
switch t {
case DestTypeSlackChannel:
return notificationchannel.TypeSlack
}
return notificationchannel.TypeUnknown
}
// CMType returns the contactmethod.Type associated with the DestType.
func (t DestType) CMType() contactmethod.Type {
switch t {
case DestTypeSMS:
return contactmethod.TypeSMS
case DestTypeVoice:
return contactmethod.TypeVoice
case DestTypeUserEmail:
return contactmethod.TypeEmail
case DestTypeUserWebhook:
return contactmethod.TypeWebhook
}
return contactmethod.TypeUnknown
}