null

package module
v7.0.0-...-707ab24 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: BSD-2-Clause Imports: 10 Imported by: 0

README

null GoDoc

import "github.com/Nidal-Bakir/null/v7"

null is a library with reasonable options for dealing with nullable SQL and JSON values

There are two packages: null and its subpackage zero.

Types in null will only be considered null on null input, and will JSON encode to null. If you need zero and null be considered separate values, use these.

Types in zero are treated like zero values in Go: blank string input will produce a null zero.String, and null Strings will JSON encode to "". Zero values of these types will be considered null to SQL. If you need zero and null treated the same, use these.

Interfaces
  • All types implement sql.Scanner and driver.Valuer, so you can use this library in place of sql.NullXXX.
  • All types also implement json.Marshaler and json.Unmarshaler, so you can marshal them to their native JSON representation.
  • All non-generic types implement encoding.TextMarshaler, encoding.TextUnmarshaler. A null object's MarshalText will return a blank string.

null package

import "github.com/Nidal-Bakir/null/v7"

null.String

Nullable string.

Marshals to JSON null if SQL source data is null. Zero (blank) input will not produce a null String.

null.Int, null.Int32, null.Int16, null.Byte

Nullable int64/int32/int16/byte.

Marshals to JSON null if SQL source data is null. Zero input will not produce a null Int.

null.Float

Nullable float64.

Marshals to JSON null if SQL source data is null. Zero input will not produce a null Float.

null.Bool

Nullable bool.

Marshals to JSON null if SQL source data is null. False input will not produce a null Bool.

null.Time

Marshals to JSON null if SQL source data is null. Zero input will not produce a null Time.

null.Value

Generic nullable value.

Will marshal to JSON null if SQL source data is null. Does not implement encoding.TextMarshaler.

zero package

import "github.com/Nidal-Bakir/null/v7/zero"

zero.String

Nullable string.

Will marshal to a blank string if null. Blank string input produces a null String. Null values and zero values are considered equivalent.

zero.Int, zero.Int32, zero.Int16, zero.Byte

Nullable int64/int32/int16/byte.

Will marshal to 0 if null. 0 produces a null Int. Null values and zero values are considered equivalent.

zero.Float

Nullable float64.

Will marshal to 0.0 if null. 0.0 produces a null Float. Null values and zero values are considered equivalent.

zero.Bool

Nullable bool.

Will marshal to false if null. false produces a null Float. Null values and zero values are considered equivalent.

zero.Time

Nullable time.

Will marshal to the zero time if null. Uses time.Time's marshaler.

zero.Value[T]

Generic nullable value.

Will marshal to zero value if null. T is required to be a comparable type. Does not implement encoding.TextMarshaler.

About

Q&A

Can you add support for other types?

This package is intentionally limited in scope. It will only support the types that driver.Value supports. Feel free to fork this and add more types if you want.

Can you add a feature that ____?

This package isn't intended to be a catch-all data-wrangling package. It is essentially finished. If you have an idea for a new feature, feel free to open an issue to talk about it or fork this package, but don't expect this to do everything.

Package history

v7
  • Now a Go module under the path github.com/Nidal-Bakir/null/v7
  • Added missing types from database/sql: Int32, Int16, Byte
  • Added generic Value[T] embedding sql.Null[T]
v4
  • Available at gopkg.in/Nidal-Bakir/null.v4
  • Unmarshaling from JSON sql.NullXXX JSON objects (e.g. {"Int64": 123, "Valid": true}) is no longer supported. It's unlikely many people used this, but if you need it, use v3.

Bugs

json's ",omitempty" struct tag does not work correctly right now. It will never omit a null or empty String. This might be fixed eventually.

License

BSD

Documentation

Overview

Package null contains SQL types that consider zero input and null input as separate values, with convenient support for JSON and text marshaling. Types in this package will always encode to their null value if null. Use the zero subpackage if you want zero values and null to be treated the same.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	sql.NullBool
}

Bool is a nullable bool. It does not consider false values to be null. It will decode to null, not false, if null.

func BoolFrom

func BoolFrom(b bool) Bool

BoolFrom creates a new Bool that will always be valid.

func BoolFromPtr

func BoolFromPtr(b *bool) Bool

BoolFromPtr creates a new Bool that will be null if f is nil.

