Skip to content

Commit 3800563

Browse files
committed
fix: fix get gorm tag column name error
1 parent 2d2e46c commit 3800563

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

cmd/gplus/gen.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/dave/jennifer/jen"
67
"go/ast"
78
"go/format"
89
"go/types"
910
"gorm.io/gorm/schema"
1011
"io"
11-
12-
"github.com/dave/jennifer/jen"
1312
"sigs.k8s.io/controller-tools/pkg/genall"
1413
"sigs.k8s.io/controller-tools/pkg/loader"
1514
"sigs.k8s.io/controller-tools/pkg/markers"
15+
"strings"
1616
)
1717

1818
type StructInfo struct {
@@ -89,7 +89,10 @@ func buildGenFile(root *loader.Package, allStructInfos []StructInfo) *jen.File {
8989
genFile.Id("}").Id("{")
9090
for i, field := range s.Fields {
9191
tag := s.FieldTags[i]
92-
tagSetting := schema.ParseTagSetting(tag, ";")
92+
// todo Need to optimize
93+
newTag := strings.ReplaceAll(tag, "gorm:", "")
94+
newTag = strings.ReplaceAll(newTag, "\"", "")
95+
tagSetting := schema.ParseTagSetting(newTag, ";")
9396
columnName := tagSetting["COLUMN"]
9497
if columnName == "" {
9598
// Use NamingStrategy by default for now

example/user.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package example
22

33
import "time"
44

5-
// +gplus:column=true
5+
// +gplus:column=true
66

77
type User struct {
88
ID int64 `gorm:"primaryKey"`
@@ -20,3 +20,15 @@ type User struct {
2020
func (User) TableName() string {
2121
return "Users"
2222
}
23+
24+
// +gplus:column=true
25+
26+
type Clazz struct {
27+
ID int64 `gorm:"primaryKey"`
28+
Name string
29+
Count string
30+
TeacherName string
31+
HouseNumber int `gorm:"column:house"`
32+
CreatedAt time.Time
33+
UpdatedAt time.Time
34+
}

example/zz_gen.column.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,20 @@ var UserColumn = struct {
2323
CreatedAt: "created_at",
2424
UpdatedAt: "updated_at",
2525
}
26+
var ClazzColumn = struct {
27+
ID string
28+
Name string
29+
Count string
30+
TeacherName string
31+
HouseNumber string
32+
CreatedAt string
33+
UpdatedAt string
34+
}{
35+
ID: "id",
36+
Name: "name",
37+
Count: "count",
38+
TeacherName: "teacher_name",
39+
HouseNumber: "house",
40+
CreatedAt: "created_at",
41+
UpdatedAt: "updated_at",
42+
}

0 commit comments

Comments
 (0)