casts

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package casts provides a small attribute-casting layer the ORM applies on reads and writes. Each cast maps SQL ↔ Go through a registered Cast. Built-in casts (and their aliases): json (jsonb), bool (boolean), int (integer), float (double), date, datetime.

Custom casts can be registered with Register() at init time.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, c Cast)

Register installs a cast under the given name.

Types

type BoolCast

type BoolCast struct{}

BoolCast normalizes 0/1/"t"/"f"/"true"/"false" to bool.

func (BoolCast) FromDB

func (BoolCast) FromDB(src any, dst any) error

FromDB writes the boolean interpretation of src into dst (*bool).

func (BoolCast) ToDB

func (BoolCast) ToDB(src any) (any, error)

ToDB returns the raw bool — drivers normalize it for the dialect.

type Cast

type Cast interface {
	FromDB(src any, dst any) error
	ToDB(src any) (any, error)
}

Cast converts a value coming back from the DB (FromDB) and going to the DB (ToDB). Implementations should be allocation-conscious; they may be called many times per row.

func Get

func Get(name string) Cast

Get returns the cast with the given name, or nil.

type DateCast

type DateCast struct{}

DateCast renders/parses values as YYYY-MM-DD.

func (DateCast) FromDB

func (DateCast) FromDB(src any, dst any) error

FromDB parses src into *time.Time.

func (DateCast) ToDB

func (DateCast) ToDB(src any) (any, error)

ToDB renders time.Time as a date string. A nil source, a nil *time.Time, or the zero time render as NULL.

type DateTimeCast added in v0.20.0

type DateTimeCast struct{}

DateTimeCast renders/parses values as RFC3339 timestamps.

func (DateTimeCast) FromDB added in v0.20.0

func (DateTimeCast) FromDB(src any, dst any) error

FromDB parses src into *time.Time, accepting RFC3339 and the common "2006-01-02 15:04:05" SQL datetime layout.

func (DateTimeCast) ToDB added in v0.20.0

func (DateTimeCast) ToDB(src any) (any, error)

ToDB renders time.Time as an RFC3339 string. A nil source, a nil *time.Time, or the zero time render as NULL.

type FloatCast added in v0.20.0

type FloatCast struct{}

FloatCast parses string/numeric into float64.

func (FloatCast) FromDB added in v0.20.0

func (FloatCast) FromDB(src any, dst any) error

FromDB parses src into *float64, accepting any numeric kind, []byte and string sources.

func (FloatCast) ToDB added in v0.20.0

func (FloatCast) ToDB(src any) (any, error)

ToDB returns src as-is.

type IntCast

type IntCast struct{}

IntCast parses string/numeric into int64.

func (IntCast) FromDB

func (IntCast) FromDB(src any, dst any) error

FromDB parses src into *int64. It accepts every signed/unsigned integer kind, integral floats (e.g. SQLite returns float64 for aggregates), and numeric strings — mirroring database/sql's convertAssign behaviour.

func (IntCast) ToDB

func (IntCast) ToDB(src any) (any, error)

ToDB returns src as-is.

type JSONCast

type JSONCast struct{}

JSONCast serializes/deserializes any value via encoding/json.

func (JSONCast) FromDB

func (JSONCast) FromDB(src any, dst any) error

FromDB unmarshals src ([]byte / string) into dst (pointer).

func (JSONCast) ToDB

func (JSONCast) ToDB(src any) (any, error)

ToDB marshals src to JSON bytes.

Jump to

Keyboard shortcuts

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