/
oncallnotificationrule.go
102 lines (87 loc) · 2.12 KB
/
oncallnotificationrule.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
90
91
92
93
94
95
96
97
98
99
100
101
102
package schedule
import (
"encoding"
"fmt"
"io"
"strconv"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/google/uuid"
"github.com/target/goalert/util/timeutil"
"github.com/target/goalert/validation"
)
// An OnCallNotificationRule defines when notifications for on-call users for a schedule
// should be sent.
type OnCallNotificationRule struct {
// ID is a persistent value for UI or other systems to track rule additions/deletions/edits.
ID RuleID
// ChannelID is the notification channel ID for notifications.
ChannelID uuid.UUID
Time *timeutil.Clock
WeekdayFilter *timeutil.WeekdayFilter
NextNotification *time.Time
}
// RuleID uniquely identifies an OnCallNotificationRule within the context of a single schedule
// and is stable across updates.
type RuleID struct {
scheduleID uuid.UUID
id int
valid bool
}
var _ encoding.TextMarshaler = RuleID{}
var _ encoding.TextUnmarshaler = &RuleID{}
var _ graphql.Marshaler = RuleID{}
var _ graphql.Unmarshaler = &RuleID{}
func (r RuleID) MarshalGQL(w io.Writer) {
graphql.MarshalString(r.String()).MarshalGQL(w)
}
func (r *RuleID) UnmarshalGQL(v interface{}) error {
s, err := graphql.UnmarshalString(v)
if err != nil {
return err
}
err = r.UnmarshalText([]byte(s))
if err != nil {
return validation.WrapError(err)
}
return nil
}
func (r RuleID) String() string {
if !r.valid {
return ""
}
return fmt.Sprintf("%s:%d", r.scheduleID.String(), r.id)
}
func (r RuleID) MarshalText() ([]byte, error) {
if !r.valid {
return nil, nil
}
return []byte(r.String()), nil
}
// UnmarshalText will parse a rule in string form.
//
// Format is a 36-char UUID string, then colon, followed by a unique int value.
func (r *RuleID) UnmarshalText(data []byte) error {
s := string(data)
if s == "" {
r.valid = false
r.id = 0
r.scheduleID = uuid.UUID{}
return nil
}
if len(s) < 38 {
return fmt.Errorf("input too short")
}
var err error
r.scheduleID, err = uuid.Parse(s[:36])
if err != nil {
return err
}
i, err := strconv.ParseInt(string(data[37:]), 10, 32)
if err != nil {
return err
}
r.id = int(i)
r.valid = true
return nil
}