Documentation ¶
Overview ¶
Package types implements some commonly used db serializable types like datetime, json, etc.
Index ¶
- Constants
- func Pointer[T any](val T) *T
- type DateTime
- func (d DateTime) Add(duration time.Duration) DateTime
- func (d DateTime) AddDate(years, months, days int) DateTime
- func (d DateTime) After(u DateTime) bool
- func (d DateTime) Before(u DateTime) bool
- func (d DateTime) Compare(u DateTime) int
- func (d DateTime) Equal(u DateTime) bool
- func (d DateTime) IsZero() bool
- func (d DateTime) MarshalJSON() ([]byte, error)
- func (d *DateTime) Scan(value any) error
- func (d DateTime) String() string
- func (d DateTime) Sub(u DateTime) time.Duration
- func (d DateTime) Time() time.Time
- func (d DateTime) Unix() int64
- func (d *DateTime) UnmarshalJSON(b []byte) error
- func (d DateTime) Value() (driver.Value, error)
- type JSONArray
- type JSONMap
- type JSONRaw
Constants ¶
const DefaultDateLayout = "2006-01-02 15:04:05.000Z"
DefaultDateLayout specifies the default app date strings layout.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DateTime ¶
type DateTime struct {
// contains filtered or unexported fields
}
DateTime represents a time.Time instance in UTC that is wrapped and serialized using the app default date layout.
func NowDateTime ¶
func NowDateTime() DateTime
NowDateTime returns new DateTime instance with the current local time.
func ParseDateTime ¶
ParseDateTime creates a new DateTime from the provided value (could be cast.ToTime supported string, time.Time, etc.).
func (DateTime) Add ¶
Add returns a new DateTime based on the current DateTime + the specified duration.
func (DateTime) AddDate ¶
AddDate returns a new DateTime based on the current one + duration.
It follows the same rules as time.AddDate.
func (DateTime) Compare ¶
Compare compares the current DateTime instance with u. If the current instance is before u, it returns -1. If the current instance is after u, it returns +1. If they're the same, it returns 0.
func (DateTime) Equal ¶
Equal reports whether the current DateTime and u represent the same time instant. Two DateTime can be equal even if they are in different locations. For example, 6:00 +0200 and 4:00 UTC are Equal.
func (DateTime) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*DateTime) Scan ¶
Scan implements [sql.Scanner] interface to scan the provided value into the current DateTime instance.
func (DateTime) String ¶
String serializes the current DateTime instance into a formatted UTC date string.
The zero value is serialized to an empty string.
func (DateTime) Sub ¶
Sub returns a time.Duration by subtracting the specified DateTime from the current one.
If the result exceeds the maximum (or minimum) value that can be stored in a time.Duration, the maximum (or minimum) duration will be returned.
func (DateTime) Unix ¶
Unix returns the current DateTime as a Unix time, aka. the number of seconds elapsed since January 1, 1970 UTC.
func (*DateTime) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type JSONArray ¶
type JSONArray[T any] []T
JSONArray defines a slice that is safe for json and db read/write.
func (JSONArray[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*JSONArray[T]) Scan ¶
Scan implements [sql.Scanner] interface to scan the provided value into the current JSONArray[T] instance.
type JSONMap ¶
JSONMap defines a map that is safe for json and db read/write.
func (JSONMap[T]) Get ¶
Get retrieves a single value from the current JSONMap[T].
This helper was added primarily to assist the goja integration since custom map types don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).
func (JSONMap[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*JSONMap[T]) Scan ¶
Scan implements [sql.Scanner] interface to scan the provided value into the current JSONMap[T] instance.
func (JSONMap[T]) Set ¶
Set sets a single value in the current JSONMap[T].
This helper was added primarily to assist the goja integration since custom map types don't have direct access to the map keys (https://pkg.go.dev/github.com/dop251/goja#hdr-Maps_with_methods).
type JSONRaw ¶
type JSONRaw []byte
JSONRaw defines a json value type that is safe for db read/write.
func ParseJSONRaw ¶
ParseJSONRaw creates a new JSONRaw instance from the provided value (could be JSONRaw, int, float, string, []byte, etc.).
func (JSONRaw) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*JSONRaw) Scan ¶
Scan implements [sql.Scanner] interface to scan the provided value into the current JSONRaw instance.
func (*JSONRaw) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.