null

package module
v0.0.0-...-9adc914 Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: Apache-2.0 Imports: 9 Imported by: 0

README

Nullable types for Go

go get -u github.com/Gurpartap/null
null

GoDoc

must (non-nullable JSONB and Int64Slice)

GoDoc

Usage
go get -u github.com/Gurpartap/null
import "github.com/Gurpartap/null"
Creating new nullable
user := struct {
	Name null.String `db:"name" json:"name"`
}{}

user.Name = null.NewString("Badam Rogan", true)
Scaning from database
_ = sqlxDB.Get(&user, `select name from users limit 1`)
Parsing from JSON
b := []byte(`{"user": "Badam Rogan"}`)
_ = json.Unmarshal(b, &user)
Reading value
fmt.Println(user.Name) // => Some("Badam Rogan")

if name, ok := user.Name.Unwrap(); ok {
	// has value
	fmt.Println(name) // => Badam Rogan
} else {
	// has no value
}
Available methods
func (opt null.String) Or(optb null.String) null.String {}
func (opt *null.String) SetValue(value string) {}
func (opt null.String) Unwrap() (string, bool) {}
func (opt null.String) UnwrapOr(def string) string {}
func (opt null.String) UnwrapOrDefault() string {}
func (opt null.String) UnwrapOrElse(fn func() string) string {}
func (opt null.String) UnwrapOrPanic() string {}

See godocs for full list and comments

Credits

This package is inspired by, and inherits bits of code and experience from each of the following:

License

Copyright 2017 Gurpartap Singh

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	// contains filtered or unexported fields
}

func NewBool

func NewBool(value bool, hasValue bool) Bool

func (Bool) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (*Bool) Scan

func (opt *Bool) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Bool) SetValue

func (opt *Bool) SetValue(value bool)

SetValue performs the conversion.

func (Bool) String

func (opt Bool) String() string

String conforms to fmt Stringer interface.

func (*Bool) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (Bool) Unwrap

func (opt Bool) Unwrap() (bool, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Bool) UnwrapOr

func (opt Bool) UnwrapOr(def bool) bool

UnwrapOr returns the contained value or a default.

func (Bool) UnwrapOrDefault

func (opt Bool) UnwrapOrDefault() bool

UnwrapOrDefault returns the contained value or the default.

func (Bool) UnwrapOrElse

func (opt Bool) UnwrapOrElse(fn func() bool) bool

UnwrapOrElse returns the contained value or computes it from a closure.

func (Bool) UnwrapOrPanic

func (opt Bool) UnwrapOrPanic() bool

UnwrapOrPanic returns the contained value or panics.

func (Bool) Value

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

Value implements the driver Valuer interface.

type Bytes

type Bytes struct {
	// contains filtered or unexported fields
}

func NewBytes

func NewBytes(value []byte, hasValue bool) Bytes

func (Bytes) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (*Bytes) Scan

func (opt *Bytes) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Bytes) SetValue

func (opt *Bytes) SetValue(value []byte)

SetValue performs the conversion.

func (Bytes) String

func (opt Bytes) String() string

String conforms to fmt Stringer interface.

func (*Bytes) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (Bytes) Unwrap

func (opt Bytes) Unwrap() ([]byte, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Bytes) UnwrapOr

func (opt Bytes) UnwrapOr(def []byte) []byte

UnwrapOr returns the contained value or a default.

func (Bytes) UnwrapOrDefault

func (opt Bytes) UnwrapOrDefault() []byte

UnwrapOrDefault returns the contained value or the default.

func (Bytes) UnwrapOrElse

func (opt Bytes) UnwrapOrElse(fn func() []byte) []byte

UnwrapOrElse returns the contained value or computes it from a closure.

func (Bytes) UnwrapOrPanic

func (opt Bytes) UnwrapOrPanic() []byte

UnwrapOrPanic returns the contained value or panics.

func (Bytes) Value

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

Value implements the driver Valuer interface.

type Float64

type Float64 struct {
	// contains filtered or unexported fields
}

func NewFloat64

func NewFloat64(value float64, hasValue bool) Float64

func (Float64) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (*Float64) Scan

func (opt *Float64) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Float64) SetValue

func (opt *Float64) SetValue(value float64)

SetValue performs the conversion.

func (Float64) String

func (opt Float64) String() string

String conforms to fmt Stringer interface.

func (*Float64) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (Float64) Unwrap

func (opt Float64) Unwrap() (float64, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Float64) UnwrapOr

