added sessions and flash storage

This commit is contained in:
2018-04-07 14:37:07 +01:00
parent 971f157307
commit b8f8c995c1
10 changed files with 263 additions and 39 deletions

57
flash.go Normal file
View File

@@ -0,0 +1,57 @@
package tuu
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
}