nullable

package module
v0.0.0-...-1a4fc35 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2025 License: MIT Imports: 3 Imported by: 0

README

Go Reference Report card

nullable

A safe way to represent nullable primitive values in Go. Supports JSON serialization.

Installation

go get github.com/LightArrayCapital/go-utilities/nullable

Documentation

Overview

Package nullable provides a safe way to represent nullable primitive values in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	Nullable[bool]
}

func FromBool

func FromBool(data bool) Bool

func FromBoolPtr

func FromBoolPtr(data *bool) Bool

type Float32

type Float32 struct {
	Nullable[float32]
}

func FromFloat32

func FromFloat32(data float32) Float32

func FromFloat32Ptr

func FromFloat32Ptr(data *float32) Float32

type Float64

type Float64 struct {
	Nullable[float64]
}

func FromFloat64

func FromFloat64(data float64) Float64

func FromFloat64Ptr

func FromFloat64Ptr(data *float64) Float64

type Int

type Int struct {
	Nullable[int]
}

func FromInt

func FromInt(data int) Int

func FromIntPtr

func FromIntPtr(data *int) Int

type Int16

type Int16 struct {
	Nullable[int16]
}

func FromInt16

func FromInt16(data int16) Int16

func FromInt16Ptr

func FromInt16Ptr(data *int16) Int16

type Int32

type Int32 struct {
	Nullable[int32]
}

func FromInt32

func FromInt32(data int32) Int32

func FromInt32Ptr

func FromInt32Ptr(data *int32) Int32

type Int64

type Int64 struct {
	Nullable[int64]
}

func FromInt64

func FromInt64(data int64) Int64

func FromInt64Ptr

func FromInt64Ptr(data *int64) Int64

type Int8

type Int8 struct {
	Nullable[int8]
}

func FromInt8

func FromInt8(data int8) Int8

func FromInt8Ptr

func FromInt8Ptr(data *int8) Int8

type Nullable

type Nullable[T Primitive] struct {
	// contains filtered or unexported fields
}

Nullable is a generic type that can be used to represent a nullable value. If valid is true, then data is considered non-null. If valid is false, then data is considered null. Nullable supports all primitive types, except pointers, slices, maps, channels and structs. Nullable supports

func From

func From[T Primitive](data T) Nullable[T]

From returns a non-null Nullable with the given data.

func New

func New[T Primitive]() Nullable[T]

New returns a new null Nullable.

func Null

func Null[T Primitive]() Nullable[T]

Null is an alias for New.

func Zero

func Zero[T Primitive]() Nullable[T]

Zero returns a non-null Nullable with the zero value of the given type.

func (Nullable[T]) Data

func (n Nullable[T]) Data() T

Data returns the without checking if Nullable is considered null. Only use this if you are sure that Nullable is non-null.

func (Nullable[T]) Equal

func (n Nullable[T]) Equal(other Nullable[T]) bool

Equal returns true if both Nullable are null or if both Nullable are non-null and have the same data.

func (Nullable[T]) Get

func (n Nullable[T]) Get() (T, bool)

Get returns the data and a boolean indicating whether the Nullable is considered null or non-null. If boolean is false, then Nullable is considered null. If boolean is true, then Nullable is considered non-null.

func (Nullable[T]) IsValid

func (n Nullable[T]) IsValid() bool

IsValid returns true if the Nullable is non-null.

func (Nullable[T]) IsZero

func (n Nullable[T]) IsZero() bool

IsZero returns true if the Nullable is non-null and is the zero value of the given type.

func (Nullable[T]) MarshalJSON

func (n Nullable[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface. If the Nullable is considered null, then "null" is returned.

func (Nullable[T]) Ptr

func (n Nullable[T]) Ptr() *T

Ptr returns a pointer to the data. If the Nullable is null, then nil is returned.

func (*Nullable[T]) Set

func (n *Nullable[T]) Set(data T)

Set sets the data and marks it as non-null.

func (*Nullable[T]) SetNull

func (n *Nullable[T]) SetNull()

SetNull marks the data as null.

func (*Nullable[T]) SetZero

func (n *Nullable[T]) SetZero()

SetZero sets the data to the zero value of the given type and marks it as non-null.

func (*Nullable[T]) UnmarshalJSON

func (n *Nullable[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler interface. If "null" is passed, then the Nullable is marked as null. Otherwise, the data is marked as non-null and the data is unmarshalled.

type Primitive

type Primitive interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 | ~bool | ~string
}

Primitive is a type constraint for all primitive types, except pointers, slices, maps, channels and structs.

type String

type String struct {
	Nullable[string]
}

func FromString

func FromString(data string) String

func FromStringPtr

func FromStringPtr(data *string) String

type Uint

type Uint struct {
	Nullable[uint]
}

func FromUint

func FromUint(data uint) Uint

func FromUintPtr

func FromUintPtr(data *uint) Uint

type Uint16

type Uint16 struct {
	Nullable[uint16]
}

func FromUint16

func FromUint16(data uint16) Uint16

func FromUint16Ptr

func FromUint16Ptr(data *uint16) Uint16

type Uint32

type Uint32 struct {
	Nullable[uint32]
}

func FromUint32

func FromUint32(data uint32) Uint32

func FromUint32Ptr

func FromUint32Ptr(data *uint32) Uint32

type Uint64

type Uint64 struct {
	Nullable[uint64]
}

func FromUint64

func FromUint64(data uint64) Uint64

func FromUint64Ptr

func FromUint64Ptr(data *uint64) Uint64

type Uint8

type Uint8 struct {
	Nullable[uint8]
}

func FromUint8

func FromUint8(data uint8) Uint8

func FromUint8Ptr

func FromUint8Ptr(data *uint8) Uint8

Jump to

Keyboard shortcuts

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