null

package module
v10.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: BSD-2-Clause Imports: 9 Imported by: 0

README

null GoDoc Coverage

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

Types in null will only be considered null on null input, and will JSON encode to null.

All types implement sql.Scanner and driver.Valuer, so you can use this library in place of sql.NullXXX. All types also implement: encoding.TextMarshaler, encoding.TextUnmarshaler, json.Marshaler, json.Unmarshaler and sql.Scanner.


Installation

Null used to be versioned with gopkg.in, so once you upgrade to v8 and beyond please stop using gopkg.in and ensure you're using go modules.

go get github.com/lenisko/null/v10

Usage

The following are all types supported in this package. All types will marshal to JSON null if Invalid or SQL source data is null.

Type Description Notes
null.JSON Nullable []byte Will marshal to JSON null if Invalid. []byte{} input will not produce an Invalid JSON, but []byte(nil) will. This should be used for storing raw JSON in the database. Also has null.JSON.Marshal and null.JSON.Unmarshal helpers to marshal and unmarshal foreign objects.
null.Bytes Nullable []byte []byte{} input will not produce an Invalid Bytes, but []byte(nil) will. This should be used for storing binary data (bytes in PSQL for example) in the database.
null.String Nullable string
null.Byte Nullable byte
null.Bool Nullable bool
null.Time Nullable `time.Time Marshals to JSON null if SQL source data is null. Uses time.Time's marshaler.
null.Float32 Nullable float32
null.Float64 Nullable float64
null.Int Nullable int
null.Int8 Nullable int8
null.Int16 Nullable int16
null.Int32 Nullable int32
null.Int64 Nullable int64
null.Uint Nullable uint
null.Uint8 Nullable uint8
null.Uint16 Nullable uint16
null.Uint32 Nullable uint32
null.Uint64 Nullable uint64

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-3 (See License file)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NullBytes = []byte("null")

NullBytes is a global byte slice of JSON null

Functions

This section is empty.

Types

type Bool

type Bool struct {
	Bool  bool
	Valid bool
	Set   bool
}

Bool is a nullable bool.

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, valid bool) Bool

NewBool creates a new Bool

func (Bool) IsSet

func (b Bool) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Bool) IsValid

func (b Bool) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Bool) IsZero

func (b Bool) IsZero() bool

IsZero returns true for invalid Bools, for future omitempty support (Go 1.4?)

func (Bool) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Bool) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

func (b *Bool) Scan(value interface{}) error

Scan implements the Scanner interface.

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.

func (*Bool) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Bool) Value

func (b Bool) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Bool) ValueOrZero

func (b Bool) ValueOrZero() bool

ValueOrZero returns the inner value if valid, otherwise false.

type Byte

type Byte struct {
	Byte  byte
	Valid bool
	Set   bool
}

Byte is an nullable int.

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

func (b Byte) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Byte) IsValid

func (b Byte) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Byte) IsZero

func (b Byte) IsZero() bool

IsZero returns true for invalid Bytes, for future omitempty support (Go 1.4?)

func (Byte) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Byte) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

func (b *Byte) Scan(value interface{}) error

Scan implements the Scanner interface.

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.

func (*Byte) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Byte) Value

func (b Byte) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Byte) ValueOrZero

func (b Byte) ValueOrZero() byte

ValueOrZero returns the inner value if valid, otherwise default.

type Bytes

type Bytes struct {
	Bytes []byte
	Valid bool
	Set   bool
}

Bytes is a nullable []byte.

func BytesFrom

func BytesFrom(b []byte) Bytes

BytesFrom creates a new Bytes that will be invalid if nil.

func BytesFromPtr

func BytesFromPtr(b *[]byte) Bytes

BytesFromPtr creates a new Bytes that will be invalid if nil.

func NewBytes

func NewBytes(b []byte, valid bool) Bytes

NewBytes creates a new Bytes

func (Bytes) IsSet

func (b Bytes) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Bytes) IsValid

func (b Bytes) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Bytes) IsZero

func (b Bytes) IsZero() bool

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

func (Bytes) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Bytes) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Bytes) Ptr

func (b Bytes) Ptr() *[]byte

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

func (*Bytes) Scan

func (b *Bytes) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Bytes) SetValid

func (b *Bytes) SetValid(n []byte)

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

func (*Bytes) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Bytes) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Bytes) Value

func (b Bytes) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Bytes) ValueOrZero

func (b Bytes) ValueOrZero() []byte

ValueOrZero returns the inner value if valid, otherwise default.

type Float32

type Float32 struct {
	Float32 float32
	Valid   bool
	Set     bool
}

Float32 is a nullable float32.

func Float32From

func Float32From(f float32) Float32

Float32From creates a new Float32 that will always be valid.

func Float32FromPtr

func Float32FromPtr(f *float32) Float32

Float32FromPtr creates a new Float32 that be null if f is nil.

func NewFloat32

func NewFloat32(f float32, valid bool) Float32

NewFloat32 creates a new Float32

func (Float32) IsSet

func (f Float32) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Float32) IsValid

func (f Float32) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Float32) IsZero

func (f Float32) IsZero() bool

IsZero returns true for invalid Float32s, for future omitempty support (Go 1.4?)

func (Float32) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Float32) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Float32) Ptr

func (f Float32) Ptr() *float32

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

func (*Float32) Scan

func (f *Float32) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Float32) SetValid

func (f *Float32) SetValid(n float32)

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

func (*Float32) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Float32) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Float32) Value

func (f Float32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Float32) ValueOrZero

func (f Float32) ValueOrZero() float32

ValueOrZero returns the inner value if valid, otherwise zero.

type Float64

type Float64 struct {
	Float64 float64
	Valid   bool
	Set     bool
}

Float64 is a nullable float64.

func Float64From

func Float64From(f float64) Float64

Float64From creates a new Float64 that will always be valid.

func Float64FromPtr

func Float64FromPtr(f *float64) Float64

Float64FromPtr creates a new Float64 that be null if f is nil.

func NewFloat64

func NewFloat64(f float64, valid bool) Float64

NewFloat64 creates a new Float64

func (Float64) IsSet

func (f Float64) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Float64) IsValid

func (f Float64) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Float64) IsZero

func (f Float64) IsZero() bool

IsZero returns true for invalid Float64s, for future omitempty support (Go 1.4?)

func (Float64) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Float64) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Float64) Ptr

func (f Float64) Ptr() *float64

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

func (*Float64) Scan

func (f *Float64) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Float64) SetValid

func (f *Float64) SetValid(n float64)

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

func (*Float64) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Float64) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Float64) Value

func (f Float64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Float64) ValueOrZero

func (f Float64) ValueOrZero() float64

ValueOrZero returns the inner value if valid, otherwise zero.

type Int

type Int struct {
	Int   int
	Valid bool
	Set   bool
}

Int is an nullable int.

func IntFrom

func IntFrom(i int) Int

IntFrom creates a new Int that will always be valid.

func IntFromPtr

func IntFromPtr(i *int) Int

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

func NewInt

func NewInt(i int, valid bool) Int

NewInt creates a new Int

func (Int) IsSet

func (i Int) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Int) IsValid

func (i Int) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Int) IsZero

func (i Int) IsZero() bool

IsZero returns true for invalid Ints, for future omitempty support (Go 1.4?)

func (Int) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Int) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Int) Ptr

func (i Int) Ptr() *int

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

func (*Int) Scan

func (i *Int) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Int) SetValid

func (i *Int) SetValid(n int)

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.

func (*Int) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Int) Value

func (i Int) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Int) ValueOrZero

func (i Int) ValueOrZero() int

ValueOrZero returns the inner value if valid, otherwise default.

type Int16

type Int16 struct {
	Int16 int16
	Valid bool
	Set   bool
}

Int16 is an nullable int16.

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

func (i Int16) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Int16) IsValid

func (i Int16) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Int16) IsZero

func (i Int16) IsZero() bool

IsZero returns true for invalid Int16's, for future omitempty support (Go 1.4?)

func (Int16) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Int16) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

func (i *Int16) Scan(value interface{}) error

Scan implements the Scanner interface.

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.

func (*Int16) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Int16) Value

func (i Int16) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Int16) ValueOrZero

func (i Int16) ValueOrZero() int16

ValueOrZero returns the inner value if valid, otherwise default.

type Int32

type Int32 struct {
	Int32 int32
	Valid bool
	Set   bool
}

Int32 is an nullable int32.

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

func (i Int32) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Int32) IsValid

func (i Int32) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Int32) IsZero

func (i Int32) IsZero() bool

IsZero returns true for invalid Int32's, for future omitempty support (Go 1.4?)

func (Int32) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Int32) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

func (i *Int32) Scan(value interface{}) error

Scan implements the Scanner interface.

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.

func (*Int32) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Int32) Value

func (i Int32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Int32) ValueOrZero

func (i Int32) ValueOrZero() int32

ValueOrZero returns the inner value if valid, otherwise default.

type Int64

type Int64 struct {
	Int64 int64
	Valid bool
	Set   bool
}

Int64 is an nullable int64.

func Int64From

func Int64From(i int64) Int64

Int64From creates a new Int64 that will always be valid.

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

func (i Int64) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Int64) IsValid

func (i Int64) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Int64) IsZero

func (i Int64) IsZero() bool

IsZero returns true for invalid Int64's, for future omitempty support (Go 1.4?)

func (Int64) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Int64) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

func (i *Int64) Scan(value interface{}) error

Scan implements the Scanner interface.

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.

func (*Int64) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Int64) Value

func (i Int64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Int64) ValueOrZero

func (i Int64) ValueOrZero() int64

ValueOrZero returns the inner value if valid, otherwise default.

type Int8

type Int8 struct {
	Int8  int8
	Valid bool
	Set   bool
}

Int8 is an nullable int8.

func Int8From

func Int8From(i int8) Int8

Int8From creates a new Int8 that will always be valid.

func Int8FromPtr

func Int8FromPtr(i *int8) Int8

Int8FromPtr creates a new Int8 that be null if i is nil.

func NewInt8

func NewInt8(i int8, valid bool) Int8

NewInt8 creates a new Int8

func (Int8) IsSet

func (i Int8) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Int8) IsValid

func (i Int8) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Int8) IsZero

func (i Int8) IsZero() bool

IsZero returns true for invalid Int8's, for future omitempty support (Go 1.4?)

func (Int8) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Int8) MarshalText

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

MarshalText implements encoding.TextMarshaler.

func (Int8) Ptr

func (i Int8) Ptr() *int8

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

func (*Int8) Scan

func (i *Int8) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Int8) SetValid

func (i *Int8) SetValid(n int8)

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

func (*Int8) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*Int8) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (Int8) Value

func (i Int8) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Int8) ValueOrZero

func (i Int8) ValueOrZero() int8

ValueOrZero returns the inner value if valid, otherwise default.

type JSON

type JSON struct {
	JSON  []byte
	Valid bool
	Set   bool
}

JSON is a nullable []byte that contains JSON.

You might want to use this in the case where you have say a nullable JSON column in postgres for instance, where there is one layer of null for the postgres column, and then you also have the opportunity to have null as a value contained in the json. When unmarshalling json however you cannot set 'null' as a value.

func JSONFrom

func JSONFrom(b []byte) JSON

JSONFrom creates a new JSON that will be invalid if nil.

func JSONFromPtr

func JSONFromPtr(b *[]byte) JSON

JSONFromPtr creates a new JSON that will be invalid if nil.

func NewJSON

func NewJSON(b []byte, valid bool) JSON

NewJSON creates a new JSON

func (JSON) IsSet

func (j JSON) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (JSON) IsValid

func (j JSON) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (JSON) IsZero

func (j JSON) IsZero() bool

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

func (*JSON) Marshal

func (j *JSON) Marshal(obj interface{}) error

Marshal will marshal the passed in object, and store it in the JSON member on the JSON object.

func (JSON) MarshalJSON

func (j JSON) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (JSON) MarshalText

func (j JSON) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (JSON) Ptr

func (j JSON) Ptr() *[]byte

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

func (*JSON) Scan

func (j *JSON) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*JSON) SetValid

func (j *JSON) SetValid(n []byte)

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

func (JSON) Unmarshal

func (j JSON) Unmarshal(dest interface{}) error

Unmarshal will unmarshal your JSON stored in your JSON object and store the result in the value pointed to by dest.

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

Example if you have a struct with a null.JSON called v:

		{}          -> does not call unmarshaljson: !set & !valid
		{"v": null} -> calls unmarshaljson, set & !valid
     {"v": {}}   -> calls unmarshaljson, set & valid (json value is '{}')

That's to say if 'null' is passed in at the json level we do not capture that value - instead we set the value-level null flag so that an sql value will turn out null.

func (*JSON) UnmarshalText

func (j *JSON) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (JSON) Value

func (j JSON) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (JSON) ValueOrZero

func (j JSON) ValueOrZero() []byte

ValueOrZero returns the inner value if valid, otherwise default.

type String

type String struct {
	String string
	Valid  bool
	Set    bool
}

String is a nullable string. It supports SQL and JSON serialization.

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

func (s String) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (String) IsValid

func (s String) IsValid() bool

IsValid returns true if this carries and explicit value and is not 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.

func (String) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

func (s *String) Scan(value interface{}) error

Scan implements the Scanner interface.

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.

func (*String) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

func (String) Value

func (s String) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (String) ValueOrZero

func (s String) ValueOrZero() string

ValueOrZero returns the inner value if valid, otherwise default.

type Time

type Time struct {
	Time  time.Time
	Valid bool
	Set   bool
}

Time is a nullable time.Time. It supports SQL and JSON serialization.

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

func (t Time) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Time) IsValid

func (t Time) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Time) IsZero

func (t Time) IsZero() bool

IsZero returns true for an invalid Time's value, for potential future omitempty support.

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (Time) MarshalText

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

MarshalText implements encoding.TextMarshaler.

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

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

Scan implements the 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.

func (*Time) UnmarshalText

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

UnmarshalText implements encoding.TextUnmarshaler.

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

type Uint

type Uint struct {
	Uint  uint
	Valid bool
	Set   bool
}

Uint is an nullable uint.

func NewUint

func NewUint(i uint, valid bool) Uint

NewUint creates a new Uint

func UintFrom

func UintFrom(i uint) Uint

UintFrom creates a new Uint that will always be valid.

func UintFromPtr

func UintFromPtr(i *uint) Uint

UintFromPtr creates a new Uint that be null if i is nil.

func (Uint) IsSet

func (u Uint) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Uint) IsValid

func (u Uint) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Uint) IsZero

func (u Uint) IsZero() bool

IsZero returns true for invalid Uints, for future omitempty support (Go 1.4?)

func (Uint) MarshalJSON

func (u Uint) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Uint) MarshalText

func (u Uint) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint) Ptr

func (u Uint) Ptr() *uint

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

func (*Uint) Scan

func (u *Uint) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Uint) SetValid

func (u *Uint) SetValid(n uint)

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

func (*Uint) UnmarshalJSON

func (u *Uint) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint) UnmarshalText

func (u *Uint) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Uint) Value

func (u Uint) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Uint) ValueOrZero

func (u Uint) ValueOrZero() uint

ValueOrZero returns the inner value if valid, otherwise default.

type Uint16

type Uint16 struct {
	Uint16 uint16
	Valid  bool
	Set    bool
}

Uint16 is an nullable uint16.

func NewUint16

func NewUint16(i uint16, valid bool) Uint16

NewUint16 creates a new Uint16

func Uint16From

func Uint16From(i uint16) Uint16

Uint16From creates a new Uint16 that will always be valid.

func Uint16FromPtr

func Uint16FromPtr(i *uint16) Uint16

Uint16FromPtr creates a new Uint16 that be null if i is nil.

func (Uint16) IsSet

func (u Uint16) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Uint16) IsValid

func (u Uint16) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Uint16) IsZero

func (u Uint16) IsZero() bool

IsZero returns true for invalid Uint16's, for future omitempty support (Go 1.4?)

func (Uint16) MarshalJSON

func (u Uint16) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Uint16) MarshalText

func (u Uint16) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint16) Ptr

func (u Uint16) Ptr() *uint16

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

func (*Uint16) Scan

func (u *Uint16) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Uint16) SetValid

func (u *Uint16) SetValid(n uint16)

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

func (*Uint16) UnmarshalJSON

func (u *Uint16) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint16) UnmarshalText

func (u *Uint16) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Uint16) Value

func (u Uint16) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Uint16) ValueOrZero

func (u Uint16) ValueOrZero() uint16

ValueOrZero returns the inner value if valid, otherwise default.

type Uint32

type Uint32 struct {
	Uint32 uint32
	Valid  bool
	Set    bool
}

Uint32 is an nullable uint32.

func NewUint32

func NewUint32(i uint32, valid bool) Uint32

NewUint32 creates a new Uint32

func Uint32From

func Uint32From(i uint32) Uint32

Uint32From creates a new Uint32 that will always be valid.

func Uint32FromPtr

func Uint32FromPtr(i *uint32) Uint32

Uint32FromPtr creates a new Uint32 that be null if i is nil.

func (Uint32) IsSet

func (u Uint32) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Uint32) IsValid

func (u Uint32) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Uint32) IsZero

func (u Uint32) IsZero() bool

IsZero returns true for invalid Uint32's, for future omitempty support (Go 1.4?)

func (Uint32) MarshalJSON

func (u Uint32) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Uint32) MarshalText

func (u Uint32) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint32) Ptr

func (u Uint32) Ptr() *uint32

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

func (*Uint32) Scan

func (u *Uint32) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Uint32) SetValid

func (u *Uint32) SetValid(n uint32)

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

func (*Uint32) UnmarshalJSON

func (u *Uint32) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint32) UnmarshalText

func (u *Uint32) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Uint32) Value

func (u Uint32) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Uint32) ValueOrZero

func (u Uint32) ValueOrZero() uint32

ValueOrZero returns the inner value if valid, otherwise default.

type Uint64

type Uint64 struct {
	Uint64 uint64
	Valid  bool
	Set    bool
}

Uint64 is an nullable uint64.

func NewUint64

func NewUint64(i uint64, valid bool) Uint64

NewUint64 creates a new Uint64

func Uint64From

func Uint64From(i uint64) Uint64

Uint64From creates a new Uint64 that will always be valid.

func Uint64FromPtr

func Uint64FromPtr(i *uint64) Uint64

Uint64FromPtr creates a new Uint64 that be null if i is nil.

func (Uint64) IsSet

func (u Uint64) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Uint64) IsValid

func (u Uint64) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Uint64) IsZero

func (u Uint64) IsZero() bool

IsZero returns true for invalid Uint64's, for future omitempty support (Go 1.4?)

func (Uint64) MarshalJSON

func (u Uint64) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Uint64) MarshalText

func (u Uint64) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint64) Ptr

func (u Uint64) Ptr() *uint64

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

func (*Uint64) Scan

func (u *Uint64) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Uint64) SetValid

func (u *Uint64) SetValid(n uint64)

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

func (*Uint64) UnmarshalJSON

func (u *Uint64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint64) UnmarshalText

func (u *Uint64) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Uint64) Value

func (u Uint64) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Uint64) ValueOrZero

func (u Uint64) ValueOrZero() uint64

ValueOrZero returns the inner value if valid, otherwise default.

type Uint8

type Uint8 struct {
	Uint8 uint8
	Valid bool
	Set   bool
}

Uint8 is an nullable uint8.

func NewUint8

func NewUint8(i uint8, valid bool) Uint8

NewUint8 creates a new Uint8

func Uint8From

func Uint8From(i uint8) Uint8

Uint8From creates a new Uint8 that will always be valid.

func Uint8FromPtr

func Uint8FromPtr(i *uint8) Uint8

Uint8FromPtr creates a new Uint8 that be null if i is nil.

func (Uint8) IsSet

func (u Uint8) IsSet() bool

IsSet returns true if this carries an explicit value (null inclusive)

func (Uint8) IsValid

func (u Uint8) IsValid() bool

IsValid returns true if this carries and explicit value and is not null.

func (Uint8) IsZero

func (u Uint8) IsZero() bool

IsZero returns true for invalid Uint8's, for future omitempty support (Go 1.4?)

func (Uint8) MarshalJSON

func (u Uint8) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Uint8) MarshalText

func (u Uint8) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Uint8) Ptr

func (u Uint8) Ptr() *uint8

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

func (*Uint8) Scan

func (u *Uint8) Scan(value interface{}) error

Scan implements the Scanner interface.

func (*Uint8) SetValid

func (u *Uint8) SetValid(n uint8)

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

func (*Uint8) UnmarshalJSON

func (u *Uint8) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Uint8) UnmarshalText

func (u *Uint8) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

func (Uint8) Value

func (u Uint8) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Uint8) ValueOrZero

func (u Uint8) ValueOrZero() uint8

ValueOrZero returns the inner value if valid, otherwise default.

type Value

type Value interface {
	// IsValid returns true iff the value is set and non-null
	IsValid() bool
	// IsSet returns true iff the value is set (null inclusive)
	IsSet() bool
}

Value specifies methods that allow introspection into the state of a value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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