nullable

package
v0.0.0-...-f3f6071 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 19 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReflectIsNull

func ReflectIsNull(v reflect.Value) bool

ReflectIsNull returns if a reflect.Value contains either a nil value or implements the Nullable interface and returns true from IsNull or implements the Zeroable interface and returns true from IsZero. It's safe to call ReflectIsNull on any reflect.Value with true returned for the zero value of reflect.Value.

func SQLArrayLiteral

func SQLArrayLiteral(s []string) string

SQLArrayLiteral joins the passed strings as an SQL array literal A nil slice will produce NULL, pass an empty non nil slice to get the empty SQL array literal {}.

func SplitArray

func SplitArray(array string) ([]string, error)

SplitArray splits an SQL or JSON array into its top level elements. Array elements that are quoted strings will not be unquoted. Returns nil in case of an empty array ("{}" or "[]"). Passing "null" or "NULL" as array will return nil without an error.

Types

type BoolArray

type BoolArray = pq.BoolArray

BoolArray implements the sql.Scanner and driver.Valuer interfaces for a slice of bool. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

type FloatArray

type FloatArray []float64

FloatArray implements the sql.Scanner and driver.Valuer interfaces for a slice of float64. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

func (FloatArray) Contains

func (a FloatArray) Contains(value float64) bool

Contains reports if the passed value is present in a.

func (FloatArray) IsNull

func (a FloatArray) IsNull() bool

IsNull returns true if a is nil. IsNull implements the Nullable interface.

func (FloatArray) Len

func (a FloatArray) Len() int

Len is the number of elements in the collection. One of the methods to implement sort.Interface.

func (FloatArray) Less

func (a FloatArray) Less(i, j int) bool

Less reports whether the element with index i should sort before the element with index j. One of the methods to implement sort.Interface.

func (*FloatArray) Scan

func (a *FloatArray) Scan(src any) error

Scan implements the sql.Scanner interface.

func (FloatArray) String

func (a FloatArray) String() string

String implements the fmt.Stringer interface

func (FloatArray) StringOr

func (a FloatArray) StringOr(nilStr string) string

func (FloatArray) Swap

func (a FloatArray) Swap(i, j int)

Swap swaps the elements with indexes i and j. One of the methods to implement sort.Interface.

func (FloatArray) Value

func (a FloatArray) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface

type IntArray

type IntArray []int64

IntArray implements the sql.Scanner and driver.Valuer interfaces for a slice of int64. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

func (IntArray) Contains

func (a IntArray) Contains(value int64) bool

Contains reports if the passed value is present in a.

func (IntArray) IsNull

func (a IntArray) IsNull() bool

IsNull returns true if a is nil. IsNull implements the Nullable interface.

func (IntArray) Len

func (a IntArray) Len() int

Len is the number of elements in the collection. One of the methods to implement sort.Interface.

func (IntArray) Less

func (a IntArray) Less(i, j int) bool

Less reports whether the element with index i should sort before the element with index j. One of the methods to implement sort.Interface.

func (*IntArray) Scan

func (a *IntArray) Scan(src any) error

Scan implements the sql.Scanner interface.

func (IntArray) String

func (a IntArray) String() string

String implements the fmt.Stringer interface.

func (IntArray) Swap

func (a IntArray) Swap(i, j int)

Swap swaps the elements with indexes i and j. One of the methods to implement sort.Interface.

func (IntArray) Value

func (a IntArray) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface

type JSON

type JSON []byte

JSON is a []byte slice containing JSON text or nil as the representation of the JSON "null" value. that implements the interfaces: json.Marshaler, json.Unmarshaler, driver.Value, sql.Scanner. The nil value of the type JSON is marshalled as the JSON "null" and SQL NULL values. The JSON "null" and SQL NULL values are unmarshalled as nil value ot the type JSON.

func MarshalJSON

func MarshalJSON(source any) (JSON, error)

func (JSON) Clone

func (j JSON) Clone() JSON

Clone returns a copy of j

func (JSON) GoString

func (j JSON) GoString() string

func (JSON) IsEmpty

func (j JSON) IsEmpty() bool

IsEmpty returns true if j is nil, or an empty JSON value like "", "{}", or "[]"

func (JSON) IsNull

func (j JSON) IsNull() bool

IsNull returns true if j is nil. IsNull implements the Nullable interface.

func (*JSON) MarshalFrom

func (j *JSON) MarshalFrom(source any) error

MarshalFrom marshalles source as JSON and sets it at j when there was no error.

func (JSON) MarshalJSON

