structs

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package structs walks Go model structs into SQL column field metadata.

[builder.TFrom], the genx model generator, [scanner.Scan], and helper insert helpers all rely on this package to discover `db`-tagged fields, including nested embeds such as AutoIncID / Rel* / Meta / State.

FieldsFor

fields := structs.FieldsFor(typx.NewRType(reflect.TypeFor[User]()))
// f_id, f_user_id, ... with Loc indexes into the struct tree

FieldsFor / FieldsSeqFor walk an exported struct type (results are cached per type). Rules:

  • skip unexported fields and `db:"-"`
  • expand anonymous (or same-named) struct embeds that are not driver.Valuer
  • column name from `db` tag name, else lowercased field name
  • parse column options into Field.ColumnDef (width, autoinc, ...)
  • keep json/name tags on Field.NameTag

Each Field carries ColumnName, FieldName, Type, Loc (field index path), and ColumnDef for DDL / scan / insert.

TableFields

tfs := structs.TableFields(&user)

TableFields / TableFieldsSeq bind FieldsFor to a live value: each TableField has the Field metadata, the addressable TableField.Value, and TableName when v implements Model. Deprecated columns (`db:",deprecated=..."`) are omitted.

See ExampleFieldsFor for embed flattening output.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldsSeqFor

func FieldsSeqFor(t typx.Type) iter.Seq[*Field]

func TableFieldsSeq

func TableFieldsSeq(v any) iter.Seq[*TableField]

Types

type Field

type Field struct {
	ColumnName string
	FieldName  string
	Type       typx.Type
	Field      typx.StructField
	Flag       *reflectx.Flag
	NameTag    reflectx.Tag
	Loc        []int
	ColumnDef  def.ColumnDef
}

func FieldsFor

func FieldsFor(t typx.Type) []*Field
Example
fields := structs.FieldsFor(typx.NewRType(reflect.TypeFor[User]()))

for _, f := range fields {
	fmt.Printf("%-10s %-6s %v\n", f.ColumnName, f.Type.Name(), f.Loc)
}

seq := structs.FieldsSeqFor(typx.NewRType(reflect.TypeFor[User]()))
for f := range seq {
	fmt.Printf("%-10s %-6s %v\n", f.ColumnName, f.Type.Name(), f.Loc)
}
Output:
f_id       uint64 [0 0]
f_user_id  uint64 [1 0]
f_org_id   uint64 [2 0]
f_name     string [3 0]
f_age      int    [3 1]
f_id       uint64 [0 0]
f_user_id  uint64 [1 0]
f_org_id   uint64 [2 0]
f_name     string [3 0]
f_age      int    [3 1]

func (*Field) FieldValue

func (f *Field) FieldValue(v reflect.Value) reflect.Value

func (*Field) ModelValue

func (f *Field) ModelValue() reflect.Value

func (*Field) Value

func (f *Field) Value(v reflect.Value) any

type TableField

type TableField struct {
	Field     Field
	Value     reflect.Value
	TableName string
}

func TableFields

func TableFields(v any) []*TableField

Jump to

Keyboard shortcuts

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