-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.go
272 lines (242 loc) · 9.01 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package flagsmithapi
import (
"encoding/json"
"log"
"time"
)
type Project struct {
ID int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name"`
Organisation int64 `json:"organisation"`
HideDisabledFlags bool `json:"hide_disabled_flags,omitempty"`
PreventFlagDefaults bool `json:"prevent_flag_defaults,omitempty"`
OnlyAllowLowerCaseFeatureNames bool `json:"only_allow_lower_case_feature_names,omitempty"`
FeatureNameRegex string `json:"feature_name_regex,omitempty"`
StaleFlagsLimitDays int64 `json:"stale_flags_limit_days,omitempty"`
EnableRealtimeUpdates bool `json:"enable_realtime_updates,omitempty"`
}
type FeatureMultivariateOption struct {
ID int64 `json:"id,omitempty"`
Type string `json:"type"`
UUID string `json:"uuid,omitempty"`
FeatureID *int64 `json:"feature,omitempty"`
IntegerValue *int64 `json:"integer_value,omitempty"`
StringValue *string `json:"string_value,omitempty"`
BooleanValue *bool `json:"boolean_value,omitempty"`
DefaultPercentageAllocation float64 `json:"default_percentage_allocation"`
FeatureUUID string `json:"-"`
ProjectID *int64 `json:"-"`
}
type Feature struct {
Name string `json:"name"`
UUID string `json:"uuid,omitempty"`
ID *int64 `json:"id,omitempty"`
Type *string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
InitialValue string `json:"initial_value,omitempty"`
DefaultEnabled bool `json:"default_enabled,omitempty"`
IsArchived bool `json:"is_archived,omitempty"`
Owners *[]int64 `json:"owners,omitempty"`
Tags []int64 `json:"tags"`
ProjectUUID string `json:"-"`
ProjectID *int64 `json:"project,omitempty"`
}
func (f *Feature) UnmarshalJSON(data []byte) error {
type owner struct {
ID int64 `json:"id"`
}
var obj struct {
Name string `json:"name"`
UUID string `json:"uuid,omitempty"`
ID *int64 `json:"id,omitempty"`
Type *string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
InitialValue string `json:"initial_value,omitempty"`
DefaultEnabled bool `json:"default_enabled,omitempty"`
IsArchived bool `json:"is_archived,omitempty"`
Owners []owner `json:"owners,omitempty"`
ProjectID *int64 `json:"project,omitempty"`
Tags []int64 `json:"tags"`
}
err := json.Unmarshal(data, &obj)
if err != nil {
return err
}
f.Name = obj.Name
f.UUID = obj.UUID
f.ID = obj.ID
f.Type = obj.Type
f.Description = obj.Description
f.InitialValue = obj.InitialValue
f.DefaultEnabled = obj.DefaultEnabled
f.IsArchived = obj.IsArchived
f.ProjectID = obj.ProjectID
f.Tags = obj.Tags
if obj.Owners != nil {
f.Owners = &[]int64{}
for _, o := range obj.Owners {
*f.Owners = append(*f.Owners, o.ID)
}
}
return nil
}
type FeatureStateValue struct {
Type string `json:"type"`
StringValue *string `json:"string_value"`
IntegerValue *int64 `json:"integer_value"`
BooleanValue *bool `json:"boolean_value"`
}
type FeatureState struct {
ID int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
FeatureStateValue *FeatureStateValue `json:"feature_state_value"`
Enabled bool `json:"enabled"`
Feature int64 `json:"feature"`
Environment *int64 `json:"environment"`
FeatureSegment *int64 `json:"feature_segment,omitempty"`
EnvironmentKey string `json:"-"`
Segment *int64 `json:"-"`
SegmentPriority *int64 `json:"-"`
}
func (fs *FeatureState) UnmarshalJSON(data []byte) error {
var obj struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
FeatureStateValue json.RawMessage `json:"feature_state_value"`
Enabled bool `json:"enabled"`
Feature int64 `json:"feature"`
Environment *int64 `json:"environment"`
FeatureSegment *int64 `json:"feature_segment"`
}
err := json.Unmarshal(data, &obj)
if err != nil {
return err
}
fs.ID = obj.ID
fs.Enabled = obj.Enabled
fs.Feature = obj.Feature
fs.Environment = obj.Environment
fs.FeatureSegment = obj.FeatureSegment
fs.UUID = obj.UUID
// If the feature state value is a struct(i.e: we are using `/features/featurestates/` endpoint) then unmarshal, set
// and exit
featureStateValue := FeatureStateValue{}
err = json.Unmarshal(obj.FeatureStateValue, &featureStateValue)
if err == nil {
fs.FeatureStateValue = &featureStateValue
return nil
}
// else(i.e: we are using `/environments/<env_key>/featurestates/` endpoint) convert the feature state value to struct, set and exit
var fsValueRaw interface{}
err = json.Unmarshal(obj.FeatureStateValue, &fsValueRaw)
if err != nil {
log.Println("flagsmithapi: Error unmarshalling FeatureStateValue: ", err)
}
switch fsv := fsValueRaw.(type) {
case int64:
fs.FeatureStateValue = &FeatureStateValue{
Type: "int",
IntegerValue: &fsv,
}
case string:
fs.FeatureStateValue = &FeatureStateValue{
Type: "unicode",
StringValue: &fsv,
}
case bool:
fs.FeatureStateValue = &FeatureStateValue{
Type: "bool",
BooleanValue: &fsv,
}
case float64:
int64Fsv := int64(fsv)
fs.FeatureStateValue = &FeatureStateValue{
Type: "int",
IntegerValue: &int64Fsv,
}
default:
fs.FeatureStateValue = nil
}
return nil
}
type Condition struct {
Operator string `json:"operator"`
Property string `json:"property"`
Value string `json:"value"`
}
type Rule struct {
Type string `json:"type"`
Rules []Rule `json:"rules,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
}
type Segment struct {
ID *int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name"`
Description *string `json:"description,omitempty"`
ProjectID *int64 `json:"project"`
ProjectUUID string `json:"-"`
FeatureID *int64 `json:"feature,omitempty"`
Rules []Rule `json:"rules"`
}
type FeatureSegment struct {
ID *int64 `json:"id,omitempty"`
Feature int64 `json:"feature"`
Segment *int64 `json:"segment"`
Environment int64 `json:"environment"`
Priority *int64 `json:"priority"`
}
type Environment struct {
ID int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name"`
APIKey string `json:"api_key,omitempty"`
Description string `json:"description"`
ProjectID int64 `json:"project"`
AllowClientTraits bool `json:"allow_client_traits,omitempty"`
BannerText string `json:"banner_text,omitempty"`
BannerColour string `json:"banner_colour,omitempty"`
HideDisabledFlags bool `json:"hide_disabled_flags,omitempty"`
HideSensitiveData bool `json:"hide_sensitive_data,omitempty"`
UseIdentityCompositeKeyForHashing bool `json:"use_identity_composite_key_for_hashing,omitempty"`
MinimumChangeRequestApprovals int64 `json:"minimum_change_request_approvals,omitempty"`
}
type Tag struct {
ID *int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"label"`
Description *string `json:"description"`
Colour string `json:"color"`
ProjectUUID string `json:"-"`
ProjectID *int64 `json:"project,omitempty"`
}
type Identity struct {
ID *int64 `json:"id,omitempty"`
Identifier string `json:"identifier"`
}
type ServerSideEnvKey struct {
ID int64 `json:"id,omitempty"`
Active bool `json:"active"`
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
}
type Trait struct {
ID int64 `json:"id,omitempty"`
TraitKey string `json:"trait_key"`
ValueType string `json:"value_type"`
IntegerValue *int `json:"integer_value,omitempty"`
StringValue *string `json:"string_value,omitempty"`
BooleanValue *bool `json:"boolean_value,omitempty"`
FloatValue *float64 `json:"float_value,omitempty"`
}
type Organisation struct {
ID int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name"`
Force2FA bool `json:"force_2fa"`
RestrictProjectCreateToAdmin bool `json:"restrict_project_create_to_admin"`
PersistTraitData bool `json:"persist_trait_data"`
}