fields

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package fields provides [tidal.Model] column types for JSON, string arrays, and normalized timestamps.

Use them as fields on [tidal.Model] structs. They implement database/sql.Scanner and database/sql/driver.Valuer, so [tidal.CRUD] queries read and write them without extra conversion code. Backing columns should store JSON (JSONB, BYTEA, BLOB, or TEXT depending on your database).

Example:

type Document struct {
	tidal.BaseModel
	Metadata fields.JSONB
	Tags     fields.StringArray
	SeenAt   fields.Timestamp
}

Index

Constants

View Source
const ISO8601Milli = "2006-01-02T15:04:05.000Z07:00"

Variables

View Source
var JSONNull = []byte("null")

JSONNull is the JSON literal null as bytes.

Functions

This section is empty.

Types

type JSONB

type JSONB json.RawMessage

JSONB stores raw JSON in a NOT NULL column. SQL NULL and JSON "null" scan as nil.

func (JSONB) Equal added in v1.2.0

func (j JSONB) Equal(other JSONB) bool

Equal compares JSON values after normalization so key order differences do not matter.

func (JSONB) IsNull

func (j JSONB) IsNull() bool

IsNull reports whether the value is empty or the JSON literal null.

func (*JSONB) MarshalFrom

func (j *JSONB) MarshalFrom(src any) (err error)

MarshalFrom JSON-encodes src into the field. A nil src clears the field.

func (JSONB) Normalize

func (j JSONB) Normalize() []byte

Normalizes the JSON bytes for hashing: values round-trip through encoding/json so object keys are emitted in sorted order. Invalid JSON is returned unchanged (copy). Numbers become float64 when decoded as any; empty input returns nil.

func (*JSONB) Scan

func (j *JSONB) Scan(src any) error

Scan implements database/sql.Scanner.

func (JSONB) UnmarshalTo

func (j JSONB) UnmarshalTo(dst any) error

UnmarshalTo decodes the JSON into dst. A nil or empty value is a no-op.

func (JSONB) Value

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

Value implements database/sql/driver.Valuer.

type NullJSONB

type NullJSONB struct {
	Valid bool
	JSONB JSONB
}

NullJSONB stores JSON in a nullable column. Check Valid after scanning.

func (NullJSONB) Equal added in v1.2.0

func (j NullJSONB) Equal(other NullJSONB) bool

Equal compares nullable JSON values with SQL NULL normalization.

func (*NullJSONB) MarshalFrom

func (j *NullJSONB) MarshalFrom(src any) (err error)

MarshalFrom JSON-encodes src and sets Valid. Nil or JSON null becomes SQL NULL.

func (*NullJSONB) Scan

func (n *NullJSONB) Scan(src any) (err error)

Scan implements database/sql.Scanner.

func (NullJSONB) UnmarshalTo

func (j NullJSONB) UnmarshalTo(dst any) error

UnmarshalTo decodes the JSON when Valid is true.

func (NullJSONB) Value

func (n NullJSONB) Value() (driver.Value, error)

Value implements database/sql/driver.Valuer.

type NullStringArray

type NullStringArray struct {
	Valid       bool
	StringArray StringArray
}

NullStringArray stores a list of strings in a nullable column. Check Valid after scanning.

func (NullStringArray) Equal added in v1.2.0

func (n NullStringArray) Equal(other NullStringArray) bool

Equal compares nullable arrays, normalizing empty arrays to SQL NULL semantics.

func (*NullStringArray) Scan

func (n *NullStringArray) Scan(src any) (err error)

Scan implements database/sql.Scanner.

func (NullStringArray) Value

func (n NullStringArray) Value() (driver.Value, error)

Value implements database/sql/driver.Valuer.

type StringArray

type StringArray []string

StringArray stores a list of strings as a JSON array in a NOT NULL column.

func (StringArray) Equal added in v1.2.0

func (s StringArray) Equal(other StringArray) bool

Equal compares two arrays, treating nil and empty arrays as equivalent.

func (*StringArray) Scan

func (s *StringArray) Scan(src any) (err error)