func (opt Float64) UnwrapOr(def float64) float64

UnwrapOr returns the contained value or a default.

func (Float64) UnwrapOrDefault

func (opt Float64) UnwrapOrDefault() float64

UnwrapOrDefault returns the contained value or the default.

func (Float64) UnwrapOrElse

func (opt Float64) UnwrapOrElse(fn func() float64) float64

UnwrapOrElse returns the contained value or computes it from a closure.

func (Float64) UnwrapOrPanic

func (opt Float64) UnwrapOrPanic() float64

UnwrapOrPanic returns the contained value or panics.

func (Float64) Value

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

Value implements the driver Valuer interface.

type Int16

type Int16 struct {
	// contains filtered or unexported fields
}

func NewInt16

func NewInt16(value int16, hasValue bool) Int16

func (Int16) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (*Int16) Scan

func (opt *Int16) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Int16) SetValue

func (opt *Int16) SetValue(value int16)

SetValue performs the conversion.

func (Int16) String

func (opt Int16) String() string

String conforms to fmt Stringer interface.

func (*Int16) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (Int16) Unwrap

func (opt Int16) Unwrap() (int16, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Int16) UnwrapOr

func (opt Int16) UnwrapOr(def int16) int16

UnwrapOr returns the contained value or a default.

func (Int16) UnwrapOrDefault

func (opt Int16) UnwrapOrDefault() int16

UnwrapOrDefault returns the contained value or the default.

func (Int16) UnwrapOrElse

func (opt Int16) UnwrapOrElse(fn func() int16) int16

UnwrapOrElse returns the contained value or computes it from a closure.

func (Int16) UnwrapOrPanic

func (opt Int16) UnwrapOrPanic() int16

UnwrapOrPanic returns the contained value or panics.

func (Int16) Value

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

Value implements the driver Valuer interface.

type Int64

type Int64 struct {
	// contains filtered or unexported fields
}

func NewInt64

func NewInt64(value int64, hasValue bool) Int64

func (Int64) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (*Int64) Scan

func (opt *Int64) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Int64) SetValue

func (opt *Int64) SetValue(value int64)

SetValue performs the conversion.

func (Int64) String

func (opt Int64) String() string

String conforms to fmt Stringer interface.

func (*Int64) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (Int64) Unwrap

func (opt Int64) Unwrap() (int64, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Int64) UnwrapOr

func (opt Int64) UnwrapOr(def int64) int64

UnwrapOr returns the contained value or a default.

func (Int64) UnwrapOrDefault

func (opt Int64) UnwrapOrDefault() int64

UnwrapOrDefault returns the contained value or the default.

func (Int64) UnwrapOrElse

func (opt Int64) UnwrapOrElse(fn func() int64) int64

UnwrapOrElse returns the contained value or computes it from a closure.

func (Int64) UnwrapOrPanic

func (opt Int64) UnwrapOrPanic() int64

UnwrapOrPanic returns the contained value or panics.

func (Int64) Value

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

Value implements the driver Valuer interface.

type Int64Slice

type Int64Slice struct {
	// contains filtered or unexported fields
}

Int64Slice is a sql scanner interface for using []int64 as postgres nullable arrays.

func NewInt64Slice

func NewInt64Slice(value []int64, hasValue bool) Int64Slice

func (Int64Slice) MarshalJSON

func (opt Int64Slice) MarshalJSON() ([]byte, error)

MarshalJSON implements the json Marshaler interface.

func (*Int64Slice) Scan

func (opt *Int64Slice) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Int64Slice) SetValue

func (opt *Int64Slice) SetValue(value []int64)

SetValue performs the conversion.

func (Int64Slice) String

func (opt Int64Slice) String() string

String conforms to fmt Stringer interface.

func (*Int64Slice) UnmarshalJSON

func (opt *Int64Slice) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json Unmarshaler interface.

func (Int64Slice) Unwrap

func (opt Int64Slice) Unwrap() ([]int64, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Int64Slice) UnwrapOr

func (opt Int64Slice) UnwrapOr(def []int64) []int64

UnwrapOr returns the contained value or a default.

func (Int64Slice) UnwrapOrDefault

func (opt Int64Slice) UnwrapOrDefault() []int64

UnwrapOrDefault returns the contained value or the default.

func (Int64Slice) UnwrapOrElse

func (opt Int64Slice) UnwrapOrElse(fn func() []int64) []int64

