null

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 2 Imported by: 4

README

null

Nullable scalar primitives for Go: null.Bool, null.Int, null.Int64, and null.Float. 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) and implement JSON marshal/unmarshal. Part of rosetta.

GoDoc

// 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

What matters here

  • The zero value is a valid "null" — no constructor required. var x null.Int is immediately usable and reads as null (IsNull() == true). This is the whole point of the package: it separates "never set" from "set to zero", which a plain int/bool cannot.
  • Unset() resets BOTH the value and the present flag. It zeroes the stored value and clears present, so an unset value reads back as the type's zero — don't rely on a stale value surviving an Unset.
  • MarshalJSON emits the bare value or null; there is no omitempty-style elision. A null value marshals to the literal null, and a present zero marshals to 0/false/0. To omit a null field from JSON output entirely, the containing struct must handle that — this type always renders something.
  • UnmarshalJSON is fuzzed (fuzz_test.go). Keep the fuzzer green when touching the parse path; it guards against panics on malformed numeric/boolean JSON.

Documentation

Overview

Package null provides nullable wrappers around Go primitive types

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) 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) 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

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) 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) 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) 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) 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) 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) 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.

Jump to

Keyboard shortcuts

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