@@ -11,9 +11,11 @@ type EvaluationDetail struct {
1111 // the default value that was passed to the Variation method.
1212 Value ldvalue.Value
1313 // VariationIndex is the index of the returned value within the flag's list of variations, e.g.
14- // 0 for the first variation. A negative number indicates that the application default value was
15- // returned because the flag could not be evaluated.
16- VariationIndex int
14+ // 0 for the first variation. This is an ldvalue.OptionalInt rather than an int, because it is
15+ // possible for the value to be undefined (there is no variation index if the application default
16+ // value was returned due to an error in evaluation) which is different from a value of 0. See
17+ // ldvalue.OptionalInt for more about how to use this type.
18+ VariationIndex ldvalue.OptionalInt
1719 // Reason is an EvaluationReason object describing the main factor that influenced the flag
1820 // evaluation value.
1921 Reason EvaluationReason
@@ -23,15 +25,21 @@ type EvaluationDetail struct {
2325// This means that an error prevented the flag from being evaluated; the Reason field should contain
2426// an error value such as NewEvalReasonError(EvalErrorFlagNotFound).
2527func (d EvaluationDetail ) IsDefaultValue () bool {
26- return d .VariationIndex < 0
28+ return ! d .VariationIndex . IsDefined ()
2729}
2830
29- // NewEvaluationDetail constructs an EvaluationDeteail, specifying all fields.
30- func NewEvaluationDetail (value ldvalue.Value , variationIndex int , reason EvaluationReason ) EvaluationDetail {
31- return EvaluationDetail {Value : value , VariationIndex : variationIndex , Reason : reason }
31+ // NewEvaluationDetail constructs an EvaluationDetail, specifying all fields. This assumes that there
32+ // is a defined value for variationIndex; if variationIndex is undefined, use NewEvaluationDetailForError
33+ // or set the struct fields directly.
34+ func NewEvaluationDetail (
35+ value ldvalue.Value ,
36+ variationIndex int ,
37+ reason EvaluationReason ,
38+ ) EvaluationDetail {
39+ return EvaluationDetail {Value : value , VariationIndex : ldvalue .NewOptionalInt (variationIndex ), Reason : reason }
3240}
3341
3442// NewEvaluationDetailForError constructs an EvaluationDetail for an error condition.
3543func NewEvaluationDetailForError (errorKind EvalErrorKind , defaultValue ldvalue.Value ) EvaluationDetail {
36- return EvaluationDetail {Value : defaultValue , VariationIndex : - 1 , Reason : NewEvalReasonError (errorKind )}
44+ return EvaluationDetail {Value : defaultValue , Reason : NewEvalReasonError (errorKind )}
3745}
0 commit comments