Documentation
¶
Overview ¶
Package zero provides nullable types where zero values are treated as null.
Unlike package opt where zero values are valid (IntFrom(0) is valid), in package zero, zero values are null (IntFrom(0) is invalid). Invalid values marshal to their zero representation ("", 0, false), not to JSON null.
Index ¶
- type Bool
- type Byte
- type Float
- type Int
- type Int16
- type Int32
- type Option
- func (v Option[T]) Equal(other Option[T]) bool
- func (v Option[T]) IsZero() bool
- func (v Option[T]) MarshalJSON() ([]byte, error)
- func (v Option[T]) Or(fallback T) T
- func (v Option[T]) OrZero() T
- func (v Option[T]) Ptr() *T
- func (v *Option[T]) SetValid(val T)
- func (v *Option[T]) UnmarshalJSON(data []byte) error
- type String
- type Time
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
Bool is a nullable bool where false is treated as null.
func BoolFromPtr ¶
BoolFromPtr creates a Bool from a pointer. Nil results in null.
func (Bool) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to false when null.
func (Bool) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Bool) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Byte ¶
Byte is a nullable byte (uint8) where 0 is treated as null.
func ByteFromPtr ¶
ByteFromPtr creates a Byte from a pointer. Nil results in null.
func (Byte) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to 0 when null.
func (Byte) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Byte) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Byte) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Float ¶
Float is a nullable float64 where 0 is treated as null.
func FloatFromPtr ¶
FloatFromPtr creates a Float from a pointer. Nil results in null.
func (Float) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to 0 when null.
func (Float) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Float) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Float) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Int ¶
Int is a nullable int64 where 0 is treated as null.
func IntFromPtr ¶
IntFromPtr creates an Int from a pointer. Nil results in null.
func (Int) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to 0 when null.
func (Int) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Int) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Int16 ¶
Int16 is a nullable int16 where 0 is treated as null.
func Int16FromPtr ¶
Int16FromPtr creates an Int16 from a pointer. Nil results in null.
func (Int16) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to 0 when null.
func (Int16) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Int16) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Int16) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Int32 ¶
Int32 is a nullable int32 where 0 is treated as null.
func Int32FromPtr ¶
Int32FromPtr creates an Int32 from a pointer. Nil results in null.
func (Int32) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to 0 when null.
func (Int32) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Int32) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Int32) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Option ¶ added in v0.3.0
type Option[T comparable] struct { sql.Null[T] }
Option is a generic nullable type where the zero value of T is considered null.
func From ¶
func From[T comparable](v T) Option[T]
From creates an Option that is null if v equals the zero value of T.
func FromPtr ¶
func FromPtr[T comparable](ptr *T) Option[T]
FromPtr creates an Option from a pointer. Nil results in null.
func New ¶
func New[T comparable](v T, valid bool) Option[T]
New creates an Option with the given value and validity.
func (Option[T]) Equal ¶ added in v0.3.0
Equal reports whether two Options are equal by comparing OrZero results.
func (Option[T]) IsZero ¶ added in v0.3.0
IsZero returns true when null OR when the value equals zero.
func (Option[T]) MarshalJSON ¶ added in v0.3.0
MarshalJSON implements json.Marshaler. Marshals to the zero value of T when null, not to JSON "null".
func (Option[T]) Or ¶ added in v0.3.0
func (v Option[T]) Or(fallback T) T
Or returns the inner value if valid, otherwise the fallback.
func (Option[T]) OrZero ¶ added in v0.3.0
func (v Option[T]) OrZero() T
OrZero returns the inner value if valid, otherwise the zero value.
func (Option[T]) Ptr ¶ added in v0.3.0
func (v Option[T]) Ptr() *T
Ptr returns a pointer to the value, or nil if null.
func (*Option[T]) SetValid ¶ added in v0.3.0
func (v *Option[T]) SetValid(val T)
SetValid sets the value and marks it as valid.
func (*Option[T]) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON implements json.Unmarshaler.
type String ¶
String is a nullable string where empty string is treated as null.
func StringFrom ¶
StringFrom creates a String that is null if s is empty.
func StringFromPtr ¶
StringFromPtr creates a String from a pointer. Nil results in null.
func (String) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to "" when null.
func (String) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*String) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*String) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type Time ¶
Time is a nullable time.Time where the zero time is treated as null.
func TimeFromPtr ¶
TimeFromPtr creates a Time from a pointer. Nil results in null.
func (Time) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Marshals to zero time when null.
func (Time) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Time) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.