null

package
v0.16.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StringFrom    = null.StringFrom
	StringFromPtr = null.StringFromPtr
	NewString     = null.NewString
	IntFrom       = null.IntFrom
	IntFromPtr    = null.IntFromPtr
	NewInt        = null.NewInt
	Int16From     = null.Int16From
	Int16FromPtr  = null.Int16FromPtr
	NewInt16      = null.NewInt16
	Int32From     = null.Int32From
	Int32FromPtr  = null.Int32FromPtr
	NewInt32      = null.NewInt32
	FloatFrom     = null.FloatFrom
	FloatFromPtr  = null.FloatFromPtr
	NewFloat      = null.NewFloat
	ByteFrom      = null.ByteFrom
	ByteFromPtr   = null.ByteFromPtr
	NewByte       = null.NewByte
)
View Source
var ErrInvalidBoolText = errors.New("null: invalid input for UnmarshalText")

ErrInvalidBoolText indicates invalid text for null.Bool UnmarshalText.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	sql.NullBool
}

Bool is a nullable bool. It does not consider false values to be null. It will decode to null, not false, if null.

func BoolFrom

func BoolFrom(b bool) Bool

BoolFrom creates a new Bool that will always be valid.

func BoolFromPtr

func BoolFromPtr(b *bool) Bool

BoolFromPtr creates a new Bool that will be null if f is nil.

func NewBool

func NewBool(b, valid bool) Bool

NewBool creates a new Bool.

func (Bool) Equal

func (b Bool) Equal(other Bool) bool

Equal returns true if both booleans have the same value or are both null.

func (Bool) IsZero

func (b Bool) IsZero() bool

IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?) A non-null Bool with a 0 value will not be considered zero.

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this Bool is null.

func (Bool) MarshalText

func (b Bool) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Bool is null.

func (Bool) Ptr

func (b Bool) Ptr() *bool

Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.

func (*Bool) SetValid

func (b *Bool) SetValid(v bool)

SetValid changes this Bool's value and also sets it to be non-null.

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will not be considered a null Bool.

func (*Bool) UnmarshalText

func (b *Bool) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Bool if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Bool) Value

func (b Bool) Value() (driver.Value, error)

func (Bool) ValueOr

func (b Bool) ValueOr(v bool) bool

ValueOr returns the inner value if valid, otherwise v.

func (Bool) ValueOrZero

func (b Bool) ValueOrZero() bool

ValueOrZero returns the inner value if valid, otherwise false.

type Byte

type Byte = null.Byte

type Date

type Date struct {
	sql.Null[datetime.Date]
}

Date is a nullable datetime.Date. It supports SQL and JSON serialization. It will marshal to null if null.

func DateFrom

func DateFrom(d datetime.Date) Date

DateFrom creates a new Date that will always be valid.

func DateFromPtr

func DateFromPtr(d *datetime.Date) Date

DateFromPtr creates a new Date that will be null if d is nil.

func NewDate

func NewDate(d datetime.Date, valid bool) Date

NewDate creates a new Date.

func (Date) Equal

func (d Date) Equal(other Date) bool

Equal returns true if both Date objects encode the same date or are both null. Two dates can be equal even if they are in different locations. For example, 2023-01-01 +0200 CEST and 2023-01-01 UTC are Equal.

func (Date) ExactEqual

func (d Date) ExactEqual(other Date) bool

ExactEqual returns true if both Date objects are equal or both null. ExactEqual returns false for dates that are in different locations or have a different monotonic clock reading.

func (Date) IsZero

func (d Date) IsZero() bool

IsZero returns true for invalid Dates, hopefully for future omitempty support. A non-null Date with a zero value will not be considered zero.

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this date is null.

func (Date) MarshalText

func (d Date) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It returns an empty string if invalid, otherwise datetime.Date's MarshalText.

func (Date) Ptr

func (d Date) Ptr() *datetime.Date

Ptr returns a pointer to this Date's value, or a nil pointer if this Date is null.

func (*Date) SetValid

