Skip to content

fix: ConvertStringDataType #4449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tools/goctl/model/sql/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func ConvertDataType(dataBaseType int, isDefaultNull, unsigned, strict bool) (st
func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict bool) (
goType string, thirdPkg string, isPQArray bool, err error) {
if env.UseExperimental() {
customTp, thirdImport := convertDatatypeWithConfig(dataBaseType, isDefaultNull, unsigned)
customTp, thirdImport := convertDatatypeWithConfig(dataBaseType, isDefaultNull, unsigned, strict)
if len(customTp) != 0 {
return customTp, thirdImport, false, nil
}
Expand Down Expand Up @@ -257,7 +257,7 @@ func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict
return mayConvertNullType(tp, isDefaultNull, unsigned, strict), "", false, nil
}

func convertDatatypeWithConfig(dataBaseType string, isDefaultNull, unsigned bool) (string, string) {
func convertDatatypeWithConfig(dataBaseType string, isDefaultNull, unsigned bool, strict bool) (string, string) {
externalConfig, err := config.GetExternalConfig()
if err != nil {
return "", ""
Expand All @@ -272,7 +272,7 @@ func convertDatatypeWithConfig(dataBaseType string, isDefaultNull, unsigned bool
if len(opt.NullType) != 0 {
return opt.NullType, opt.Pkg
}
} else if unsigned {
} else if unsigned && strict {
if len(opt.UnsignedType) != 0 {
return opt.UnsignedType, opt.Pkg
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func mayConvertNullType(goDataType string, isDefaultNull, unsigned, strict bool)
case "time.Time":
return "sql.NullTime"
default:
if unsigned {
if unsigned && strict {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you should consider whether to use goctl config. If so, don't use strict control, otherwise it will be regarded as an old function and can be repaired compatible.

Copy link
Contributor Author

@wuqinqiang wuqinqiang Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convertDatatypeWithConfig is already called in ConvertStringDataType before mayConvertNullType, there's no need to modify mayConvertNullType to check goctl config again.
https://github.com/zeromicro/go-zero/blob/master/tools/goctl/model/sql/converter/types.go#L230C28-L230C53

ret, ok := unsignedTypeMap[goDataType]
if ok {
return ret
Expand Down
11 changes: 11 additions & 0 deletions tools/goctl/model/sql/converter/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func TestConvertStringDataType(t *testing.T) {
unsigned: true,
strict: false,
},
want: result{
goType: "int64",
},
},
{
input: input{
dataType: "bigint",
isDefaultNull: false,
unsigned: true,
strict: true,
},
want: result{
goType: "uint64",
},
Expand Down