r3tag

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package r3tag provides struct tag parsing for r3 entities.

It handles the `r3` tag (primary) with `db` tag as fallback for column mapping, and finally the `gorm` tag (column:/primaryKey) so gorm-tagged models resolve to the same physical columns the GORM driver binds.

Column tag syntax (r3 or db):

r3:"column_name"
r3:"column_name,pk"
r3:"column_name,pk,soft_delete"
r3:"-"               // skip this field
r3:"soft_delete"      // column name derived from field name
db:"column_name,pk"  // fallback when no r3 tag
gorm:"column:name"   // fallback when neither r3 nor db names the column

Relation tag syntax (r3 only):

r3:"rel:has-many,fk:city_id"
r3:"rel:belongs-to,fk:city_id"
r3:"rel:has-many,fk:city_id,table:translations"
r3:"rel:many-to-many,join:city_tags,fk:city_id,ref:tag_id,order:sort_order"

order: names an integer column on the M2M join table that persists the slice order: sync writes each element's index into it, preload orders by it.

Capability flags (additive — they tighten the permissive defaults, never widen):

r3:"secret_token,no-filter,no-sort,no-output" // hidden from filter/sort/select
r3:"population,readonly"                        // not creatable, not mutable
r3:"slug,immutable"                             // creatable once, not mutable
r3:"status,enum:draft|planned|published"        // enum type + allowed values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnTag

type ColumnTag struct {
	Column     string // resolved column name
	IsPK       bool   // field is the primary key
	Skip       bool   // field should be skipped (tag value is "-")
	SoftDelete bool   // r3:"soft_delete"

	// Capability flags. They are additive and only ever tighten the schema's
	// permissive defaults (see r3.SchemaOf) — never widen them.
	NoFilter  bool     // r3:"...,no-filter"  — not allowed in Query.Filters
	NoSort    bool     // r3:"...,no-sort"    — not allowed in Query.Sorts
	NoOutput  bool     // r3:"...,no-output"  — hidden from SELECT and serialized output
	ReadOnly  bool     // r3:"...,readonly"   — not creatable and not mutable
	Immutable bool     // r3:"...,immutable"  — creatable once, then not mutable
	Enum      []string // r3:"...,enum:a|b|c" — enum data type with the allowed values
	Codec     string   // r3:"...,codec:name" — value codec name (resolved in schema derivation)
}

ColumnTag holds parsed column-level tag info for a single struct field.

func ParseColumnTag

func ParseColumnTag(field reflect.StructField) ColumnTag

ParseColumnTag reads column metadata from struct tags, in priority order: `r3`, then `db`, then the `gorm` tag's column/primaryKey info (so gorm-tagged models resolve to the same physical columns the GORM driver binds). With no column name from any tag, the field name's snake_case is used.

type RelationKind

type RelationKind int

RelationKind describes the type of relationship between two entities.

const (
	// RelHasMany represents a one-to-many relationship.
	RelHasMany RelationKind = iota
	// RelBelongsTo represents a many-to-one relationship.
	RelBelongsTo
	// RelManyToMany represents a many-to-many relationship via a join table.
	RelManyToMany
)

type RelationTag

type RelationTag struct {
	Kind      RelationKind
	FKColumn  string // foreign key column name (or left FK for M2M)
	RefColumn string // right FK column for M2M join table
	TableName string // explicit table name override (optional)
	JoinTable string // join table name for M2M relations
	// OrderColumn is an integer column on the M2M join table that persists the
	// relation slice's order: sync writes each element's index into it, preload
	// orders by it. Empty means order is not persisted.
	OrderColumn string
	Owned       bool // if true, children are lifecycle-bound to parent (delete orphans on update)
}

RelationTag holds parsed relation tag info.

func ParseRelationTag

func ParseRelationTag(field reflect.StructField) (RelationTag, bool)

ParseRelationTag parses the `r3` struct tag on a relation field. Returns a RelationTag and true if the tag declares a valid relation.

Jump to

Keyboard shortcuts

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