UnwrapOrElse returns the contained value or computes it from a closure.

func (Int64Slice) UnwrapOrPanic

func (opt Int64Slice) UnwrapOrPanic() []int64

UnwrapOrPanic returns the contained value or panics.

func (Int64Slice) Value

func (opt Int64Slice) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type JSONB

type JSONB struct {
	// contains filtered or unexported fields
}

func NewJSONB

func NewJSONB(value []byte, hasValue bool) JSONB

func (JSONB) MarshalJSON

func (opt JSONB) MarshalJSON() ([]byte, error)

MarshalJSON implements the json Marshaler interface.

func (*JSONB) Scan

func (opt *JSONB) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*JSONB) SetValue

func (opt *JSONB) SetValue(value []byte)

SetValue performs the conversion.

func (JSONB) String

func (opt JSONB) String() string

String conforms to fmt Stringer interface.

func (*JSONB) UnmarshalJSON

func (opt *JSONB) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json Unmarshaler interface.

func (JSONB) Unwrap

func (opt JSONB) Unwrap() ([]byte, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (JSONB) UnwrapOr

func (opt JSONB) UnwrapOr(def []byte) []byte

UnwrapOr returns the contained value or a default.

func (JSONB) UnwrapOrDefault

func (opt JSONB) UnwrapOrDefault() []byte

UnwrapOrDefault returns the contained value or the default.

func (JSONB) UnwrapOrElse

func (opt JSONB) UnwrapOrElse(fn func() []byte) []byte

UnwrapOrElse returns the contained value or computes it from a closure.

func (JSONB) UnwrapOrPanic

func (opt JSONB) UnwrapOrPanic() []byte

UnwrapOrPanic returns the contained value or panics.

func (JSONB) Value

func (opt JSONB) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type String

type String struct {
	// contains filtered or unexported fields
}

func NewString

func NewString(value string, hasValue bool) String

func (String) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (String) Or

func (opt String) Or(optb String) String

Or returns the optional if it contains a value, otherwise returns optb.

func (*String) Scan

func (opt *String) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*String) SetValue

func (opt *String) SetValue(value string)

SetValue performs the conversion.

func (String) String

func (opt String) String() string

String conforms to fmt Stringer interface.

func (*String) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (String) Unwrap

func (opt String) Unwrap() (string, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (String) UnwrapOr

func (opt String) UnwrapOr(def string) string

UnwrapOr returns the contained value or a default.

func (String) UnwrapOrDefault

func (opt String) UnwrapOrDefault() string

UnwrapOrDefault returns the contained value or the default.

func (String) UnwrapOrElse

func (opt String) UnwrapOrElse(fn func() string) string

UnwrapOrElse returns the contained value or computes it from a closure.

func (String) UnwrapOrPanic

func (opt String) UnwrapOrPanic() string

UnwrapOrPanic returns the contained value or panics.

func (String) Value

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

Value implements the driver Valuer interface.

type Time

type Time struct {
	// contains filtered or unexported fields
}

func NewTime

func NewTime(value time.Time, hasValue bool) Time

func (Time) MarshalJSON

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

MarshalJSON implements the json Marshaler interface.

func (*Time) Scan

func (opt *Time) Scan(src interface{}) error

Scan implements the sql Scanner interface.

func (*Time) SetValue

func (opt *Time) SetValue(value time.Time)

SetValue performs the conversion.

func (Time) String

func (opt Time) String() string

String conforms to fmt Stringer interface.

func (*Time) UnmarshalJSON

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

UnmarshalJSON implements the json Unmarshaler interface.

func (Time) Unwrap

func (opt Time) Unwrap() (time.Time, bool)

Unwrap moves the value out of the optional, if it is Some(value). This function returns multiple values, and if that's undesirable, consider using Some and None functions.

func (Time) UnwrapOr

func (opt Time) UnwrapOr(def time.Time) time.Time

UnwrapOr returns the contained value or a default.

func (Time) UnwrapOrDefault

func (opt Time) UnwrapOrDefault() time.Time

UnwrapOrDefault returns the contained value or the default.

func (Time) UnwrapOrElse

func (opt Time) UnwrapOrElse(fn func() time.Time) time.Time

UnwrapOrElse returns the contained value or computes it from a closure.

func (Time) UnwrapOrPanic

func (opt Time) UnwrapOrPanic() time.Time

UnwrapOrPanic returns the contained value or panics.

func (Time) Value

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

Value implements the driver Valuer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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