null

package module
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package null provides types and functions for representing absent values without pointers, while being compatible with pointers, encoding/json, database/sql and github.com/go-playground/validator.

Null values of these types shall be null.

Warning: deep copies are not made.

Example
var x struct {
	X *StrP `json:"x,omitempty"`
	Y *StrP `json:"y,omitempty"`
}
_ = json.Unmarshal([]byte(`{"x": "x"}`), &x)
fmt.Println("x.X.IsSet():", x.X.IsSet())
fmt.Printf("x.X.Val(): %#v\n", x.X.Val())
fmt.Println("x.X.ToNull().Set:", x.X.ToNull().IsSet)
fmt.Printf("x.X.ToNull().Val: %#v\n", x.X.ToNull().Val)
fmt.Println("x.Y.IsSet():", x.Y.IsSet())
fmt.Println("x.Y.ToNull().Set:", x.Y.ToNull().IsSet)

xXV := x.X.ToNull()
xYV := x.Y.ToNull()
yBytes, _ := json.Marshal(
	struct {
		X *StrP `json:"x,omitempty"`
		Y *StrP `json:"y,omitempty"`
		Z *StrP `json:"z,omitempty"`
	}{
		xXV.Ptr(),
		xYV.Ptr(),
		nil,
	},
)

fmt.Printf("string(yBytes): %#q\n", string(yBytes))
Output:

x.X.IsSet(): true
x.X.Val(): "x"
x.X.ToNull().Set: true
x.X.ToNull().Val: "x"
x.Y.IsSet(): false
x.Y.ToNull().Set: false
string(yBytes): `{"x":"x"}`

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	J[bool]
}

Bool wraps J[bool] and sql.NullBool.

func NewBool

func NewBool(v bool) (nullV Bool)

func (*Bool) Ptr added in v1.0.7

func (v *Bool) Ptr() *BoolP

func (*Bool) Scan

func (v *Bool) Scan(value any) (err error)

func (Bool) Value

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

type BoolP

type BoolP bool

BoolP acts as bool in *BoolP, but has convenient methods.

func NewBoolP added in v1.0.6

func NewBoolP(v bool) *BoolP

func (*BoolP) IsSet added in v1.0.5

func (p *BoolP) IsSet() bool

func (*BoolP) ToNull added in v1.0.7

func (p *BoolP) ToNull() Bool

func (*BoolP) Val

func (p *BoolP) Val() bool

type Byte

type Byte struct {
	J[byte]
}

Byte wraps J[byte] and sql.NullByte.

func NewByte

func NewByte(v byte) (nullV Byte)

func (*Byte) Ptr added in v1.0.7

func (v *Byte) Ptr() *ByteP

func (*Byte) Scan

func (v *Byte) Scan(value any) (err error)

func (Byte) Value

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

type ByteP

type ByteP byte

ByteP acts as byte in *ByteP, but has convenient methods.

func NewByteP added in v1.0.6

func NewByteP(v byte) *ByteP

func (*ByteP) IsSet added in v1.0.5

func (p *ByteP) IsSet() bool

func (*ByteP) ToNull added in v1.0.7

func (p *ByteP) ToNull() Byte

func (*ByteP) Val

func (p *ByteP) Val() byte

type CustomT added in v1.0.7

type CustomT[
	T any,
	pT interface {
		*T
		ScannerValuer
	},
] struct {
	J[T]
}

CustomT wraps J[ScannerValuer], sql.Scanner and driver.Valuer. Internal type T has to be given as T and as *T. *T must support json marshaling and unmarshaling.

func NewCustomT added in v1.0.7

func NewCustomT[
	T any,
	pT interface {
		*T
		ScannerValuer
	},
](v T) (nullV CustomT[T, pT])

func (*CustomT[T, pT]) Scan added in v1.0.7

func (v *CustomT[T, pT]) Scan(value any) (err error)

func (CustomT[T, pT]) Value added in v1.0.7

func (v CustomT[T, pT]) Value() (driver.Value, error)

type CustomTP added in v1.0.7

type CustomTP[
	T any,
	pT interface {
		*T
		ScannerValuer
	},
] struct {
	Internal T
}

CustomTP acts as CustomT in *CustomTP, but has convenient methods. *T must support json marshaling and unmarshaling.

func NewDBTypeP added in v1.0.6

func NewDBTypeP[
	T any,
	pT interface {
		*T
		ScannerValuer
	},
](v T) *CustomTP[T, pT]

func (*CustomTP[T, pT]) IsSet added in v1.0.7

func (p *CustomTP[T, pT]) IsSet() bool

func (*CustomTP[T, pT]) MarshalJSON added in v1.0.7

func (p *CustomTP[T, pT]) MarshalJSON() ([]byte, error)

func (*CustomTP[T, pT]) Scan added in v1.0.8

func (p *CustomTP[T, pT]) Scan(value any) (err error)

func (*CustomTP[T, pT]) ToNull added in v1.0.7

func (p *CustomTP[T, pT]) ToNull() (v CustomT[T, pT])

func (*CustomTP[T, pT]) UnmarshalJSON added in v1.0.7

func (p *CustomTP[T, pT]) UnmarshalJSON(data []byte) error

func (*CustomTP[T, pT]) Val added in v1.0.7

func (p *CustomTP[T, pT]) Val() T

func (CustomTP[T, pT]) Value added in v1.0.8

func (p CustomTP[T, pT]) Value() (driver.Value, error)

type Float64 added in v1.0.10

type Float64 struct {
	J[float64]
}

Float64 wraps J[float64] and sql.NullFloat64.

func NewFloat64 added in v1.0.10