func (j JSON) MarshalJSON() ([]byte, error)

MarshalJSON returns j as the JSON encoding of j. MarshalJSON implements encoding/json.Marshaler See the package function MarshalJSON to marshal a struct into JSON

func (JSON) PrettyPrint

func (j JSON) PrettyPrint(w io.Writer)

func (*JSON) Scan

func (j *JSON) Scan(src any) error

Scan stores the src in *j. No validation is done.

func (JSON) String

func (j JSON) String() string

String returns the JSON as string. String implements the fmt.Stringer interface.

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(sourceJSON []byte) error

UnmarshalJSON sets *j to a copy of sourceJSON. UnarshalJSON implements encoding/json.Unmarshaler See method Unmarshal for unmarshalling into a struct.

func (JSON) UnmarshalTo

func (j JSON) UnmarshalTo(dest any) error

UnmarshalTo unmashalles the JSON of j to dest

func (JSON) Valid

func (j JSON) Valid() bool

Valid reports whether j is a valid JSON encoding.

func (JSON) Value

func (j JSON) Value() (driver.Value, error)

Value returns j as a SQL value.

type NonEmptyString

type NonEmptyString string

NonEmptyString is a string type where the empty string value is interpreted as SQL NULL and JSON null by implementing the sql.Scanner and driver.Valuer interfaces and also json.Marshaler and json.Unmarshaler. Note that this type can't hold an empty string without interpreting it as not null SQL or JSON value.

const NonEmptyStringNull NonEmptyString = ""

NonEmptyStringNull is the SQL NULL and JSON null value for NonEmptyString.

func JoinNonEmptyStrings

func JoinNonEmptyStrings(separator string, strs ...NonEmptyString) NonEmptyString

JoinNonEmptyStrings joins only those strings that are not empty/null with the passed separator between them.

func NonEmptyStringFromError

func NonEmptyStringFromError(err error) NonEmptyString

NonEmptyStringFromError converts an error to a NonEmptyString interpreting a nil error as null value "" or else using err.Error() as value.

func NonEmptyStringFromPtr

func NonEmptyStringFromPtr(ptr *string) NonEmptyString

NonEmptyStringFromPtr converts a string pointer to a NonEmptyString interpreting nil as null value "".

func NonEmptyStringTrimSpace

func NonEmptyStringTrimSpace(str string) NonEmptyString

NonEmptyStringTrimSpace returns a NonEmptyString by trimming space from the passed string. If the passed string with trimmed space is an empty string then the NonEmptyString will represent null.

func NonEmptyStringf

func NonEmptyStringf(format string, a ...any) NonEmptyString

NonEmptyStringf formats a string using fmt.Sprintf and returns it as NonEmptyString. An empty string will be interpreted as null value.

func (NonEmptyString) Get

func (n NonEmptyString) Get() string

Get returns the non nullable string value or panics if the NonEmptyString is null. Note: check with IsNull before using Get!

func (NonEmptyString) IsNotNull

func (n NonEmptyString) IsNotNull() bool

IsNotNull returns true if the string n is not empty.

func (NonEmptyString) IsNull

func (n NonEmptyString) IsNull() bool

IsNull returns true if the string n is empty. IsNull implements the Nullable interface.

func (NonEmptyString) MarshalJSON

func (n NonEmptyString) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler by returning the JSON null value for an empty (null) string.

func (NonEmptyString) Ptr

func (n NonEmptyString) Ptr() *string

Ptr returns the address of the string value or nil if n.IsNull()

func (*NonEmptyString) Scan

func (n *NonEmptyString) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*NonEmptyString) Set

func (n *NonEmptyString) Set(s string)

Set the passed string as NonEmptyString. Passing an empty string will be interpreted as setting NULL.

func (*NonEmptyString) SetNull

func (n *NonEmptyString) SetNull()

SetNull sets the string to its null value

func (NonEmptyString) StringOr

func (n NonEmptyString) StringOr(nullString string) string

StringOr returns the string value of n or the passed nullString if n.IsNull()

func (NonEmptyString) TrimSpace

func (n NonEmptyString) TrimSpace() NonEmptyString

TrimSpace returns the string with all white-space characters trimmed from beginning and end. A potentially resulting empty string will be interpreted as null.

func (*NonEmptyString) UnmarshalText

func (n *NonEmptyString) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface

func (NonEmptyString) Value

func (n NonEmptyString) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface.

type NullBoolArray

type NullBoolArray []sql.NullBool

NullBoolArray implements the sql.Scanner and driver.Valuer interfaces for a slice of sql.NullBool. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

