reflectmap

package
v0.0.0-...-6f40dae Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package reflectmap maps struct fields to database columns, the single definition of the name-mapping rules (sqlkit tag, exact name, snake_case, flattened embeds) shared by result scanning, INSERT value extraction, and the ORM preloader.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fields

func Fields(t reflect.Type) map[string]Field

Fields maps lowercased column names to the field that maps to each. Fields register under their `sqlkit` tag name when present, otherwise under both the lowercased and snake_case forms of the field name. Embedded structs are flattened (except types that scan as a single column, such as time.Time and sql.Scanner implementations); shallower fields win conflicts.

func FieldsByColumn

func FieldsByColumn(t reflect.Type) map[string][]int

FieldsByColumn is the path-only view of Fields, kept for callers that need only a field's location and not its tag options.

func MapsAsStruct

func MapsAsStruct(t reflect.Type) bool

MapsAsStruct reports whether values of t are filled field-by-field rather than scanned as a single column.

func SnakeCase

func SnakeCase(name string) string

SnakeCase converts a Go field name to snake_case: UserID -> user_id, CreatedAt -> created_at, HTTPCode -> http_code.

Types

type Field

type Field struct {
	// Path is the field index path, for reflect.Value.FieldByIndex.
	Path []int
	// ReadOnly marks a column scanned on read but never written by INSERT or
	// UPDATE — the `readonly` option, for database-generated columns
	// (identity keys, computed columns, trigger-maintained timestamps).
	ReadOnly bool
	// JSON marks a column whose value is the JSON encoding of the field — the
	// `json` (or `jsonb`) option, so a struct/map/slice field round-trips
	// through a JSON/JSONB column.
	JSON bool
	// Version marks the optimistic-locking version column — the `version`
	// option; SetStruct bumps it and guards the UPDATE with its current value.
	Version bool
	// Touch marks an app-maintained timestamp column — the `touch` option; the
	// field is set to CURRENT_TIMESTAMP on every INSERT and implicit SetStruct
	// UPDATE rather than copied from the struct.
	Touch bool
}

Field describes how a struct field maps to a column, carrying both its index path and the write-time options declared after the column name in the `sqlkit` tag (`sqlkit:"name,opt,..."`).

Jump to

Keyboard shortcuts

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