-
Notifications
You must be signed in to change notification settings - Fork 15
/
models.go
163 lines (145 loc) · 4.19 KB
/
models.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package flagsmith
import (
"encoding/json"
"fmt"
"github.com/Flagsmith/flagsmith-go-client/v4/flagengine/features"
"github.com/Flagsmith/flagsmith-go-client/v4/flagengine/identities/traits"
)
type Flag struct {
Enabled bool
Value interface{}
IsDefault bool
FeatureID int
FeatureName string
}
type Trait struct {
TraitKey string `json:"trait_key"`
TraitValue interface{} `json:"trait_value"`
Transient bool `json:"transient,omitempty"`
}
type IdentityTraits struct {
Identifier string `json:"identifier"`
Traits []*Trait `json:"traits"`
Transient bool `json:"transient,omitempty"`
}
func (t *Trait) ToTraitModel() *traits.TraitModel {
return &traits.TraitModel{
TraitKey: t.TraitKey,
TraitValue: fmt.Sprint(t.TraitValue),
}
}
func makeFlagFromFeatureState(featureState *features.FeatureStateModel, identityID string) Flag {
return Flag{
Enabled: featureState.Enabled,
Value: featureState.Value(identityID),
IsDefault: false,
FeatureID: featureState.Feature.ID,
FeatureName: featureState.Feature.Name,
}
}
type Flags struct {
flags []Flag
analyticsProcessor *AnalyticsProcessor
defaultFlagHandler func(featureName string) (Flag, error)
}
func makeFlagsFromFeatureStates(featureStates []*features.FeatureStateModel,
analyticsProcessor *AnalyticsProcessor,
defaultFlagHandler func(featureName string) (Flag, error),
identityID string) Flags {
flags := make([]Flag, len(featureStates))
for i, featureState := range featureStates {
flags[i] = makeFlagFromFeatureState(featureState, identityID)
}
return Flags{
flags: flags,
analyticsProcessor: analyticsProcessor,
defaultFlagHandler: defaultFlagHandler,
}
}
type jsonFeature struct {
ID int `json:"id"`
Name string `json:"name"`
}
type jsonFlag struct {
Enabled bool `json:"enabled"`
Value interface{} `json:"feature_state_value"`
Feature jsonFeature `json:"feature"`
}
func (jf *jsonFlag) toFlag() Flag {
return Flag{
Enabled: jf.Enabled,
Value: jf.Value,
IsDefault: false,
FeatureID: jf.Feature.ID,
FeatureName: jf.Feature.Name,
}
}
func makeFlagsFromAPIFlags(flagsJson []byte, analyticsProcessor *AnalyticsProcessor, defaultFlagHandler func(string) (Flag, error)) (Flags, error) {
var jsonflags []jsonFlag
err := json.Unmarshal(flagsJson, &jsonflags)
if err != nil {
return Flags{}, err
}
flags := make([]Flag, len(jsonflags))
for i, jf := range jsonflags {
flags[i] = jf.toFlag()
}
return Flags{
flags: flags,
analyticsProcessor: analyticsProcessor,
defaultFlagHandler: defaultFlagHandler,
}, err
}
func makeFlagsfromIdentityAPIJson(jsonResponse []byte, analyticsProcessor *AnalyticsProcessor, defaultFlagHandler func(string) (Flag, error)) (Flags, error) {
resonse := struct {
Flags interface{} `json:"flags"`
}{}
err := json.Unmarshal(jsonResponse, &resonse)
if err != nil {
return Flags{}, err
}
b, err := json.Marshal(resonse.Flags)
if err != nil {
return Flags{}, err
}
return makeFlagsFromAPIFlags(b, analyticsProcessor, defaultFlagHandler)
}
// Returns an array of all flag objects.
func (f *Flags) AllFlags() []Flag {
return f.flags
}
// Returns the value of a particular flag.
func (f *Flags) GetFeatureValue(featureName string) (interface{}, error) {
flag, err := f.GetFlag(featureName)
if err != nil {
return nil, err
}
return flag.Value, nil
}
// Returns a boolean indicating whether a particular flag is enabled.
func (f *Flags) IsFeatureEnabled(featureName string) (bool, error) {
flag, err := f.GetFlag(featureName)
if err != nil {
return false, err
}
return flag.Enabled, nil
}
// Returns a specific flag given the name of the feature.
func (f *Flags) GetFlag(featureName string) (Flag, error) {
var resultFlag Flag
for _, flag := range f.flags {
if flag.FeatureName == featureName {
resultFlag = flag
}
}
if resultFlag.FeatureID == 0 {
if f.defaultFlagHandler != nil {
return f.defaultFlagHandler(featureName)
}
return resultFlag, &FlagsmithClientError{fmt.Sprintf("flagsmith: No feature found with name %q", featureName)}
}
if f.analyticsProcessor != nil {
f.analyticsProcessor.TrackFeature(resultFlag.FeatureName)
}
return resultFlag, nil
}