From faaf66dbedca33433dd502d6af862f1d0d058d9d Mon Sep 17 00:00:00 2001 From: qqxhb <1252905006@qq.com> Date: Fri, 18 Oct 2024 20:17:06 +0800 Subject: [PATCH] feat: support NullType --- config.go | 23 ++++++++++++++++++ field_options.go | 45 ++++++++++++++++++++++++++++++++++++ go.mod | 12 ++++++---- go.sum | 38 +++++++++++++++--------------- internal/model/tbl_column.go | 2 +- 5 files changed, 96 insertions(+), 24 deletions(-) diff --git a/config.go b/config.go index f2161b8f..1f4c00ee 100644 --- a/config.go +++ b/config.go @@ -115,6 +115,29 @@ func (cfg *Config) WithImportPkgPath(paths ...string) { cfg.importPkgPaths = append(cfg.importPkgPaths, paths...) } +// WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts. +/** + * + * @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types. + * If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types. + * + * Examples: + * + * When `all` is true: + * - `int64` will be replaced with `datatypes.NullInt64` + * - `string` will be replaced with `datatypes.NullString` + * + * When `all` is false: + * - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types. + * + * Note: + * Ensure that proper error handling is implemented when converting + * fields to their `datatypes.Null[T]` types to avoid runtime issues. + */ +func (cfg *Config) WithDataTypesNullType(all bool) { + cfg.WithOpts(WithDataTypesNullType(all)) +} + // Revise format path and db func (cfg *Config) Revise() (err error) { if strings.TrimSpace(cfg.ModelPkgPath) == "" { diff --git a/field_options.go b/field_options.go index 0ff6a489..072e3e22 100644 --- a/field_options.go +++ b/field_options.go @@ -1,6 +1,7 @@ package gen import ( + "fmt" "reflect" "regexp" "strings" @@ -26,6 +27,50 @@ var ( } } + // WithDataTypesNullType configures the types of fields to use their datatypes nullable counterparts. + /** + * + * @param {boolean} all - If true, all basic types of fields will be replaced with their `datatypes.Null[T]` types. + * If false, only fields that are allowed to be null will be replaced with `datatypes.Null[T]` types. + * + * Examples: + * + * When `all` is true: + * - `int64` will be replaced with `datatypes.NullInt64` + * - `string` will be replaced with `datatypes.NullString` + * + * When `all` is false: + * - Only fields that can be null (e.g., `*string` or `*int`) will be replaced with `datatypes.Null[T]` types. + * + * Note: + * Ensure that proper error handling is implemented when converting + * fields to their `datatypes.Null[T]` types to avoid runtime issues. + */ + WithDataTypesNullType = func(all bool) model.ModifyFieldOpt { + return func(f *model.Field) *model.Field { + ft := f.Type + nullable := false + if strings.HasPrefix(ft, "*") { + nullable = true + ft = strings.TrimLeft(ft, "*") + } + if !all && !nullable { + return f + } + switch ft { + case "time.Time", "string", "int", "int8", "int16", + "int32", "int64", "uint", "uint8", "uint16", "uint32", + "uint64", "float64", "float32", "byte", "bool": + ft = fmt.Sprintf("datatypes.Null[%s]", ft) + default: + return f + } + f.CustomGenType = f.GenType() + f.Type = ft + return f + } + } + // FieldNew add new field (any type your want) FieldNew = func(fieldName, fieldType string, fieldTag field.Tag) model.CreateFieldOpt { return func(*model.Field) *model.Field { diff --git a/go.mod b/go.mod index adcdee35..bc6eeed6 100644 --- a/go.mod +++ b/go.mod @@ -4,17 +4,19 @@ go 1.18 require ( golang.org/x/tools v0.17.0 - gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c - gorm.io/gorm v1.25.9 + gorm.io/datatypes v1.2.4 + gorm.io/gorm v1.25.11 gorm.io/hints v1.1.0 gorm.io/plugin/dbresolver v1.5.0 ) require ( - github.com/go-sql-driver/mysql v1.7.0 // indirect + filippo.io/edwards25519 v1.1.0 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect + github.com/google/uuid v1.3.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - golang.org/x/crypto v0.14.0 // indirect golang.org/x/mod v0.14.0 // indirect - gorm.io/driver/mysql v1.4.4 // indirect + golang.org/x/text v0.14.0 // indirect + gorm.io/driver/mysql v1.5.6 // indirect ) diff --git a/go.sum b/go.sum index 8e7d4d18..6b983a2b 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,19 @@ +filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= +filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= +github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA= github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= -github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= -github.com/jackc/pgconn v1.13.0 h1:3L1XMNV2Zvca/8BYhzcRFS70Lr0WlDg16Di6SFGAbys= -github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= -github.com/jackc/pgproto3/v2 v2.3.1 h1:nwj7qwf0S+Q7ISFfBndqeLwSwxs+4DPsbRFjECT1Y4Y= -github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= -github.com/jackc/pgtype v1.12.0 h1:Dlq8Qvcch7kiehm8wPGIW0W3KsCCHJnRacKW0UM8n5w= -github.com/jackc/pgx/v4 v4.17.2 h1:0Ut0rpeKwvIVbMQ1KbMBU4h6wxehBI535LK6Flheh8E= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= @@ -25,22 +26,22 @@ github.com/microsoft/go-mssqldb v0.17.0 h1:Fto83dMZPnYv1Zwx5vHHxpNraeEaUlQ/hhHLg github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= -golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= +golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c h1:jWdr7cHgl8c/ua5vYbR2WhSp+NQmzhsj0xoY3foTzW8= -gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c/go.mod h1:SH2K9R+2RMjuX1CkCONrPwoe9JzVv2hkQvEu4bXGojE= +gorm.io/datatypes v1.2.4 h1:uZmGAcK/QZ0uyfCuVg0VQY1ZmV9h1fuG0tMwKByO1z4= +gorm.io/datatypes v1.2.4/go.mod h1:f4BsLcFAX67szSv8svwLRjklArSHAvHLeE3pXAS5DZI= gorm.io/driver/mysql v1.4.3/go.mod h1:sSIebwZAVPiT+27jK9HIwvsqOGKx3YMPmrA3mBJR10c= -gorm.io/driver/mysql v1.4.4 h1:MX0K9Qvy0Na4o7qSC/YI7XxqUw5KDw01umqgID+svdQ= -gorm.io/driver/mysql v1.4.4/go.mod h1:BCg8cKI+R0j/rZRQxeKis/forqRwRSYOR8OM3Wo6hOM= -gorm.io/driver/postgres v1.4.5 h1:mTeXTTtHAgnS9PgmhN2YeUbazYpLhUI1doLnw42XUZc= +gorm.io/driver/mysql v1.5.6 h1:Ld4mkIickM+EliaQZQx3uOJDJHtrd70MxAUqWqlx3Y8= +gorm.io/driver/mysql v1.5.6/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/driver/postgres v1.5.0 h1:u2FXTy14l45qc3UeCJ7QaAXZmZfDDv0YrthvmRq1l0U= gorm.io/driver/sqlite v1.1.6/go.mod h1:W8LmC/6UvVbHKah0+QOC7Ja66EaZXHwUTjgXY8YNWX8= gorm.io/driver/sqlite v1.4.3 h1:HBBcZSDnWi5BW3B3rwvVTc510KGkBkexlOg0QrmLUuU= gorm.io/driver/sqlserver v1.4.1 h1:t4r4r6Jam5E6ejqP7N82qAJIJAht27EGT41HyPfXRw0= @@ -48,8 +49,9 @@ gorm.io/gorm v1.21.15/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= gorm.io/gorm v1.22.2/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= gorm.io/gorm v1.23.8/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= -gorm.io/gorm v1.25.9 h1:wct0gxZIELDk8+ZqF/MVnHLkA1rvYlBWUMv2EdsK1g8= -gorm.io/gorm v1.25.9/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg= +gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= gorm.io/hints v1.1.0 h1:Lp4z3rxREufSdxn4qmkK3TLDltrM10FLTHiuqwDPvXw= gorm.io/hints v1.1.0/go.mod h1:lKQ0JjySsPBj3uslFzY3JhYDtqEwzm+G1hv8rWujB6Y= gorm.io/plugin/dbresolver v1.5.0 h1:XVHLxh775eP0CqVh3vcfJtYqja3uFl5Wr3cKlY8jgDY= diff --git a/internal/model/tbl_column.go b/internal/model/tbl_column.go index 3dd59673..f1c394e0 100644 --- a/internal/model/tbl_column.go +++ b/internal/model/tbl_column.go @@ -136,7 +136,7 @@ func (c *Column) needDefaultTag(defaultTagValue string) bool { case reflect.String: return defaultTagValue != "" case reflect.Struct: - return strings.Trim(defaultTagValue, "'0:- ") != "" + return strings.Trim(defaultTagValue, "'0:-") != "" } return c.Name() != "created_at" && c.Name() != "updated_at" }