This repository has been archived by the owner on Mar 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
flash.go
150 lines (123 loc) · 2.82 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2015 The Tango Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flash
import (
"net/url"
"github.com/lunny/tango"
"github.com/tango-contrib/session"
)
var (
FlashName = "tango_flash"
FlashSeperator = "TANGOFLASH"
_ Flasher = &Flash{}
)
type Data map[string]interface{}
// FlashData is a tools to maintain data when using across request.
type Flash struct {
readed Data
flushed Data
session *session.Session
*Options
saved bool
}
func (f *Flash) setFlash(sess *session.Session, readed Data, opt *Options) {
f.readed = readed
f.flushed = make(Data)
f.session = sess
f.Options = opt
}
func (f *Flash) FlushData() Data {
return f.flushed
}
func (f *Flash) Merge() {
for k, v := range f.readed {
f.flushed[k] = v
}
}
func (f *Flash) Data() Data {
return f.readed
}
func (f *Flash) Get(key string) interface{} {
return f.readed[key]
}
func (f *Flash) Set(key string, value interface{}) {
f.readed[key] = value
f.flushed[key] = value
}
func (f *Flash) Add(kvs Data) {
for k, v := range kvs {
f.Set(k, v)
}
}
func (f *Flash) AddValues(values url.Values) {
for k, vals := range values {
f.Set(k, vals[0])
}
}
func (f *Flash) Save() {
if f.saved {
return
}
for key := range f.readed {
f.session.Del(f.Options.FlashName + f.Options.FlashSeperator + key)
}
var keys = make([]string, 0)
for k, v := range f.flushed {
f.session.Set(f.Options.FlashName+f.Options.FlashSeperator+k, v)
keys = append(keys, k)
}
f.session.Set(f.Options.FlashName, keys)
f.saved = true
}
type Flasher interface {
setFlash(*session.Session, Data, *Options)
FlushData() Data
Save()
}
type Options struct {
FlashName string
FlashSeperator string
}
func prepareOptions(opts []Options) Options {
var opt Options
if len(opts) > 0 {
opt = opts[0]
}
if len(opt.FlashName) == 0 {
opt.FlashName = FlashName
}
if len(opt.FlashSeperator) == 0 {
opt.FlashSeperator = FlashSeperator
}
return opt
}
// Flashes return a FlashData handler.
func Flashes(sessions *session.Sessions, opts ...Options) tango.HandlerFunc {
opt := prepareOptions(opts)
return func(ctx *tango.Context) {
var flasher Flasher
var flashed, ok bool
if flasher, ok = ctx.Action().(Flasher); ok {
// if session.Sessioner is implmentated just get it
var sess *session.Session
if sessinoer, ok := ctx.Action().(session.Sessioner); ok {
sess = sessinoer.GetSession()
} else {
sess = sessions.Session(ctx.Req(), ctx.ResponseWriter)
}
fd := make(Data)
if keys, has := sess.Get(opt.FlashName).([]string); has {
for _, key := range keys {
fd[key] = sess.Get(opt.FlashName + opt.FlashSeperator + key)
}
}
flasher.setFlash(sess, fd, &opt)
flashed = true
}
ctx.Next()
if flashed {
flasher.Save()
}
}
}