Documentation
¶
Overview ¶
Package notnull provides never-null counterparts of some types of package github.com/domonda/go-types/nullable, for NOT NULL columns and JSON fields that must not be null.
The zero value of every type is written as a value:
- StringArray, IntArray, FloatArray and NullBoolArray write a nil slice as the empty PostgreSQL array '{}' and the JSON array []. Scan and UnmarshalJSON read SQL NULL and JSON null as a non-nil empty slice, so the arrays can be used without nil checks.
- JSON writes nil as the empty JSON object {}.
- TrimmedString trims every value and has no null state, the empty string is a value.
Use the nullable types instead where SQL NULL and JSON null must stay distinct from an empty value. Note that switching a field between the two packages changes what a nil slice is written as.
SplitArray, SplitArrayValues and SQLArrayLiteral parse and build PostgreSQL array literals and never return nil or NULL for an empty array.
See the README for tables of all functions and the exact serialization rules.
Index ¶
- func SQLArrayLiteral(s []string) string
- func SplitArray(array string) ([]string, error)
- func SplitArrayValues(array string) ([]string, error)
- type FloatArray
- func (a FloatArray) Contains(value float64) bool
- func (a FloatArray) Len() int
- func (a FloatArray) Less(i, j int) bool
- func (a FloatArray) MarshalJSON() ([]byte, error)
- func (a *FloatArray) Scan(src any) error
- func (a FloatArray) String() string
- func (a FloatArray) Swap(i, j int)
- func (a *FloatArray) UnmarshalJSON(data []byte) error
- func (a FloatArray) Value() (driver.Value, error)
- type IntArray
- func (a IntArray) Contains(value int64) bool
- func (a IntArray) Len() int
- func (a IntArray) Less(i, j int) bool
- func (a IntArray) MarshalJSON() ([]byte, error)
- func (a *IntArray) Scan(src any) error
- func (a IntArray) String() string
- func (a IntArray) Swap(i, j int)
- func (a *IntArray) UnmarshalJSON(data []byte) error
- func (a IntArray) Value() (driver.Value, error)
- type JSON
- func (j JSON) Clone() JSON
- func (j JSON) GoString() string
- func (j JSON) IsEmpty() bool
- func (j *JSON) MarshalFrom(source any) error
- func (j JSON) MarshalJSON() ([]byte, error)
- func (j JSON) PrettyPrint(w io.Writer) (int, error)
- func (j *JSON) Scan(src any) error
- func (j JSON) String() string
- func (j *JSON) UnmarshalJSON(sourceJSON []byte) error
- func (j JSON) UnmarshalTo(dest any) error
- func (j JSON) Valid() bool
- func (j JSON) Value() (driver.Value, error)
- type NullBoolArray
- type StringArray
- func (a StringArray) Contains(value string) bool
- func (a StringArray) Len() int
- func (a StringArray) Less(i, j int) bool
- func (a StringArray) MarshalJSON() ([]byte, error)
- func (a *StringArray) Scan(src any) error
- func (a StringArray) Swap(i, j int)
- func (a *StringArray) UnmarshalJSON(data []byte) error
- func (a StringArray) Value() (driver.Value, error)
- type TrimmedString
- func (s TrimmedString) Contains(substr string) bool
- func (s TrimmedString) ContainsAny(chars string) bool
- func (s TrimmedString) ContainsRune(r rune) bool
- func (s TrimmedString) HasPrefix(prefix string) bool
- func (s TrimmedString) HasSuffix(suffix string) bool
- func (s TrimmedString) IsEmpty() bool
- func (s TrimmedString) MarshalJSON() ([]byte, error)
- func (s TrimmedString) MarshalText() ([]byte, error)
- func (s TrimmedString) MarshalXML(e *xml.Encoder, start xml.StartElement) error
- func (s *TrimmedString) MarshalXMLAttr(name xml.Name) (xml.Attr, error)
- func (s TrimmedString) ReplaceAll(old, new string) TrimmedString
- func (s *TrimmedString) Scan(value any) error
- func (s *TrimmedString) Set(str string)
- func (s TrimmedString) Split(sep string) []TrimmedString
- func (s TrimmedString) String() string
- func (s TrimmedString) ToLower() TrimmedString
- func (s TrimmedString) ToUpper() TrimmedString
- func (s TrimmedString) ToValidUTF8(replacement string) TrimmedString
- func (s TrimmedString) TrimPrefix(prefix string) TrimmedString
- func (s TrimmedString) TrimSuffix(suffix string) TrimmedString
- func (s *TrimmedString) UnmarshalJSON(j []byte) error
- func (s *TrimmedString) UnmarshalText(text []byte) error
- func (s *TrimmedString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
- func (s TrimmedString) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SQLArrayLiteral ¶
SQLArrayLiteral joins the passed strings as an SQL array literal. Both a nil and an empty slice produce the empty array literal {} (use nullable.SQLArrayLiteral if a nil slice should produce NULL).
The result uses the PostgreSQL array text format ({"a","b"}), see https://www.postgresql.org/docs/current/arrays.html. That format is understood by PostgreSQL and array-compatible databases such as CockroachDB and YugabyteDB; databases without a native array type (MySQL, MariaDB, SQLite, SQL Server, Oracle) are not supported.
func SplitArray ¶
SplitArray splits an SQL or JSON array into its top level elements. Array elements that are quoted strings will not be unquoted, use SplitArrayValues to get the values of quoted string elements. Returns a non nil empty slice in case of an empty array ("{}" or "[]") or when passing "null" or "NULL" as array.
An unquoted NULL element of an SQL array (null in a JSON array) is returned as the unquoted string "NULL" ("null").
A trailing comma announces an element that is not there and yields an empty string as last element. PostgreSQL rejects such a literal, this parser reports the announced element instead of dropping it silently.
func SplitArrayValues ¶
SplitArrayValues splits an SQL or JSON array into its top level elements like SplitArray and returns the value of every element that is a double quoted string with the quotes removed and the escape sequences of the parsed array syntax undone. Returns a non nil empty slice in case of an empty array ("{}" or "[]") or when passing "null" or "NULL" as array.
A backslash escapes the following character in a PostgreSQL array element whether it is quoted or not, so every element of an SQL array is unescaped. In a JSON array only double quoted strings are, every other element, like the objects of a JSON array of objects, is returned unchanged.
An unquoted NULL element of an SQL array (null in a JSON array) is returned as the string "NULL" ("null") and is indistinguishable from a quoted "NULL" ("null") string element. Use SplitArray to tell them apart, its elements correspond by index and a quoted element still has its quotes there.
Returns an error for a quoted element of a JSON array that is not a valid JSON string, like one with an invalid escape sequence.
Types ¶
type FloatArray ¶
type FloatArray []float64
FloatArray implements the sql.Scanner and driver.Valuer interfaces for a slice of float64. The nil default value of the slice is returned as an empty (non null) array for SQL and JSON. Use nullable.FloatArray if the nil value should be treated as SQL and JSON null.
Value and Scan use the PostgreSQL array text format ({1.5,2,3}), see https://www.postgresql.org/docs/current/arrays.html. That format is understood by PostgreSQL and array-compatible databases such as CockroachDB and YugabyteDB; databases without a native array type (MySQL, MariaDB, SQLite, SQL Server, Oracle) are not supported.
func (FloatArray) Contains ¶
func (a FloatArray) Contains(value float64) bool
Contains reports if the passed value is present in a.
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) MarshalJSON ¶
func (a FloatArray) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler. A nil or empty array is encoded as the empty JSON array [] instead of null; a notnull array is never null. JSON has no NaN or infinity, so like encoding/json it returns a *json.UnsupportedValueError for them.
func (*FloatArray) Scan ¶
func (a *FloatArray) Scan(src any) error
Scan implements the sql.Scanner interface. It parses a PostgreSQL float array literal like {1.5,2,3} from a string or []byte. A nil source (SQL NULL), an empty string, or {} all scan to a non-nil empty slice; a notnull array is never nil.
func (FloatArray) String ¶
func (a FloatArray) String() string
String implements the fmt.Stringer interface.
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) UnmarshalJSON ¶
func (a *FloatArray) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. JSON null and [] both unmarshal to a non-nil empty slice; a notnull array is never nil.
type IntArray ¶
type IntArray []int64
IntArray implements the sql.Scanner and driver.Valuer interfaces for a slice of int64. The nil default value of the slice is returned as an empty (non null) array for SQL and JSON. Use nullable.IntArray if the nil value should be treated as SQL and JSON null.
Value and Scan use the PostgreSQL array text format ({1,2,3}), see https://www.postgresql.org/docs/current/arrays.html. That format is understood by PostgreSQL and array-compatible databases such as CockroachDB and YugabyteDB; databases without a native array type (MySQL, MariaDB, SQLite, SQL Server, Oracle) are not supported.
func (IntArray) Len ¶
Len is the number of elements in the collection. One of the methods to implement sort.Interface.
func (IntArray) Less ¶
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) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler. A nil or empty array is encoded as the empty JSON array [] instead of null; a notnull array is never null.
func (*IntArray) Scan ¶
Scan implements the sql.Scanner interface. It parses a PostgreSQL integer array literal like {1,2,3} from a string or []byte. A nil source (SQL NULL), an empty string, or {} all scan to a non-nil empty slice; a notnull array is never nil.
func (IntArray) Swap ¶
Swap swaps the elements with indexes i and j. One of the methods to implement sort.Interface.
func (*IntArray) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler. JSON null and [] both unmarshal to a non-nil empty slice; a notnull array is never nil.
type JSON ¶
type JSON []byte
JSON is a []byte slice containing JSON text. JSON(nil) is interpreted as an empty json object: {} Implements the interfaces json.Marshaler, json.Unmarshaler, driver.Valuer and sql.Scanner. Use nullable.JSON if the JSON(nil) value should be interpreted as JSON "null" and SQL "NULL".
func MarshalJSON ¶
MarshalJSON marshals source as JSON and returns it as a notnull JSON value.
func (JSON) Clone ¶
Clone returns a copy of j. A nil j returns nil, so the clone also encodes as {}.
func (JSON) GoString ¶
GoString returns a Go syntax representation of j as a notnull.JSON literal. GoString implements the fmt.GoStringer interface.
func (JSON) IsEmpty ¶
IsEmpty returns true if j is nil, or an empty JSON value like "", "{}", or "[]"
func (*JSON) MarshalFrom ¶
MarshalFrom marshals source as JSON and sets it at j when there was no error.
func (JSON) MarshalJSON ¶
MarshalJSON returns j as the JSON encoding of j. A nil or empty j holds no JSON text and returns {}. MarshalJSON implements encoding/json.Marshaler See the package function MarshalJSON to marshal a struct into JSON
func (JSON) PrettyPrint ¶
PrettyPrint implements the pretty.PrintableWithResult interface returning the JSON as a Go literal.
func (*JSON) Scan ¶
Scan stores the src in *j. No validation is done. SQL NULL and an empty string or []byte, which hold no JSON text, store the empty object {}.
func (JSON) String ¶
String returns the JSON as string, or {} for a nil or empty j. String implements the fmt.Stringer interface.
func (*JSON) UnmarshalJSON ¶
UnmarshalJSON sets *j to a copy of sourceJSON. The JSON null value is stored as the text null, not as nil. UnmarshalJSON implements encoding/json.Unmarshaler See method UnmarshalTo for unmarshalling into a struct.
func (JSON) UnmarshalTo ¶
UnmarshalTo unmarshals the JSON of j to dest. A nil or empty j is unmarshalled as the empty object {}.
type NullBoolArray ¶
NullBoolArray implements the sql.Scanner and driver.Valuer interfaces for a slice of sql.NullBool. The nil default value of the slice is returned as an empty (non null) array for SQL and JSON. Use nullable.NullBoolArray if the nil value should be treated as SQL and JSON null.
Value and Scan use the PostgreSQL array text format ({t,f,NULL}), see https://www.postgresql.org/docs/current/arrays.html. That format is understood by PostgreSQL and array-compatible databases such as CockroachDB and YugabyteDB; databases without a native array type (MySQL, MariaDB, SQLite, SQL Server, Oracle) are not supported.
func (NullBoolArray) Bools ¶
func (a NullBoolArray) Bools() []bool
Bools returns all NullBoolArray elements as []bool with NULL elements set to false.
func (NullBoolArray) MarshalJSON ¶
func (a NullBoolArray) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler. Valid elements are encoded as JSON true or false, invalid (NULL) elements as JSON null. A nil or empty array is encoded as the empty JSON array [] instead of null; a notnull array is never null.
func (*NullBoolArray) Scan ¶
func (a *NullBoolArray) Scan(src any) error
Scan implements the sql.Scanner interface. It parses a PostgreSQL boolean array literal like {t,f,NULL} from a string or []byte. The elements t, f, true and false scan as valid and NULL as an invalid sql.NullBool, all case-insensitive. Any other element returns an error and leaves a unchanged. A nil source (SQL NULL), an empty string, or {} all scan to a non-nil empty slice; a notnull array is never nil.
func (NullBoolArray) String ¶
func (a NullBoolArray) String() string
String implements the fmt.Stringer interface.
func (*NullBoolArray) UnmarshalJSON ¶
func (a *NullBoolArray) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. JSON null and [] both unmarshal to a non-nil empty slice; a notnull array is never nil. JSON null elements unmarshal to an invalid sql.NullBool.
func (NullBoolArray) Value ¶
func (a NullBoolArray) Value() (driver.Value, error)
Value implements the database/sql/driver.Valuer interface. It returns the slice as a PostgreSQL boolean array literal like {t,f,NULL}, with invalid (NULL) elements written as NULL. A nil or empty slice returns the empty array {}.
type StringArray ¶
type StringArray []string
StringArray implements the sql.Scanner, driver.Valuer, and encoding/json.Marshaler interfaces for a slice of strings. The nil default value of the slice is returned as an empty (non null) array for SQL and JSON. Use nullable.StringArray if the nil value should be treated as SQL and JSON null.
Value and Scan use the PostgreSQL array text format ({"a","b"}), see https://www.postgresql.org/docs/current/arrays.html. That format is understood by PostgreSQL and array-compatible databases such as CockroachDB and YugabyteDB; databases without a native array type (MySQL, MariaDB, SQLite, SQL Server, Oracle) are not supported.
func (StringArray) Contains ¶
func (a StringArray) Contains(value string) bool
Contains reports if the passed value is present in a.
func (StringArray) Len ¶
func (a StringArray) Len() int
Len is the number of elements in the collection. One of the methods to implement sort.Interface.
func (StringArray) Less ¶
func (a StringArray) 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 (StringArray) MarshalJSON ¶
func (a StringArray) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler. A nil or empty array is encoded as the empty JSON array [] instead of null; a notnull array is never null.
func (*StringArray) Scan ¶
func (a *StringArray) Scan(src any) error
Scan implements the sql.Scanner interface. It parses a PostgreSQL text array literal like {"a","b"} (quoted or unquoted elements) from a string or []byte. A nil source (SQL NULL) or {} scan to a non-nil empty slice; a notnull array is never nil.
func (StringArray) Swap ¶
func (a StringArray) Swap(i, j int)
Swap swaps the elements with indexes i and j. One of the methods to implement sort.Interface.
func (*StringArray) UnmarshalJSON ¶
func (a *StringArray) UnmarshalJSON(data []byte) error
UnmarshalJSON implements encoding/json.Unmarshaler. JSON null and [] both unmarshal to a non-nil empty slice; a notnull array is never nil.
type TrimmedString ¶
type TrimmedString string
TrimmedString is a string type where all marshaller and unmarshaller will trim whitespace first before returning or using a value. TrimmedString can hold an empty string.
func TrimmedStringFrom ¶
func TrimmedStringFrom(str string) TrimmedString
TrimmedStringFrom trims the passed str and returns it as TrimmedString
func TrimmedStringJoin ¶
func TrimmedStringJoin[S ~string](separator string, strs ...S) TrimmedString
TrimmedStringJoin joins trimmed strings with the passed separator between them. Unlike nullable.TrimmedStringJoin it keeps strings that are empty after trimming.
func TrimmedStringf ¶
func TrimmedStringf(format string, a ...any) TrimmedString
TrimmedStringf formats a string using fmt.Sprintf and returns it as TrimmedString.
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) 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) IsEmpty ¶
func (s TrimmedString) IsEmpty() bool
IsEmpty indicates if the trimmed string is empty which is also the case when the underlying string consists only of runes removed by strutil.TrimSpace.
func (TrimmedString) MarshalJSON ¶
func (s TrimmedString) MarshalJSON() ([]byte, error)
MarshalJSON implements encoding/json.Marshaler
func (TrimmedString) MarshalText ¶
func (s TrimmedString) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface
func (TrimmedString) MarshalXML ¶
func (s TrimmedString) MarshalXML(e *xml.Encoder, start xml.StartElement) error
MarshalXML implements encoding/xml.Marshaler by encoding the trimmed string as the character data of an XML element.
func (*TrimmedString) MarshalXMLAttr ¶
MarshalXMLAttr implements encoding/xml.MarshalerAttr by writing the text of MarshalText, and no attribute for a nil pointer, because encoding/xml calls MarshalText through a nil pointer attribute field and panics.
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. SQL NULL returns an error because TrimmedString has no null state, use nullable.TrimmedString for nullable columns.
func (*TrimmedString) Set ¶
func (s *TrimmedString) Set(str string)
Set the passed string as TrimmedString
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 if the underlying string consists only of whitespace.
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
UnmarshalXML implements encoding/xml.Unmarshaler by decoding the character data of the XML element, trimming whitespace, and storing the result.