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 ¶
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 ¶
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.
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.
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.
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".
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".
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.
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 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 ¶
All returns every registered model's metadata, sorted by Name for a deterministic order.
func (*Registry) Register ¶
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 ¶
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 ¶
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.