...

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

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

     1  package ldvalue
     2  
     3  // This generic internal representation of optional values reduces the amount of boilerplate
     4  // in the implementation of OptionalBool, OptionalInt, and OptionalString.
     5  
     6  type optional[T any] struct {
     7  	defined bool
     8  	value   T
     9  }
    10  
    11  func newOptional[T any](value T) optional[T] {
    12  	return optional[T]{defined: true, value: value}
    13  }
    14  
    15  func newOptionalFromPointer[T any](valuePointer *T) optional[T] {
    16  	if valuePointer == nil {
    17  		return optional[T]{}
    18  	}
    19  	return optional[T]{defined: true, value: *valuePointer}
    20  }
    21  
    22  func (o optional[T]) isDefined() bool {
    23  	return o.defined
    24  }
    25  
    26  func (o optional[T]) getOrZeroValue() T {
    27  	return o.value
    28  }
    29  
    30  func (o optional[T]) getOrElse(defaultValue T) T {
    31  	if o.defined {
    32  		return o.value
    33  	}
    34  	return defaultValue
    35  }
    36  
    37  func (o optional[T]) get() (T, bool) {
    38  	return o.value, o.defined
    39  }
    40  
    41  func (o optional[T]) getAsPointer() *T {
    42  	if !o.defined {
    43  		return nil
    44  	}
    45  	return &o.value
    46  }
    47  

View as plain text