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

model

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package model reads tanGO models: plain Go structs whose fields, through their tango:"…" struct tags, describe a table's columns, primary key, unique and indexed columns, and foreign keys.

Apps register their models with a Registry, which validates each one and records its ModelMeta and FieldMeta. The rest of tanGO (the store, the migration generator and the admin) works from that metadata. See https://tangoframework.com/docs/guides/models-and-tags/.

Index

Constants

View Source
const MaxVarcharLength = 10485760

MaxVarcharLength is the largest length tango:"varchar=n" accepts: the largest VARCHAR(n) PostgreSQL allows, so tanGO adds no ceiling of its own.

Variables

View Source
var ErrDuplicateModel = errors.New("tango: duplicate model name")

ErrDuplicateModel is returned by Registry.Register when a model with the same derived Name has already been registered.

View Source
var ErrEmbeddedField = errors.New("tango: embedded fields are not supported")

ErrEmbeddedField is returned by Registry.Register when a struct has an embedded (anonymous) field. Embedded fields are rejected outright, not flattened.

View Source
var ErrInvalidFieldTag = errors.New("tango: invalid field tag")

ErrInvalidFieldTag is returned by Registry.Register when a field's tango tag declares a string length wrongly: varchar without a positive length, a length beyond MaxVarcharLength, varchar together with text, a repeated varchar or text, or either on a field that is not a string. The error names the model and field.

View Source
var ErrMultiplePrimaryKeys = errors.New("tango: model has multiple primary key fields")

ErrMultiplePrimaryKeys is returned by Registry.Register when a struct has more than one field tagged tango:"pk".

View Source
var ErrNoPrimaryKey = errors.New("tango: model has no primary key field")

ErrNoPrimaryKey is returned by Registry.Register when a struct has no field tagged tango:"pk".

View Source
var ErrUnknownForeignKeyTarget = errors.New("tango: foreign key targets an unregistered model")

ErrUnknownForeignKeyTarget is returned by ValidateForeignKeys when a field's tango:"fk=X" tag names a model X that no installed app ever registered.

View Source
var ErrUnsupportedField = errors.New("tango: unsupported field type")

ErrUnsupportedField is returned by Registry.Register when a field's kind isn't supported by the metadata layer.

Functions

This section is empty.

Types

type FieldMeta

type FieldMeta struct {
	Name       string
	Type       reflect.Type
	PrimaryKey bool
	Unique     bool
	Indexed    bool
	Editable   bool
	ForeignKey string // target model name from tango:"fk=<Name>"; "" if not a foreign key
	// MaxLength is the most runes a bounded string may hold, from
	// tango:"varchar=n"; 0 means unbounded (a bare string or tango:"text").
	MaxLength int
	// JSON is true for a string field declared tango:"json": it holds JSON
	// text, which Store.Create and Store.Update check is valid. The column is
	// ordinary text.
	JSON bool
}

FieldMeta describes an exported Go struct field.

type ModelMeta

type ModelMeta struct {
	Name   string
	App    string
	Type   reflect.Type
	Fields []FieldMeta
}

ModelMeta describes a registered Go struct model.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry stores model metadata by Go type name.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty, ready-to-use model Registry.

func (*Registry) All

func (r *Registry) All() []ModelMeta

All returns every registered model's metadata, sorted by Name for a deterministic order.

func (*Registry) Get

func (r *Registry) Get(name string) (ModelMeta, bool)

Get returns model metadata by Go type name.

func (*Registry) Register

func (r *Registry) Register(value any) error

Register introspects a struct value and stores its model metadata. It returns an error, and registers nothing, if the struct's derived name is already registered, if it has zero or multiple primary key fields, if it has an embedded field, or if any field's kind is unsupported.

func (*Registry) SetCurrentApp

func (r *Registry) SetCurrentApp(name string)

SetCurrentApp records the name of the App currently registering models, populating ModelMeta.App for every model Register call that follows, until the next SetCurrentApp call. It is intended to be called by tango.Registry.RunRegistration immediately before invoking each app's Register callback, not by app authors directly.

func (*Registry) ValidateForeignKeys

func (r *Registry) ValidateForeignKeys() error

ValidateForeignKeys checks that every registered field's tango:"fk=X" tag names a model X that is also registered in this same Registry. It is intended to run once, after every installed app has finished registering (e.g. from tango.Check), not per-model at Register time — so InstalledApps order never constrains which app may declare a foreign key relative to the app that registers its target.

Jump to

Keyboard shortcuts

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