zero

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	Option[bool]
}

Bool is a nullable bool where false is treated as null.

func BoolFrom

func BoolFrom(b bool) Bool

BoolFrom creates a Bool that is null if b is false.

func BoolFromPtr

func BoolFromPtr(b *bool) Bool

BoolFromPtr creates a Bool from a pointer. Nil results in null.

func NewBool

func NewBool(b bool, valid bool) Bool

NewBool creates a Bool with the given value and validity.

func (Bool) Equal

func (b Bool) Equal(other Bool) bool

Equal reports whether two Bools are equal.

func (Bool) MarshalJSON

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

MarshalJSON implements json.Marshaler. Marshals to false when null.

func (Bool) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Bool) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

type Byte

type Byte struct {
	Option[byte]
}

Byte is a nullable byte (uint8) where 0 is treated as null.

func ByteFrom

func ByteFrom(b byte) Byte

ByteFrom creates a Byte that is null if b is 0.

func ByteFromPtr

func ByteFromPtr(b *byte) Byte

ByteFromPtr creates a Byte from a pointer. Nil results in null.

func NewByte

func NewByte(b byte, valid bool) Byte

NewByte creates a Byte with the given value and validity.

func (Byte) Equal

func (b Byte) Equal(other Byte) bool

Equal reports whether two Bytes are equal.

func (Byte) MarshalJSON

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

MarshalJSON implements json.Marshaler. Marshals to 0 when null.

func (Byte) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (*Byte) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Byte) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

type Float

type Float struct {
	Option[float64]
}

Float is a nullable float64 where 0 is treated as null.

func FloatFrom

func FloatFrom(f float64) Float

FloatFrom creates a Float that is null if f is 0.

func FloatFromPtr

func FloatFromPtr(f *float64) Float

FloatFromPtr creates a Float from a pointer. Nil results in null.

func NewFloat

func NewFloat(f float64, valid bool) Float

NewFloat creates a Float with the given value and validity.

func (Float) Equal

func (f Float) Equal(other Float) bool

Equal reports whether two Floats are equal.

func (Float) MarshalJSON

func (f Float) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Marshals to 0 when null.

func (Float) MarshalText

func (f Float) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Float) UnmarshalJSON

func (f *Float) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Float) UnmarshalText

func (f *Float) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Int

type Int struct {
	Option[int64]
}

Int is a nullable int64 where 0 is treated as null.

func IntFrom

func IntFrom(i int64) Int

IntFrom creates an Int that is null if i is 0.

func IntFromPtr

func IntFromPtr(i *int64) Int

IntFromPtr creates an Int from a pointer. Nil results in null.

func NewInt

func NewInt(i int64, valid bool) Int

NewInt creates an Int with the given value and validity.

func (Int) Equal

func (i Int) Equal(other Int) bool

Equal reports whether two Ints are equal.

func (Int) MarshalJSON

func (i Int) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Marshals to 0 when null.

func (Int) MarshalText

func (i Int) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Int) UnmarshalJSON

func (i *Int) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Int) UnmarshalText

func (i *Int) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Int16

type Int16 struct {
	Option[int16]
}

Int16 is a nullable int16 where 0 is treated as null.

func Int16From

func Int16From(i int16) Int16

Int16From creates an Int16 that is null if i is 0.

func Int16FromPtr

func Int16FromPtr(i *int16) Int16

Int16FromPtr creates an Int16 from a pointer. Nil results in null.

func NewInt16

func NewInt16(i int16, valid bool) Int16

NewInt16 creates an Int16 with the given value and validity.

func (Int16) Equal

func (i Int16) Equal(other Int16) bool

Equal reports whether two Int16s are equal.

func (Int16) MarshalJSON

func (i Int16) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Marshals to 0 when null.

func (Int16) MarshalText

func (i Int16) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Int16) UnmarshalJSON

func (i *Int16) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Int16) UnmarshalText

func (i *Int16) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Int32

type Int32 struct {
	Option[int32]
}

Int32 is a nullable int32 where 0 is treated as null.

func Int32From

func Int32From(i int32) Int32

Int32From creates an Int32 that is null if i is 0.

func Int32FromPtr

func Int32FromPtr(i *int32) Int32

Int32FromPtr creates an Int32 from a pointer. Nil results in null.

func NewInt32

func NewInt32(i int32, valid bool) Int32

NewInt32 creates an Int32 with the given value and validity.

func (Int32) Equal

func (i Int32) Equal(other Int32) bool

Equal reports whether two Int32s are equal.

func (Int32) MarshalJSON

func (i Int32) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Marshals to 0 when null.

func (Int32) MarshalText

func (i Int32) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*Int32) UnmarshalJSON

func (i *Int32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Int32) UnmarshalText

func (i *Int32) UnmarshalText(text []byte) error

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

func (v Option[T]) Equal(other Option[T]) bool

Equal reports whether two Options are equal by comparing OrZero results.

func (Option[T]) IsZero added in v0.3.0

func (v Option[T]) IsZero() bool

IsZero returns true when null OR when the value equals zero.

func (Option[T]) MarshalJSON added in v0.3.0

func (v Option[T]) MarshalJSON() ([]byte, error)

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

func (v *Option[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type String

type String struct {
	Option[string]
}

String is a nullable string where empty string is treated as null.

func NewString

func NewString(s string, valid bool) String

NewString creates a String with the given value and validity.

func StringFrom

func StringFrom(s string) String

StringFrom creates a String that is null if s is empty.

func StringFromPtr

func StringFromPtr(s *string) String

StringFromPtr creates a String from a pointer. Nil results in null.

func (String) Equal

func (s String) Equal(other String) bool

Equal reports whether two Strings are equal.

func (String) MarshalJSON

func (s String) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. Marshals to "" when null.

func (String) MarshalText

func (s String) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*String) UnmarshalText

func (s *String) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Time

type Time struct {
	Option[time.Time]
}

Time is a nullable time.Time where the zero time is treated as null.

func NewTime

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

NewTime creates a Time with the given value and validity.

func TimeFrom

func TimeFrom(t time.Time) Time

TimeFrom creates a Time that is null if t is the zero time.

func TimeFromPtr

func TimeFromPtr(t *time.Time) Time

TimeFromPtr creates a Time from a pointer. Nil results in null.

func (Time) Equal

func (t Time) Equal(other Time) bool

Equal reports whether two Times represent the same instant.

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler. Marshals to zero time when null.

func (Time) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (*Time) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Time) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

Jump to

Keyboard shortcuts

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