Skip to content

Commit faaf66d

Browse files
committed
feat: support NullType
1 parent d7de699 commit faaf66d

File tree

5 files changed

+96
-24
lines changed

5 files changed

+96
-24
lines changed

config.go

+23
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ func (cfg *Config) WithImportPkgPath(paths ...string) {
115115
cfg.importPkgPaths = append(cfg.importPkgPaths, paths...)
116116
}
117117

118+
// WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts.
119+
/**
120+
*
121+
* @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types.
122+
* If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types.
123+
*
124+
* Examples:
125+
*
126+
* When `all` is true:
127+
* - `int64` will be replaced with `datatypes.NullInt64`
128+
* - `string` will be replaced with `datatypes.NullString`
129+
*
130+
* When `all` is false:
131+
* - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types.
132+
*
133+
* Note:
134+
* Ensure that proper error handling is implemented when converting
135+
* fields to their `datatypes.Null[T]` types to avoid runtime issues.
136+
*/
137+
func (cfg *Config) WithDataTypesNullType(all bool) {
138+
cfg.WithOpts(WithDataTypesNullType(all))
139+
}
140+
118141
// Revise format path and db
119142
func (cfg *Config) Revise() (err error) {
120143
if strings.TrimSpace(cfg.ModelPkgPath) == "" {

field_options.go

+45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gen
22

33
import (
4+
"fmt"
45
"reflect"
56
"regexp"
67
"strings"
@@ -26,6 +27,50 @@ var (
2627
}
2728
}
2829

30+
// WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts.
31+
/**
32+
*
33+
* @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types.
34+
* If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types.
35+
*
36+
* Examples:
37+
*
38+
* When `all` is true:
39+
* - `int64` will be replaced with `datatypes.NullInt64`
40+
* - `string` will be replaced with `datatypes.NullString`
41+
*
42+
* When `all` is false:
43+
* - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types.
44+
*
45+
* Note:
46+
* Ensure that proper error handling is implemented when converting
47+
* fields to their `datatypes.Null[T]` types to avoid runtime issues.
48+
*/
49+
WithDataTypesNullType = func(all bool) model.ModifyFieldOpt {
50+
return func(f *model.Field) *model.Field {
51+
ft := f.Type
52+
nullable := false
53+
if strings.HasPrefix(ft, "*") {
54+
nullable = true
55+
ft = strings.TrimLeft(ft, "*")
56+
}
57+
if !all && !nullable {
58+
return f
59+
}
60+
switch ft {
61+
case "time.Time", "string", "int", "int8", "int16",
62+
"int32", "int64", "uint", "uint8", "uint16", "uint32",
63+
"uint64", "float64", "float32", "byte", "bool":
64+
ft = fmt.Sprintf("datatypes.Null[%s]", ft)
65+
default:
66+
return f
67+
}
68+
f.CustomGenType = f.GenType()
69+
f.Type = ft
70+
return f
71+
}
72+
}
73+
2974
// FieldNew add new field (any type your want)
3075
FieldNew = func(fieldName, fieldType string, fieldTag field.Tag) model.CreateFieldOpt {
3176
return func(*model.Field) *model.Field {

go.mod

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ go 1.18
44

55
require (
66
golang.org/x/tools v0.17.0
7-
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c
8-
gorm.io/gorm v1.25.9
7+
gorm.io/datatypes v1.2.4
8+
gorm.io/gorm v1.25.11
99
gorm.io/hints v1.1.0
1010
gorm.io/plugin/dbresolver v1.5.0
1111
)
1212

1313
require (
14-
github.com/go-sql-driver/mysql v1.7.0 // indirect
14+
filippo.io/edwards25519 v1.1.0 // indirect
15+
github.com/go-sql-driver/mysql v1.8.1 // indirect
16+
github.com/google/uuid v1.3.0 // indirect
1517
github.com/jinzhu/inflection v1.0.0 // indirect
1618
github.com/jinzhu/now v1.1.5 // indirect
17-
golang.org/x/crypto v0.14.0 // indirect
1819
golang.org/x/mod v0.14.0 // indirect
19-
gorm.io/driver/mysql v1.4.4 // indirect
20+
golang.org/x/text v0.14.0 // indirect
21+
gorm.io/driver/mysql v1.5.6 // indirect
2022
)

go.sum

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
2+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
35
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
4-
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
56
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
7+
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
8+
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
69
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
710
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
8-
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
9-
github.com/jackc/pgconn v1.13.0 h1:3L1XMNV2Zvca/8BYhzcRFS70Lr0WlDg16Di6SFGAbys=
10-
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
11+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
12+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1113
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
12-
github.com/jackc/pgproto3/v2 v2.3.1 h1:nwj7qwf0S+Q7ISFfBndqeLwSwxs+4DPsbRFjECT1Y4Y=
13-
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg=
14-
github.com/jackc/pgtype v1.12.0 h1:Dlq8Qvcch7kiehm8wPGIW0W3KsCCHJnRacKW0UM8n5w=
15-
github.com/jackc/pgx/v4 v4.17.2 h1:0Ut0rpeKwvIVbMQ1KbMBU4h6wxehBI535LK6Flheh8E=
14+
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
15+
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
16+
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
1617
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
1718
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
1819
github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
@@ -25,31 +26,32 @@ github.com/microsoft/go-mssqldb v0.17.0 h1:Fto83dMZPnYv1Zwx5vHHxpNraeEaUlQ/hhHLg
2526
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2627
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2728
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
28-
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
29-
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
29+
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
3030
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
3131
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3232
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
33-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
33+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
34+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
3435
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
3536
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
3637
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3738
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
38-
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c h1:jWdr7cHgl8c/ua5vYbR2WhSp+NQmzhsj0xoY3foTzW8=
39-
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c/go.mod h1:SH2K9R+2RMjuX1CkCONrPwoe9JzVv2hkQvEu4bXGojE=
39+
gorm.io/datatypes v1.2.4 h1:uZmGAcK/QZ0uyfCuVg0VQY1ZmV9h1fuG0tMwKByO1z4=
40+
gorm.io/datatypes v1.2.4/go.mod h1:f4BsLcFAX67szSv8svwLRjklArSHAvHLeE3pXAS5DZI=
4041
gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c=
41-
gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ=
42-
gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM=
43-
gorm.io/driver/postgres v1.4.5 h1:mTeXTTtHAgnS9PgmhN2YeUbazYpLhUI1doLnw42XUZc=
42+
gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8=
43+
gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
44+
gorm.io/driver/postgres v1.5.0 h1:u2FXTy14l45qc3UeCJ7QaAXZmZfDDv0YrthvmRq1l0U=
4445
gorm.io/driver/sqlite v1.1.6/go.mod h1:W8LmC/6UvVbHKah0+QOC7Ja66EaZXHwUTjgXY8YNWX8=
4546
gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU=
4647
gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0=
4748
gorm.io/gorm v1.21.15/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
4849
gorm.io/gorm v1.22.2/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0=
4950
gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
5051
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
51-
gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8=
52-
gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
52+
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
53+
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
54+
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
5355
gorm.io/hints v1.1.0 h1:Lp4z3rxREufSdxn4qmkK3TLDltrM10FLTHiuqwDPvXw=
5456
gorm.io/hints v1.1.0/go.mod h1:lKQ0JjySsPBj3uslFzY3JhYDtqEwzm+G1hv8rWujB6Y=
5557
gorm.io/plugin/dbresolver v1.5.0 h1:XVHLxh775eP0CqVh3vcfJtYqja3uFl5Wr3cKlY8jgDY=

internal/model/tbl_column.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *Column) needDefaultTag(defaultTagValue string) bool {
136136
case reflect.String:
137137
return defaultTagValue != ""
138138
case reflect.Struct:
139-
return strings.Trim(defaultTagValue, "'0:- ") != ""
139+
return strings.Trim(defaultTagValue, "'0:-") != ""
140140
}
141141
return c.Name() != "created_at" && c.Name() != "updated_at"
142142
}

0 commit comments

Comments
 (0)