-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoggle.go
148 lines (118 loc) · 3.64 KB
/
toggle.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package toggle
import (
"fmt"
"reflect"
"github.com/xchapter7x/goutil"
"github.com/xchapter7x/toggle/engines/localengine"
"github.com/xchapter7x/toggle/engines/storageinterface"
)
func createReflectValueArgsArray(iargs []interface{}) (args []reflect.Value) {
for _, arg := range iargs {
args = append(args, reflect.ValueOf(arg))
}
return
}
func createInterfaceArrayFromValuesArray(responseValuesArray []reflect.Value) (responseInterfaceArray []interface{}) {
for _, ri := range responseValuesArray {
responseInterfaceArray = append(responseInterfaceArray, ri.Interface())
}
return
}
func getActivePointer(flg string, defaultFeature, newFeature interface{}) (activePointer interface{}) {
if IsActive(flg) {
activePointer = newFeature
} else {
activePointer = defaultFeature
}
return
}
func FlipP(flg string, responseInterfaceArray []interface{}, defaultFeature, newFeature interface{}, iargs ...interface{}) (err error) {
res := Flip(flg, defaultFeature, newFeature, iargs...)
err = goutil.UnpackArray(res, responseInterfaceArray)
return
}
func Flip(flg string, defaultFeature, newFeature interface{}, iargs ...interface{}) (responseInterfaceArray []interface{}) {
args := createReflectValueArgsArray(iargs)
ptr := getActivePointer(flg, defaultFeature, newFeature)
responseValuesArray := reflect.ValueOf(ptr).Call(args)
responseInterfaceArray = createInterfaceArrayFromValuesArray(responseValuesArray)
return
}
func SetFeatureStatus(featureSignature, featureStatus string) (err error) {
fullSignature := GetFullFeatureSignature(featureSignature)
if _, exists := featureList[fullSignature]; exists {
featureList[fullSignature].Status = featureStatus
} else {
err = fmt.Errorf("Feature toggle doesnt exist")
}
return
}
func IsActive(featureSignature string) (active bool) {
fullSignature := GetFullFeatureSignature(featureSignature)
if feature, exists := featureList[fullSignature]; !exists || feature.Status == FEATURE_OFF {
active = false
} else {
active = true
}
return
}
type Feature struct {
name string
Status string
filter func(...interface{}) bool
settings map[string]interface{}
}
func (s *Feature) UpdateStatus(newStatus string) {
s.Status = newStatus
}
const (
FEATURE_ON = "true"
FEATURE_OFF = "false"
FEATURE_FILTER = "filter:x:x"
)
var featureList map[string]*Feature = make(map[string]*Feature)
var namespace string
var toggleEngine storageinterface.StorageEngine
func Close() {
featureList = make(map[string]*Feature)
namespace = ""
toggleEngine = nil
}
func Init(ns string, engine storageinterface.StorageEngine) {
namespace = ns
if engine != nil {
toggleEngine = engine
} else {
toggleEngine = localengine.NewLocalEngine()
}
}
func ShowFeatures() map[string]*Feature {
return featureList
}
func getFeatureStatusValue(featureSignature, statusValue string) (status string) {
var err error
if status, err = toggleEngine.GetFeatureStatusValue(featureSignature); err != nil {
status = statusValue
}
return
}
func RegisterFeature(featureSignature string) (err error) {
err = RegisterFeatureWithStatus(featureSignature, FEATURE_OFF)
return
}
func GetFullFeatureSignature(partialSignature string) (fullSignature string) {
fullSignature = fmt.Sprintf("%s_%s", namespace, partialSignature)
return
}
func RegisterFeatureWithStatus(featureSignature, statusValue string) (err error) {
fullSignature := GetFullFeatureSignature(featureSignature)
if _, exists := featureList[fullSignature]; !exists {
featureList[fullSignature] = &Feature{
name: fullSignature,
Status: getFeatureStatusValue(fullSignature, statusValue),
}
} else {
err = fmt.Errorf("feature already registered")
}
return
}