forked from gobuffalo/plush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_map.go
55 lines (47 loc) · 1.19 KB
/
helper_map.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
package plush
import (
"sync"
"github.com/pkg/errors"
)
// HelperMap holds onto helpers and validates they are properly formed.
type HelperMap struct {
helpers map[string]interface{}
moot *sync.Mutex
}
// NewHelperMap containing all of the "default" helpers from "plush.Helpers".
func NewHelperMap() (HelperMap, error) {
hm := HelperMap{
helpers: map[string]interface{}{},
moot: &sync.Mutex{},
}
err := hm.AddMany(Helpers.Helpers())
if err != nil {
return hm, errors.WithStack(err)
}
return hm, nil
}
// 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
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 errors.WithStack(err)
}
}
return nil
}
// Helpers returns the underlying list of helpers from the map
func (h HelperMap) Helpers() map[string]interface{} {
return h.helpers
}