func NewBool

func NewBool(b bool, valid bool) Bool

NewBool creates a new Bool

func (Bool) Equal

func (b Bool) Equal(other Bool) bool

Equal returns true if both booleans have the same value or are both null.

func (Bool) IsZero

func (b Bool) IsZero() bool

IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?) A non-null Bool with a 0 value will not be considered zero.

func (Bool) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this Bool is null.

func (Bool) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Bool is null.

func (Bool) Ptr

func (b Bool) Ptr() *bool

Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.

func (*Bool) SetValid

func (b *Bool) SetValid(v bool)

SetValid changes this Bool's value and also sets it to be non-null.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will not be considered a null Bool.

func (*Bool) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Bool if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Bool) ValueOrZero

func (b Bool) ValueOrZero() bool

ValueOrZero returns the inner value if valid, otherwise false.

type Byte

type Byte struct {
	sql.NullByte
}

Byte is an nullable byte. It does not consider zero values to be null. It will decode to null, not zero, if null.

func ByteFrom

func ByteFrom(b byte) Byte

ByteFrom creates a new Byte that will always be valid.

func ByteFromPtr

func ByteFromPtr(b *byte) Byte

ByteFromPtr creates a new Byte that be null if i is nil.

func NewByte

func NewByte(b byte, valid bool) Byte

NewByte creates a new Byte.

func (Byte) Equal

func (b Byte) Equal(other Byte) bool

Equal returns true if both ints have the same value or are both null.

func (Byte) IsZero

func (b Byte) IsZero() bool

IsZero returns true for invalid Bytes, for future omitempty support (Go 1.4?) A non-null Byte with a 0 value will not be considered zero.

func (Byte) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this Byte is null.

func (Byte) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Byte is null.

func (Byte) Ptr

func (b Byte) Ptr() *byte

Ptr returns a pointer to this Byte's value, or a nil pointer if this Byte is null.

func (*Byte) SetValid

func (b *Byte) SetValid(n byte)

SetValid changes this Byte's value and also sets it to be non-null.

func (*Byte) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number, string, and null input. 0 will not be considered a null Byte.

func (*Byte) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Byte if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Byte) ValueOrZero

func (b Byte) ValueOrZero() byte

ValueOrZero returns the inner value if valid, otherwise zero.

type Float

type Float struct {
	sql.NullFloat64
}

Float is a nullable float64. It does not consider zero values to be null. It will decode to null, not zero, if null.

func FloatFrom

func FloatFrom(f float64) Float

FloatFrom creates a new Float that will always be valid.

func FloatFromPtr

func FloatFromPtr(f *float64) Float

FloatFromPtr creates a new Float that be null if f is nil.

func NewFloat

func NewFloat(f float64, valid bool) Float

NewFloat creates a new Float

func (Float) Equal

func (f Float) Equal(other Float) bool

Equal returns true if both floats have the same value or are both null. Warning: calculations using floating point numbers can result in different ways the numbers are stored in memory. Therefore, this function is not suitable to compare the result of a calculation. Use this method only to check if the value has changed in comparison to some previous value.

func (Float) IsZero

func (f Float) IsZero() bool

IsZero returns true for invalid Floats, for future omitempty support (Go 1.4?) A non-null Float with a 0 value will not be considered zero.

func (Float) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this Float is null.

func (Float) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Float is null.

func (Float) Ptr

func (f Float) Ptr() *float64

Ptr returns a pointer to this Float's value, or a nil pointer if this Float is null.

func (*Float) SetValid

func (f *Float) SetValid(n float64)

SetValid changes this Float's value and also sets it to be non-null.

func (*Float) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will not be considered a null Float.

func (*Float) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Float if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Float) ValueOrZero

func (f Float) ValueOrZero() float64

ValueOrZero returns the inner value if valid, otherwise zero.

type Int

type Int struct {
	sql.NullInt64
}

Int is an nullable int64. It does not consider zero values to be null. It will decode to null, not zero, if null.

func IntFrom

func IntFrom(i int64) Int

IntFrom creates a new Int that will always be valid.

func IntFromPtr

func IntFromPtr(i *int64) Int

IntFromPtr creates a new Int that be null if i is nil.

func NewInt

func NewInt(i int64, valid bool) Int

NewInt creates a new Int.