Scan implements database/sql.Scanner.

func (StringArray) Value

func (s StringArray) Value() (driver.Value, error)

Value implements database/sql/driver.Valuer.

type Timestamp added in v1.5.0

type Timestamp struct {
	// contains filtered or unexported fields
}

Timestamp wraps a time.Time value in order to ensure that the underlying database always has a timestamp stored in the UTC timezone and truncated to millisecond precision. It also treats zero valued timestamps as NULL values so there is no need for a NullTime type. This timestamp is not well suited for all use cases, but generally can unify timestamp behavior across different databases.

func Time added in v1.5.0

func Time(ts time.Time) Timestamp

Creates a new Timestamp with the given time.Time value.

func TimeNow added in v1.5.1

func TimeNow() Timestamp

Returns a new Timestamp with the current time.

func TimeParse added in v1.5.1

func TimeParse(layout, value string) (Timestamp, error)

Parses a time string in the given layout into a Timestamp.

func (Timestamp) Add added in v1.5.0

func (t Timestamp) Add(d time.Duration) Timestamp

Returns a new normalized Timestamp by adding the given duration.

func (Timestamp) After added in v1.5.1

func (t Timestamp) After(other Timestamp) bool

Returns true if the timestamp is after the other timestamp.

func (Timestamp) Before added in v1.5.1

func (t Timestamp) Before(other Timestamp) bool

Returns true if the timestamp is before the other timestamp.

func (Timestamp) Compare added in v1.5.0

func (t Timestamp) Compare(other Timestamp) int

Returns the result of comparing the two timestamps (0 if equal, -1 if less, 1 if greater).

func (Timestamp) Equal added in v1.5.0

func (t Timestamp) Equal(other Timestamp) bool

Returns true if the timestamp is equal to the other timestamp.

func (Timestamp) Format added in v1.5.1

func (t Timestamp) Format(layout string) string

Returns the timestamp as a formatted string.

func (Timestamp) IsZero added in v1.5.0

func (t Timestamp) IsZero() bool

Returns true if the timestamp is zero (considered null).

func (Timestamp) MarshalJSON added in v1.5.0

func (t Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) Now added in v1.5.0

func (t *Timestamp) Now()

Sets the timestamp to the current UTC time.

func (*Timestamp) Scan added in v1.5.0

func (t *Timestamp) Scan(src any) (err error)

func (*Timestamp) Set added in v1.5.0

func (t *Timestamp) Set(ts time.Time)

Set the timestamp to the given time in the UTC timezone, truncated to millisecond precision.

func (Timestamp) Since added in v1.5.1

func (t Timestamp) Since(other Timestamp) time.Duration

Returns the duration since the other timestamp. Equivalent to Timestamp.Sub.

func (Timestamp) String added in v1.5.0

func (t Timestamp) String() string

Returns the timestamp as a string in the RFC3339 format.

func (Timestamp) Sub added in v1.5.0

func (t Timestamp) Sub(other Timestamp) time.Duration

Returns the duration between the two timestamps.

func (Timestamp) Time added in v1.5.0

func (t Timestamp) Time() time.Time

Returns the underlying time.Time value (already normalized when set).

func (Timestamp) UTC added in v1.5.1

func (t Timestamp) UTC() time.Time

Returns the underlying time.Time value in the UTC timezone.

func (Timestamp) Unix added in v1.5.1

func (t Timestamp) Unix() int64

Returns the unix epoch seconds of the timestamp.

func (Timestamp) UnixMilli added in v1.5.1

func (t Timestamp) UnixMilli() int64

Returns the unix milliseconds of the timestamp.

func (*Timestamp) UnmarshalJSON added in v1.5.0

func (t *Timestamp) UnmarshalJSON(data []byte) (err error)

func (Timestamp) Until added in v1.5.1

func (t Timestamp) Until(other Timestamp) time.Duration

Returns the duration until the other timestamp. Equivalent to argument-inverted Timestamp.Sub.

func (Timestamp) Value added in v1.5.0

func (t Timestamp) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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