Documentation
¶
Overview ¶
Package gonull provides a generic Nullable type for handling nullable values in a convenient way. This is useful when working with databases and JSON, where nullable values are common.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportedConversion is an error that occurs when attempting to convert a value to an unsupported type. // This typically happens when Scan is called with a value that cannot be converted to the target type T. ErrUnsupportedConversion = errors.New("unsupported type conversion") )
Functions ¶
This section is empty.
Types ¶
type Nullable ¶
Nullable is a generic struct that holds a nullable value of any type T. It keeps track of the value (Val), a flag (Valid) indicating whether the value has been set and a flag (Present) indicating if the value is in the struct. This allows for better handling of nullable and undefined values, ensuring proper value management and serialization.
func NewNullable ¶
NewNullable creates a new Nullable with the given value and sets Valid to true. This is useful when you want to create a Nullable with an initial value, explicitly marking it as set.
func (Nullable[T]) IsZero ¶
IsZero implements the json.Zeroed interface for Nullable, enabling it to be used as a nullable field in JSON operations. This method ensures proper marshalling of Nullable values into JSON data, representing unset values as null in the serialized output.
func (Nullable[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for Nullable, enabling it to be used as a nullable field in JSON operations. This method ensures proper marshalling of Nullable values into JSON data, representing unset values as null in the serialized output.
func (Nullable[T]) OrElse ¶
func (n Nullable[T]) OrElse(defaultVal T) T
OrElse returns the underlying Val if valid otherwise returns the provided defaultVal
func (*Nullable[T]) Scan ¶
Scan implements the sql.Scanner interface for Nullable, allowing it to be used as a nullable field in database operations. It is responsible for properly setting the Valid flag and converting the scanned value to the target type T. This enables seamless integration with database/sql when working with nullable values.
func (*Nullable[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for Nullable, allowing it to be used as a nullable field in JSON operations. This method ensures proper unmarshalling of JSON data into the Nullable value, correctly setting the Valid flag based on the JSON data.