/
reason.go
295 lines (259 loc) · 11.9 KB
/
reason.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package ldreason
import (
"fmt"
"gopkg.in/launchdarkly/go-sdk-common.v2/jsonstream" //nolint:staticcheck // using a deprecated API
"gopkg.in/launchdarkly/go-sdk-common.v2/ldvalue"
"gopkg.in/launchdarkly/go-jsonstream.v1/jreader"
"gopkg.in/launchdarkly/go-jsonstream.v1/jwriter"
)
// EvalReasonKind defines the possible values of EvaluationReason.GetKind().
type EvalReasonKind string
const (
// EvalReasonOff indicates that the flag was off and therefore returned its configured off value.
EvalReasonOff EvalReasonKind = "OFF"
// EvalReasonTargetMatch indicates that the user key was specifically targeted for this flag.
EvalReasonTargetMatch EvalReasonKind = "TARGET_MATCH"
// EvalReasonRuleMatch indicates that the user matched one of the flag's rules.
EvalReasonRuleMatch EvalReasonKind = "RULE_MATCH"
// EvalReasonPrerequisiteFailed indicates that the flag was considered off because it had at
// least one prerequisite flag that either was off or did not return the desired variation.
EvalReasonPrerequisiteFailed EvalReasonKind = "PREREQUISITE_FAILED"
// EvalReasonFallthrough indicates that the flag was on but the user did not match any targets
// or rules.
EvalReasonFallthrough EvalReasonKind = "FALLTHROUGH"
// EvalReasonError indicates that the flag could not be evaluated, e.g. because it does not
// exist or due to an unexpected error. In this case the result value will be the default value
// that the caller passed to the client.
EvalReasonError EvalReasonKind = "ERROR"
)
// EvalErrorKind defines the possible values of EvaluationReason.GetErrorKind().
type EvalErrorKind string
const (
// EvalErrorClientNotReady indicates that the caller tried to evaluate a flag before the client
// had successfully initialized.
EvalErrorClientNotReady EvalErrorKind = "CLIENT_NOT_READY"
// EvalErrorFlagNotFound indicates that the caller provided a flag key that did not match any
// known flag.
EvalErrorFlagNotFound EvalErrorKind = "FLAG_NOT_FOUND"
// EvalErrorMalformedFlag indicates that there was an internal inconsistency in the flag data,
// e.g. a rule specified a nonexistent variation.
EvalErrorMalformedFlag EvalErrorKind = "MALFORMED_FLAG"
// EvalErrorUserNotSpecified indicates that the caller passed a user without a key for the user
// parameter.
EvalErrorUserNotSpecified EvalErrorKind = "USER_NOT_SPECIFIED"
// EvalErrorWrongType indicates that the result value was not of the requested type, e.g. you
// called BoolVariationDetail but the value was an integer.
EvalErrorWrongType EvalErrorKind = "WRONG_TYPE"
// EvalErrorException indicates that an unexpected error stopped flag evaluation; check the
// log for details.
EvalErrorException EvalErrorKind = "EXCEPTION"
)
// BigSegmentsStatus defines the possible values of EvaluationReason.GetBigSegmentsStatus().
//
// "Big segments" are a specific type of user segments. For more information, read the LaunchDarkly
// documentation about user segments: https://docs.launchdarkly.com/home/users
type BigSegmentsStatus string
const (
// BigSegmentsHealthy indicates that the big segment query involved in the flag
// evaluation was successful, and that the segment state is considered up to date.
BigSegmentsHealthy BigSegmentsStatus = "HEALTHY"
// BigSegmentsStale indicates that the big segment query involved in the flag
// evaluation was successful, but that the segment state may not be up to date.
BigSegmentsStale BigSegmentsStatus = "STALE"
// BigSegmentsNotConfigured indicates that big segments could not be queried for the
// flag evaluation because the SDK configuration did not include a big segment store.
BigSegmentsNotConfigured BigSegmentsStatus = "NOT_CONFIGURED"
// BigSegmentsStoreError indicates that the big segment query involved in the flag
// evaluation failed, for instance due to a database error.
BigSegmentsStoreError BigSegmentsStatus = "STORE_ERROR"
)
// EvaluationReason describes the reason that a flag evaluation producted a particular value.
//
// This struct is immutable; its properties can be accessed only via getter methods.
type EvaluationReason struct {
kind EvalReasonKind
ruleIndex ldvalue.OptionalInt
ruleID string
prerequisiteKey string
inExperiment bool
errorKind EvalErrorKind
bigSegmentsStatus BigSegmentsStatus
}
// IsDefined returns true if this EvaluationReason has a non-empty GetKind(). It is false for a
// zero value of EvaluationReason{}.
func (r EvaluationReason) IsDefined() bool {
return r.kind != ""
}
// String returns a concise string representation of the reason. Examples: "OFF", "ERROR(WRONG_TYPE)".
func (r EvaluationReason) String() string {
switch r.kind {
case EvalReasonRuleMatch:
return fmt.Sprintf("%s(%d,%s)", r.kind, r.ruleIndex.OrElse(0), r.ruleID)
case EvalReasonPrerequisiteFailed:
return fmt.Sprintf("%s(%s)", r.kind, r.prerequisiteKey)
case EvalReasonError:
return fmt.Sprintf("%s(%s)", r.kind, r.errorKind)
default:
return string(r.GetKind())
}
}
// GetKind describes the general category of the reason.
func (r EvaluationReason) GetKind() EvalReasonKind {
return r.kind
}
// GetRuleIndex provides the index of the rule that was matched (0 being the first), if
// the Kind is EvalReasonRuleMatch. Otherwise it returns -1.
func (r EvaluationReason) GetRuleIndex() int {
return r.ruleIndex.OrElse(-1)
}
// GetRuleID provides the unique identifier of the rule that was matched, if the Kind is
// EvalReasonRuleMatch. Otherwise it returns an empty string. Unlike the rule index, this
// identifier will not change if other rules are added or deleted.
func (r EvaluationReason) GetRuleID() string {
return r.ruleID
}
// GetPrerequisiteKey provides the flag key of the prerequisite that failed, if the Kind
// is EvalReasonPrerequisiteFailed. Otherwise it returns an empty string.
func (r EvaluationReason) GetPrerequisiteKey() string {
return r.prerequisiteKey
}
// IsInExperiment describes whether the evaluation was part of an experiment. It returns
// true if the evaluation resulted in an experiment rollout *and* served one of the
// variations in the experiment. Otherwise it returns false.
func (r EvaluationReason) IsInExperiment() bool {
return r.inExperiment
}
// GetErrorKind describes the general category of the error, if the Kind is EvalReasonError.
// Otherwise it returns an empty string.
func (r EvaluationReason) GetErrorKind() EvalErrorKind {
return r.errorKind
}
// GetBigSegmentsStatus describes the validity of big segment information, if and only if the flag
// evaluation required querying at least one big segment. Otherwise it returns an empty string.
//
// "Big segments" are a specific kind of user segments. For more information, read the LaunchDarkly
// documentation about user segments: https://docs.launchdarkly.com/home/users
func (r EvaluationReason) GetBigSegmentsStatus() BigSegmentsStatus {
return r.bigSegmentsStatus
}
// NewEvalReasonOff returns an EvaluationReason whose Kind is EvalReasonOff.
func NewEvalReasonOff() EvaluationReason {
return EvaluationReason{kind: EvalReasonOff}
}
// NewEvalReasonFallthrough returns an EvaluationReason whose Kind is EvalReasonFallthrough.
func NewEvalReasonFallthrough() EvaluationReason {
return EvaluationReason{kind: EvalReasonFallthrough}
}
// NewEvalReasonFallthroughExperiment returns an EvaluationReason whose Kind is
// EvalReasonFallthrough. The inExperiment parameter represents whether the evaluation was
// part of an experiment.
func NewEvalReasonFallthroughExperiment(inExperiment bool) EvaluationReason {
return EvaluationReason{kind: EvalReasonFallthrough, inExperiment: inExperiment}
}
// NewEvalReasonTargetMatch returns an EvaluationReason whose Kind is EvalReasonTargetMatch.
func NewEvalReasonTargetMatch() EvaluationReason {
return EvaluationReason{kind: EvalReasonTargetMatch}
}
// NewEvalReasonRuleMatch returns an EvaluationReason whose Kind is EvalReasonRuleMatch.
func NewEvalReasonRuleMatch(ruleIndex int, ruleID string) EvaluationReason {
return EvaluationReason{kind: EvalReasonRuleMatch,
ruleIndex: ldvalue.NewOptionalInt(ruleIndex), ruleID: ruleID}
}
// NewEvalReasonRuleMatchExperiment returns an EvaluationReason whose Kind is
// EvalReasonRuleMatch. The inExperiment parameter represents whether the evaluation was
// part of an experiment.
func NewEvalReasonRuleMatchExperiment(ruleIndex int, ruleID string, inExperiment bool) EvaluationReason {
return EvaluationReason{
kind: EvalReasonRuleMatch,
ruleIndex: ldvalue.NewOptionalInt(ruleIndex),
ruleID: ruleID,
inExperiment: inExperiment,
}
}
// NewEvalReasonPrerequisiteFailed returns an EvaluationReason whose Kind is EvalReasonPrerequisiteFailed.
func NewEvalReasonPrerequisiteFailed(prereqKey string) EvaluationReason {
return EvaluationReason{kind: EvalReasonPrerequisiteFailed, prerequisiteKey: prereqKey}
}
// NewEvalReasonError returns an EvaluationReason whose Kind is EvalReasonError.
func NewEvalReasonError(errorKind EvalErrorKind) EvaluationReason {
return EvaluationReason{kind: EvalReasonError, errorKind: errorKind}
}
// NewEvalReasonFromReasonWithBigSegmentsStatus returns a copy of an EvaluationReason
// with a specific BigSegmentsStatus value added.
func NewEvalReasonFromReasonWithBigSegmentsStatus(
reason EvaluationReason,
bigSegmentsStatus BigSegmentsStatus,
) EvaluationReason {
reason.bigSegmentsStatus = bigSegmentsStatus
return reason
}
// MarshalJSON implements custom JSON serialization for EvaluationReason.
func (r EvaluationReason) MarshalJSON() ([]byte, error) {
return jwriter.MarshalJSONWithWriter(r)
}
// UnmarshalJSON implements custom JSON deserialization for EvaluationReason.
func (r *EvaluationReason) UnmarshalJSON(data []byte) error {
return jreader.UnmarshalJSONWithReader(data, r)
}
// ReadFromJSONReader provides JSON deserialization for use with the jsonstream API.
//
// This implementation is used by the SDK in cases where it is more efficient than JSON.Unmarshal.
// See https://github.com/launchdarkly/go-jsonstream for more details.
func (r *EvaluationReason) ReadFromJSONReader(reader *jreader.Reader) {
var ret EvaluationReason
for obj := reader.ObjectOrNull(); obj.Next(); {
switch string(obj.Name()) {
case "kind":
ret.kind = EvalReasonKind(reader.String())
case "ruleId":
ret.ruleID = reader.String()
case "ruleIndex":
ret.ruleIndex = ldvalue.NewOptionalInt(reader.Int())
case "errorKind":
ret.errorKind = EvalErrorKind(reader.String())
case "prerequisiteKey":
ret.prerequisiteKey = reader.String()
case "inExperiment":
ret.inExperiment = reader.Bool()
case "bigSegmentsStatus":
ret.bigSegmentsStatus = BigSegmentsStatus(reader.String())
}
}
if reader.Error() == nil {
*r = ret
}
}
// WriteToJSONWriter provides JSON serialization for use with the jsonstream API.
//
// This implementation is used by the SDK in cases where it is more efficient than JSON.Marshal.
// See https://github.com/launchdarkly/go-jsonstream for more details.
func (r EvaluationReason) WriteToJSONWriter(w *jwriter.Writer) {
if r.kind == "" {
w.Null()
return
}
obj := w.Object()
obj.Name("kind").String(string(r.kind))
if r.ruleIndex.IsDefined() {
obj.Name("ruleIndex").Int(r.ruleIndex.OrElse(0))
obj.Maybe("ruleId", r.ruleID != "").String(r.ruleID)
}
obj.Maybe("inExperiment", r.inExperiment).Bool(r.inExperiment)
if r.kind == EvalReasonPrerequisiteFailed {
obj.Name("prerequisiteKey").String(r.prerequisiteKey)
}
if r.kind == EvalReasonError {
obj.Name("errorKind").String(string(r.errorKind))
}
if r.bigSegmentsStatus != "" {
obj.Name("bigSegmentsStatus").String(string(r.bigSegmentsStatus))
}
obj.End()
}
// WriteToJSONBuffer provides JSON serialization for use with the deprecated jsonstream API.
//
// Deprecated: this method is provided for backward compatibility. The LaunchDarkly SDK no longer
// uses this API; instead it uses the newer https://github.com/launchdarkly/go-jsonstream.
func (r EvaluationReason) WriteToJSONBuffer(j *jsonstream.JSONBuffer) {
jsonstream.WriteToJSONBufferThroughWriter(r, j)
}