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 ¶
- type Bool
- type BoolP
- type Byte
- type ByteP
- type CustomT
- type CustomTP
- func (p *CustomTP[T, pT]) IsSet() bool
- func (p *CustomTP[T, pT]) MarshalJSON() ([]byte, error)
- func (p *CustomTP[T, pT]) Scan(value any) (err error)
- func (p *CustomTP[T, pT]) ToNull() (v CustomT[T, pT])
- func (p *CustomTP[T, pT]) UnmarshalJSON(data []byte) error
- func (p *CustomTP[T, pT]) Val() T
- func (p CustomTP[T, pT]) Value() (driver.Value, error)
- type Float64
- type Float64P
- type Int16
- type Int16P
- type Int32
- type Int32P
- type Int64
- type Int64P
- type J
- type ScannerValuer
- type Str
- type StrP
- type Time
- type TimeP
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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])
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]) MarshalJSON ¶ added in v1.0.7
func (*CustomTP[T, pT]) UnmarshalJSON ¶ added in v1.0.7
type Float64 ¶ added in v1.0.10
Float64 wraps J[float64] and sql.NullFloat64.
func NewFloat64 ¶ added in v1.0.10
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
type J ¶ added in v1.0.7
J can be used to create new nullable values that work with encoding/json and pointers. *T must support json marshaling and unmarshaling.