Documentation
¶
Overview ¶
Package nullable provides a generic Nullable type for handling nullable values. This package supports serialization to JSON and YAML, as well as integration with databases through the sql.Scanner and driver.Valuer interfaces.
Index ¶
- Variables
- type Nullable
- func (n Nullable[T]) GetValue() T
- func (n Nullable[T]) HasValue() bool
- func (n Nullable[T]) IsNull() bool
- func (n Nullable[T]) MarshalJSON() ([]byte, error)
- func (n Nullable[T]) MarshalYAML() (any, error)
- func (n Nullable[T]) OrElse(defaultVal T) T
- func (n *Nullable[T]) Scan(value any) error
- func (n *Nullable[T]) UnmarshalJSON(data []byte) error
- func (n *Nullable[T]) UnmarshalYAML(unmarshal func(any) error) error
- func (n Nullable[T]) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var (
ErrUnsupportedConversion = errors.New("unsupported type conversion")
)
ErrUnsupportedConversion occurs when attempting to convert a value to an unsupported type.
Functions ¶
This section is empty.
Types ¶
type Nullable ¶
type Nullable[T any] struct { // contains filtered or unexported fields }
Nullable represents a nullable value of any type T. The value field holds the actual value of type T. valid indicates whether the value is set (true) or is null (false).
func FromPointer ¶
FromPointer creates a Nullable from a pointer. If the pointer is nil, valid is set to false.
func (Nullable[T]) GetValue ¶
func (n Nullable[T]) GetValue() T
GetValue returns the actual value T.
func (Nullable[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for Nullable.
func (Nullable[T]) MarshalYAML ¶
MarshalYAML implements the marshaling of YAML data.
func (Nullable[T]) OrElse ¶
func (n Nullable[T]) OrElse(defaultVal T) T
OrElse returns the value if valid is true; otherwise, it returns the provided defaultVal.
func (*Nullable[T]) Scan ¶
Scan implements the sql.Scanner interface for Nullable, allowing it to be used in database operations.
func (*Nullable[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for Nullable.
func (*Nullable[T]) UnmarshalYAML ¶
UnmarshalYAML implements the unmarshaling of YAML data.