-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathall_flags.go
55 lines (47 loc) · 1.5 KB
/
all_flags.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 flagstate
import (
"encoding/json"
)
// NewAllFlags will create a new AllFlags for a specific user.
// It sets valid to true, because until we have an error everything is valid.
func NewAllFlags() AllFlags {
return AllFlags{
valid: true,
flags: map[string]FlagState{},
}
}
// AllFlags is a snapshot of the state of multiple feature flags with regard to a specific user.
// This is the return type of ffclient.AllFlagsState().
// Serializing this object to JSON MarshalJSON() will produce a JSON you can sent to your front-end.
type AllFlags struct {
// flags is the list of flag for the user.
flags map[string]FlagState
// Valid if false it means there was an error (such as the data store not being available),
// in which case no flag data is in this object.
valid bool
}
// AddFlag is adding a flag in the list for the specific user.
func (a *AllFlags) AddFlag(flagKey string, state FlagState) {
a.flags[flagKey] = state
if a.valid && state.Failed {
a.valid = false
}
}
// MarshalJSON is serializing the object to JSON, it will produce a JSON you can sent to your front-end.
func (a AllFlags) MarshalJSON() ([]byte, error) {
res := struct {
Flags map[string]FlagState `json:"flags,omitempty"`
Valid bool `json:"valid"`
}{
Flags: a.flags,
Valid: a.IsValid(),
}
return json.Marshal(res)
}
// IsValid is a getter to know if the AllFlags object is valid.
func (a *AllFlags) IsValid() bool {
return a.valid
}
func (a *AllFlags) GetFlags() map[string]FlagState {
return a.flags
}