Documentation ¶
Overview ¶
Package null provides nullable types that are conscious of undefined values when marshaling or unmarshaling.
JSON ¶
Nullable types in this package can be marshaled to (unmarshaled from) JSON, as they all implement the json.Marshaler and json.Unmarshaler interfaces.
var json := struct { Mandatory string `json:"mandatory"` Optional null.String `json:"optional"` }{ Mandatory: "foo", Optional: bar, }
In the example, bar is a String. If bar is valid, the resulting JSON is going to be:
{ "mandatory": "foo", "optional": <bar.Str> }
otherwise:
{ "mandatory": "foo", "optional": null }
Unmarshaling from JSON works the other way around: JSON value null is converted to an invalid nullable, otherwise a compatible JSON type is unmarshaled into the appropriate nullable.
JSON and the omitempty struct tag ¶
Nullable types in this package offer a Ptr() method that is useful to deal with the omitempty struct tag:
var json := struct { Mandatory string `json:"mandatory"` Optional *string `json:"optional,omitempty"` }{ Mandatory: "foo", Optional: bar.Ptr(), }
In the example, bar is a String. If bar is valid, Ptr() returns a pointer to the underlying value, otherwise returns nil. json.Marshal will recognize nil pointers as empty values, omitting the associated name from the JSON output.
SQL ¶
Nullable types in this package recognize SQL NULL values and implement the driver.Valuer and sql.Scanner interfaces. Suppose we have the following table in our database:
CREATE TABLE example ( mandatory varchar(50) primary key not null, optional varchar(50) default null );
We may use the following struct that matches the table struct:
var sql := struct { Mandatory string `db:"mandatory"` Optional null.String `db:"optional"` }{ Mandatory: "foo", Optional: bar, }
In the example, bar is a String. If sql is inserted into the database, and bar is not valid, a SELECT query will return:
+-----------+-----------+ | mandatory | optional | +-----------+-----------+ | foo | <bar.Str> | +-----------+-----------+
otherwise:
+-----------+----------+ | mandatory | optional | +-----------+----------+ | foo | NULL | +-----------+----------+
It is also possible to scan values from the database. In that case, if the scanned value corresponds to an SQL NULL, an invalid nullable is initialized, otherwise, a compatible SQL value is scanned into the appropriate nullable.
Package flag ¶
Nullable types may also receive values from the command line via the flag package, as they implement the flag.Value interface:
var bar null.String func init() { flag.Var(&bar, "bar", "holds a bar") } func main() { flag.Parse() // now bar can be used }
bar will be invalid if the command line option "-bar" is not passed or is empty, otherwise it will be valid and it will hold the content of "-bar".
Index ¶
- Constants
- type Bool
- func (b *Bool) From(v bool)
- func (b *Bool) FromPtr(p *bool)
- func (b *Bool) FromZero(v bool)
- func (b Bool) MarshalJSON() (data []byte, err error)
- func (b Bool) MarshalText() (data []byte, err error)
- func (b Bool) Ptr() *bool
- func (b *Bool) Scan(obj interface{}) error
- func (b *Bool) Set(str string) error
- func (b Bool) String() string
- func (b *Bool) UnmarshalJSON(data []byte) error
- func (b *Bool) UnmarshalText(text []byte) error
- func (b Bool) Value() (v driver.Value, err error)
- func (b Bool) Zero() bool
- type ConversionError
- type Float64
- func (f *Float64) From(v float64)
- func (f *Float64) FromPtr(p *float64)
- func (f *Float64) FromZero(v float64)
- func (f Float64) MarshalJSON() (data []byte, err error)
- func (f Float64) MarshalText() (data []byte, err error)
- func (f Float64) Ptr() *float64
- func (f *Float64) Scan(obj interface{}) error
- func (f *Float64) Set(str string) error
- func (f Float64) String() string
- func (f *Float64) UnmarshalJSON(data []byte) error
- func (f *Float64) UnmarshalText(text []byte) error
- func (f Float64) Value() (v driver.Value, err error)
- func (f Float64) Zero() float64
- type Int
- func (i *Int) From(v int)
- func (i *Int) FromPtr(p *int)
- func (i *Int) FromZero(v int)
- func (i Int) MarshalJSON() (data []byte, err error)
- func (i Int) MarshalText() (data []byte, err error)
- func (i Int) Ptr() *int
- func (i *Int) Scan(obj interface{}) error
- func (i *Int) Set(str string) error
- func (i Int) String() string
- func (i *Int) UnmarshalJSON(data []byte) error
- func (i *Int) UnmarshalText(text []byte) error
- func (i Int) Value() (v driver.Value, err error)
- func (i Int) Zero() int
- type MarshalError
- type ParseError
- type String
- func (s *String) From(v string)
- func (s *String) FromPtr(p *string)
- func (s *String) FromZero(v string)
- func (s String) MarshalJSON() (data []byte, err error)
- func (s String) MarshalText() (data []byte, err error)
- func (s String) Ptr() *string
- func (s *String) Scan(obj interface{}) error
- func (s *String) Set(str string) error
- func (s String) String() string
- func (s *String) UnmarshalJSON(data []byte) error
- func (s *String) UnmarshalText(text []byte) error
- func (s String) Value() (v driver.Value, err error)
- func (s String) Zero() string
- type Time
- func (t *Time) From(v time.Time)
- func (t *Time) FromPtr(v *time.Time)
- func (t *Time) FromZero(v time.Time)
- func (t Time) MarshalJSON() (data []byte, err error)
- func (t Time) MarshalText() (data []byte, err error)
- func (t Time) Ptr() *time.Time
- func (t *Time) Scan(obj interface{}) error
- func (t *Time) Set(str string) error
- func (t Time) String() string
- func (t *Time) UnmarshalJSON(data []byte) error
- func (t *Time) UnmarshalText(text []byte) error
- func (t Time) Value() (v driver.Value, err error)
- func (t Time) Zero() time.Time
- type TypeError
- type Uint
- func (u *Uint) From(v uint)
- func (u *Uint) FromPtr(p *uint)
- func (u *Uint) FromZero(v uint)
- func (u Uint) MarshalJSON() (data []byte, err error)
- func (u Uint) MarshalText() (data []byte, err error)
- func (u Uint) Ptr() *uint
- func (u *Uint) Scan(obj interface{}) error
- func (u *Uint) Set(str string) error
- func (u Uint) String() string
- func (u *Uint) UnmarshalJSON(data []byte) error
- func (u *Uint) UnmarshalText(text []byte) error
- func (u Uint) Value() (v driver.Value, err error)
- func (u Uint) Zero() uint
- type UnmarshalError
Constants ¶
const ( // InvalidNullableString is returned by String methods when the nullable is // not valid. InvalidNullableString = "<invalid>" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct { // Bool holds the underlying bool value. Bool bool // Valid holds the validity flag. If true, the underlying value is valid. // If false, it is invalid, and thus meaningless. Valid bool }
Bool implements a nullable bool.
func BoolFromPtr ¶
BoolFromPtr creates a Bool from pointer p. If p is nil, the returned Bool is invalid.
func BoolFromZero ¶
BoolFromZero creates a Bool from v. If v is false, the returned Bool is invalid.
func (*Bool) FromPtr ¶
FromPtr invalidates b if p is nil, otherwise it sets the underlying value of b to the value pointed to by p, and b becomes valid.
func (*Bool) FromZero ¶
FromZero invalidates b if v is false, otherwise it sets the underlying value of b to v, and b becomes valid.
func (Bool) MarshalJSON ¶
MarshalJSON encodes the underlying value of b to a JSON boolean if b is valid, otherwise it returns the JSON null value. err is always nil.
func (Bool) MarshalText ¶
MarshalText marshals b to a byte string representation. If b is valid, it marshals the underlying value of b to either "true" or "false", otherwise it returns nil. err is always nil.
func (Bool) Ptr ¶
Ptr returns a pointer to the underlying value of b if b is valid, otherwise returns nil.
func (*Bool) Scan ¶
Scan assigns a value from a database driver. If obj's type is bool, b becomes valid, and the underlying value of b becomes the value of obj. If obj is nil, b becomes invalid. If obj's type is any other type, b becomes invalid, and a TypeError is returned.
func (*Bool) Set ¶
Set invalidates b if str is the empty string, otherwise it parses str into the underlying value of b, and b becomes valid. If str is not a recognized boolean string, b becomes invalid and a ParseError is returned. Recognized boolean strings are 1, true, True, TRUE, T, t, 0, false, False, FALSE, f, F.
func (Bool) String ¶
String returns a string representation of b. If b is valid, it returns either "true" or "false", otherwise it returns InvalidNullableString.
func (*Bool) UnmarshalJSON ¶
UnmarshalJSON unmarshals from a JSON encoded byte string to b. If the encoded JSON data represent the JSON null value, or an error is produced, b becomes invalid. If the encoded JSON data represent a JSON boolean, b becomes valid, and the underlying value of b is set to the JSON boolean. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.
func (*Bool) UnmarshalText ¶
UnmarshalText unmarshals from a byte string to b. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.
type ConversionError ¶
type ConversionError struct { // SrcValue holds the value that couldn't be converted SrcValue interface{} // SrcType holds the name of SrcValue's type SrcType string // DestType holds the destination type DestType string // contains filtered or unexported fields }
ConversionError is returned when a type conversion cannot happen without data loss.
func (ConversionError) Error ¶
func (e ConversionError) Error() string
Error returns a string representation of e.
type Float64 ¶
type Float64 struct { // Float64 holds the underlying float64 value. Float64 float64 // Valid holds the validity flag. If true, the underlying value is valid. // If false, it is invalid, and thus meaningless. Valid bool }
Float64 implements a nullable float64.
func Float64FromPtr ¶
Float64FromPtr creates a Float64 from pointer p. If p is nil, the returned Float64 is invalid.
func Float64FromZero ¶
Float64FromZero creates a Float64 from v. If v is 0, the returned Float64 is invalid.
func (*Float64) FromPtr ¶
FromPtr invalidates f if p is nil, otherwise it sets the underlying value of f to the value pointed to by p, and f becomes valid.
func (*Float64) FromZero ¶
FromZero invalidates f if v is false, otherwise it sets the underlying value of f to v, and f becomes valid.
func (Float64) MarshalJSON ¶
MarshalJSON encodes the underlying value of f to a JSON number if f is valid, otherwise it returns the JSON null value. err is always nil.
func (Float64) MarshalText ¶
MarshalText marshals f to a byte string representation. If f is valid, it marshals the underlying value of f to a byte string representation, otherwise it returns nil. err is always nil.
func (Float64) Ptr ¶
Ptr returns a pointer to the underlying value of f if f is valid, otherwise returns nil.
func (*Float64) Scan ¶
Scan assigns a value from a database driver. If obj's type is float64, f becomes valid, and the underlying value of f becomes the value of obj. If obj is nil, f becomes invalid. If obj's type is any other type, f becomes invalid, and a TypeError is returned.
func (*Float64) Set ¶
Set invalidates f if str is the empty string, otherwise it parses str into the underlying value of f, and f becomes valid. If str is not a valid string representation of a floating point number, f becomes invalid and a ParseError is returned.
func (Float64) String ¶
String returns a string representation of f. If f is valid, it returns a string representation of the underlying value of f, otherwise it returns InvalidNullableString.
func (*Float64) UnmarshalJSON ¶
UnmarshalJSON unmarshals from a JSON encoded byte string to f. If the encoded JSON data represent the JSON null value, or an error is produced, f becomes invalid. If the encoded JSON data represent a JSON number, f becomes valid, and the underlying value of f is set to the JSON number. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.
func (*Float64) UnmarshalText ¶
UnmarshalText unmarshals from a byte string to f. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.
type Int ¶
type Int struct { // Int holds the underlying int value. Int int // Valid holds the validity flag. If true, the underlying value is valid. // If false, it is invalid, and thus meaningless. Valid bool }
Int implements a nullable int.
func IntFromPtr ¶
IntFromPtr creates an Int from pointer p. If p is nil, the returned Int is invalid.
func IntFromZero ¶
IntFromZero creates an Int from v. If v is 0, the returned Int is invalid.
func (*Int) FromPtr ¶
FromPtr invalidates i if p is nil, otherwise it sets the underlying value of i to the value pointed to by p, and i becomes valid.
func (*Int) FromZero ¶
FromZero invalidates i if v is false, otherwise it sets the underlying value of i to v, and i becomes valid.
func (Int) MarshalJSON ¶
MarshalJSON encodes the underlying value of i to a JSON number if i is valid, otherwise it returns the JSON null value. err is always nil.
func (Int) MarshalText ¶
MarshalText marshals i to a byte string representation. If i is valid, it marshals the underlying value of i to a byte string representation, otherwise it returns nil. err is always nil.
func (Int) Ptr ¶
Ptr returns a pointer to the underlying value of i if i is valid, otherwise returns nil.
func (*Int) Scan ¶
Scan assigns a value from a database driver. If obj's type is int64, if the number can be stored in an int without data loss, i becomes valid, and the underlying value of i becomes the value of obj. If obj's type is int64, and the number cannot be stored in an int without data loss, i becomes invalid, and a ConversionError is returned. If obj is nil, i becomes invalid. If obj's type is any other type, i becomes invalid, and a TypeError is returned.
func (*Int) Set ¶
Set invalidates i if str is the empty string, otherwise it parses str into the underlying value of i, and i becomes valid. If str is not a valid string representation of an integer, or if the represented integer is too large to be stored in an int, i becomes invalid and a ParseError is returned.
func (Int) String ¶
String returns a string representation of i. If i is valid, it returns a string representation of the underlying value of i, otherwise it returns InvalidNullableString.
func (*Int) UnmarshalJSON ¶
UnmarshalJSON unmarshals from a JSON encoded byte string to i. If the encoded JSON data represent the JSON null value, or an error is produced, i becomes invalid. If the encoded JSON data represent a JSON number, and can be stored in an int without data loss, i becomes valid, and the underlying value of i is set to the JSON number. If the encoded JSON data represent a JSON number, and cannot be stored in an int without data loss, i becomes invalid, and a ConversionError is returned. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.
func (*Int) UnmarshalText ¶
UnmarshalText unmarshals from a byte string to i. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.
type MarshalError ¶
type MarshalError struct { // SrcValue holds the value that couldn't be marshaled SrcValue interface{} // SrcType holds the name of SrcValue's type SrcType string // contains filtered or unexported fields }
MarshalError is returned if marshaling is not successful.
func (MarshalError) Error ¶
func (e MarshalError) Error() string
Error returns a string representation of e.
type ParseError ¶
type ParseError struct { // SrcString holds the string that couldn't be parsed SrcString string // DestType holds the destination type DestType string // contains filtered or unexported fields }
ParseError is returned when a string cannot be parsed.
func (ParseError) Error ¶
func (e ParseError) Error() string
Error returns a string representation of e.
type String ¶
type String struct { // Str holds the underlying string value. The field can't be named String // because it conflicts with the flag.Value interface implementation. Str string // Valid holds the validity flag. If true, the underlying value is valid. // If false, it is invalid, and thus meaningless. Valid bool }
String implements a nullable string.
func StringFromPtr ¶
StringFromPtr creates a String from pointer p. If p is nil, the returned String is invalid.
func StringFromZero ¶
StringFromZero creates a String from v. If v is the empty string, the returned String is invalid.
func (*String) FromPtr ¶
FromPtr invalidates s if p is nil, otherwise it sets the underlying value of s to the value pointed to by p, and s becomes valid.
func (*String) FromZero ¶
FromZero invalidates s if v is the empty string, otherwise it sets the underlying value of s to v, and s becomes valid.
func (String) MarshalJSON ¶
MarshalJSON encodes the underlying value of s to a JSON string if s is valid, otherwise it returns the JSON null value. err is always nil.
func (String) MarshalText ¶
MarshalText converts the underlying value of s to []byte if s is valid, and returns nil if not valid. err is always nil.
func (String) Ptr ¶
Ptr returns a pointer to the underlying value of s if s is valid, otherwise returns nil.
func (*String) Scan ¶
Scan assigns a value from a database driver. If obj's type is string, s becomes valid, and the underlying value of s becomes the value of obj. If obj is nil, s becomes invalid. If obj's type is any other type, s becomes invalid, and a TypeError is returned.
func (*String) Set ¶
Set invalidates s if str is the empty string, otherwise it sets the underlying value of s to str, and s becomes valid. This function always returns nil.
func (String) String ¶
String returns the underlying value of s if s is valid, and InvalidNullableString if not valid.
func (*String) UnmarshalJSON ¶
UnmarshalJSON unmarshals from a JSON encoded byte string to s. If the encoded JSON data represent the JSON null value, or an error is produced, s becomes invalid. If the encoded JSON data represent a JSON string, s becomes valid, and the underlying value of s is set to the JSON string. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.
func (*String) UnmarshalText ¶
UnmarshalText unmarshals from a byte string to s. If the byte string is nil, s becomes invalid, otherwise the underlying value of s is set to the converted byte string and s becomes valid. The returned error is always nil.
type Time ¶
type Time struct { // Time holds the underlying time.Time value. Time time.Time // Valid holds the validity flag. If true, the underlying value is valid. // If false, it is invalid, and thus meaningless. Valid bool }
Time implements a nullable time.Time.
func TimeFromPtr ¶
TimeFromPtr creates a Time from pointer p. If p is nil, the returned Time is invalid.
func TimeFromZero ¶
TimeFromZero creates a Time from v. If v represents the zero time instant, the returned Time is invalid.
func (*Time) FromPtr ¶
FromPtr invalidates t if p is nil, otherwise it sets the underlying value of t to the value pointed to by p, and t becomes valid.
func (*Time) FromZero ¶
FromZero invalidates t if v represents the zero time instant, otherwise it sets the underlying value of t to v, and t becomes valid.
func (Time) MarshalJSON ¶
MarshalJSON encodes the underlying value of t to a JSON string representation if t is valid, otherwise it returns the JSON null value. The string representation is formatted according to the RFC3339 standard with nanoseconds. If the underlying value of t cannot be marshaled, a MarshalError is returned.
func (Time) MarshalText ¶
MarshalText marshals t to a byte string representation. If t is valid, it formats the underlying value of t according to the RFC3339 standard with nanoseconds, otherwise it returns nil. If the underlying value of t cannot be marshaled, a MarshalError is returned.
func (Time) Ptr ¶
Ptr returns a pointer to the underlying value of t if t is valid, otherwise returns nil.
func (*Time) Scan ¶
Scan assigns a value from a database driver. If obj's type is time.Time, t becomes valid, and the underlying value of t becomes the value of obj. If obj is nil, t becomes invalid. If obj's type is any other type, t becomes invalid, and a TypeError is returned.
func (*Time) Set ¶
Set invalidates t if str is the empty string, otherwise it parses str into the underlying value of t, and t becomes valid. If str is not a valid RFC3339 string, t becomes invalid and a ParseError is returned.
func (Time) String ¶
String returns a string representation of t. If t is valid, it formats the underlying value of t according to the RFC3339 standard with nanoseconds. For time instants which year is beyond 10000, not allowed by the standard, String tries to print a meaningful datetime regardless. If t is not valid, it returns InvalidNullableString.
func (*Time) UnmarshalJSON ¶
UnmarshalJSON unmarshals from a JSON encoded byte string to t. If the encoded JSON data represent the JSON null value, or an error is produced, t becomes invalid. If the encoded JSON data represent a valid RFC3339 JSON string, t becomes valid, and the underlying value of t is set to the JSON string. If the encoded JSON data represent a JSON string, but not formatted according to the RFC3339 standard, a ParseError is returned. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.
func (*Time) UnmarshalText ¶
UnmarshalText unmarshals from a byte string to t. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.
type TypeError ¶
type TypeError struct { // InvalidType holds the name of the type that is not valid InvalidType string // ExpectedTypes holds the names of accepted types ExpectedTypes []string // contains filtered or unexported fields }
TypeError is returned when a value of the wrong type is supplied.
type Uint ¶
type Uint struct { // Uint holds the underlying uint value. Uint uint // Valid holds the validity flag. If true, the underlying value is valid. // If false, it is invalid, and thus meaningless. Valid bool }
Uint implements a nullable uint.
func UintFromPtr ¶
UintFromPtr creates an Uint from pointer p. If p is nil, the returned Uint is invalid.
func UintFromZero ¶
UintFromZero creates an Uint from v. If v is 0, the returned Uint is invalid.
func (*Uint) FromPtr ¶
FromPtr invalidates u if p is nil, otherwise it sets the underlying value of u to the value pointed to by p, and u becomes valid.
func (*Uint) FromZero ¶
FromZero invalidates u if v is false, otherwise it sets the underlying value of u to v, and u becomes valid.
func (Uint) MarshalJSON ¶
MarshalJSON encodes the underlying value of u to a JSON number if u is valid, otherwise it returns the JSON null value. err is always nil.
func (Uint) MarshalText ¶
MarshalText marshals u to a byte string representation. If u is valid, it marshals the underlying value of u to a byte string representation, otherwise it returns nil. err is always nil.
func (Uint) Ptr ¶
Ptr returns a pointer to the underlying value of u if u is valid, otherwise returns nil.
func (*Uint) Scan ¶
Scan assigns a value from a database driver. If obj's type is int64, if the number can be stored in an uint without data loss, u becomes valid, and the underlying value of u becomes the value of obj. If obj's type is int64, and the number cannot be stored in an uint without data loss, u becomes invalid, and a ConversionError is returned. If obj is nil, u becomes invalid. If obj's type is any other type, u becomes invalid, and a TypeError is returned.
func (*Uint) Set ¶
Set invalidates u if str is the empty string, otherwise it parses str into the underlying value of u, and u becomes valid. If str is not a valid string representation of an unsigned integer, or if the represented integer is too large to be stored in an uint, u becomes invalid and a ParseError is returned.
func (Uint) String ¶
String returns a string representation of u. If u is valid, it returns a string representation of the underlying value of u, otherwise it returns InvalidNullableString.
func (*Uint) UnmarshalJSON ¶
UnmarshalJSON unmarshals from a JSON encoded byte string to u. If the encoded JSON data represent the JSON null value, or an error is produced, u becomes invalid. If the encoded JSON data represent a JSON number, and can be stored in an uint without data loss, u becomes valid, and the underlying value of u is set to the JSON number. If the encoded JSON data represent a JSON number, and cannot be stored in an uint without data loss, u becomes invalid, and a ConversionError is returned. Other JSON types produce a TypeError. Malformed JSON produces an UnmarshalError.
func (*Uint) UnmarshalText ¶
UnmarshalText unmarshals from a byte string to u. It behaves like Set, except that it returns an UnmarshalError instead of a ParseError in case text cannot be parsed.
type UnmarshalError ¶
type UnmarshalError struct { // SrcValue holds the data that couldn't be unmarshaled SrcValue string // DestType holds the destination type DestType string // contains filtered or unexported fields }
UnmarshalError is returned if unmarshaling is not successful.
func (UnmarshalError) Error ¶
func (e UnmarshalError) Error() string
Error returns a string representation of e.