casts

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 5 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: json, bool, int, float, 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.

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.

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