-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.go
111 lines (90 loc) · 2.43 KB
/
manager.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package fitcha
import "context"
type FeatureManager interface {
Store() Storage
Evaluate(context.Context, string) (bool, error)
EvaluateExpr(context.Context, string, string) (bool, error)
IsEnabled(context.Context, string) (bool, error)
Enable(context.Context, string) error
Disable(context.Context, string) error
AvailableFeatures() []string
EnabledFeatures() []string
}
type simpleFeatureManager struct {
store Storage
}
func NewFeatureManager(store Storage) FeatureManager {
return &simpleFeatureManager{
store: store,
}
}
func (sfm *simpleFeatureManager) Store() Storage {
return sfm.store
}
func (sfm *simpleFeatureManager) IsEnabled(ctx context.Context, featureName string) (bool, error) {
feature, err := sfm.Store().Find(featureName)
if err != nil {
return false, err
}
if !feature.IsEnabled {
return false, ErrFeatureIsNotEnabled
}
return true, nil
}
func (sfm *simpleFeatureManager) Enable(ctx context.Context, featureName string) error {
feature, err := sfm.Store().Find(featureName)
if err != nil {
return err
}
feature.IsEnabled = true
sfm.Store().Update(feature)
return nil
}
func (sfm *simpleFeatureManager) Disable(ctx context.Context, featureName string) error {
feature, err := sfm.Store().Find(featureName)
if err != nil {
return err
}
feature.IsEnabled = false
sfm.Store().Update(feature)
return nil
}
func (sfm *simpleFeatureManager) Evaluate(ctx context.Context, featureName string) (bool, error) {
feature, err := sfm.Store().Find(featureName)
if err != nil {
return false, err
}
if !feature.IsEnabled {
return false, ErrFeatureIsNotEnabled
}
return sfm.EvaluateExpr(ctx, featureName, feature.Expr)
}
func (sfm *simpleFeatureManager) EvaluateExpr(ctx context.Context, featureName string, cond string) (bool, error) {
feature, err := sfm.Store().Find(featureName)
if err != nil {
return false, err
}
if !feature.IsEnabled {
return false, ErrFeatureIsNotEnabled
}
if cond == "" {
return false, ErrEmptyExpression
}
return evaluateCondition(ctx, cond)
}
func (sfm *simpleFeatureManager) AvailableFeatures() []string {
available := make([]string, 0)
for _, f := range sfm.store.List() {
available = append(available, string(f.Name))
}
return available
}
func (sfm *simpleFeatureManager) EnabledFeatures() []string {
available := make([]string, 0)
for _, f := range sfm.store.List() {
if f.IsEnabled {
available = append(available, string(f.Name))
}
}
return available
}