...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldvalue/optional_int.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/ldvalue

     1  package ldvalue
     2  
     3  // OptionalInt represents an int that may or may not have a value. This is similar to using an
     4  // int pointer to distinguish between a zero value and nil, but it is safer because it does not
     5  // expose a pointer to any mutable value.
     6  //
     7  // To create an instance with an int value, use [NewOptionalInt]. There is no corresponding method
     8  // for creating an instance with no value; simply use the empty literal OptionalInt{}.
     9  //
    10  //	oi1 := NewOptionalInt(1)
    11  //	oi2 := NewOptionalInt(0) // this has a value which is zero
    12  //	oi3 := OptionalInt{}     // this does not have a value
    13  //
    14  // This can also be used as a convenient way to construct an int pointer within an expression.
    15  // For instance, this example causes myIntPointer to point to the int value 2:
    16  //
    17  //	var myIntPointer *int = NewOptionalInt("x").AsPointer()
    18  //
    19  // This type is used in ldreason.EvaluationDetail.VariationIndex, and for other similar fields
    20  // in the LaunchDarkly Go SDK where an int value may or may not be defined.
    21  //
    22  // The reason LaunchDarkly code uses this specific type instead of a generic Optional[T] is for
    23  // efficiency in JSON marshaling/unmarshaling. A generic type would have to use reflection and
    24  // dynamic typecasting for its marshal/unmarshal methods.
    25  type OptionalInt struct {
    26  	optValue optional[int]
    27  }
    28  
    29  // NewOptionalInt constructs an OptionalInt that has an int value.
    30  //
    31  // There is no corresponding method for creating an OptionalInt with no value; simply use the
    32  // empty literal OptionalInt{}.
    33  func NewOptionalInt(value int) OptionalInt {
    34  	return OptionalInt{optValue: newOptional(value)}
    35  }
    36  
    37  // NewOptionalIntFromPointer constructs an OptionalInt from an int pointer. If the pointer is
    38  // non-nil, then the OptionalInt copies its value; otherwise the OptionalInt has no value.
    39  func NewOptionalIntFromPointer(valuePointer *int) OptionalInt {
    40  	return OptionalInt{optValue: newOptionalFromPointer(valuePointer)}
    41  }
    42  
    43  // IsDefined returns true if the OptionalInt contains an int value, or false if it has no value.
    44  func (o OptionalInt) IsDefined() bool {
    45  	return o.optValue.isDefined()
    46  }
    47  
    48  // IntValue returns the OptionalInt's value, or zero if it has no value.
    49  func (o OptionalInt) IntValue() int {
    50  	return o.optValue.getOrZeroValue()
    51  }
    52  
    53  // Get is a combination of IntValue and IsDefined. If the OptionalInt contains an int value, it
    54  // returns that value and true; otherwise it returns zero and false.
    55  func (o OptionalInt) Get() (int, bool) {
    56  	return o.optValue.get()
    57  }
    58  
    59  // OrElse returns the OptionalInt's value if it has one, or else the specified fallback value.
    60  func (o OptionalInt) OrElse(valueIfEmpty int) int {
    61  	return o.optValue.getOrElse(valueIfEmpty)
    62  }
    63  
    64  // AsPointer returns the OptionalInt's value as an int pointer if it has a value, or nil
    65  // otherwise.
    66  //
    67  // The int value, if any, is copied rather than returning to a pointer to the internal field.
    68  func (o OptionalInt) AsPointer() *int {
    69  	return o.optValue.getAsPointer()
    70  }
    71  
    72  // AsValue converts the OptionalInt to a [Value], which is either [Null]() or a number value.
    73  func (o OptionalInt) AsValue() Value {
    74  	if value, ok := o.optValue.get(); ok {
    75  		return Int(value)
    76  	}
    77  	return Null()
    78  }
    79  

View as plain text