forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.go
57 lines (46 loc) · 1.28 KB
/
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
48
49
50
51
52
53
54
55
56
57
package buffalo
import "encoding/json"
// flashKey is the prefix inside the Session.
const flashKey = "_flash_"
//Flash is a struct that helps with the operations over flash messages.
type Flash struct {
data map[string][]string
}
//Delete removes a particular key from the Flash.
func (f Flash) Delete(key string) {
delete(f.data, key)
}
//Clear removes all keys from the Flash.
func (f *Flash) Clear() {
f.data = map[string][]string{}
}
//Set allows to set a list of values into a particular key.
func (f Flash) Set(key string, values []string) {
f.data[key] = values
}
//Add adds a flash value for a flash key, if the key already has values the list for that value grows.
func (f Flash) Add(key, value string) {
if len(f.data[key]) == 0 {
f.data[key] = []string{value}
return
}
f.data[key] = append(f.data[key], value)
}
//Persist the flash inside the session.
func (f Flash) persist(session *Session) {
b, _ := json.Marshal(f.data)
session.Set(flashKey, b)
session.Save()
}
//newFlash creates a new Flash and loads the session data inside its data.
func newFlash(session *Session) *Flash {
result := &Flash{
data: map[string][]string{},
}
if session.Session != nil {
if f := session.Get(flashKey); f != nil {
json.Unmarshal(f.([]byte), &result.data)
}
}
return result
}