func NewFloat64(v float64) (nullV Float64)

func (*Float64) Ptr added in v1.0.10

func (v *Float64) Ptr() *Float64P

func (*Float64) Scan added in v1.0.10

func (v *Float64) Scan(value any) (err error)

func (Float64) Value added in v1.0.10

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

type Float64P added in v1.0.10

type Float64P float64

Float64P acts as float64 in *Float64P, but has convenient methods.

func NewFloat64P added in v1.0.10

func NewFloat64P(v float64) *Float64P

func (*Float64P) IsSet added in v1.0.10

func (p *Float64P) IsSet() bool

func (*Float64P) ToNull added in v1.0.10

func (p *Float64P) ToNull() Float64

func (*Float64P) Val added in v1.0.10

func (p *Float64P) Val() float64

type Int16

type Int16 struct {
	J[int16]
}

Int16 wraps J[int16] and sql.NullInt16.

func NewInt16

func NewInt16(v int16) (nullV Int16)

func (*Int16) Ptr added in v1.0.7

func (v *Int16) Ptr() *Int16P

func (*Int16) Scan

func (v *Int16) Scan(value any) (err error)

func (Int16) Value

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

type Int16P

type Int16P int16

Int16P acts as int16 in *Int16P, but has convenient methods.

func NewInt16P added in v1.0.6

func NewInt16P(v int16) *Int16P

func (*Int16P) IsSet added in v1.0.5

func (p *Int16P) IsSet() bool

func (*Int16P) ToNull added in v1.0.7

func (p *Int16P) ToNull() Int16

func (*Int16P) Val

func (p *Int16P) Val() int16

type Int32

type Int32 struct {
	J[int32]
}

Int32 wraps J[int32] and sql.NullInt32.

func NewInt32

func NewInt32(v int32) (nullV Int32)

func (*Int32) Ptr added in v1.0.7

func (v *Int32) Ptr() *Int32P

func (*Int32) Scan

func (v *Int32) Scan(value any) (err error)

func (Int32) Value

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

type Int32P

type Int32P int32

Int32P acts as int32 in *Int32P, but has convenient methods.

func NewInt32P added in v1.0.6

func NewInt32P(v int32) *Int32P

func (*Int32P) IsSet added in v1.0.5

func (p *Int32P) IsSet() bool

func (*Int32P) ToNull added in v1.0.7

func (p *Int32P) ToNull() Int32

func (*Int32P) Val

func (p *Int32P) Val() int32

type Int64

type Int64 struct {
	J[int64]
}

Int64 wraps J[int64] and sql.NullInt64.

func NewInt64

func NewInt64(v int64) (nullV Int64)

func (*Int64) Ptr added in v1.0.7

func (v *Int64) Ptr() *Int64P

func (*Int64) Scan

func (v *Int64) Scan(value any) (err error)

func (Int64) Value

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

type Int64P

type Int64P int64

Int64P acts as int64 in *Int64P, but has convenient methods.

func NewInt64P added in v1.0.6

func NewInt64P(v int64) *Int64P

func (*Int64P) IsSet added in v1.0.5

func (p *Int64P) IsSet() bool

func (*Int64P) ToNull added in v1.0.7

func (p *Int64P) ToNull() Int64

func (*Int64P) Val

func (p *Int64P) Val() int64

type J added in v1.0.7

type J[T any] struct {
	Val   T
	IsSet bool
}

J can be used to create new nullable values that work with encoding/json and pointers. *T must support json marshaling and unmarshaling.

func NewJ added in v1.0.7

func NewJ[T any](p *T) (v J[T])

func (*J[T]) MarshalJSON added in v1.0.7

func (v *J[T]) MarshalJSON() ([]byte, error)

func (*J[T]) Ptr added in v1.0.7

func (v *J[T]) Ptr() *T

func (*J[T]) UnmarshalJSON added in v1.0.7

func (v *J[T]) UnmarshalJSON(data []byte) error

type ScannerValuer

type ScannerValuer interface {
	sql.Scanner
	driver.Valuer
}

type Str

type Str struct {
	J[string]
}

Str wraps J[string] and sql.NullString.

func NewStr

func NewStr(v string) (nullV Str)

func (*Str) Ptr added in v1.0.7

func (v *Str) Ptr() *StrP

func (*Str) Scan

func (v *Str) Scan(value any) (err error)

func (Str) Value

func (v Str) Value() (driver.Value, error)

type StrP

type StrP string

StrP acts as string in *StrP, but has convenient methods.

func NewStrP added in v1.0.6

func NewStrP(v string) *StrP

func (*StrP) IsSet added in v1.0.5

func (p *StrP) IsSet() bool

func (*StrP) ToNull added in v1.0.7

func (p *StrP) ToNull() Str

func (*StrP) Val

func (p *StrP) Val() string

type Time

type Time struct {
	J[time.Time]
}

Time wraps J[time.Time] and sql.NullTime.

func NewTime

func NewTime(v time.Time) (nullV Time)

func (*Time) Ptr added in v1.0.7

func (v *Time) Ptr() *TimeP

func (*Time) Scan

func (v *Time) Scan(value any) (err error)

func (Time) Value

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

type TimeP

type TimeP time.Time

TimeP acts as time.Time in *TimeP, but has convenient methods.

func NewTimeP added in v1.0.6

func NewTimeP(v time.Time) *TimeP

func (*TimeP) IsSet added in v1.0.5

func (p *TimeP) IsSet() bool

func (*TimeP) ToNull added in v1.0.7

func (p *TimeP) ToNull() Time

func (*TimeP) Val

func (p *TimeP) Val() time.Time

Jump to

Keyboard shortcuts

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