-
Notifications
You must be signed in to change notification settings - Fork 0
/
feato.go
51 lines (43 loc) · 1 KB
/
feato.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
package feato
import "fmt"
type (
// FlagStore interface for flag storage
FlagStore interface {
Put(key string, flag Flag)
IsEnabled(name string) bool
ClearAll()
}
)
var (
// Instance for global use
Instance FlagStore = make(FlagMap, 0)
)
// RegisterGlobal register features to global instance
func RegisterGlobal(features []*Feature) {
Register(Instance, features)
}
// Register the features to store
func Register(store FlagStore, features []*Feature) {
for _, feat := range features {
store.Put(feat.Name, feat.Flag)
registerChild(store, feat.Name, feat.Childs)
}
}
func registerChild(store FlagStore, parent string, childs []*Feature) {
for _, child := range childs {
key := fmt.Sprintf("%s.%s", parent, child.Name)
store.Put(key, child.Flag)
registerChild(store, key, child.Childs)
}
}
// IsEnabled return flag by name
func IsEnabled(name string) bool {
return Instance.IsEnabled(name)
}
// Bool convert flag to bool
func Bool(f Flag) bool {
if f == nil {
return false
}
return *f
}