Documentation
¶
Index ¶
- type Optional
- func (e Optional[T]) MarshalJSON() ([]byte, error)
- func (e Optional[T]) Nil() bool
- func (e Optional[T]) OrElse(d T) T
- func (e Optional[T]) Present() bool
- func (e Optional[T]) Ptr() *T
- func (e *Optional[T]) Scan(src any) error
- func (e *Optional[T]) UnmarshalJSON(data []byte) error
- func (e Optional[T]) Valid() bool
- func (e 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 { Item T // contains filtered or unexported fields }
func (Optional[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. It returns "null" if the value is either absent or nil.
func (Optional[T]) OrElse ¶
func (e Optional[T]) OrElse(d T) T
OrElse checks if the value of Optional struct exists and is not nil, if so it returns it, otherwise it returns the given argument d.
func (Optional[T]) Ptr ¶
func (e Optional[T]) Ptr() *T
Ptr returns a pointer to the inner value if the value is present and not nil. Otherwise it returns nil.
func (*Optional[T]) Scan ¶
Scan implements sql.Scanner interface. Upon calling this function (e.g. from sql.Rows Scan function), the isPresent is set to true. It sets isNil to true if the database value (src) is null, otherwise it sets the value to [Optional.Item] and sets isNil to false.
func (*Optional[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. When this function is called (e.g. by json.Unmarshal), the isPresent value is set to true. This is because it means there is a field matching this value field name, and there is a value for it. If the JSON value of data is null, the soil is set to true. If the json.Unmarshal returns error isPresent and isNil are set to false before returning the error.
func (Optional[T]) Value ¶
Value implements driver.Valuer interface. If the wrapped value Item implements driver.Valuer that Value() function will be called, otherwise it will return the Item directly. driver.Valuer mentions that the returned value must be of type:
[int64]
[float64]
[bool]
[[]byte]
[string]
It's up to the end-usage of the Optional struct whether this requirement will be met depending on the type of [Optional.Item].