-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash.go
47 lines (36 loc) · 979 Bytes
/
flash.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 contour
// Flash defines a flash message container
type Flash struct {
msgs map[string][]string
}
// NewFlash returns a new flash message container
func NewFlash() *Flash {
return &Flash{msgs: make(map[string][]string)}
}
// PushTo pushes a new message onto the stack with given type i.e. `info`, `error`, or `success`
func (f *Flash) PushTo(key, msg string) {
f.msgs[key] = append(f.msgs[key], msg)
}
// Push pushes a new message onto the stack with default type `info`
func (f *Flash) Push(msg string) {
f.PushTo("info", msg)
}
// Get returns all messages of given type
func (f *Flash) Get(key string) []string {
val := f.msgs[key]
delete(f.msgs, key)
return val
}
// Clear clears all messages
func (f *Flash) Clear() {
f.msgs = make(map[string][]string, 0)
}
// All returns all messages regardless of their type
func (f *Flash) All() []string {
var msgs []string
for _, v := range f.msgs {
msgs = append(msgs, v...)
}
f.Clear()
return msgs
}