func (Int) Equal

func (i Int) Equal(other Int) bool

Equal returns true if both ints have the same value or are both null.

func (Int) IsZero

func (i Int) IsZero() bool

IsZero returns true for invalid Ints, for future omitempty support (Go 1.4?) A non-null Int with a 0 value will not be considered zero.

func (Int) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this Int is null.

func (Int) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Int is null.

func (Int) Ptr

func (i Int) Ptr() *int64

Ptr returns a pointer to this Int's value, or a nil pointer if this Int is null.

func (*Int) SetValid

func (i *Int) SetValid(n int64)

SetValid changes this Int's value and also sets it to be non-null.

func (*Int) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number, string, and null input. 0 will not be considered a null Int.

func (*Int) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Int) ValueOrZero

func (i Int) ValueOrZero() int64

ValueOrZero returns the inner value if valid, otherwise zero.

type Int16

type Int16 struct {
	sql.NullInt16
}

Int16 is an nullable int16. It does not consider zero values to be null. It will decode to null, not zero, if null.

func Int16From

func Int16From(i int16) Int16

Int16From creates a new Int16 that will always be valid.

func Int16FromPtr

func Int16FromPtr(i *int16) Int16

Int16FromPtr creates a new Int16 that be null if i is nil.

func NewInt16

func NewInt16(i int16, valid bool) Int16

NewInt16 creates a new Int16.

func (Int16) Equal

func (i Int16) Equal(other Int16) bool

Equal returns true if both ints have the same value or are both null.

func (Int16) IsZero

func (i Int16) IsZero() bool

IsZero returns true for invalid Int16s, for future omitempty support (Go 1.4?) A non-null Int16 with a 0 value will not be considered zero.

func (Int16) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this Int16 is null.

func (Int16) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Int16 is null.

func (Int16) Ptr

func (i Int16) Ptr() *int16

Ptr returns a pointer to this Int16's value, or a nil pointer if this Int16 is null.

func (*Int16) SetValid

func (i *Int16) SetValid(n int16)

SetValid changes this Int16's value and also sets it to be non-null.

func (*Int16) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number, string, and null input. 0 will not be considered a null Int16.

func (*Int16) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int16 if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Int16) ValueOrZero

func (i Int16) ValueOrZero() int16

ValueOrZero returns the inner value if valid, otherwise zero.

type Int32

type Int32 struct {
	sql.NullInt32
}

Int32 is an nullable int32. It does not consider zero values to be null. It will decode to null, not zero, if null.

func Int32From

func Int32From(i int32) Int32

Int32From creates a new Int32 that will always be valid.

func Int32FromPtr

func Int32FromPtr(i *int32) Int32

Int32FromPtr creates a new Int32 that be null if i is nil.

func NewInt32

func NewInt32(i int32, valid bool) Int32

NewInt32 creates a new Int32.

func (Int32) Equal

func (i Int32) Equal(other Int32) bool

Equal returns true if both ints have the same value or are both null.

func (Int32) IsZero

func (i Int32) IsZero() bool

IsZero returns true for invalid Int32s, for future omitempty support (Go 1.4?) A non-null Int32 with a 0 value will not be considered zero.

func (Int32) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this Int32 is null.

func (Int32) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string if this Int32 is null.

func (Int32) Ptr

func (i Int32) Ptr() *int32

Ptr returns a pointer to this Int32's value, or a nil pointer if this Int32 is null.

func (*Int32) SetValid

func (i *Int32) SetValid(n int32)

SetValid changes this Int32's value and also sets it to be non-null.

func (*Int32) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number, string, and null input. 0 will not be considered a null Int32.

func (*Int32) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int32 if the input is blank. It will return an error if the input is not an integer, blank, or "null".

func (Int32) ValueOrZero

func (i Int32) ValueOrZero() int32

ValueOrZero returns the inner value if valid, otherwise zero.

type Int64

type Int64 = Int

Int64 is an alias for Int.

type String

type String struct {
	sql.NullString
}

String is a nullable string. It supports SQL and JSON serialization. It will marshal to null if null. Blank string input will be considered null.

func NewString

func NewString(s string, valid bool) String

NewString creates a new String

func StringFrom

func StringFrom(s string) String

StringFrom creates a new String that will never be blank.

func StringFromPtr

