std

package module
v2.0.0-beta.1+incompat... Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2018 License: MIT Imports: 8 Imported by: 7

README

Go Standard Library Last release

Go Report Card

Branch Status Coverage
master Build Status Coveralls

go-std is a library with reasonable options for dealing with nullable SQL and JSON values.

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 fmt.Stringer.

Types

  • std.Bool: Nullable bool
  • std.Float: Nullable float64
  • std.String: Nullable string
  • std.Int: Nullable int64
  • std.Uint: Nullable uint64
  • std.Time: Nullable Time
  • std.DateTime: Nullable Time with ISO8601 format
  • std.Date: Nullable Time with ISO8601 (yyyy-mm-dd) format

License

go-std is licensed under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	Data  bool
	Valid bool // Valid is true if Bool is not NULL
}

Bool represents a bool that may be null. Bool implements the Scanner interface so it can be used as a scan destination, similar to NullString.

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

func (b Bool) String() string

String implements fmt.Stringer interface

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

func (Bool) Value

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

Value implements the driver Valuer interface.

type Date

type Date struct {
	Data  time.Time
	Valid bool
}

Date is a nullable time.Time with ISO8601 format. It supports SQL and JSON serialization. It will marshal to null if null. swagger:strfmt date-time

func DateFrom

func DateFrom(t time.Time) Date

DateFrom creates a new Time that will always be valid.

func DateFromPtr

func DateFromPtr(t *time.Time) Date

DateFromPtr creates a new Date that will be null if t is nil.

func NewDate

func NewDate(t time.Time, valid bool) Date

NewDate creates a new Date.

func (Date) IsZero

func (t Date) IsZero() bool

IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

func (Date) MarshalJSON

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

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

func (Date) MarshalText

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

MarshalText implement the json.Marshaler interface

func (Date) Ptr

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

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

func (*Date) Scan

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

Scan implements the Scanner interface.

func (*Date) SetValid

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

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

func (Date) String

func (t Date) String() string

String implements fmt.Stringer interface

func (*Date) UnmarshalJSON

func (t *Date) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*Date) UnmarshalText

func (t *Date) UnmarshalText(b []byte) error

UnmarshalText allows ISO8601Time to implement the TextUnmarshaler interface

func (Date) Value

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

Value implements the driver Valuer interface.

type DateTime

type DateTime struct {
	Data  time.Time
	Valid bool
}

DateTime is a nullable time.Time with ISO8601 format. It supports SQL and JSON serialization. It will marshal to null if null. swagger:strfmt date-time

func DateTimeFrom

func DateTimeFrom(t time.Time) DateTime

DateTimeFrom creates a new Time that will always be valid.

func DateTimeFromPtr

func DateTimeFromPtr(t *time.Time) DateTime

DateTimeFromPtr creates a new DateTime that will be null if t is nil.

func NewDateTime

func NewDateTime(t time.Time, valid bool) DateTime

NewDateTime creates a new DateTime.

func (DateTime) IsZero

func (t DateTime) IsZero() bool

IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.

func (DateTime) MarshalJSON

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

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

func (DateTime) MarshalText

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

MarshalText implement the json.Marshaler interface

func (DateTime) Ptr

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

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

func (*DateTime) Scan

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

Scan implements the Scanner interface.

func (*DateTime) SetValid

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

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

func (DateTime) String

func (t DateTime) String() string

String implements fmt.Stringer interface

func (*DateTime) UnmarshalJSON

func (t *DateTime) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler. It supports string, object (e.g. pq.NullTime and friends) and null input.

func (*DateTime) UnmarshalText

func (t *DateTime) UnmarshalText(b []byte) error

UnmarshalText allows ISO8601Time to implement the TextUnmarshaler interface

func (DateTime) Value

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

Value implements the driver Valuer interface.

type Float

type Float struct {
	Data  float64
	Valid bool // Valid is true if Float64 is not NULL
}

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

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

Scan implements the Scanner interface.

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

func (f Float) String() string

String implements fmt.Stringer interface

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

func (Float) Value

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

Value implements the driver Valuer interface.

type Int

type Int struct {
	Data  int64
	Valid bool // Valid is true if Int64 is not NULL
}

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

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

Scan implements the Scanner interface.

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

func (i Int) String() string

String implements fmt.Stringer interface

func (*Int) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler. It supports number and null input. 0 will not 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 or not an integer. It will return an error if the input is not an integer, blank, or "null".

func (Int) Value

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

Value implements the driver Valuer interface.

type String

type String struct {
	Data  string
	Valid bool
}

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

func (s String) String() string

String implements fmt.Stringer interface

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. 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) Value

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

Value implements the driver Valuer interface.

type Time

type Time struct {
	Time  time.Time
	Valid bool
}

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) 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 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. 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 TextUnmarshaler

func (Time) Value

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

Value implements the driver Valuer interface.

type Uint

type Uint struct {
	Data  uint64
	Valid bool // Valid is true if Uint64 is not NULL
}

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

func NewUint

func NewUint(i uint64, valid bool) Uint

NewUint creates a new Uint

func UintFrom

func UintFrom(i uint64) Uint

UintFrom creates a new Uint that will always be valid.

func UintFromPtr

func UintFromPtr(i *uint64) Uint

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

func (Uint) IsZero

func (i Uint) IsZero() bool

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

func (Uint) MarshalJSON

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

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

func (Uint) MarshalText

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

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

func (Uint) Ptr

func (i Uint) Ptr() *uint64

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

func (*Uint) Scan

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

Scan implements the Scanner interface.

func (*Uint) SetValid

func (i *Uint) SetValid(n uint64)

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

func (Uint) String

func (i Uint) String() string

String implements fmt.Stringer interface

func (*Uint) UnmarshalJSON

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

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

func (*Uint) UnmarshalText

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

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

func (Uint) Value

func (i Uint) 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