Documentation ¶
Overview ¶
optional.go is a simple wrapper around a value and a presence flag
Index ¶
- type Optional
- func (optional Optional[T]) GetOrAlternative(alternative T) T
- func (optional Optional[T]) GetOrDefault(alternative T) T
- func (optional Optional[T]) GetOrGenerate(alternative func() T) T
- func (optional Optional[T]) GetTransformedOrEmpty(transformer func(T) any) Optional[any]
- func (optional Optional[T]) GetTransformedOrSelf(transformer func(T) T) Optional[T]
- func (optional Optional[T]) IsZero() bool
- func (optional Optional[T]) MarshalCSV() (string, error)
- func (optional Optional[T]) MarshalJSON() ([]byte, error)
- func (optional *Optional[T]) MarshalText() (text []byte, err error)
- func (optional Optional[T]) Match(someHandler func(T), noneHandler func(T))
- func (optional *Optional[T]) Scan(value any) error
- func (optional *Optional[T]) Set(val T)
- func (optional Optional[T]) String() string
- func (optional *Optional[T]) UnmarshalCSV(val string) error
- func (optional *Optional[T]) UnmarshalJSON(input []byte) error
- func (optional *Optional[T]) UnmarshalText(text []byte) error
- func (optional *Optional[T]) Unset() T
- func (optional Optional[T]) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Optional ¶
type Optional[T any] struct { Wrappee T `json:"wrapee" db:"wrapee"` HasValue bool `json:"has_value" db:"has_value"` }
Optional holds a Wrappe and a flag indicating the existence or abscence of the Wrapee.
func Make ¶ added in v1.1.0
Make creates an optional holding the specified wrappee and sets the HasValue flag to true.
func (Optional[T]) GetOrAlternative ¶ added in v1.5.0
func (optional Optional[T]) GetOrAlternative(alternative T) T
GetOrAlternative returns the Wrapee if the HasValue flag is true, otherwise alternative is returned.
func (Optional[T]) GetOrDefault ¶ added in v1.5.0
func (optional Optional[T]) GetOrDefault(alternative T) T
GetOrDefualt returns the Wrapee if the HasValue flag is true, otherwise T's default value is returned.
func (Optional[T]) GetOrGenerate ¶ added in v1.5.0
func (optional Optional[T]) GetOrGenerate(alternative func() T) T
GetOrGenerate returns the Wrapee if the HasValue flag is true, otherwise the result of alternative() is returned.
func (Optional[T]) GetTransformedOrEmpty ¶ added in v1.5.0
GetTransformedOrEmpty returns transformer(Wrappee) if HasValue == true, otherwise returns an empty Optional.
func (Optional[T]) GetTransformedOrSelf ¶ added in v1.5.0
GetTransformedOrEmpty returns transformer(Wrappee) if HasValue == true, otherwise returns self.
func (Optional[T]) MarshalCSV ¶ added in v1.2.0
MarshalCSV implements the gocarina/gocsv.TypeMarshaller interface.
func (Optional[T]) MarshalJSON ¶ added in v1.6.0
func (*Optional[T]) MarshalText ¶ added in v1.2.0
MarshalText implements the encoding.TextMarshaller interface.
func (Optional[T]) Match ¶ added in v1.5.0
func (optional Optional[T]) Match(someHandler func(T), noneHandler func(T))
Match executes someHandler if HasValue is true and noneHandler otherwise.
func (*Optional[T]) Set ¶ added in v1.5.0
func (optional *Optional[T]) Set(val T)
Set sets a Wrappee to val value and the HasValue flag to true.
func (*Optional[T]) UnmarshalCSV ¶ added in v1.2.0
UnmarshalCSV implements the gocarina/gocsv.TypeUnmarshaller interface.
func (*Optional[T]) UnmarshalJSON ¶
func (*Optional[T]) UnmarshalText ¶ added in v1.2.0
UnmarshalText implements the encoding.TextUnmarshaller interface.