zero

package
v7.0.1+incompatible Latest Latest
Warning

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

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

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

func (b Bool) GetBSON() (interface{}, error)

GetBSON implements bson.Getter.

func (Bool) GetValue

func (b Bool) GetValue() reflect.Value

GetValue implements the compare.Valuable interface

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

func (b *Bool) LoremDecode(tag, example string) error

LoremDecode implements lorem.Decoder

func (Bool) MarshalJSON

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

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

func (Bool) MarshalText

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

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

func (Bool) MarshalXML

func (b Bool) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements the xml.Marshaler interface

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

func (b *Bool) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter.

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

func (b *Bool) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implments the xml.Unmarshaler interface

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

func (f Float) GetBSON() (interface{}, error)

GetBSON implements bson.Getter.

func (Float) GetValue

func (f Float) GetValue() reflect.Value

GetValue implements the compare.Valuable interface

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

func (f *Float) LoremDecode(tag, example string) error

LoremDecode implements lorem.Decoder

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

func (f Float) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements the xml.Marshaler interface

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

func (f *Float) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter.

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

func (f *Float) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implments the xml.Unmarshaler interface

type Int32

type Int32 struct {
	sql.NullInt32
}

Int32 is a nullable int32. JSON marshals to zero if null. Considered null to SQL if zero.

func Int32From

func Int32From(i int32) Int32

Int32From creates a new Int32 that will be null if zero.

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

func (i Int32) GetBSON() (interface{}, error)

GetBSON implements bson.Getter.

func (Int32) GetValue

func (i Int32) GetValue() reflect.Value

GetValue implements the compare.Valuable interface

func (Int32) IsZero

func (i Int32) IsZero() bool

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

func (*Int32) LoremDecode

func (i *Int32) LoremDecode(tag, example string) error

LoremDecode implements lorem.Decoder

func (Int32) MarshalJSON

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

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

func (Int32) MarshalText

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

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

func (Int32) MarshalXML

func (i Int32) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements the xml.Marshaler interface

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

func (i *Int32) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter.

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 and null input. 0 will be considered a null Int32. It also supports unmarshalling a sql.NullInt32.

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

func (*Int32) UnmarshalXML

func (i *Int32) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implments the xml.Unmarshaler interface

type Int64

type Int64 struct {
	sql.NullInt64
}

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

func Int64From

func Int64From(i int64) Int64

Int64From creates a new Int64 that will be null if zero.

func Int64FromPtr

func Int64FromPtr(i *int64) Int64

Int64FromPtr creates a new Int64 that be null if i is nil.

func NewInt64

func NewInt64(i int64, valid bool) Int64

NewInt64 creates a new Int64

func (Int64) GetBSON

func (i Int64) GetBSON() (interface{}, error)

GetBSON implements bson.Getter.

func (Int64) GetValue

func (i Int64) GetValue() reflect.Value

GetValue implements the compare.Valuable interface

func (Int64) IsZero

func (i Int64) IsZero() bool

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

func (*Int64) LoremDecode

func (i *Int64) LoremDecode(tag, example string) error

LoremDecode implements lorem.Decoder

func (Int64) MarshalJSON

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

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

func (Int64) MarshalText

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

MarshalText implements encoding.TextMarshaler. It will encode a zero if this Int64 is null.

func (Int64) MarshalXML

func (i Int64) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements the xml.Marshaler interface

func (Int64) Ptr

func (i Int64) Ptr() *int64

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

func (*Int64) SetBSON

func (i *Int64) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter.

func (*Int64) SetValid

func (i *Int64) SetValid(n int64)

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

func (*Int64) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will be considered a null Int64. It also supports unmarshalling a sql.NullInt64.

func (*Int64) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler. It will unmarshal to a null Int64 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 (*Int64) UnmarshalXML

func (i *Int64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implments the xml.Unmarshaler interface

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

func (s String) GetBSON() (interface{}, error)

GetBSON implements bson.Getter.

func (String) GetValue

func (s String) GetValue() reflect.Value

GetValue implements the compare.Valuable interface

func (String) IsZero

func (s String) IsZero() bool

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

func (*String) LoremDecode

func (s *String) LoremDecode(tag, example string) error

LoremDecode implements lorem.Decoder

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

func (s *String) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter.

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

func (s *String) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implments the xml.Unmarshaler interface

type Time

type Time struct {
	Time  time.Time
	Valid bool
}

Time is a zeroable 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) AppendValue

func (t Time) AppendValue(b []byte, quote int) ([]byte, error)

AppendValue implements the types.ValueAppender interface

func (Time) GetBSON

func (t Time) GetBSON() (interface{}, error)

GetBSON implements bson.Getter.

func (Time) GetValue

func (t Time) GetValue() reflect.Value

GetValue implements the compare.Valuable interface

func (Time) IsZero

func (t Time) IsZero() bool

IsZero implements the orm.isZeroer interface

func (*Time) LoremDecode

func (t *Time) LoremDecode(tag, example string) error

LoremDecode implements lorem.Decoder

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)

MarshalText implements encoding.TextMarshaler.

func (Time) MarshalXML

func (t Time) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements the xml.Marshaler interface

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

func (t *Time) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter.

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

UnmarshalText implements encoding.TextUnmarshaler.

func (*Time) UnmarshalXML

func (t *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implments the xml.Unmarshaler interface

func (Time) Value

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

Value implements the driver Valuer interface.

Jump to

Keyboard shortcuts

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