-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathmap.go
More file actions
77 lines (62 loc) · 1.52 KB
/
Copy pathmap.go
File metadata and controls
77 lines (62 loc) · 1.52 KB
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
package helpers
import (
"sync"
"sync/atomic"
)
// HelperMap holds onto helpers and validates they are properly formed.
type HelperMap struct {
helpers map[string]interface{}
moot *sync.Mutex
version uint64
latest uint64
}
// NewHelperMap containing all of the "default" helpers from "plush.Helpers".
func NewMap(helpers map[string]interface{}) HelperMap {
hm := HelperMap{
helpers: helpers,
moot: &sync.Mutex{},
}
return hm
}
// Add a new helper to the map. New Helpers will be validated to ensure they
// meet the requirements for a helper:
func (h *HelperMap) Add(key string, helper interface{}) error {
h.moot.Lock()
defer h.moot.Unlock()
if h.helpers == nil {
h.helpers = map[string]interface{}{}
}
h.helpers[key] = helper
h.version++
atomic.StoreUint64(&h.latest, h.version)
return nil
}
// AddMany helpers at the same time.
func (h *HelperMap) AddMany(helpers map[string]interface{}) error {
for k, v := range helpers {
err := h.Add(k, v)
if err != nil {
return err
}
}
return nil
}
// Helpers returns the underlying list of helpers from the map
func (h HelperMap) Helpers() map[string]interface{} {
return h.helpers
}
func (h HelperMap) All() map[string]interface{} {
return h.helpers
}
func (h *HelperMap) Version() uint64 {
return atomic.LoadUint64(&h.latest)
}
func (h *HelperMap) Snapshot() (map[string]interface{}, uint64) {
h.moot.Lock()
defer h.moot.Unlock()
out := make(map[string]interface{}, len(h.helpers))
for k, v := range h.helpers {
out[k] = v
}
return out, h.version
}