Skip to content

Commit 096dc57

Browse files
committed
Fix generics types with numbers support, close #19
1 parent 937d3b9 commit 096dc57

File tree

10 files changed

+146
-115
lines changed

10 files changed

+146
-115
lines changed

examples/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ require (
1212
require (
1313
filippo.io/edwards25519 v1.1.0 // indirect
1414
github.com/go-sql-driver/mysql v1.8.1 // indirect
15+
github.com/google/uuid v1.3.0 // indirect
1516
github.com/jinzhu/inflection v1.0.0 // indirect
1617
github.com/jinzhu/now v1.1.5 // indirect
1718
github.com/mattn/go-sqlite3 v1.14.22 // indirect
1819
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
1920
golang.org/x/text v0.20.0 // indirect
21+
gorm.io/datatypes v1.2.4 // indirect
2022
)
2123

2224
replace gorm.io/cli/gorm => ../

examples/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
22
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
33
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
44
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
5+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
6+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
57
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
68
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
79
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -12,6 +14,8 @@ golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu
1214
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
1315
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
1416
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
17+
gorm.io/datatypes v1.2.4 h1:uZmGAcK/QZ0uyfCuVg0VQY1ZmV9h1fuG0tMwKByO1z4=
18+
gorm.io/datatypes v1.2.4/go.mod h1:f4BsLcFAX67szSv8svwLRjklArSHAvHLeE3pXAS5DZI=
1519
gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg=
1620
gorm.io/driver/mysql v1.6.0/go.mod h1:D/oCC2GWK3M/dqoLxnOlaNKmXz8WNTfcS9y5ovaSqKo=
1721
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=

examples/models/user.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"gorm.io/cli/gorm/genconfig"
8+
"gorm.io/datatypes"
89
"gorm.io/gorm"
910
)
1011

@@ -18,24 +19,25 @@ var _ = genconfig.Config{
1819
// His pet also has one Toy (has one - polymorphic)
1920
type User struct {
2021
gorm.Model
21-
Name string
22-
Age int
23-
Birthday *time.Time
24-
Score sql.NullInt64
25-
LastLogin sql.NullTime
26-
Account Account
27-
Pets []*Pet
28-
Toys []Toy `gorm:"polymorphic:Owner"`
29-
CompanyID *int
30-
Company Company
31-
ManagerID *uint
32-
Manager *User
33-
Team []User `gorm:"foreignkey:ManagerID"`
34-
Languages []Language `gorm:"many2many:UserSpeak"`
35-
Friends []*User `gorm:"many2many:user_friends"`
36-
Role string
37-
IsAdult bool `gorm:"column:is_adult"`
38-
Profile string `gen:"json"`
22+
Name string
23+
Age int
24+
Birthday *time.Time
25+
Score sql.NullInt64
26+
LastLogin sql.NullTime
27+
Account Account
28+
Pets []*Pet
29+
Toys []Toy `gorm:"polymorphic:Owner"`
30+
CompanyID *int
31+
Company Company
32+
ManagerID *uint
33+
Manager *User
34+
Team []User `gorm:"foreignkey:ManagerID"`
35+
Languages []Language `gorm:"many2many:UserSpeak"`
36+
Friends []*User `gorm:"many2many:user_friends"`
37+
Role string
38+
IsAdult bool `gorm:"column:is_adult"`
39+
Profile string `gen:"json"`
40+
AwardTypes datatypes.JSONSlice[int]
3941
}
4042

4143
type Account struct {

examples/output/models/user.go

Lines changed: 47 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/typed/models/user.go

Lines changed: 47 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ go 1.21
55
require (
66
github.com/spf13/cobra v1.9.1
77
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
8-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
8+
golang.org/x/mod v0.20.0
9+
golang.org/x/tools v0.24.1
910
gorm.io/gorm v1.31.1
1011
)
1112

@@ -14,7 +15,6 @@ require (
1415
github.com/jinzhu/inflection v1.0.0 // indirect
1516
github.com/jinzhu/now v1.1.5 // indirect
1617
github.com/spf13/pflag v1.0.6 // indirect
17-
golang.org/x/mod v0.17.0 // indirect
1818
golang.org/x/sync v0.9.0 // indirect
1919
golang.org/x/text v0.20.0 // indirect
2020
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
1212
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1313
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o=
1414
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
15-
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
16-
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
15+
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
16+
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
1717
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
1818
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
1919
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
2020
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
21-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
22-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
21+
golang.org/x/tools v0.24.1 h1:vxuHLTNS3Np5zrYoPRpcheASHX/7KiGo+8Y4ZM1J2O8=
22+
golang.org/x/tools v0.24.1/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=
2323
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2424
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2525
gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg=

internal/gen/generator.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,22 @@ var typeMap = map[string]string{
396396
"time.Time": "field.Time",
397397
}
398398

399+
var primitiveNumberTypes = map[string]struct{}{
400+
"int": {},
401+
"int8": {},
402+
"int16": {},
403+
"int32": {},
404+
"int64": {},
405+
"uint": {},
406+
"uint8": {},
407+
"uint16": {},
408+
"uint32": {},
409+
"uint64": {},
410+
"uintptr": {},
411+
"float32": {},
412+
"float64": {},
413+
}
414+
399415
// Type returns the field type string for template generation
400416
func (f Field) Type() string {
401417
// Check FieldTypeMap and FieldNameMap from configs first
@@ -426,7 +442,7 @@ func (f Field) Type() string {
426442
return mapped
427443
}
428444

429-
if strings.Contains(goType, "int") || strings.Contains(goType, "float") {
445+
if _, ok := primitiveNumberTypes[goType]; ok {
430446
return fmt.Sprintf("field.Number[%s]", goType)
431447
}
432448

internal/gen/generator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func TestProcessStructType(t *testing.T) {
125125
{Name: "Role", DBName: "role", GoType: "string"},
126126
{Name: "IsAdult", DBName: "is_adult", GoType: "bool"},
127127
{Name: "Profile", DBName: "profile", GoType: "string", NamedGoType: "json"},
128+
{Name: "AwardTypes", DBName: "award_types", GoType: "datatypes.JSONSlice[int]"},
128129
},
129130
}
130131

internal/gen/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func getCurrentPackagePath(filename string) string {
9797
// loadNamedType returns a named type from a package with basic caching.
9898
func loadNamedType(modRoot, pkgPath, name string) types.Type {
9999
cfg := &packages.Config{
100-
Mode: packages.NeedTypes | packages.NeedName,
100+
Mode: packages.NeedTypes | packages.NeedName | packages.NeedDeps,
101101
Dir: modRoot,
102102
}
103103

@@ -114,7 +114,7 @@ func loadNamedType(modRoot, pkgPath, name string) types.Type {
114114
// loadStructFromPackage loads a struct type definition from an external package by name
115115
func loadNamedStructType(modRoot, pkgPath, name string) (*ast.StructType, error) {
116116
cfg := &packages.Config{
117-
Mode: packages.NeedSyntax | packages.NeedTypes | packages.NeedImports,
117+
Mode: packages.NeedSyntax | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedName,
118118
Dir: modRoot,
119119
}
120120

0 commit comments

Comments
 (0)