func (NullBoolArray) Bools

func (a NullBoolArray) Bools() []bool

Bools returns all NullBoolArray elements as []bool with NULL elements set to false.

func (NullBoolArray) IsNull

func (a NullBoolArray) IsNull() bool

IsNull returns true if a is nil. IsNull implements the Nullable interface.

func (*NullBoolArray) Scan

func (a *NullBoolArray) Scan(src any) error

Scan implements the sql.Scanner interface

func (NullBoolArray) String

func (a NullBoolArray) String() string

String implements the fmt.Stringer interface.

func (NullBoolArray) Value

func (a NullBoolArray) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface

type NullFloatArray

type NullFloatArray []sql.NullFloat64

NullFloatArray implements the sql.Scanner and driver.Valuer interfaces for a slice of sql.NullFloat64. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

func (NullFloatArray) Floats

func (a NullFloatArray) Floats() []float64

Floats returns all NullFloatArray elements as []float64 with NULL elements set to 0.

func (NullFloatArray) IsNull

func (a NullFloatArray) IsNull() bool

IsNull returns true if a is nil. IsNull implements the Nullable interface.

func (*NullFloatArray) Scan

func (a *NullFloatArray) Scan(src any) error

Scan implements the sql.Scanner interface

func (NullFloatArray) String

func (a NullFloatArray) String() string

String implements the fmt.Stringer interface.

func (NullFloatArray) Value

func (a NullFloatArray) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface

type NullIntArray

type NullIntArray []sql.NullInt64

NullIntArray implements the sql.Scanner and driver.Valuer interfaces for a slice of sql.NullInt64. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

func (NullIntArray) Ints

func (a NullIntArray) Ints() []int64

Ints returns all NullIntArray elements as []int64 with NULL elements set to 0.

func (NullIntArray) IsNull

func (a NullIntArray) IsNull() bool

IsNull returns true if a is nil. IsNull implements the Nullable interface.

func (*NullIntArray) Scan

func (a *NullIntArray) Scan(src any) error

Scan implements the sql.Scanner interface

func (NullIntArray) String

func (a NullIntArray) String() string

String implements the fmt.Stringer interface.

func (NullIntArray) Value

func (a NullIntArray) Value() (driver.Value, error)

Value implements the database/sql/driver.Valuer interface

type Nullable

type Nullable interface {
	// IsNull returns true if the implementing value is considered null.
	IsNull() bool
}

Nullable is an interface with an IsNull method

type StringArray

type StringArray = pq.StringArray

StringArray implements the sql.Scanner and driver.Valuer interfaces for a slice of strings. A nil slice is mapped to the SQL NULL value, and a non nil zero length slice to an empty SQL array '{}'.

type Time

type Time struct {
	time.Time
}

Time represents a time.Time where the zero time instant (which is the empty default value of the type) is interpreted as SQL NULL and JSON null. It implements the sql.Scanner and driver.Valuer interfaces and also json.Marshaler and json.Unmarshaler. It assumes that zero time instant is never used in any real life application so it's free to be used as magical value for representing NULL. IsNull uses time.Time.IsZero internally.

var TimeNull Time

TimeNull is a null Time value.

Note: use Time.IsNull or IsNotNull to check for null instead of comparing a Time with TimeNull because Time.IsNull uses time.Time.IsZero internally which can return true for times that are not the empty time.Time{} default value.

func TimeFrom

func TimeFrom(t time.Time) Time

TimeFrom returns a nullable.Time from a time.Time

func TimeFromPtr

func TimeFromPtr(ptr *time.Time) Time

TimeFromPtr returns a nullable.Time from a time.Time pointer with nil interpreted as null.

func TimeNow

func TimeNow() Time

TimeNow returns the current time

func TimeParse

func TimeParse(layout, value string) (Time, error)

TimeParse parses a time value with the provided layout using time.Parse(layout, value) except for when value is on of "", "null", "NULL", then a null/zero time and no error are returned.

func TimeParseInLocation

func TimeParseInLocation(layout, value string, loc *time.Location) (Time, error)

TimeParseInLocation parses a time value with the provided layout and location using time.ParseInLocation(layout, value, loc) except for when value is on of "", "null", "NULL", then a null/zero time and no error are returned.

func (Time) Add

func (n Time) Add(duration time.Duration) Time

Add returns n+duration or null if n is null

func (Time) AddDate

func (n Time) AddDate(years int, months int, days int) Time

AddDate returns the nullable time corresponding to adding the given number of years, months, and days to t. For example, AddDate(-1, 2, 3) applied to January 1, 2011 returns March 4, 2010.

