zero

package
v3.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2020 License: BSD-2-Clause Imports: 9 Imported by: 15

Documentation

Overview

Package zero contains SQL types that consider zero input and null input to be equivalent with convenient support for JSON and text marshaling. Types in this package will JSON marshal to their zero value, even if null. Use the null parent package if you don't want this.

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. False input is considered null. JSON marshals to false if null. Considered null to SQL unmarshaled from a false value.

func BoolFrom

func BoolFrom(b bool) Bool

BoolFrom creates a new Bool that will be null if false.

func BoolFromPtr

func BoolFromPtr(b *bool) Bool

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

func NewBool

func NewBool(b bool, valid bool) Bool

NewBool creates a new Bool

func (Bool) Equal added in v3.5.0

func (b Bool) Equal(other Bool) bool

Equal returns true if both booleans are true and valid, or if both booleans are either false or invalid.

func (Bool) IsZero

func (b Bool) IsZero() bool

IsZero returns true for null or zero Bools, for future omitempty support (Go 1.4?)

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 zero if this Bool is null.

func (Bool) Ptr

func (b Bool) Ptr() *bool

Ptr returns a poBooler to this Bool's value, or a nil poBooler 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. "false" will be considered a null Bool. It also supports unmarshalling a sql.NullBool.

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 a false or not a bool. It will return an error if the input is not a float, blank, or "null".

func (Bool) ValueOrZero added in v3.5.0

func (b Bool) ValueOrZero() bool

ValueOrZero returns the inner value if valid, otherwise false.

type Float

type Float struct {
	sql.NullFloat64
}

Float is a nullable float64. Zero input will be considered null. JSON marshals to zero if null. Considered null to SQL if zero.

func FloatFrom

func FloatFrom(f float64) Float

FloatFrom creates a new Float that will be null if zero.

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 added in v3.5.0

func (f Float) Equal(other Float) bool

Equal returns true if both floats have the same value or are both either null or zero. 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 null or zero Floats, for future omitempty support (Go 1.4?)

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 zero if this Float is null.

func (Float) Ptr

func (f Float) Ptr() *float64

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

func (*Float) SetValid

func (f *Float) SetValid(v 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 be considered a null Float. It also supports unmarshalling a sql.NullFloat64.

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 a blank, zero, or not a float. It will return an error if the input is not a float, blank, or "null".

func (Float) ValueOrZero added in v3.5.0

func (f Float) ValueOrZero() float64

ValueOrZero returns the inner value if valid, otherwise zero.

type Int

type Int struct {
	sql.NullInt64
}

Int is a nullable int64. JSON marshals to zero if null. Considered null to SQL if zero.

func IntFrom

func IntFrom(i int64) Int

IntFrom creates a new Int that will be null if zero.

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 added in v3.5.0

func (i Int) Equal(other Int) bool

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

func (Int) IsZero

func (i Int) IsZero() bool

IsZero returns true for null or zero Ints, for future omitempty support (Go 1.4?)

func (Int) MarshalJSON

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

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

func (Int) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a zero 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 and null input. 0 will be considered a null Int. It also supports unmarshalling a sql.NullInt64.

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 a blank, zero, or not an integer. It will return an error if the input is not an integer, blank, or "null".

func (Int) ValueOrZero added in v3.5.0

func (i Int) ValueOrZero() int64

ValueOrZero returns the inner value if valid, otherwise zero.

type String

type String struct {
	sql.NullString
}

String is a nullable string. JSON marshals to a blank string if null. Considered null to SQL if zero.

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 be null if s is blank.

func StringFromPtr

func StringFromPtr(s *string) String

StringFromPtr creates a new String that be null if s is nil or blank. It will make s point to the String's value.

func (String) Equal added in v3.5.0

func (s String) Equal(other String) bool

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

func (String) IsZero

func (s String) IsZero() bool

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

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 produces a null String. It also supports unmarshalling a sql.NullString.

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 added in v3.5.0

func (s String) ValueOrZero() string

ValueOrZero returns the inner value if valid, otherwise zero.

type Time

type Time struct {
	Time  time.Time
	Valid bool
}

Time is a nullable time.Time. JSON marshals to the zero value for time.Time if null. Considered to be null to SQL if zero.

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 be null if t is the zero value.

func TimeFromPtr

func TimeFromPtr(t *time.Time) Time

TimeFromPtr creates a new Time that will be null if t is nil or *t is the zero value.

func (Time) Equal added in v3.5.0

func (t Time) Equal(other Time) bool

Equal returns true if both Time objects encode the same time or are both are either null or zero. 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 added in v3.5.0

func (t Time) ExactEqual(other Time) bool

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

func (Time) IsZero added in v3.4.0

func (t Time) IsZero() bool

IsZero returns true for null or zero Times, for potential future omitempty support.

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler. It will encode the zero value of time.Time if this time is invalid.

func (Time) MarshalText

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

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

func (*Time) Scan

func (t *Time) Scan(value interface{}) error

Scan implements Scanner interface.

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, object (e.g. pq.NullTime and friends) and null input.

func (*Time) UnmarshalText

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

func (Time) Value

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

Value implements the driver Valuer interface.

func (Time) ValueOrZero added in v3.5.0

func (t Time) ValueOrZero() time.Time

ValueOrZero returns the inner value if valid, otherwise zero.

Jump to

Keyboard shortcuts

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