Help us shape the future of Go! Take the 2026 Go Developer Survey

reflectutil

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package reflectutil provides a thread-safe, allocation-amortized reflection cache. Models are parsed once on first use; subsequent accesses are O(1).

It is the single source of truth for: column name resolution, primary-key discovery, fillable/hidden lists, cast tags, and embedded base-model handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssignScanned added in v0.27.0

func AssignScanned(fv reflect.Value, raw any) error

AssignScanned writes a value scanned from the DB (as an any holding the driver's concrete type, or nil for NULL) into the destination field. NULL coalesces to the field's Go zero value. Fields implementing sql.Scanner (sql.NullString, decimal/uuid types, ...) receive the raw value through their Scan method; everything else goes through ConvertAssign.

func ConvertAssign added in v0.27.0

func ConvertAssign(dest, src any) error

ConvertAssign converts src (a driver value: int64, float64, bool, []byte, string or time.Time) into the pointer dest. It mirrors the subset of database/sql's convertAssign that the ORM relies on, with a reflection fallback for numeric/string kinds so non-default field types (int, uint, float32, named string types, ...) all work. Textual numbers and booleans ([]byte on MySQL's text protocol) are parsed.

func IndirectValue

func IndirectValue(v reflect.Value) reflect.Value

IndirectValue dereferences a Value through pointers.

func ToInt64 added in v0.27.0

func ToInt64(src any) (int64, bool)

ToInt64 converts a driver value (int64, int, float64, []byte, string) to int64.

Types

type Field

type Field struct {
	// Name is the Go struct field name (e.g. "CreatedAt").
	Name string
	// Column is the database column name (e.g. "created_at"), resolved from
	// `column:"..."` tag or snake_case of Name.
	Column string
	// Index is the index path for reflect.Value.FieldByIndex.
	Index []int
	// Type is the field's Go type.
	Type reflect.Type
	// IsPrimary indicates the field is (part of) the primary key.
	IsPrimary bool
	// IsAutoIncrement marks an integer primary key as auto-increment.
	IsAutoIncrement bool
	// IsCreatedAt / IsUpdatedAt mark timestamp columns.
	IsCreatedAt bool
	IsUpdatedAt bool
	// IsDeletedAt marks the soft-delete timestamp column.
	IsDeletedAt bool
	// IsZeroOnInsert means "send NULL/skip when zero on insert".
	IsZeroOnInsert bool
	// Hidden columns are never serialized via ToMap.
	Hidden bool
	// Fillable indicates the column accepts mass assignment.
	Fillable bool
	// Cast is the registered cast name (e.g. "json", "date", "bool").
	Cast string
	// HasDefault is true when the column has a struct-tag default.
	HasDefault bool
	// Default is the literal default value as written in the tag.
	Default string
	// Nullable as declared on the field tag.
	Nullable bool
	// Skip means: ignore this field for persistence (`column:"-"`).
	Skip bool
	// IsRelation marks the field as a relation (slice of struct / pointer to
	// struct) discovered automatically; relations are not persisted as columns.
	IsRelation bool
}

Field describes one persistent struct field.

type Schema

type Schema struct {
	Type   reflect.Type
	Table  string
	Fields []*Field

	PrimaryKey *Field
	CreatedAt  *Field
	UpdatedAt  *Field
	DeletedAt  *Field
	// contains filtered or unexported fields
}

Schema describes a parsed model type.

func Parse

func Parse(v any) *Schema

Parse returns the cached Schema for v's underlying struct type. It transparently dereferences pointers and slices.

func ParseType

func ParseType(t reflect.Type) *Schema

ParseType returns the cached Schema for a reflect.Type directly.

func (*Schema) Columns

func (s *Schema) Columns() []string

Columns returns all persistent column names (excludes relations & skips).

func (*Schema) FieldByColumn

func (s *Schema) FieldByColumn(col string) *Field

FieldByColumn returns the Field with matching column name, or nil.

func (*Schema) FieldByName

func (s *Schema) FieldByName(name string) *Field

FieldByName returns the Field with matching Go name, or nil.

func (*Schema) InsertableColumns

func (s *Schema) InsertableColumns(v reflect.Value) ([]string, []any)

InsertableColumns returns columns to include on insert (skips relations, skip-marked, and zero-valued auto fields).

func (*Schema) SoftDeletes

func (s *Schema) SoftDeletes() bool

SoftDeletes reports whether the model carries a soft-delete timestamp column (a DeletedAt/deleted_at column, typically via embedding orm.SoftDeletes).

Jump to

Keyboard shortcuts

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