AddDate normalizes its result in the same way that Date does, so, for example, adding one month to October 31 yields December 1, the normalized form for November 31.

Returns null if n is null.

func (Time) AppendFormat

func (n Time) AppendFormat(b []byte, layout string) []byte

AppendFormat the time to b using time.Time.AppendFormat or b if n is null.

func (Time) Equal

func (n Time) Equal(o Time) bool

Equal reports whether n and o represent the same time instant or both are null. Two times can be equal even if they are in different locations. For example, 6:00 +0200 and 4:00 UTC are Equal. See the documentation on the Time type for the pitfalls of using == with Time values; most code should use Equal instead.

func (Time) Format

func (n Time) Format(layout string) string

Format the time using time.Time.Format or return and empty string if n is null.

func (Time) Get

func (n Time) Get() time.Time

Get returns the non nullable time.Time value or panics if the Time is null. Note: check with IsNull before using Get!

func (Time) GetOr

func (n Time) GetOr(defaultTime time.Time) time.Time

GetOr returns the non nullable time.Time value or the passed defaultTime if the Time is null.

func (Time) IsNotNull

func (n Time) IsNotNull() bool

IsNotNull returns true if the Time is not null. Uses time.Time.IsZero internally.

func (Time) IsNull

func (n Time) IsNull() bool

IsNull returns true if the Time is null. Uses time.Time.IsZero internally. IsNull implements the Nullable interface.

func (Time) MarshalJSON

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

MarshalJSON implements encoding/json.Marshaler

func (Time) MarshalText

func (n Time) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in RFC 3339 format, with sub-second precision added if present. "NULL" is returned as text if the time is null.

func (Time) PrettyPrint

func (n Time) PrettyPrint(w io.Writer)

PrettyPrint implements the pretty.Printable interface

func (Time) Ptr

func (n Time) Ptr() *time.Time

Ptr returns a pointer to Time or nil if n is null

func (*Time) Scan

func (n *Time) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*Time) Set

func (n *Time) Set(t time.Time)

Set a time.Time. Note that if t.IsZero() then n will be set to null.

func (*Time) SetNull

func (n *Time) SetNull()

SetNull sets the time to its null value

func (Time) String

func (n Time) String() string

String returns Time.String() or "NULL" if n is null.

func (Time) StringOr

func (n Time) StringOr(nullStr string) string

StringOr returns Time.String() or the passed nullStr if n is null.

func (Time) UTC

func (n Time) UTC() Time

UTC returns the time in UTC or null if n is null

func (*Time) UnmarshalJSON

func (n *Time) UnmarshalJSON(sourceJSON []byte) error

UnarshalJSON implements encoding/json.Unmarshaler. Interprets []byte(nil), []byte(""), []byte("null") as null.

func (*Time) UnmarshalText

func (n *Time) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format. Empty text, "null", or "NULL" will set the time to null.

func (Time) Value

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

Value implements the driver database/sql/driver.Valuer interface.

type TrimmedString

type TrimmedString string

TrimmedString is a string type where the empty trimmed string value is interpreted as SQL NULL and JSON null by implementing the sql.Scanner and driver.Valuer interfaces and also json.Marshaler and json.Unmarshaler.

Note that this type can't hold a not null empty string, because it will interpret it as null SQL or JSON value.

const TrimmedStringNull TrimmedString = ""

TrimmedStringNull is the NULL value "" for TrimmedString

func JoinTrimmedStrings

func JoinTrimmedStrings(separator string, strs ...TrimmedString) TrimmedString

JoinTrimmedStrings joins only those strings that are not empty/null with the passed separator between them.

func TrimmedStringFrom

func TrimmedStringFrom(str string) TrimmedString

TrimmedStringFrom trims the passed str and returns it as TrimmedString An empty trimmed string will be interpreted as null value.

func TrimmedStringFromError

func TrimmedStringFromError(err error) TrimmedString

TrimmedStringFromError converts an error to a TrimmedString interpreting a nil error as null value "" or else using err.Error() as value.

func TrimmedStringFromPtr

func TrimmedStringFromPtr(ptr *string) TrimmedString

TrimmedStringFromPtr converts a string pointer to a TrimmedString interpreting nil as null value "". An empty trimmed string will be interpreted as null value.

func TrimmedStringf

func TrimmedStringf(format string, a ...any) TrimmedString

TrimmedStringf formats a string using fmt.Sprintf and returns it as TrimmedString. An empty trimmed string will be interpreted as null value.