func (d *Date) SetValid(v datetime.Date)

SetValid changes this Date's value and sets it to be non-null.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string and null input.

func (*Date) UnmarshalText

func (d *Date) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It has backwards compatibility with v3 in that the string "null" is considered equivalent to an empty string and unmarshaling will succeed. This may be removed in a future version.

func (Date) ValueOr

func (d Date) ValueOr(v datetime.Date) datetime.Date

ValueOr returns the inner value if valid, otherwise v.

func (Date) ValueOrZero

func (d Date) ValueOrZero() datetime.Date

ValueOrZero returns the inner value if valid, otherwise zero.

type DateTime

type DateTime struct {
	sql.Null[datetime.DateTime]
}

DateTime is a nullable datetime.DateTime. It supports SQL and JSON serialization. It will marshal to null if null.

func DateTimeFrom

func DateTimeFrom(dt datetime.DateTime) DateTime

DateTimeFrom creates a new DateTime that will always be valid.

func DateTimeFromPtr

func DateTimeFromPtr(dt *datetime.DateTime) DateTime

DateTimeFromPtr creates a new DateTime that will be null if dt is nil.

func NewDateTime

func NewDateTime(dt datetime.DateTime, valid bool) DateTime

NewDateTime creates a new DateTime.

func (DateTime) Equal

func (dt DateTime) Equal(other DateTime) bool

Equal returns true if both DateTime objects encode the same datetime or are both null. Two datetimes can be equal even if they are in different locations. For example, 2023-01-01 12:00:00 +0200 CEST and 2023-01-01 10:00:00 UTC are Equal.

func (DateTime) ExactEqual

func (dt DateTime) ExactEqual(other DateTime) bool

ExactEqual returns true if both DateTime objects are equal or both null. ExactEqual returns false for datetimes that are in different locations or have a different monotonic clock reading.

func (DateTime) IsZero

func (dt DateTime) IsZero() bool

IsZero returns true for invalid DateTimes, hopefully for future omitempty support. A non-null DateTime with a zero value will not be considered zero.

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this datetime is null.

func (DateTime) MarshalText

func (dt DateTime) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It returns an empty string if invalid, otherwise datetime.DateTime's MarshalText.

func (DateTime) Ptr

func (dt DateTime) Ptr() *datetime.DateTime

Ptr returns a pointer to this DateTime's value, or a nil pointer if this DateTime is null.

func (*DateTime) SetValid

func (dt *DateTime) SetValid(v datetime.DateTime)

SetValid changes this DateTime's value and sets it to be non-null.

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string and null input.

func (*DateTime) UnmarshalText

func (dt *DateTime) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It has backwards compatibility with v3 in that the string "null" is considered equivalent to an empty string and unmarshaling will succeed. This may be removed in a future version.

func (DateTime) ValueOr

func (dt DateTime) ValueOr(v datetime.DateTime) datetime.DateTime

ValueOr returns the inner value if valid, otherwise v.

func (DateTime) ValueOrZero

func (dt DateTime) ValueOrZero() datetime.DateTime

ValueOrZero returns the inner value if valid, otherwise zero.

type Decimal

type Decimal struct {
	dec.NullDecimal
}

Decimal represents a decimal.Decimal that may be null. It wraps shopspring/decimal.NullDecimal and provides additional convenience methods for common operations. The underlying NullDecimal handles SQL database integration through the driver.Valuer and sql.Scanner interfaces.

func DecimalFrom

func DecimalFrom(d decimal.Decimal) Decimal

DecimalFrom creates a valid (non-null) Decimal from a decimal.Decimal value. This is equivalent to NewDecimal(d, true).

func DecimalFromPtr

func DecimalFromPtr(d *decimal.Decimal) Decimal

DecimalFromPtr creates a Decimal from a pointer to decimal.Decimal. If the pointer is nil, returns an invalid (null) Decimal. If the pointer is not nil, returns a valid Decimal with the dereferenced value.

func NewDecimal