func StringFromPtr(s *string) String

StringFromPtr creates a new String that be null if s is nil.

func (String) Equal

func (s String) Equal(other String) bool

Equal returns true if both strings have the same value or are both null.

func (String) IsZero

func (s String) IsZero() bool

IsZero returns true for null strings, for potential future omitempty support.

func (String) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this String is null.

func (String) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a blank string when this String is null.

func (String) Ptr

func (s String) Ptr() *string

Ptr returns a pointer to this String's value, or a nil pointer if this String is null.

func (*String) SetValid

func (s *String) SetValid(v string)

SetValid changes this String's value and also sets it to be non-null.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports string and null input. Blank string input does not produce a null String.

func (*String) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null String if the input is a blank string.

func (String) ValueOrZero

func (s String) ValueOrZero() string

ValueOrZero returns the inner value if valid, otherwise zero.

type Time

type Time struct {
	sql.NullTime
}

Time is a nullable time.Time. It supports SQL and JSON serialization. It will marshal to null if null.

func NewTime

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

NewTime creates a new Time.

func TimeFrom

func TimeFrom(t time.Time) Time

TimeFrom creates a new Time that will always be valid.

func TimeFromPtr

func TimeFromPtr(t *time.Time) Time

TimeFromPtr creates a new Time that will be null if t is nil.

func (Time) Equal

func (t Time) Equal(other Time) bool

Equal returns true if both Time objects encode the same time or are both null. Two times can be equal even if they are in different locations. For example, 6:00 +0200 CEST and 4:00 UTC are Equal.

func (Time) ExactEqual

func (t Time) ExactEqual(other Time) bool

ExactEqual returns true if both Time objects are equal or both null. ExactEqual returns false for times that are in different locations or have a different monotonic clock reading.

func (Time) IsZero

func (t Time) IsZero() bool

IsZero returns true for invalid Times, hopefully for future omitempty support. A non-null Time with a zero value will not be considered zero.

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode null if this time is null.

func (Time) MarshalText

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

MarshalText implements encoding.TextMarshaler. It returns an empty string if invalid, otherwise time.Time's MarshalText.

func (Time) Ptr

func (t Time) Ptr() *time.Time

Ptr returns a pointer to this Time's value, or a nil pointer if this Time is null.

func (*Time) SetValid

func (t *Time) SetValid(v time.Time)

SetValid changes this Time's value and sets it to be non-null.

func (*Time) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports string and null input.

func (*Time) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It has backwards compatibility with v3 in that the string "null" is considered equivalent to an empty string and unmarshaling will succeed. This may be removed in a future version.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Time) ValueOrZero

func (t Time) ValueOrZero() time.Time

ValueOrZero returns the inner value if valid, otherwise zero.

type Value

type Value[T any] struct {
	sql.Null[T]
}

Value represents a value that may be null.

func NewValue

func NewValue[T any](t T, valid bool) Value[T]

NewValue creates a new Value.

func ValueFrom

func ValueFrom[T any](t T) Value[T]

ValueFrom creates a new Value that will always be valid.

func ValueFromPtr

func ValueFromPtr[T any](t *T) Value[T]

ValueFromPtr creates a new Value that will be null if t is nil.

func (Value[T]) IsZero

func (t Value[T]) IsZero() bool

IsZero returns true for invalid Values, hopefully for future omitempty support. A non-null Value with a zero value will not be considered zero.

func (Value[T]) MarshalJSON

func (t Value[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler. It will encode null if this value is null.

func (Value[T]) Ptr

func (t Value[T]) Ptr() *T

Ptr returns a pointer to this Value's value, or a nil pointer if this Value is null.

func (*Value[T]) SetValid

func (t *Value[T]) SetValid(v T)

SetValid changes this Value's value and sets it to be non-null.

func (*Value[T]) UnmarshalJSON

func (t *Value[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string and null input.

func (Value[T]) ValueOrZero

func (t Value[T]) ValueOrZero() T

ValueOrZero returns the inner value if valid, otherwise zero.

Directories

Path Synopsis
Package zero contains SQL types that consider zero input and null input to be equivalent with convenient support for JSON and text marshaling.
Package zero contains SQL types that consider zero input and null input to be equivalent with convenient support for JSON and text marshaling.

Jump to

Keyboard shortcuts

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