Use fluent Set method for Cols initialization
This commit is contained in:
@@ -25,7 +25,7 @@ var PsModuleShopCols = struct {
|
||||
IDShop gormcol.Field
|
||||
EnableDevice gormcol.Field
|
||||
}{
|
||||
IDModule: gormcol.Field{Table: (&PsModuleShop{}).TableName(), Column: "id_module"},
|
||||
IDShop: gormcol.Field{Table: (&PsModuleShop{}).TableName(), Column: "id_shop"},
|
||||
EnableDevice: gormcol.Field{Table: (&PsModuleShop{}).TableName(), Column: "enable_device"},
|
||||
IDModule: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "id_module"),
|
||||
IDShop: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "id_shop"),
|
||||
EnableDevice: gormcol.Field{}.Set((&PsModuleShop{}).TableName(), "enable_device"),
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.ma-al.com/goc_marek/gormcol"
|
||||
"git.ma-al.com/goc_marek/gormcol/app/model/dbmodel"
|
||||
"github.com/joho/godotenv"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -19,6 +20,9 @@ import (
|
||||
// It parses flags, loads configuration from .env, connects to the database,
|
||||
// and generates GORM models with column descriptors.
|
||||
func main() {
|
||||
|
||||
fmt.Printf("gormcol.Column(dbmodel.PsModuleShop.IDModule): %v\n", dbmodel.PsModuleShopCols.EnableDevice.TabCol())
|
||||
|
||||
dsn := flag.String("dsn", "", "database DSN (e.g. user:pass@tcp(host:3306)/dbname)")
|
||||
filter := flag.String("filter", "", "regex to match table names (triggers batch mode)")
|
||||
all := flag.Bool("all", false, "generate all tables matching filter (shows confirmation)")
|
||||
|
||||
@@ -175,7 +175,7 @@ func generateColsVarBlock(si *structInfo) string {
|
||||
}
|
||||
b.WriteString("}{\n")
|
||||
for _, f := range si.Fields {
|
||||
b.WriteString(fmt.Sprintf("\t%s: gormcol.Field{Table: (&%s{}).TableName(), Column: %q},\n", f.GoName, si.Name, f.ColName))
|
||||
b.WriteString(fmt.Sprintf("\t%s: gormcol.Field{}.Set((&%s{}).TableName(), %q),\n", f.GoName, si.Name, f.ColName))
|
||||
}
|
||||
b.WriteString("}\n")
|
||||
return b.String()
|
||||
|
||||
28
gormcol.go
28
gormcol.go
@@ -9,19 +9,33 @@
|
||||
// IDProfile gormcol.Field
|
||||
// IDAuthorizationRole gormcol.Field
|
||||
// }{
|
||||
// IDProfile: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_profile"},
|
||||
// IDAuthorizationRole: gormcol.Field{Table: (PsAccess{}).TableName(), Column: "id_authorization_role"},
|
||||
// IDProfile: gormcol.Field{}.Set((&PsAccess{}).TableName(), "id_profile"),
|
||||
// IDAuthorizationRole: gormcol.Field{}.Set((&PsAccess{}).TableName(), "id_authorization_role"),
|
||||
// }
|
||||
package gormcol
|
||||
|
||||
// Field represents a GORM column descriptor.
|
||||
// Table should be set using the model's TableName() function for type safety.
|
||||
type Field struct {
|
||||
Table string // Database table name (use model's TableName())
|
||||
Column string // Database column name
|
||||
table string // Database table name (use model's TableName())
|
||||
column string // Database column name
|
||||
}
|
||||
|
||||
// Column returns the column name from a Field descriptor.
|
||||
func Column(f Field) string {
|
||||
return f.Column
|
||||
func (f Field) TabCol() string {
|
||||
return f.table + "." + f.column
|
||||
}
|
||||
|
||||
func (f Field) Col() string {
|
||||
return f.column
|
||||
}
|
||||
|
||||
func (f Field) Tab() string {
|
||||
return f.table
|
||||
}
|
||||
|
||||
func (f Field) Set(tab, field string) Field {
|
||||
return Field{
|
||||
table: tab,
|
||||
column: field,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user