Documentation
¶
Overview ¶
Package null provides nullable wrappers around Go primitive types: Bool, String, Int, Int64, and Float, plus the generic Object[T] for everything else.
Each type carries a value alongside a flag recording whether that value was ever set, which is the one thing a plain int or bool cannot tell you. The zero value of every type here is a valid null and needs no constructor. IsNull (aliased as IsNil) asks whether a value was ever set; IsZero asks whether it is empty, and answers TRUE for a null value and for a present zero alike.
JSON handling is strict in and plain out. Marshaling emits either the bare value or the literal null, with no omitempty-style elision — dropping the field entirely is the containing struct's decision. Unmarshaling never coerces across JSON types: a bare number is an error for String, and a quoted number is an error for Int. When a remote peer's encoding is not yours to control, the lenient package is the tolerant counterpart to this one.
Index ¶
- type Bool
- func (b Bool) Bool() bool
- func (b Bool) Interface() any
- func (b Bool) IsNil() bool
- func (b Bool) IsNull() bool
- func (b Bool) IsPresent() bool
- func (b Bool) IsZero() bool
- func (b Bool) MarshalJSON() ([]byte, error)
- func (b *Bool) Set(value bool)
- func (b Bool) String() string
- func (b *Bool) UnmarshalJSON(value []byte) error
- func (b *Bool) Unset()
- type Float
- func (f Float) Float() float64
- func (f Float) Interface() any
- func (f Float) IsNil() bool
- func (f Float) IsNull() bool
- func (f Float) IsPresent() bool
- func (f Float) IsZero() bool
- func (f Float) MarshalJSON() ([]byte, error)
- func (f *Float) Set(value float64)
- func (f Float) String() string
- func (f *Float) UnmarshalJSON(value []byte) error
- func (f *Float) Unset()
- type Int
- func (i Int) Int() int
- func (i Int) Interface() any
- func (i Int) IsNil() bool
- func (i Int) IsNull() bool
- func (i Int) IsPresent() bool
- func (i Int) IsZero() bool
- func (i Int) MarshalJSON() ([]byte, error)
- func (i *Int) Set(value int)
- func (i Int) String() string
- func (i *Int) UnmarshalJSON(value []byte) error
- func (i *Int) Unset()
- type Int64
- func (i Int64) Int64() int64
- func (i Int64) Interface() any
- func (i Int64) IsNil() bool
- func (i Int64) IsNull() bool
- func (i Int64) IsPresent() bool
- func (i Int64) IsZero() bool
- func (i Int64) MarshalJSON() ([]byte, error)
- func (i *Int64) Set(value int64)
- func (i Int64) String() string
- func (i *Int64) UnmarshalJSON(value []byte) error
- func (i *Int64) Unset()
- type Nullable
- type Object
- func (x Object[T]) Interface() any
- func (x Object[T]) IsNil() bool
- func (x Object[T]) IsNull() bool
- func (x Object[T]) IsPresent() bool
- func (x Object[T]) IsZero() bool
- func (x Object[T]) MarshalJSON() ([]byte, error)
- func (x Object[T]) Object() T
- func (x *Object[T]) Set(value T)
- func (x *Object[T]) UnmarshalJSON(value []byte) error
- func (x *Object[T]) Unset()
- type String
- func (s String) Interface() any
- func (s String) IsNil() bool
- func (s String) IsNull() bool
- func (s String) IsPresent() bool
- func (s String) IsZero() bool
- func (s String) MarshalJSON() ([]byte, error)
- func (s *String) Set(value string)
- func (s String) String() string
- func (s *String) UnmarshalJSON(value []byte) error
- func (s *String) Unset()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool provides a nullable bool
func (Bool) IsNil ¶ added in v0.35.0
IsNil returns TRUE if this value is null. It is an alias for IsNull
func (Bool) IsZero ¶ added in v0.35.0
IsZero returns TRUE if this value is null, or contains the zero value for its data type
func (Bool) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface
func (Bool) String ¶
String returns "true" or "false" for a present value, or an empty string when null
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaller interface
type Float ¶
type Float struct {
// contains filtered or unexported fields
}
Float provides a nullable float64
func (Float) IsNil ¶ added in v0.35.0
IsNil returns TRUE if this value is null. It is an alias for IsNull
func (Float) IsZero ¶ added in v0.35.0
IsZero returns TRUE if this value is null, or contains the zero value for its data type
func (Float) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface
func (*Float) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaller interface
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int provides a nullable int
func (Int) IsNil ¶ added in v0.35.0
IsNil returns TRUE if this value is null. It is an alias for IsNull
func (Int) IsZero ¶ added in v0.35.0
IsZero returns TRUE if this value is null, or contains the zero value for its data type
func (Int) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface
func (*Int) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaller interface
type Int64 ¶
type Int64 struct {
// contains filtered or unexported fields
}
Int64 provides a nullable int64
func (Int64) IsNil ¶ added in v0.35.0
IsNil returns TRUE if this value is null. It is an alias for IsNull
func (Int64) IsZero ¶ added in v0.35.0
IsZero returns TRUE if this value is null, or contains the zero value for its data type
func (Int64) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface
func (*Int64) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaller interface
type Nullable ¶
type Nullable interface {
// IsNull returns TRUE if the value of this object is null. FALSE otherwise.
IsNull() bool
}
Nullable interface wraps the "IsNull" function, which allows an object to identify if it is a null value or not.
type Object ¶ added in v0.35.0
type Object[T any] struct { // contains filtered or unexported fields }
Object provides a nullable wrapper around any type T
func (Object[T]) Interface ¶ added in v0.35.0
Interface returns the underlying value (if present) or NIL
func (Object[T]) IsNil ¶ added in v0.35.0
IsNil returns TRUE if this value is null. It is an alias for IsNull
func (Object[T]) IsZero ¶ added in v0.35.0
IsZero returns TRUE if this value is null, or contains the zero value for its data type
func (Object[T]) MarshalJSON ¶ added in v0.35.0
MarshalJSON implements the json.Marshaller interface
func (Object[T]) Object ¶ added in v0.35.0
func (x Object[T]) Object() T
Object returns the actual value of this object
func (*Object[T]) Set ¶ added in v0.35.0
func (x *Object[T]) Set(value T)
Set applies a new value to the nullable item
func (*Object[T]) UnmarshalJSON ¶ added in v0.35.0
UnmarshalJSON implements the json.Unmarshaller interface
type String ¶ added in v0.35.0
type String struct {
// contains filtered or unexported fields
}
String provides a nullable string
func (String) IsNil ¶ added in v0.35.0
IsNil returns TRUE if this value is null. It is an alias for IsNull
func (String) IsZero ¶ added in v0.35.0
IsZero returns TRUE if this value is null, or contains the zero value for its data type
func (String) MarshalJSON ¶ added in v0.35.0
MarshalJSON implements the json.Marshaller interface
func (String) String ¶ added in v0.35.0
String returns the actual value of this object. A null String reads back as "", which is also its zero value.
func (*String) UnmarshalJSON ¶ added in v0.35.0
UnmarshalJSON implements the json.Unmarshaller interface