-
Notifications
You must be signed in to change notification settings - Fork 209
/
views.go
58 lines (45 loc) · 1.37 KB
/
views.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
package authboss
import "net/http"
// ViewDataMaker asks for an HTMLData object to assist with rendering.
type ViewDataMaker func(http.ResponseWriter, *http.Request) HTMLData
// HTMLData is used to render templates with.
type HTMLData map[string]interface{}
// NewHTMLData creates HTMLData from key-value pairs. The input is a key-value
// slice, where odd elements are keys, and the following even element is their value.
func NewHTMLData(data ...interface{}) HTMLData {
if len(data)%2 != 0 {
panic("It should be a key value list of arguments.")
}
h := make(HTMLData)
for i := 0; i < len(data)-1; i += 2 {
k, ok := data[i].(string)
if !ok {
panic("Keys must be strings.")
}
h[k] = data[i+1]
}
return h
}
// Merge adds the data from other to h. If there are conflicting keys
// they are overwritten by other's values.
func (h HTMLData) Merge(other HTMLData) HTMLData {
for k, v := range other {
h[k] = v
}
return h
}
// MergeKV adds extra key-values to the HTMLData. The input is a key-value
// slice, where odd elements are keys, and the following even element is their value.
func (h HTMLData) MergeKV(data ...interface{}) HTMLData {
if len(data)%2 != 0 {
panic("It should be a key value list of arguments.")
}
for i := 0; i < len(data)-1; i += 2 {
k, ok := data[i].(string)
if !ok {
panic("Keys must be strings.")
}
h[k] = data[i+1]
}
return h
}