func NewDecimal(d decimal.Decimal, valid bool) Decimal

NewDecimal creates a new Decimal with the given value and validity. If valid is false, the decimal is considered null regardless of the decimal value.

func (Decimal) Equal

func (d Decimal) Equal(other Decimal) bool

Equal performs semantic equality comparison between two Decimal values. Two decimals are equal if they have the same validity status and, if both are valid, their decimal values are mathematically equal (using decimal.Equal which handles precision differences correctly).

func (Decimal) ExactEqual

func (d Decimal) ExactEqual(other Decimal) bool

ExactEqual performs exact equality comparison between two Decimal values. Unlike Equal, this method requires both the validity status and the underlying decimal representation (including precision and scale) to be identical. This is stricter than Equal and should be used when exact representation matters.

func (Decimal) IsZero

func (d Decimal) IsZero() bool

IsZero returns true if the decimal is null (invalid). Note: This checks for null status, not whether the decimal value itself is zero. A valid decimal with value 0 will return false.

func (Decimal) Ptr

func (d Decimal) Ptr() *decimal.Decimal

Ptr returns a pointer to the decimal value if valid, or nil if null. This is useful when you need to pass the value to Apis that expect *decimal.Decimal, with nil representing the absence of a value.

func (*Decimal) SetValid

func (d *Decimal) SetValid(v decimal.Decimal)

This method modifies the receiver, so it must be called on a pointer.

func (Decimal) ValueOr

func (d Decimal) ValueOr(v decimal.Decimal) decimal.Decimal

ValueOr returns the decimal value if valid, or the provided fallback value if null. This allows for custom default values when the decimal is null.

func (Decimal) ValueOrZero

func (d Decimal) ValueOrZero() decimal.Decimal

ValueOrZero returns the decimal value if valid, or decimal.Zero if null. This method provides a safe way to get a usable decimal value without having to check validity manually.

type Float

type Float = null.Float

type Int

type Int = null.Int

type Int16

type Int16 = null.Int16

type Int32

type Int32 = null.Int32

type String

type String = null.String

type Time

type Time struct {
	sql.Null[datetime.Time]
}

Time is a nullable datetime.Time. It supports SQL and JSON serialization. It will marshal to null if null.

func NewTime

func NewTime(t datetime.Time, valid bool) Time

NewTime creates a new Time.

func TimeFrom

func TimeFrom(t datetime.Time) Time

TimeFrom creates a new Time that will always be valid.

func TimeFromPtr

func TimeFromPtr(t *datetime.Time) Time

TimeFromPtr creates a new Time that will be null if t is nil.

func (Time) Equal

func (t Time) Equal(other Time) bool

Equal returns true if both Time objects encode the same time or are both null. 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

func (t Time) ExactEqual(other Time) bool

ExactEqual returns true if both Time objects are equal or both null. ExactEqual returns false for times that are in different locations or have a different monotonic clock reading.

func (Time) IsZero

func (t Time) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Time) MarshalText

func (t Time) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler. It returns an empty string if invalid, otherwise datetime.Time's MarshalText.

func (Time) Ptr

func (t Time) Ptr() *datetime.Time

Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.

func (*Time) SetValid

func (t *Time) SetValid(v datetime.Time)

SetValid changes this Time's value and sets it to be non-null.

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string and null input.

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler. It has backwards compatibility with v3 in that the string "null" is considered equivalent to an empty string and unmarshaling will succeed. This may be removed in a future version.

func (Time) ValueOr

func (t Time) ValueOr(v datetime.Time) datetime.Time

ValueOr returns the inner value if valid, otherwise v.

func (Time) ValueOrZero

func (t Time) ValueOrZero() datetime.Time

ValueOrZero returns the inner value if valid, otherwise zero.

type Value

type Value[T any] = null.Value[T]

func NewValue

func NewValue[T any](t T, valid bool) Value[T]

func ValueFrom

func ValueFrom[T any](t T) Value[T]

func ValueFromPtr

func ValueFromPtr[T any](t *T) Value[T]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL