null

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 4 Imported by: 4

README

null

Nullable types for Go: the scalars null.Bool, null.String, null.Int, null.Int64, and null.Float, plus the generic null.Object[T] for everything else. Each is a struct carrying a value plus a present flag, so it distinguishes "absent/null" from "present-but-zero". All satisfy the Nullable interface (IsNull() bool, aliased as IsNil()) and implement JSON marshal/unmarshal. Part of rosetta.

Go Reference

// A zero-value null.Bool is null and ready to use — no constructor needed
var b null.Bool

b.Set(true)   // now present, value true
b.Bool()      // read the value
b.IsNull()    // false
b.Unset()     // back to null

b := null.NewBool(true) // or construct a non-null value directly

// null.Object[T] wraps any type with the same API
var o null.Object[time.Time]

o.Set(time.Now()) // now present
o.Object()        // read the value (T)
o.IsNull()        // false

o := null.NewObject(myStruct) // or construct a non-null value directly
Null, zero, and present

A plain int or bool cannot tell you whether it was ever assigned. That is the entire reason this package exists: the zero value of every type here is a valid null, immediately usable, and IsNull() reports it as such until someone calls Set().

Three predicates answer three different questions. IsNull() (and its alias IsNil()) asks was this ever set. IsPresent() is its inverse. IsZero() asks is this empty — it is TRUE for a null value and for a present zero, which makes it the right test for "nothing meaningful here" and the wrong test for "unassigned". Interface() rounds this out for code that works in any: it returns the value when present and an untyped nil when not.

Unset() returns a value to null, clearing the stored value along with the flag.

JSON

These types are strict on the way in and plain on the way out.

Marshaling emits either the bare value or the literal null. There is no omitempty-style elision — a null field always renders as null, and a present zero always renders as 0/false/"". Dropping the field entirely is the containing struct's decision, not this package's.

Unmarshaling never coerces across JSON types. A bare 123 is an error for null.String, not the text "123"; a quoted "123" is an error for null.Int. This is the deliberate opposite of the sibling lenient package, which exists for the receiving half of Postel's law — reach for lenient when a remote peer's encoding is out of your control, and for null when the schema is yours to enforce.

null.Object[T] is the exception in one respect: it hands T to encoding/json rather than parsing it itself, so T marshals and unmarshals exactly as it would anywhere else, and only the null literal is special-cased.

Fuzzing

All six UnmarshalJSON implementations are fuzzed in fuzz_test.go. Each target asserts that decoding arbitrary bytes never panics, and that any input the type accepts marshals back to bytes that decode to an equal value. Keep them green when touching a parse path.

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

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 NewBool

func NewBool(value bool) Bool

NewBool returns a fully populated, nullable bool

func (Bool) Bool

func (b Bool) Bool() bool

Bool returns the actual value of this object

func (Bool) Interface

func (b Bool) Interface() any

Interface returns the boolean value (if present) or NIL

func (Bool) IsNil added in v0.35.0

func (b Bool) IsNil() bool

IsNil returns TRUE if this value is null. It is an alias for IsNull

func (Bool) IsNull

func (b Bool) IsNull() bool

IsNull returns TRUE if this value is null

func (Bool) IsPresent

func (b Bool) IsPresent() bool

IsPresent returns TRUE if this value is present

func (Bool) IsZero added in v0.35.0

func (b Bool) IsZero() bool

IsZero returns TRUE if this value is null, or contains the zero value for its data type

func (Bool) MarshalJSON

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

MarshalJSON implements the json.Marshaller interface

func (*Bool) Set

func (b *Bool) Set(value bool)

Set applies a new value to the nullable item

func (Bool) String

func (b Bool) String() string

String returns "true" or "false" for a present value, or an empty string when null

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaller interface

func (*Bool) Unset

func (b *Bool) Unset()

Unset removes the value from this item, and sets it to null

type Float

type Float struct {
	// contains filtered or unexported fields
}

Float provides a nullable float64

func NewFloat

func NewFloat(value float64) Float

NewFloat returns a fully populated, nullable float64

func (Float) Float

func (f Float) Float() float64

Float returns the actual value of this object

func (Float) Interface

func (f Float) Interface() any

Interface returns the float64 value (if present) or NIL

func (Float) IsNil added in v0.35.0

func (f Float) IsNil() bool

IsNil returns TRUE if this value is null. It is an alias for IsNull

func (Float) IsNull

func (f Float) IsNull() bool

IsNull returns TRUE if this value is null

func (Float) IsPresent

func (f Float) IsPresent() bool

IsPresent returns TRUE if this value is present

func (Float) IsZero added in v0.35.0

func (f Float) IsZero() bool

IsZero returns TRUE if this value is null, or contains the zero value for its data type

func (Float) MarshalJSON

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

MarshalJSON implements the json.Marshaller interface

func (*Float) Set

func (f *Float) Set(value float64)

Set applies a new value to the nullable item

func (Float) String

func (f Float) String() string

String returns a string representation of this value

func (*Float) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaller interface

func (*Float) Unset

func (f *Float) Unset()

Unset removes the value from this item, and sets it to null

