/
hooks.go
67 lines (52 loc) · 1.45 KB
/
hooks.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
package hooks
import (
"fmt"
"strings"
)
// Hooks carries workflow and activity dependencies.
type Hooks struct {
Hooks map[string]map[string]interface{}
}
// NewHooks returns a pointer to a new Hooks.
func NewHooks(hooks map[string]map[string]interface{}) *Hooks {
return &Hooks{
Hooks: hooks,
}
}
func HookAttr(hooks map[string]map[string]interface{}, hook string, attr string) (interface{}, error) {
hook = strings.ToLower(hook)
attr = strings.ToLower(attr)
configMap, ok := hooks[hook]
if !ok {
return "", fmt.Errorf("hook %q not found", hook)
}
value, ok := configMap[attr]
if !ok {
return "", fmt.Errorf("attr %q not found", attr)
}
return value, nil
}
func HookAttrString(hooks map[string]map[string]interface{}, hook string, attr string) (string, error) {
accessor := fmt.Sprintf("%s:%s", hook, attr)
value, err := HookAttr(hooks, hook, attr)
if err != nil {
return "", fmt.Errorf("error accessing %q", accessor)
}
v, ok := value.(string)
if !ok {
return "", fmt.Errorf("error accessing %q: not a string", accessor)
}
return v, nil
}
func HookAttrBool(hooks map[string]map[string]interface{}, hook string, attr string) (bool, error) {
accessor := fmt.Sprintf("%s:%s", hook, attr)
value, err := HookAttr(hooks, hook, attr)
if err != nil {
return false, fmt.Errorf("error accessing %q", accessor)
}
v, ok := value.(bool)
if !ok {
return false, fmt.Errorf("error accessing %q: not a boolean", accessor)
}
return v, nil
}