Documentation ¶
Overview ¶
Package zero contains SQL types that consider zero input and null input to be equivalent with convenient support for JSON and text marshaling. Types in this package will JSON marshal to their zero value, even if null. Use the null parent package if you don't want this.
Index ¶
- type Bool
- func (b Bool) Equal(other Bool) bool
- func (b Bool) IsZero() bool
- func (b Bool) MarshalJSON() ([]byte, error)
- func (b Bool) MarshalText() ([]byte, error)
- func (b Bool) Ptr() *bool
- func (b *Bool) SetValid(v bool)
- func (b *Bool) UnmarshalJSON(data []byte) error
- func (b *Bool) UnmarshalText(text []byte) error
- func (b Bool) ValueOrZero() bool
- type Byte
- func (b Byte) Equal(other Byte) bool
- func (b Byte) IsZero() bool
- func (b Byte) MarshalJSON() ([]byte, error)
- func (b Byte) MarshalText() ([]byte, error)
- func (b Byte) Ptr() *byte
- func (b *Byte) SetValid(n byte)
- func (b *Byte) UnmarshalJSON(data []byte) error
- func (b *Byte) UnmarshalText(text []byte) error
- func (b Byte) ValueOrZero() byte
- type Float
- func (f Float) Equal(other Float) bool
- func (f Float) IsZero() bool
- func (f Float) MarshalJSON() ([]byte, error)
- func (f Float) MarshalText() ([]byte, error)
- func (f Float) Ptr() *float64
- func (f *Float) SetValid(v float64)
- func (f *Float) UnmarshalJSON(data []byte) error
- func (f *Float) UnmarshalText(text []byte) error
- func (f Float) ValueOrZero() float64
- type Int
- func (i Int) Equal(other Int) bool
- func (i Int) IsZero() bool
- func (i Int) MarshalJSON() ([]byte, error)
- func (i Int) MarshalText() ([]byte, error)
- func (i Int) Ptr() *int64
- func (i *Int) SetValid(n int64)
- func (i *Int) UnmarshalJSON(data []byte) error
- func (i *Int) UnmarshalText(text []byte) error
- func (i Int) ValueOrZero() int64
- type Int16
- func (i Int16) Equal(other Int16) bool
- func (i Int16) IsZero() bool
- func (i Int16) MarshalJSON() ([]byte, error)
- func (i Int16) MarshalText() ([]byte, error)
- func (i Int16) Ptr() *int16
- func (i *Int16) SetValid(n int16)
- func (i *Int16) UnmarshalJSON(data []byte) error
- func (i *Int16) UnmarshalText(text []byte) error
- func (i Int16) ValueOrZero() int16
- type Int32
- func (i Int32) Equal(other Int32) bool
- func (i Int32) IsZero() bool
- func (i Int32) MarshalJSON() ([]byte, error)
- func (i Int32) MarshalText() ([]byte, error)
- func (i Int32) Ptr() *int32
- func (i *Int32) SetValid(n int32)
- func (i *Int32) UnmarshalJSON(data []byte) error
- func (i *Int32) UnmarshalText(text []byte) error
- func (i Int32) ValueOrZero() int32
- type Int64
- type String
- func (s String) Equal(other String) bool
- func (s String) IsZero() bool
- func (s String) MarshalText() ([]byte, error)
- func (s String) Ptr() *string
- func (s *String) SetValid(v string)
- func (s *String) UnmarshalJSON(data []byte) error
- func (s *String) UnmarshalText(text []byte) error
- func (s String) ValueOrZero() string
- type Time
- func (t Time) Equal(other Time) bool
- func (t Time) ExactEqual(other Time) bool
- func (t Time) IsZero() bool
- func (t Time) MarshalJSON() ([]byte, error)
- func (t Time) MarshalText() ([]byte, error)
- func (t Time) Ptr() *time.Time
- func (t *Time) SetValid(v time.Time)
- func (t *Time) UnmarshalJSON(data []byte) error
- func (t *Time) UnmarshalText(text []byte) error
- func (t Time) Value() (driver.Value, error)
- func (t Time) ValueOrZero() time.Time
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
Bool is a nullable bool. False input is considered null. JSON marshals to false if null. Considered null to SQL unmarshaled from a false value.
func BoolFromPtr ¶
BoolFromPtr creates a new Bool that be null if b is nil.
func (Bool) Equal ¶
Equal returns true if both booleans are true and valid, or if both booleans are either false or invalid.
func (Bool) IsZero ¶
IsZero returns true for null or zero Bools, for future omitempty support (Go 1.4?)
func (Bool) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode null if this Bool is null.
func (Bool) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a zero if this Bool is null.
func (Bool) Ptr ¶
Ptr returns a poBooler to this Bool's value, or a nil poBooler if this Bool is null.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. "false" will be considered a null Bool.
func (*Bool) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Bool if the input is false or blank. It will return an error if the input is not a float, blank, or "null".
func (Bool) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise false.
type Byte ¶
Byte is a nullable byte. JSON marshals to zero if null. Considered null to SQL if zero.
func ByteFromPtr ¶
ByteFromPtr creates a new Byte that be null if i is nil.
func (Byte) Equal ¶
Equal returns true if both ints have the same value or are both either null or zero.
func (Byte) IsZero ¶
IsZero returns true for null or zero Bytes, for future omitempty support (Go 1.4?)
func (Byte) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode 0 if this Byte is null.
func (Byte) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a zero if this Byte is null.
func (Byte) Ptr ¶
Ptr returns a pointer to this Byte's value, or a nil pointer if this Byte is null.
func (*Byte) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Byte.
func (*Byte) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Byte if the input is a blank, or zero. It will return an error if the input is not an integer, blank, or "null".
func (Byte) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type Float ¶
type Float struct {
sql.NullFloat64
}
Float is a nullable float64. Zero input will be considered null. JSON marshals to zero if null. Considered null to SQL if zero.
func FloatFromPtr ¶
FloatFromPtr creates a new Float that be null if f is nil.
func (Float) Equal ¶
Equal returns true if both floats have the same value or are both either null or zero. Warning: calculations using floating point numbers can result in different ways the numbers are stored in memory. Therefore, this function is not suitable to compare the result of a calculation. Use this method only to check if the value has changed in comparison to some previous value.
func (Float) IsZero ¶
IsZero returns true for null or zero Floats, for future omitempty support (Go 1.4?)
func (Float) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode null if this Float is null.
func (Float) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a zero if this Float is null.
func (Float) Ptr ¶
Ptr returns a poFloater to this Float's value, or a nil poFloater if this Float is null.
func (*Float) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Float.
func (*Float) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Float if the input is blank or zero. It will return an error if the input is not a float, blank, or "null".
func (Float) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type Int ¶
Int is a nullable int64. JSON marshals to zero if null. Considered null to SQL if zero.
func IntFromPtr ¶
IntFromPtr creates a new Int that be null if i is nil.
func (Int) Equal ¶
Equal returns true if both ints have the same value or are both either null or zero.
func (Int) IsZero ¶
IsZero returns true for null or zero Ints, for future omitempty support (Go 1.4?)
func (Int) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode 0 if this Int is null.
func (Int) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a zero if this Int is null.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Int.
func (*Int) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int if the input is a blank, or zero. It will return an error if the input is not an integer, blank, or "null".
func (Int) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type Int16 ¶
Int16 is a nullable int16. JSON marshals to zero if null. Considered null to SQL if zero.
func Int16FromPtr ¶
Int16FromPtr creates a new Int16 that be null if i is nil.
func (Int16) Equal ¶
Equal returns true if both ints have the same value or are both either null or zero.
func (Int16) IsZero ¶
IsZero returns true for null or zero Int16s, for future omitempty support (Go 1.4?)
func (Int16) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode 0 if this Int16 is null.
func (Int16) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a zero if this Int16 is null.
func (Int16) Ptr ¶
Ptr returns a pointer to this Int16's value, or a nil pointer if this Int16 is null.
func (*Int16) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Int16.
func (*Int16) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int16 if the input is a blank, or zero. It will return an error if the input is not an integer, blank, or "null".
func (Int16) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type Int32 ¶
Int32 is a nullable int32. JSON marshals to zero if null. Considered null to SQL if zero.
func Int32FromPtr ¶
Int32FromPtr creates a new Int32 that be null if i is nil.
func (Int32) Equal ¶
Equal returns true if both ints have the same value or are both either null or zero.
func (Int32) IsZero ¶
IsZero returns true for null or zero Int32s, for future omitempty support (Go 1.4?)
func (Int32) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode 0 if this Int32 is null.
func (Int32) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a zero if this Int32 is null.
func (Int32) Ptr ¶
Ptr returns a pointer to this Int32's value, or a nil pointer if this Int32 is null.
func (*Int32) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Int32.
func (*Int32) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int32 if the input is a blank, or zero. It will return an error if the input is not an integer, blank, or "null".
func (Int32) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type String ¶
type String struct {
sql.NullString
}
String is a nullable string. JSON marshals to a blank string if null. Considered null to SQL if zero.
func StringFrom ¶
StringFrom creates a new String that will be null if s is blank.
func StringFromPtr ¶
StringFromPtr creates a new String that be null if s is nil or blank. It will make s point to the String's value.
func (String) Equal ¶
Equal returns true if both strings have the same value or are both either null or empty.
func (String) IsZero ¶
IsZero returns true for null or empty strings, for potential future omitempty support.
func (String) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode a blank string when this String is null.
func (String) Ptr ¶
Ptr returns a pointer to this String's value, or a nil pointer if this String is null.
func (*String) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports string and null input. Blank string input produces a null String.
func (*String) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null String if the input is a blank string.
func (String) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type Time ¶
Time is a nullable time.Time. JSON marshals to the zero value for time.Time if null. Considered to be null to SQL if zero.
func TimeFromPtr ¶
TimeFromPtr creates a new Time that will be null if t is nil or *t is the zero value.
func (Time) Equal ¶
Equal returns true if both Time objects encode the same time or are both are either null or zero. Two times can be equal even if they are in different locations. For example, 6:00 +0200 CEST and 4:00 UTC are Equal.
func (Time) ExactEqual ¶
ExactEqual returns true if both Time objects are equal or both are either null or zero. ExactEqual returns false for times that are in different locations or have a different monotonic clock reading.
func (Time) IsZero ¶
IsZero returns true for null or zero Times, for potential future omitempty support.
func (Time) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode the zero value of time.Time if this time is invalid.
func (Time) MarshalText ¶
MarshalText implements encoding.TextMarshaler. It will encode to an empty time.Time if invalid.
func (Time) Ptr ¶
Ptr returns a pointer to this Time's value, or a nil pointer if this Time is zero.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports string and null input.
func (*Time) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler. It has compatibility with the null package in that it will accept empty strings as invalid values, which will be unmarshaled to an invalid zero value.
func (Time) ValueOrZero ¶
ValueOrZero returns the inner value if valid, otherwise zero.
type Value ¶
type Value[T comparable] struct { sql.Null[T] }
func ValueFrom ¶
func ValueFrom[T comparable](t T) Value[T]
ValueFrom creates a new Value that will always be valid.
func ValueFromPtr ¶
func ValueFromPtr[T comparable](t *T) Value[T]
ValueFromPtr creates a new Value that will be null if t is nil.
func (Value[T]) Equal ¶
Equal returns true if both Value objects encode the same value or are both null.
func (Value[T]) IsZero ¶
IsZero returns true for invalid or zero Values, hopefully for future omitempty support.
func (Value[T]) MarshalJSON ¶
MarshalJSON implements json.Marshaler. It will encode null if this value is null or zero.
func (Value[T]) Ptr ¶
func (t Value[T]) Ptr() *T
Ptr returns a pointer to this Value's value, or a nil pointer if this Value is null.
func (*Value[T]) SetValid ¶
func (t *Value[T]) SetValid(v T)
SetValid changes this Value's value and sets it to be non-null.
func (*Value[T]) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. It supports string and null input.
func (Value[T]) ValueOrZero ¶
func (t Value[T]) ValueOrZero() T
ValueOrZero returns the inner value if valid, otherwise zero.