types

package
v1.5.107 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 6 Imported by: 1

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

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

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

Types

type Actor added in v1.5.103

type Actor struct {
	Source string `form:"source" json:"source"`
	Id     string `form:"id" json:"id"`
	Name   string `form:"name" json:"name"`
}

Actor is who wrote a record: where the write came from, and the person or program that made it.

Id is the writer's auth record id, the value `@request.auth.id` answers, so a rule can ask `createdBy.id = @request.auth.id`. Name is what a person reads.

func (Actor) AsMap added in v1.5.103

func (v Actor) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (Actor) IsZero added in v1.5.103

func (v Actor) IsZero() bool

IsZero reports whether the value holds nothing.

func (Actor) MarshalJSON added in v1.5.103

func (v Actor) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*Actor) Scan added in v1.5.103

func (v *Actor) Scan(value any) error

Scan reads a stored document; see Currency.Scan.

func (Actor) Value added in v1.5.103

func (v Actor) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

type Address added in v1.5.103

type Address struct {
	AddressStreet1  string   `form:"addressStreet1" json:"addressStreet1"`
	AddressStreet2  string   `form:"addressStreet2" json:"addressStreet2"`
	AddressCity     string   `form:"addressCity" json:"addressCity"`
	AddressState    string   `form:"addressState" json:"addressState"`
	AddressPostcode string   `form:"addressPostcode" json:"addressPostcode"`
	AddressCountry  string   `form:"addressCountry" json:"addressCountry"`
	AddressLat      *float64 `form:"addressLat" json:"addressLat"`
	AddressLng      *float64 `form:"addressLng" json:"addressLng"`
}

Address is a postal address, and where it is when that is known.

func (Address) AsMap added in v1.5.103

func (v Address) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (Address) IsZero added in v1.5.103

func (v Address) IsZero() bool

IsZero reports whether the value holds nothing.

func (Address) MarshalJSON added in v1.5.103

func (v Address) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*Address) Scan added in v1.5.103

func (v *Address) Scan(value any) error

Scan reads a stored document; see Currency.Scan.

func (Address) Value added in v1.5.103

func (v Address) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

type Currency added in v1.5.103

type Currency struct {
	AmountMicros int64  `form:"amountMicros" json:"amountMicros"`
	CurrencyCode string `form:"currencyCode" json:"currencyCode"`
}

Currency is an amount of one currency. The amount is in millionths of the currency's major unit (1.50 USD is 1_500_000), an integer so that money is never a float.

func (Currency) AsMap added in v1.5.103

func (v Currency) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (Currency) IsZero added in v1.5.103

func (v Currency) IsZero() bool

IsZero reports whether the value holds nothing.

func (Currency) MarshalJSON added in v1.5.103

func (v Currency) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*Currency) Scan added in v1.5.103

func (v *Currency) Scan(value any) error

Scan reads a stored document, a map, JSON text or bytes, or a value of the same type. nil and an empty document leave the value empty.

func (Currency) Value added in v1.5.103

func (v Currency) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

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 Emails added in v1.5.103

type Emails struct {
	PrimaryEmail     string   `form:"primaryEmail" json:"primaryEmail"`
	AdditionalEmails []string `form:"additionalEmails" json:"additionalEmails"`
}

Emails is a primary address and any others.

func (Emails) AsMap added in v1.5.103

func (v Emails) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (Emails) IsZero added in v1.5.103

func (v Emails) IsZero() bool

IsZero reports whether the value holds nothing.

func (Emails) MarshalJSON added in v1.5.103

func (v Emails) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*Emails) Scan added in v1.5.103

func (v *Emails) Scan(value any) error

Scan reads a stored document; see Currency.Scan.

func (Emails) Value added in v1.5.103

func (v Emails) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

type FullName added in v1.5.103

type FullName struct {
	FirstName string `form:"firstName" json:"firstName"`
	LastName  string `form:"lastName" json:"lastName"`
}

FullName is a person's name in two parts.

func (FullName) AsMap added in v1.5.103

func (v FullName) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (FullName) IsZero added in v1.5.103

