fix(goctl): fix redundant import (#2551)

master
anqiansong 2 years ago committed by GitHub
parent ce73b9a85c
commit 9504d30049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,7 +6,7 @@ import (
) )
// BuildVersion is the version of goctl. // BuildVersion is the version of goctl.
const BuildVersion = "1.4.2" const BuildVersion = "1.4.3"
var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5} var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5}

@ -153,17 +153,18 @@ func ConvertDataType(dataBaseType int, isDefaultNull, unsigned, strict bool) (st
} }
// ConvertStringDataType converts mysql column type into golang type // ConvertStringDataType converts mysql column type into golang type
func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict bool) (string, error) { func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict bool) (
goType string, isPQArray bool, err error) {
tp, ok := commonMysqlDataTypeMapString[strings.ToLower(dataBaseType)] tp, ok := commonMysqlDataTypeMapString[strings.ToLower(dataBaseType)]
if !ok { if !ok {
return "", fmt.Errorf("unsupported database type: %s", dataBaseType) return "", false, fmt.Errorf("unsupported database type: %s", dataBaseType)
} }
if strings.HasPrefix(dataBaseType, "_") { if strings.HasPrefix(dataBaseType, "_") {
return tp, nil return tp, true, nil
} }
return mayConvertNullType(tp, isDefaultNull, unsigned, strict), nil return mayConvertNullType(tp, isDefaultNull, unsigned, strict), false, nil
} }
func mayConvertNullType(goDataType string, isDefaultNull, unsigned, strict bool) string { func mayConvertNullType(goDataType string, isDefaultNull, unsigned, strict bool) string {

@ -15,6 +15,7 @@ func genImports(table Table, withCache, timeImport bool) (string, error) {
buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{ buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
"time": timeImport, "time": timeImport,
"containsPQ": table.ContainsPQ,
"data": table, "data": table,
}) })
if err != nil { if err != nil {
@ -31,6 +32,7 @@ func genImports(table Table, withCache, timeImport bool) (string, error) {
buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{ buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
"time": timeImport, "time": timeImport,
"containsPQ": table.ContainsPQ,
"data": table, "data": table,
}) })
if err != nil { if err != nil {

@ -26,6 +26,7 @@ type (
PrimaryKey Primary PrimaryKey Primary
UniqueIndex map[string][]*Field UniqueIndex map[string][]*Field
Fields []*Field Fields []*Field
ContainsPQ bool
} }
// Primary describes a primary key // Primary describes a primary key
@ -42,6 +43,7 @@ type (
Comment string Comment string
SeqInIndex int SeqInIndex int
OrdinalPosition int OrdinalPosition int
ContainsPQ bool
} }
// KeyType types alias of int // KeyType types alias of int
@ -268,12 +270,13 @@ func (t *Table) ContainsTime() bool {
func ConvertDataType(table *model.Table, strict bool) (*Table, error) { func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
isPrimaryDefaultNull := table.PrimaryKey.ColumnDefault == nil && table.PrimaryKey.IsNullAble == "YES" isPrimaryDefaultNull := table.PrimaryKey.ColumnDefault == nil && table.PrimaryKey.IsNullAble == "YES"
isPrimaryUnsigned := strings.Contains(table.PrimaryKey.DbColumn.ColumnType, "unsigned") isPrimaryUnsigned := strings.Contains(table.PrimaryKey.DbColumn.ColumnType, "unsigned")
primaryDataType, err := converter.ConvertStringDataType(table.PrimaryKey.DataType, isPrimaryDefaultNull, isPrimaryUnsigned, strict) primaryDataType, containsPQ, err := converter.ConvertStringDataType(table.PrimaryKey.DataType, isPrimaryDefaultNull, isPrimaryUnsigned, strict)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var reply Table var reply Table
reply.ContainsPQ = containsPQ
reply.UniqueIndex = map[string][]*Field{} reply.UniqueIndex = map[string][]*Field{}
reply.Name = stringx.From(table.Table) reply.Name = stringx.From(table.Table)
reply.Db = stringx.From(table.Db) reply.Db = stringx.From(table.Db)
@ -299,6 +302,9 @@ func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
} }
for _, each := range fieldM { for _, each := range fieldM {
if each.ContainsPQ {
reply.ContainsPQ = true
}
reply.Fields = append(reply.Fields, each) reply.Fields = append(reply.Fields, each)
} }
sort.Slice(reply.Fields, func(i, j int) bool { sort.Slice(reply.Fields, func(i, j int) bool {
@ -348,7 +354,7 @@ func getTableFields(table *model.Table, strict bool) (map[string]*Field, error)
for _, each := range table.Columns { for _, each := range table.Columns {
isDefaultNull := each.ColumnDefault == nil && each.IsNullAble == "YES" isDefaultNull := each.ColumnDefault == nil && each.IsNullAble == "YES"
isPrimaryUnsigned := strings.Contains(each.ColumnType, "unsigned") isPrimaryUnsigned := strings.Contains(each.ColumnType, "unsigned")
dt, err := converter.ConvertStringDataType(each.DataType, isDefaultNull, isPrimaryUnsigned, strict) dt, containsPQ, err := converter.ConvertStringDataType(each.DataType, isDefaultNull, isPrimaryUnsigned, strict)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -364,6 +370,7 @@ func getTableFields(table *model.Table, strict bool) (map[string]*Field, error)
Comment: each.Comment, Comment: each.Comment,
SeqInIndex: columnSeqInIndex, SeqInIndex: columnSeqInIndex,
OrdinalPosition: each.OrdinalPosition, OrdinalPosition: each.OrdinalPosition,
ContainsPQ: containsPQ,
} }
fieldM[each.Name] = field fieldM[each.Name] = field
} }

@ -9,6 +9,7 @@ const (
"strings" "strings"
{{if .time}}"time"{{end}} {{if .time}}"time"{{end}}
{{if .containsPQ}}"github.com/lib/pq"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlc"
@ -24,6 +25,7 @@ const (
"strings" "strings"
{{if .time}}"time"{{end}} {{if .time}}"time"{{end}}
{{if .containsPQ}}"github.com/lib/pq"{{end}}
"github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"

@ -10,16 +10,12 @@ import (
const ModelCustom = `package {{.pkg}} const ModelCustom = `package {{.pkg}}
{{if .withCache}} {{if .withCache}}
import ( import (
"github.com/lib/pq"
"github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stores/sqlx"
) )
{{else}} {{else}}
import ( import "github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/lib/pq"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
{{end}} {{end}}
var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil) var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil)

Loading…
Cancel
Save