types

package
v0.23.0-rc9 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: MIT Imports: 6 Imported by: 242

Documentation

Overview

Package types implements some commonly used db serializable types like datetime, json, etc.

Index

Constants

View Source
const DefaultDateLayout = "2006-01-02 15:04:05.000Z"

DefaultDateLayout specifies the default app date strings layout.

Variables

This section is empty.

Functions

func Pointer added in v0.8.0

func Pointer[T any](val T) *T

Pointer is a generic helper that returns val as *T.

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

func ParseDateTime(value any) (DateTime, error)

ParseDateTime creates a new DateTime from the provided value (could be cast.ToTime supported string, time.Time, etc.).

func (DateTime) Add

func (d DateTime) Add(duration time.Duration) DateTime

Add returns a new DateTime based on the current DateTime + the specified duration.

func (DateTime) AddDate

func (d DateTime) AddDate(years, months, days int) DateTime

AddDate returns a new DateTime based on the current one + duration.

It follows the same rules as time.AddDate.

func (DateTime) After

func (d DateTime) After(u DateTime) bool

After reports whether the current DateTime instance is after u.

func (DateTime) Before

func (d DateTime) Before(u DateTime) bool

Before reports whether the current DateTime instance is before u.

func (DateTime) Compare

func (d DateTime) Compare(u DateTime) int

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

func (d DateTime) Equal(u DateTime) bool

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) IsZero

func (d DateTime) IsZero() bool

IsZero checks whether the current DateTime instance has zero time value.

func (DateTime) MarshalJSON

func (d DateTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*DateTime) Scan

func (d *DateTime) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current DateTime instance.

func (DateTime) String

func (d DateTime) String() 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

func (d DateTime) Sub(u DateTime) time.Duration

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) Time

func (d DateTime) Time() time.Time

Time returns the internal time.Time instance.

func (DateTime) Unix

func (d DateTime) Unix() int64

Unix returns the current DateTime as a Unix time, aka. the number of seconds elapsed since January 1, 1970 UTC.

func (*DateTime) UnmarshalJSON

func (d *DateTime) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (DateTime) Value

func (d DateTime) Value() (driver.Value, error)

Value implements the driver.Valuer 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

func (m JSONArray[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*JSONArray[T]) Scan

func (m *JSONArray[T]) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current JSONArray[T] instance.

func (JSONArray[T]) String

func (m JSONArray[T]) String() string

String returns the string representation of the current json array.

func (JSONArray[T]) Value

func (m JSONArray[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type JSONMap

type JSONMap[T any] map[string]T

JSONMap defines a map that is safe for json and db read/write.

func (JSONMap[T]) Get

func (m JSONMap[T]) Get(key string) T

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

func (m JSONMap[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*JSONMap[T]) Scan

func (m *JSONMap[T]) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current JSONMap[T] instance.

func (JSONMap[T]) Set

func (m JSONMap[T]) Set(key string, value T)

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).

func (JSONMap[T]) String

func (m JSONMap[T]) String() string

String returns the string representation of the current json map.

func (JSONMap[T]) Value

func (m JSONMap[T]) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type JSONRaw

type JSONRaw []byte

JSONRaw defines a json value type that is safe for db read/write.

func ParseJSONRaw

func ParseJSONRaw(value any) (JSONRaw, error)

ParseJSONRaw creates a new JSONRaw instance from the provided value (could be JSONRaw, int, float, string, []byte, etc.).

func (JSONRaw) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*JSONRaw) Scan

func (j *JSONRaw) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current JSONRaw instance.

func (JSONRaw) String

func (j JSONRaw) String() string

String returns the current JSONRaw instance as a json encoded string.

func (*JSONRaw) UnmarshalJSON

func (j *JSONRaw) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (JSONRaw) Value

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

Value implements the driver.Valuer interface.

Jump to

Keyboard shortcuts

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