Files
gormcol/gormcol.go
2026-03-29 14:55:16 +02:00

59 lines
1.7 KiB
Go

// Package gormcol provides type-safe GORM column descriptors.
//
// This package enables defining struct-like variables that map Go field names
// to database table.column pairs, allowing type-safe queries in GORM.
//
// Example usage:
//
// var PsAccess = struct {
// IDProfile gormcol.Field
// IDAuthorizationRole gormcol.Field
// }{
// IDProfile: gormcol.Field{Table: "ps_access", Column: "id_profile"},
// IDAuthorizationRole: gormcol.Field{Table: "ps_access", Column: "id_authorization_role"},
// }
package gormcol
// Field represents a GORM column descriptor with table context.
// It holds the table name and column name for type-safe column references.
type Field struct {
Table string // Database table name
Column string // Database column name
}
// Column returns the column name from a Field descriptor.
//
// Example:
//
// gormcol.Column(dbmodel.PsAccess.IDAuthorizationRole) // "id_authorization_role"
func Column(f Field) string {
return f.Column
}
// ColumnOnTable returns a qualified "table.column" string from a Field descriptor.
//
// Example:
//
// gormcol.ColumnOnTable(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access.id_authorization_role"
func ColumnOnTable(f Field) string {
return f.Table + "." + f.Column
}
// TableField returns the table name from a Field descriptor.
//
// Example:
//
// gormcol.TableField(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access"
func TableField(f Field) string {
return f.Table
}
// Table is an alias for TableField, returning the table name from a Field descriptor.
//
// Example:
//
// gormcol.Table(dbmodel.PsAccess.IDAuthorizationRole) // "ps_access"
func Table(f Field) string {
return f.Table
}