func (v FullName) IsZero() bool

IsZero reports whether the value holds nothing.

func (FullName) MarshalJSON added in v1.5.103

func (v FullName) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*FullName) Scan added in v1.5.103

func (v *FullName) Scan(value any) error

Scan reads a stored document; see Currency.Scan.

func (FullName) Value added in v1.5.103

func (v FullName) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

type GeoPoint added in v0.29.4

type GeoPoint struct {
	Lon float64 `form:"lon" json:"lon"`
	Lat float64 `form:"lat" json:"lat"`
}

GeoPoint defines a struct for storing geo coordinates as serialized json object (e.g. {lon:0,lat:0}).

Note: using object notation and not a plain array to avoid the confusion as there doesn't seem to be a fixed standard for the coordinates order.

func (GeoPoint) AsMap added in v0.29.4

func (p GeoPoint) AsMap() map[string]any

AsMap implements [core.mapExtractor] and returns a value suitable to be used in an API rule expression.

func (*GeoPoint) Scan added in v0.29.4

func (p *GeoPoint) Scan(value any) error

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

The value argument could be nil (no-op), another GeoPoint instance, map or serialized json object with lat-lon props.

func (GeoPoint) String added in v0.29.4

func (p GeoPoint) String() string

String returns the string representation of the current GeoPoint instance.

func (GeoPoint) Value added in v0.29.4

func (p GeoPoint) 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.

type Link struct {
	URL   string `form:"url" json:"url"`
	Label string `form:"label" json:"label"`
}

Link is one URL and the label a person reads for it.

type Links struct {
	PrimaryLinkUrl   string `form:"primaryLinkUrl" json:"primaryLinkUrl"`
	PrimaryLinkLabel string `form:"primaryLinkLabel" json:"primaryLinkLabel"`
	SecondaryLinks   []Link `form:"secondaryLinks" json:"secondaryLinks"`
}

Links is a primary link and any others.

func (Links) AsMap added in v1.5.103

func (v Links) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (Links) IsZero added in v1.5.103

func (v Links) IsZero() bool

IsZero reports whether the value holds nothing.

func (Links) MarshalJSON added in v1.5.103

func (v Links) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*Links) Scan added in v1.5.103

func (v *Links) Scan(value any) error

Scan reads a stored document; see Currency.Scan.

func (Links) Value added in v1.5.103

func (v Links) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

type Phone added in v1.5.103

type Phone struct {
	Number      string `form:"number" json:"number"`
	CountryCode string `form:"countryCode" json:"countryCode"`
	CallingCode string `form:"callingCode" json:"callingCode"`
}

Phone is one number: the digits, the ISO 3166-1 alpha-2 country it is dialled in and that country's calling code ("+1").

type Phones added in v1.5.103

type Phones struct {
	PrimaryPhoneNumber      string  `form:"primaryPhoneNumber" json:"primaryPhoneNumber"`
	PrimaryPhoneCountryCode string  `form:"primaryPhoneCountryCode" json:"primaryPhoneCountryCode"`
	PrimaryPhoneCallingCode string  `form:"primaryPhoneCallingCode" json:"primaryPhoneCallingCode"`
	AdditionalPhones        []Phone `form:"additionalPhones" json:"additionalPhones"`
}

Phones is a primary number and any others.

func (Phones) AsMap added in v1.5.103

func (v Phones) AsMap() map[string]any

AsMap returns the parts, for a rule expression to reach into.

func (Phones) IsZero added in v1.5.103

func (v Phones) IsZero() bool

IsZero reports whether the value holds nothing.

func (Phones) MarshalJSON added in v1.5.103

func (v Phones) MarshalJSON() ([]byte, error)

MarshalJSON writes null for an empty value.

func (*Phones) Scan added in v1.5.103

func (v *Phones) Scan(value any) error

Scan reads a stored document; see Currency.Scan.

func (Phones) Value added in v1.5.103

func (v Phones) Value() (driver.Value, error)

Value writes NULL for an empty value and the JSON document otherwise.

Jump to

Keyboard shortcuts

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