-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnotifier.go
111 lines (90 loc) · 2.22 KB
/
gnotifier.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
103
104
105
106
107
108
109
110
111
package gnotifier
import (
"errors"
)
// GNotifier interface
type GNotifier interface {
Push() error
GetConfig() *Config
IsValid() error
}
// Config define the notification options
type Config struct {
Title string
Message string
Expiration int32
ApplicationName string
}
func (c *Config) IsValid() error {
if c.Title == "" {
return errors.New("A Title is mandatory")
}
if c.Message == "" {
return errors.New("A Message is mandatory")
}
return nil
}
// Builder abstracts the concrete function Notification
type Builder func(title, message string) GNotifier
type notifier struct {
Config *Config
}
func (n *notifier) GetConfig() *Config {
return n.Config
}
func (n *notifier) IsValid() error {
return n.GetConfig().IsValid()
}
// Notification is the builder
func Notification(title, message string) GNotifier {
config := &Config{title, message, 5000, ""}
n := ¬ifier{Config: config}
return n
}
type nullNotifier struct {
Config *Config
}
func (n *nullNotifier) GetConfig() *Config {
return n.Config
}
func (n *nullNotifier) IsValid() error {
return n.GetConfig().IsValid()
}
func (n *nullNotifier) Push() error {
return nil
}
// NullNotification is the builder for tests where no side effects are desired
func NullNotification(title, message string) GNotifier {
config := &Config{title, message, 5000, ""}
n := &nullNotifier{Config: config}
return n
}
type recordingNotifier struct {
Config *Config
Recorder *TestRecorder
}
func (n *recordingNotifier) GetConfig() *Config {
return n.Config
}
func (n *recordingNotifier) IsValid() error {
return n.GetConfig().IsValid()
}
func (n *recordingNotifier) Push() error {
n.Recorder.Pushed = append(n.Recorder.Pushed, n.GetConfig())
return nil
}
// TestRecorder provides a way to verify the GNotifier api use
// (not intended for production code)
type TestRecorder struct {
Pushed []*Config
}
// NewTestRecorder constructs a new TestRecorder. Use its
// Notification method as test Builder.
func NewTestRecorder() *TestRecorder {
return &TestRecorder{[]*Config{}}
}
func (r *TestRecorder) Notification(title, message string) GNotifier {
config := &Config{title, message, 5000, ""}
n := &recordingNotifier{Config: config, Recorder: r}
return n
}