This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
flash.go
105 lines (89 loc) · 2.85 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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package flash implements flash messages.
package flash
import (
"encoding/gob"
"fmt"
)
// flashKey is a custom type for inserting data into a map.
type flashKey string
const (
flashKeyAlert flashKey = "_alert"
flashKeyError flashKey = "_error"
flashKeyWarning flashKey = "_warning"
)
func init() {
gob.Register(*new(flashKey))
}
// Flash is a collection of data that is discarded on read. It's designed to be
// compatible with sessions.Values.
type Flash struct {
values map[interface{}]interface{}
}
// New creates a new flash handler.
func New(values map[interface{}]interface{}) *Flash {
if values == nil {
values = make(map[interface{}]interface{})
}
return &Flash{values}
}
// Error adds a new error to the upcoming flash instance.
func (f *Flash) Error(msg string, vars ...interface{}) {
f.add(flashKeyError, msg, vars...)
}
// Errors returns the list of errors in flash, if any.
func (f *Flash) Errors() []string {
return f.get(flashKeyError)
}
// Warning adds a new alert to the upcoming flash instance.
func (f *Flash) Warning(msg string, vars ...interface{}) {
f.add(flashKeyWarning, msg, vars...)
}
// Warnings returns the list of warnings in flash, if any.
func (f *Flash) Warnings() []string {
return f.get(flashKeyWarning)
}
// Alert adds a new alert to the upcoming flash instance.
func (f *Flash) Alert(msg string, vars ...interface{}) {
f.add(flashKeyAlert, msg, vars...)
}
// Alerts returns the list of alerts in flash, if any.
func (f *Flash) Alerts() []string {
return f.get(flashKeyAlert)
}
// Clear removes all items from the flash. It's rare to call Clear since flashes
// are cleared automatically upon reading.
func (f *Flash) Clear() {
delete(f.values, flashKeyAlert)
delete(f.values, flashKeyError)
delete(f.values, flashKeyWarning)
}
// add inserts the message into the upcoming flash for the given key.
func (f *Flash) add(key flashKey, msg string, vars ...interface{}) {
var data []string
if v, ok := f.values[key]; ok {
data, _ = v.([]string)
}
f.values[key] = append(data, fmt.Sprintf(msg, vars...))
}
// get returns the messages in the key, clearing the values stored at the key.
func (f *Flash) get(key flashKey) []string {
if v, ok := f.values[key]; ok {
delete(f.values, key)
flashes, _ := v.([]string)
return flashes
}
return nil
}