func (TrimmedString) Contains

func (s TrimmedString) Contains(substr string) bool

Contains reports whether substr is within s.

func (TrimmedString) ContainsAny

func (s TrimmedString) ContainsAny(chars string) bool

ContainsAny reports whether any Unicode code points in chars are within s.

func (TrimmedString) ContainsRune

func (s TrimmedString) ContainsRune(r rune) bool

ContainsRune reports whether the Unicode code point r is within s.

func (TrimmedString) Get

func (s TrimmedString) Get() string

Get returns the non nullable string value or panics if the TrimmedString is null. Note: check with IsNull before using Get!

func (TrimmedString) HasPrefix

func (s TrimmedString) HasPrefix(prefix string) bool

HasPrefix tests whether the TrimmedString begins with prefix.

func (TrimmedString) HasSuffix

func (s TrimmedString) HasSuffix(suffix string) bool

HasSuffix tests whether the TrimmedString ends with suffix.

func (TrimmedString) IsNotNull

func (s TrimmedString) IsNotNull() bool

IsNotNull returns true if the string is not empty.

func (TrimmedString) IsNull

func (s TrimmedString) IsNull() bool

IsNull returns true if the string is empty. IsNull implements the Nullable interface.

func (TrimmedString) MarshalJSON

func (s TrimmedString) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler by returning the JSON null value for an empty (null) string.

func (TrimmedString) MarshalText

func (s TrimmedString) MarshalText() ([]byte, error)

UnmarshalText implements the encoding.TextMarshaler interface

func (TrimmedString) MarshalXML

func (s TrimmedString) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (TrimmedString) Ptr

func (s TrimmedString) Ptr() *string

Ptr returns the address of the string value or nil if n.IsNull()

func (TrimmedString) ReplaceAll

func (s TrimmedString) ReplaceAll(old, new string) TrimmedString

ReplaceAll returns a copy of the TrimmedString with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.

func (*TrimmedString) Scan

func (s *TrimmedString) Scan(value any) error

Scan implements the database/sql.Scanner interface.

func (*TrimmedString) Set

func (s *TrimmedString) Set(str string)

Set the passed string as TrimmedString. Passing an empty trimmed string will be interpreted as setting NULL.

func (*TrimmedString) SetNull

func (s *TrimmedString) SetNull()

SetNull sets the string to its null value

func (TrimmedString) Split

func (s TrimmedString) Split(sep string) []TrimmedString

Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.

If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.

If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.

It is equivalent to SplitN with a count of -1.

To split around the first instance of a separator, see Cut.

func (TrimmedString) String

func (s TrimmedString) String() string

String implements the fmt.Stringer interface by returning a trimmed string that might be empty in case of the NULL value or an underlying string consisting only of whitespace.

func (TrimmedString) StringOr

func (s TrimmedString) StringOr(nullString string) string

StringOr returns the trimmed string value of n or the passed nullString if n.IsNull()

func (TrimmedString) ToLower

func (s TrimmedString) ToLower() TrimmedString

ToLower returns s with all Unicode letters mapped to their lower case.

func (TrimmedString) ToUpper

func (s TrimmedString) ToUpper() TrimmedString

ToUpper returns s with all Unicode letters mapped to their upper case.

func (TrimmedString) ToValidUTF8

func (s TrimmedString) ToValidUTF8(replacement string) TrimmedString

ToValidUTF8 returns a copy of the TrimmedString with each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.

func (TrimmedString) TrimPrefix

func (s TrimmedString) TrimPrefix(prefix string) TrimmedString

TrimPrefix returns s without the provided leading prefix string. If the TrimmedString doesn't start with prefix, s is returned unchanged.

func (TrimmedString) TrimSuffix

func (s TrimmedString) TrimSuffix(suffix string) TrimmedString

TrimSuffix returns s without the provided trailing suffix string. If the TrimmedString doesn't end with suffix, s is returned unchanged.

func (*TrimmedString) UnmarshalJSON

func (s *TrimmedString) UnmarshalJSON(j []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler.

func (*TrimmedString) UnmarshalText

func (s *TrimmedString) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface

func (*TrimmedString) UnmarshalXML

func (s *TrimmedString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

func (TrimmedString) Value

func (s TrimmedString) Value() (driver.Value, error)

Value implements the driver database/sql/driver.Valuer interface.

type Zeroable

type Zeroable interface {
	// IsZero returns true if the implementing value is considered zero.
	IsZero() bool
}

Zeroable is an interface with an IsZero method

Jump to

Keyboard shortcuts

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