Documentation
¶
Index ¶
- func FromPtr[T any](ptr *T) (T, bool)
- func FromPtrOrZero[T any](ptr *T) T
- func Must[T any](val T, err error) T
- func Must2[T, U any](val T, val2 U, err error) (T, U)
- func Must3[T, U, V any](val T, val2 U, val3 V, err error) (T, U, V)
- func Ptr[T any](value T) *Tdeprecated
- type Bound
- type DateTime
- func (t DateTime) AddDuration(d Duration) DateTime
- func (t DateTime) Compare(other DateTime) int
- func (t DateTime) MarshalPGBound() ([]byte, error)
- func (t *DateTime) Scan(value any) error
- func (t DateTime) SubDateTime(other DateTime) Duration
- func (t *DateTime) UnmarshalPGBound(text []byte) error
- func (t DateTime) Value() (driver.Value, error)
- type Duration
- type Dyn
- func (d Dyn) MarshalBSON() ([]byte, error)
- func (d Dyn) MarshalBSONValue() (byte, []byte, error)
- func (d Dyn) MarshalBinary() ([]byte, error)
- func (d Dyn) MarshalJSON() ([]byte, error)
- func (d Dyn) MarshalText() ([]byte, error)
- func (d *Dyn) Scan(src any) error
- func (d *Dyn) UnmarshalBSON(data []byte) error
- func (d *Dyn) UnmarshalBSONValue(t byte, data []byte) error
- func (d *Dyn) UnmarshalBinary(data []byte) error
- func (d *Dyn) UnmarshalJSON(data []byte) error
- func (d *Dyn) UnmarshalText(data []byte) error
- func (d Dyn) Value() (driver.Value, error)
- type KV
- type KVs
- func (kvs KVs[K, V]) Keys() []K
- func (kvs KVs[K, V]) Map() map[K]V
- func (kvs KVs[K, V]) MarshalBSON() ([]byte, error)
- func (kvs KVs[K, V]) MarshalBSONValue() (byte, []byte, error)
- func (kvs KVs[K, V]) MarshalJSON() ([]byte, error)
- func (kvs *KVs[K, V]) Scan(src any) error
- func (kvs *KVs[K, V]) UnmarshalBSON(data []byte) error
- func (kvs *KVs[K, V]) UnmarshalBSONValue(t byte, data []byte) error
- func (kvs *KVs[K, V]) UnmarshalJSON(data []byte) error
- func (kvs KVs[K, V]) Vals() []V
- func (kvs KVs[K, V]) Value() (driver.Value, error)
- type MultiRange
- type Nil
- func (n Nil[T]) MarshalBSONValue() (byte, []byte, error)
- func (n Nil[T]) MarshalBinary() ([]byte, error)
- func (n Nil[T]) MarshalJSON() ([]byte, error)
- func (n Nil[T]) MarshalText() ([]byte, error)
- func (n Nil[T]) Ptr() *T
- func (n *Nil[T]) Scan(src any) error
- func (n *Nil[T]) UnmarshalBSONValue(t byte, data []byte) error
- func (n *Nil[T]) UnmarshalBinary(data []byte) error
- func (n *Nil[T]) UnmarshalJSON(data []byte) error
- func (n *Nil[T]) UnmarshalText(data []byte) error
- func (n Nil[T]) Value() (driver.Value, error)
- type Opt
- type Ordered
- type OrderedBound
- type OrderedMultiRange
- type OrderedRange
- type PGBoundMarshaler
- type PGBoundUnmarshaler
- type Range
- type Str64
- type URL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromPtr ¶
FromPtr safely dereferences the given pointer and returns it along with a boolean indicating whether the pointer was non-nil.
func FromPtrOrZero ¶ added in v1.2.0
func FromPtrOrZero[T any](ptr *T) T
FromPtrOrZero safely dereferences the given pointer and returns its value. If the pointer is nil, it returns the zero value of T.
func Must ¶ added in v1.4.0
Must is a helper that panics if the error is non-nil, otherwise returning the value.
func Must2 ¶ added in v1.4.0
Must2 is a helper that panics if the error is non-nil, otherwise returning the two values.
Types ¶
type Bound ¶ added in v1.4.0
type Bound[O cmp.Ordered] struct { Val O `json:"val" bson:"val"` // the bound value Exclusive bool `json:"exclusive,omitempty" bson:"exclusive,omitempty"` // whether the bound is exclusive (true) or inclusive (false); ignored when Unbounded is true Unbounded bool `json:"unbounded,omitempty" bson:"unbounded,omitempty"` // whether this is an infinite/unbounded bound (true) or a finite bound (false) }
Bound represents one end of a range with its value and whether it is exclusive. The zero value is inclusive (Exclusive == false) and bounded. When Unbounded is true the bound is ±∞; Val and Exclusive are ignored.
type DateTime ¶ added in v1.4.0
DateTime is a time.Time wrapper for use as an OrderedRange / OrderedMultiRange with tsrange, tstzrange, tsmultirange, and tstzmultirange backing types. All constructors and methods strip the monotonic clock reading so that two DateTime values representing the same instant compare and marshal identically.
This type also supports operations with the Duration type, which supports dynamic Month and Day components.
func (DateTime) AddDuration ¶ added in v1.5.0
AddDuration adds a Duration with dynamic Month and Day components to the DateTime, returning a new DateTime.
func (DateTime) Compare ¶ added in v1.4.0
Compare implements the Ordered interface by comparing the time values with the monotonic clock stripped.
func (DateTime) MarshalPGBound ¶ added in v1.4.0
MarshalPGBound implements PGBoundMarshaler. Produces a PostgreSQL timestamptz literal.
func (*DateTime) Scan ¶ added in v1.4.0
Scan implements sql.Scanner. The driver is expected to deliver a time.Time value (which pgx and database/sql do for DATE, TIMESTAMP, and TIMESTAMPTZ columns alike). A nil value (SQL NULL) zeroes the receiver. Use Nil[DateTime] for a type that can be nil instead of zero.
func (DateTime) SubDateTime ¶ added in v1.5.0
SubDateTime returns the Duration d such that t.Equal(other.AddDuration(d)).
func (*DateTime) UnmarshalPGBound ¶ added in v1.4.0
UnmarshalPGBound implements PGBoundUnmarshaler. Accepts a PostgreSQL timestamptz literal, including the double-quoted form that PostgreSQL uses for timestamp bounds inside range literals
type Duration ¶ added in v1.5.0
type Duration struct {
Time time.Duration // The [time.Duration] field represents the time part of duration in terms of hours, minutes, seconds, etc.
Day int64 // The Day field represents the number of days in the duration. This is useful for calculations that need to account for varying lengths of days (e.g., due to daylight saving time changes).
Month int64 // The Month field represents the number of months in the duration. This is useful for calculations that need to account for varying lengths of months (e.g., due to the number of days in each month).
}
Duration is a struct that represents a duration of time, including both a time.Duration and additional fields for days and months. This allows for more flexible time calculations that can account for varying lengths of days and months.
func (Duration) Abs ¶ added in v1.5.0
Abs returns a new Duration with the absolute values of the time, day, and month components. This is useful for ensuring that all components of the duration are non-negative, regardless of their original signs.
func (Duration) Add ¶ added in v1.5.0
Add adds another Duration to the current Duration, combining their time, day, and month components. The operation is similar to a vector addition, as they are independent unless a point in time is being calculated.
func (Duration) MarshalText ¶ added in v1.5.0
MarshalText implements the encoding.TextMarshaler interface. Produces an ISO 8601 duration string (e.g. "P1Y2M3DT4H5M6.789S"). Month values >= 12 are expressed as years + remaining months.
func (Duration) String ¶ added in v1.5.0
String returns a string representation of the Duration, combining the time, day, and month components into a human-readable format.
func (Duration) Sub ¶ added in v1.5.0
Sub subtracts another Duration from the current Duration, combining their time, day, and month components. The operation is similar to a vector subtraction, as they are independent unless a point in time is being calculated.
func (*Duration) UnmarshalText ¶ added in v1.5.0
UnmarshalText implements the encoding.TextUnmarshaler interface. Accepts an ISO 8601 duration string (e.g. "P1Y2M3DT4H5M6.789S"). Years are converted to months (1Y = 12M). A leading "-P" negates all components.
type Dyn ¶ added in v1.1.0
type Dyn struct{ Val any }
Dyn is a dynamic type that can hold any value type. When using with SQL, the column should be a type that can hold JSON data (JSONB, JSON, TEXT, etc).
func (Dyn) MarshalBSON ¶ added in v1.4.0
MarshalBSON implements the bson.Marshaler interface for use as a standalone document.
func (Dyn) MarshalBSONValue ¶ added in v1.1.0
MarshalBSONValue implements the bson.ValueMarshaler interface.
func (Dyn) MarshalBinary ¶ added in v1.1.0
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Dyn) MarshalJSON ¶ added in v1.1.0
MarshalJSON implements the json.Marshaler interface.
func (Dyn) MarshalText ¶ added in v1.1.0
MarshalText implements the encoding.TextMarshaler interface.
func (*Dyn) Scan ¶ added in v1.1.0
Scan implements the sql.Scanner interface.
func (*Dyn) UnmarshalBSON ¶ added in v1.4.0
UnmarshalBSON implements the bson.Unmarshaler interface for use as a standalone document.
func (*Dyn) UnmarshalBSONValue ¶ added in v1.1.0
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface.
func (*Dyn) UnmarshalBinary ¶ added in v1.1.0
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (*Dyn) UnmarshalJSON ¶ added in v1.1.0
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Dyn) UnmarshalText ¶ added in v1.1.0
UnmarshalText implements the encoding.TextUnmarshaler interface.
type KV ¶ added in v1.3.0
type KV[K comparable, V any] struct { Key K Val V }
KV represents a key-value pair with a comparable key and any value.
type KVs ¶ added in v1.3.0
type KVs[K comparable, V any] []KV[K, V]
KVs is a slice of KV pairs that provides utility methods for working with key-value collections.
func KVsFromMap ¶ added in v1.3.0
func KVsFromMap[K comparable, V any](m map[K]V) KVs[K, V]
KVsFromMap creates a KVs slice from the given map of key-value pairs.
func (KVs[K, V]) Keys ¶ added in v1.3.0
func (kvs KVs[K, V]) Keys() []K
Keys returns a slice of all keys in the KVs collection.
func (KVs[K, V]) Map ¶ added in v1.3.0
func (kvs KVs[K, V]) Map() map[K]V
Map converts the KVs collection into a map of key-value pairs.
func (KVs[K, V]) MarshalBSON ¶ added in v1.4.0
MarshalBSON implements the bson.Marshaler interface for use as a standalone document.
func (KVs[K, V]) MarshalBSONValue ¶ added in v1.4.0
MarshalBSONValue implements the bson.ValueMarshaler interface. KVs is serialized as a single BSON document: {k1: v1, k2: v2, ...}.
func (KVs[K, V]) MarshalJSON ¶ added in v1.4.0
MarshalJSON implements the json.Marshaler interface. KVs is serialized as a single JSON object: {"k1": v1, "k2": v2, ...}.
func (*KVs[K, V]) Scan ¶ added in v1.4.0
Scan implements the sql.Scanner interface. The column should be a type that can hold JSON data (JSONB, JSON, TEXT, etc).
func (*KVs[K, V]) UnmarshalBSON ¶ added in v1.4.0
UnmarshalBSON implements the bson.Unmarshaler interface for use as a standalone document.
func (*KVs[K, V]) UnmarshalBSONValue ¶ added in v1.4.0
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface.
func (*KVs[K, V]) UnmarshalJSON ¶ added in v1.4.0
UnmarshalJSON implements the json.Unmarshaler interface.
type MultiRange ¶ added in v1.4.0
func NewMultiRange ¶ added in v1.4.0
func NewMultiRange[O cmp.Ordered](src []Range[O]) MultiRange[O]
NewMultiRange takes a slice of [Range]s and returns a new MultiRange that is optimized by merging overlapping ranges.
func (MultiRange[O]) Contains ¶ added in v1.4.0
func (mr MultiRange[O]) Contains(val O) bool
Contains returns true if the given value is contained within any of the ranges in the multirange.
func (*MultiRange[O]) Scan ¶ added in v1.4.0
func (mr *MultiRange[O]) Scan(src any) error
Scan implements the sql.Scanner interface for MultiRange. It expects a PostgreSQL multirange literal such as {[1,5],[10,20]}.
func (MultiRange[O]) Value ¶ added in v1.4.0
func (mr MultiRange[O]) Value() (driver.Value, error)
Value implements the driver.Valuer interface for MultiRange. It returns a PostgreSQL multirange literal such as {[1,5],[10,20]}.
type Nil ¶
Nil is a type that can be used to represent a nil/nullable value. It implements most of the interfaces that are used to marshal and unmarshal values. For optional values that are not present, use Opt instead.
func NilFromPtr ¶
NilFromPtr creates a Nil from a pointer. If the pointer is nil, NotNil is false.
func (Nil[T]) MarshalBSONValue ¶
MarshalBSONValue implements the bson.ValueMarshaler interface.
func (Nil[T]) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (Nil[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (Nil[T]) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (Nil[T]) Ptr ¶
func (n Nil[T]) Ptr() *T
Ptr returns a pointer to the value if NotNil is true, otherwise nil. It uses a non-pointer receiver so that the modified pointer does not affect the original value.
func (*Nil[T]) Scan ¶
Scan implements the sql.Scanner interface.
func (*Nil[T]) UnmarshalBSONValue ¶
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface.
func (*Nil[T]) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (*Nil[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Nil[T]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.
type Opt ¶
Opt is a type that can be used to represent an optional value (present vs. absent). It should be used for fields that may be missing entirely in serialized form. For nullable values (present but null), use Nil instead. Combining both is possible: Opt[Nil[T]] represents a field that can be absent, null, or a value.
Use the `omitzero` JSON struct tag to omit absent fields. Example:
type Struct struct {
Field Opt[int] `json:"field,omitzero"`
}
func OptFromPtr ¶
OptFromPtr creates an Opt from a pointer. If the pointer is nil, the result is unset.
func (Opt[T]) IsZero ¶ added in v1.4.0
IsZero returns true when the value is absent. This enables the `omitzero` struct tag in encoding/json (Go 1.24+).
func (Opt[T]) MarshalBSONValue ¶ added in v1.4.0
MarshalBSONValue implements the bson.ValueMarshaler interface. Returns an error if the value is not set, to prevent silent null serialization. Use the `omitempty` struct tag to omit absent fields instead of marshaling them directly.
func (Opt[T]) MarshalJSON ¶ added in v1.4.0
MarshalJSON marshals the inner value transparently. Returns an error if the value is not set, to prevent silent null serialization. Use the `omitzero` struct tag to omit absent fields instead of marshaling them directly.
func (*Opt[T]) UnmarshalBSONValue ¶ added in v1.4.0
UnmarshalBSONValue implements the bson.ValueUnmarshaler interface. It always marks the Opt as set and delegates to the inner type.
type Ordered ¶ added in v1.4.0
Ordered is a type constraint that matches any type that implements a total ordering via the Compare method. Types implementing Ordered can use the OrderedRange and OrderedMultiRange types in this package. Additionally, if an Ordered type implements PGBoundMarshaler and PGBoundUnmarshaler, it can be used with PostgreSQL range types in a way that produces native range literals instead of JSON-encoded values.
type OrderedBound ¶ added in v1.4.0
type OrderedBound[O Ordered[O]] struct { Val O `json:"val" bson:"val"` // the bound value Exclusive bool `json:"exclusive,omitempty" bson:"exclusive,omitempty"` // whether the bound is exclusive (true) or inclusive (false); ignored when Unbounded is true Unbounded bool `json:"unbounded,omitempty" bson:"unbounded,omitempty"` // whether this is an infinite/unbounded bound (true) or a finite bound (false) }
OrderedBound represents one end of an OrderedRange with its value and whether it is exclusive. The zero value is inclusive (Exclusive == false) and bounded. When Unbounded is true the bound is ±∞; Val and Exclusive are ignored.
type OrderedMultiRange ¶ added in v1.4.0
type OrderedMultiRange[O Ordered[O]] []OrderedRange[O]
OrderedMultiRange represents a slice of non-overlapping [OrderedRange]s of the same type, sorted by lower bound.
func NewOrderedMultiRange ¶ added in v1.4.0
func NewOrderedMultiRange[O Ordered[O]](src []OrderedRange[O]) OrderedMultiRange[O]
NewOrderedMultiRange takes a slice of [OrderedRange]s and returns an OrderedMultiRange with overlapping or adjacent ranges merged.
func (OrderedMultiRange[O]) Contains ¶ added in v1.4.0
func (mr OrderedMultiRange[O]) Contains(val O) bool
Contains returns true if the given value is contained within any of the ranges in the multi range.
func (*OrderedMultiRange[O]) Scan ¶ added in v1.4.0
func (mr *OrderedMultiRange[O]) Scan(src any) error
Scan implements the sql.Scanner interface for OrderedMultiRange. If *O implements PGBoundUnmarshaler it parses a PostgreSQL multirange literal (e.g. {[1,5],[10,20]}). Otherwise it JSON-decodes a JSON array of range objects, suitable for text/jsonb columns.
func (OrderedMultiRange[O]) Value ¶ added in v1.4.0
func (mr OrderedMultiRange[O]) Value() (driver.Value, error)
Value implements the driver.Valuer interface for OrderedMultiRange. If O or *O implements PGBoundMarshaler it returns a PostgreSQL multirange literal (e.g. {[1,5],[10,20]}). Otherwise it JSON-encodes the multirange as a JSON array for storage in a text/jsonb column.
type OrderedRange ¶ added in v1.4.0
type OrderedRange[O Ordered[O]] struct { Lower OrderedBound[O] `json:"lower" bson:"lower"` // lower bound Upper OrderedBound[O] `json:"upper" bson:"upper"` // upper bound }
OrderedRange supports any Ordered type for in-process use, JSON, and BSON storage. The Scan and Value methods implement PostgreSQL range literals; note that string-based types have no corresponding PostgreSQL range type and should not be used with SQL. In the case of a type implementing PGBoundUnmarshaler/PGBoundMarshaler, those are used to parse/format the bounds of the range literal.
func (OrderedRange[O]) Contains ¶ added in v1.4.0
func (r OrderedRange[O]) Contains(val O) bool
Contains returns true if the given value is contained within the range.
func (*OrderedRange[O]) Scan ¶ added in v1.4.0
func (r *OrderedRange[O]) Scan(src any) error
Scan implements the sql.Scanner interface for OrderedRange. If *O implements PGBoundUnmarshaler it parses a PostgreSQL range literal (e.g. [min,max]). Otherwise it JSON-decodes a range object, suitable for text/jsonb columns.
func (OrderedRange[O]) Value ¶ added in v1.4.0
func (r OrderedRange[O]) Value() (driver.Value, error)
Value implements the driver.Valuer interface for OrderedRange. If O or *O implements PGBoundMarshaler it returns a PostgreSQL range literal (e.g. [min,max)). Otherwise it JSON-encodes the range for storage in a text/jsonb column.
type PGBoundMarshaler ¶ added in v1.4.0
PGBoundMarshaler is implemented by values that can serialize themselves as a PostgreSQL range bound literal (e.g. "2024-01-01 00:00:00+00" for tstzrange, "12345" for numrange). OrderedRange.Value and OrderedMultiRange.Value use this to produce a native PG range literal; types that don't implement it are JSON-encoded instead. In non-PostgreSQL but still SQL contexts, the same PostgreSQL range literal syntax is used but without relying on the database to parse it.
type PGBoundUnmarshaler ¶ added in v1.4.0
PGBoundUnmarshaler is implemented by values that can deserialize themselves from a PostgreSQL range bound literal. OrderedRange.Scan and OrderedMultiRange.Scan use this to parse a native PG range literal; types that don't implement it are JSON-decoded instead. In non-PostgreSQL but still SQL contexts, the same PostgreSQL range literal syntax is used but without relying on the database to parse it.
type Range ¶ added in v1.4.0
type Range[O cmp.Ordered] struct { Lower Bound[O] `json:"lower" bson:"lower"` // lower bound Upper Bound[O] `json:"upper" bson:"upper"` // upper bound }
Range supports any cmp.Ordered type for in-process use, JSON, and BSON storage. The Scan and Value methods implement PostgreSQL range literals; note that string-based types have no corresponding PostgreSQL range type and should not be used with SQL.
func (Range[O]) Contains ¶ added in v1.4.0
Contains returns true if the given value is contained within the range.
func (*Range[O]) Scan ¶ added in v1.4.0
Scan implements the sql.Scanner interface for Range. It expects a PostgreSQL range literal such as [min,max] or (min,max).
type Str64 ¶ added in v1.4.0
type Str64 []byte
Str64 is a named []byte type that encodes arbitrary byte slices as unpadded base64url strings (alphabet: A-Z, a-z, 0-9, -, _) via base64.RawURLEncoding. Because it implements encoding.TextMarshaler / encoding.TextUnmarshaler, it serialises automatically in JSON, and any other text-based transport or storage without extra conversion, while having the possibility of being saved as raw bytes, such as in Redis or a database.
Typical use cases:
Shorter UUID representation. A UUID is 16 bytes. As a standard hyphenated string it is 36 characters; as Str64 it is 22 characters — a 39 % reduction — while still being URL-safe.
Custom Variable-length identifiers. When a UUID's 128 bits of entropy is more (or less) than needed, Str64 works for any byte length: 8 bytes (64-bit) encodes to 11 chars, 32 bytes to 43 chars.
Compact string storage. If a string's content is limited to the Str64 alphabet (A-Z, a-z, 0-9, -, _), storing it as Str64 bytes saves 25 % of space (6 bits stored per byte instead of 8).
Loggable binary values. Any []byte field (hashes, tokens, raw IDs) becomes a human-readable, URL- and filename-safe string automatically in logs, JSON responses, and etc.
func Str64From ¶ added in v1.5.0
Str64From decodes a base64 RawURL string back to the original raw bytes.
func (Str64) MarshalText ¶ added in v1.4.0
MarshalText implements encoding.TextMarshaler.
func (Str64) String ¶ added in v1.4.0
String encodes the raw bytes to their base64 RawURL text representation.
func (*Str64) UnmarshalText ¶ added in v1.4.0
UnmarshalText implements encoding.TextUnmarshaler.
type URL ¶ added in v1.5.0
URL is a url.URL wrapper that serialises as a plain URL string in all common transports.
Because it implements encoding.TextMarshaler / encoding.TextUnmarshaler, JSON (and any other text-based format) represents the value as a quoted URL string with no extra nesting.
It also implements driver.Valuer and sql.Scanner so it can be stored directly in any SQL column that holds text (VARCHAR, TEXT, etc.), without manual .String() / url.Parse calls at the call site:
type Row struct {
ID int
Callback typx.URL
}
Use Nil[URL] for a nullable SQL column.
func (URL) MarshalText ¶ added in v1.5.0
MarshalText implements encoding.TextMarshaler.
func (*URL) Scan ¶ added in v1.5.0
Scan implements sql.Scanner. Accepts string or []byte from the driver. A nil src (SQL NULL) zeroes the receiver. Use Nil[URL] for a type that can be nil instead of zero.
func (*URL) UnmarshalText ¶ added in v1.5.0
UnmarshalText implements encoding.TextUnmarshaler.