type Int

type Int struct {
	// contains filtered or unexported fields
}

Int provides a nullable int

func NewInt

func NewInt(value int) Int

NewInt returns a fully populated, nullable int

func (Int) Int

func (i Int) Int() int

Int returns the actual value of this object

func (Int) Interface

func (i Int) Interface() any

Interface returns the int value (if present) or NIL

func (Int) IsNil added in v0.35.0

func (i Int) IsNil() bool

IsNil returns TRUE if this value is null. It is an alias for IsNull

func (Int) IsNull

func (i Int) IsNull() bool

IsNull returns TRUE if this value is null

func (Int) IsPresent

func (i Int) IsPresent() bool

IsPresent returns TRUE if this value is present

func (Int) IsZero added in v0.35.0

func (i Int) IsZero() bool

IsZero returns TRUE if this value is null, or contains the zero value for its data type

func (Int) MarshalJSON

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

MarshalJSON implements the json.Marshaller interface

func (*Int) Set

func (i *Int) Set(value int)

Set applies a new value to the nullable item

func (Int) String

func (i Int) String() string

String returns a string representation of this value

func (*Int) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaller interface

func (*Int) Unset

func (i *Int) Unset()

Unset removes the value from this item, and sets it to null

type Int64

type Int64 struct {
	// contains filtered or unexported fields
}

Int64 provides a nullable int64

func NewInt64

func NewInt64(value int64) Int64

NewInt64 returns a fully populated, nullable int64

func (Int64) Int64

func (i Int64) Int64() int64

Int64 returns the actual value of this object

func (Int64) Interface

func (i Int64) Interface() any

Interface returns the int value (if present) or NIL

func (Int64) IsNil added in v0.35.0

func (i Int64) IsNil() bool

IsNil returns TRUE if this value is null. It is an alias for IsNull

func (Int64) IsNull

func (i Int64) IsNull() bool

IsNull returns TRUE if this value is null

func (Int64) IsPresent

func (i Int64) IsPresent() bool

IsPresent returns TRUE if this value is present

func (Int64) IsZero added in v0.35.0

func (i Int64) IsZero() bool

IsZero returns TRUE if this value is null, or contains the zero value for its data type

func (Int64) MarshalJSON

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

MarshalJSON implements the json.Marshaller interface

func (*Int64) Set

func (i *Int64) Set(value int64)

Set applies a new value to the nullable item

func (Int64) String

func (i Int64) String() string

String returns a string representation of this value

func (*Int64) UnmarshalJSON

func (i *Int64) UnmarshalJSON(value []byte) error

UnmarshalJSON implements the json.Unmarshaller interface

func (*Int64) Unset

func (i *Int64) Unset()

Unset removes the value from this item, and sets it to null

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 NewObject added in v0.35.0

func NewObject[T any](value T) Object[T]

NewObject returns a fully populated, nullable Object

func (Object[T]) Interface added in v0.35.0

func (x Object[T]) Interface() any

Interface returns the underlying value (if present) or NIL

func (Object[T]) IsNil added in v0.35.0

func (x Object[T]) IsNil() bool

IsNil returns TRUE if this value is null. It is an alias for IsNull

func (Object[T]) IsNull added in v0.35.0

func (x Object[T]) IsNull() bool

IsNull returns TRUE if this value is null

func (Object[T]) IsPresent added in v0.35.0

func (x Object[T]) IsPresent() bool

IsPresent returns TRUE if this value is present

func (Object[T]) IsZero added in v0.35.0

func (x Object[T]) IsZero() bool

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

func (x Object[T]) MarshalJSON() ([]byte, error)

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

func (x *Object[T]) UnmarshalJSON(value []byte) error

UnmarshalJSON implements the json.Unmarshaller interface

func (*Object[T]) Unset added in v0.35.0

func (x *Object[T]) Unset()

Unset removes the value from this item, and sets it to null

type String added in v0.35.0

type String struct {
	// contains filtered or unexported fields
}

String provides a nullable string

func NewString added in v0.35.0

func NewString(value string) String

NewString returns a fully populated, nullable string

func (String) Interface added in v0.35.0

func (s String) Interface() any

Interface returns the string value (if present) or NIL

func (String) IsNil added in v0.35.0

func (s String) IsNil() bool

IsNil returns TRUE if this value is null. It is an alias for IsNull

func (String) IsNull added in v0.35.0

func (s String) IsNull() bool

IsNull returns TRUE if this value is null

func (String) IsPresent added in v0.35.0

func (s String) IsPresent() bool

IsPresent returns TRUE if this value is present

func (String) IsZero added in v0.35.0

func (s String) IsZero() bool

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

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

MarshalJSON implements the json.Marshaller interface

func (*String) Set added in v0.35.0

func (s *String) Set(value string)

Set applies a new value to the nullable item

func (String) String added in v0.35.0

func (s String) String() string

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

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

UnmarshalJSON implements the json.Unmarshaller interface

func (*String) Unset added in v0.35.0

func (s *String) Unset()

Unset removes the value from this item, and sets it to null

Jump to

Keyboard shortcuts

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