Skip to content

Commit 99c6c22

Browse files
committed
feat: get column name from tag
1 parent 12810e0 commit 99c6c22

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

cmd/gplus/gen.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"go/ast"
77
"go/format"
88
"go/types"
9+
"gorm.io/gorm/schema"
910
"io"
1011

1112
"github.com/dave/jennifer/jen"
@@ -87,9 +88,16 @@ func buildGenFile(root *loader.Package, allStructInfos []StructInfo) *jen.File {
8788
}
8889
genFile.Id("}").Id("{")
8990
for i, field := range s.Fields {
90-
tagName := s.FieldTags[i]
91-
filedName := fmt.Sprintf("\"%s\"", tagName)
92-
genFile.Id(field).Op(":").Id(filedName).Id(",")
91+
tag := s.FieldTags[i]
92+
tagSetting := schema.ParseTagSetting(tag, ";")
93+
columnName := tagSetting["COLUMN"]
94+
if columnName == "" {
95+
// Use NamingStrategy by default for now
96+
namingStrategy := schema.NamingStrategy{}
97+
columnName = namingStrategy.ColumnName("", field)
98+
}
99+
columnName = fmt.Sprintf("\"%s\"", columnName)
100+
genFile.Id(field).Op(":").Id(columnName).Id(",")
93101
}
94102
genFile.Id("}")
95103
}

example/user.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import "time"
55
// +gplus:column=true
66

77
type User struct {
8-
//ID int64 `gorm:"primaryKey"`
9-
//Username string `gorm:"column:username"`
10-
ID int64 `id`
11-
Username string `username`
8+
ID int64 `gorm:"primaryKey"`
9+
Username string `gorm:"column:username"`
1210
Password string
1311
Address string
1412
Age int

0 commit comments

Comments
 (0)