Repository: zeromicro/go-zero
Pull request #2551 (2022-10-24 13:05:16)

fix(goctl): fix redundant import


Patch:
diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml
index 7e8ee5db..6ba019ea 100644
--- a/.github/workflows/go.yml
+++ b/.github/workflows/go.yml
@@ -11,15 +11,17 @@ jobs:
     name: Linux
     runs-on: ubuntu-latest
     steps:
+      - name: Check out code into the Go module directory
+        uses: actions/checkout@v3
+
       - name: Set up Go 1.x
         uses: actions/setup-go@v3
         with:
           go-version: ^1.16
+          check-latest: true
+          cache: true
         id: go
 
-      - name: Check out code into the Go module directory
-        uses: actions/checkout@v3
-
       - name: Get dependencies
         run: |
           go get -v -t -d ./...
@@ -40,13 +42,15 @@ jobs:
     name: Windows
     runs-on: windows-latest
     steps:
+      - name: Checkout codebase
+        uses: actions/checkout@v3
+
       - name: Set up Go 1.x
         uses: actions/setup-go@v3
         with:
           go-version: ^1.16
-
-      - name: Checkout codebase
-        uses: actions/checkout@v3
+          check-latest: true
+          cache: true
 
       - name: Test
         run: |
diff --git a/tools/goctl/internal/version/version.go b/tools/goctl/internal/version/version.go
index 2fc7caaf..3534b68d 100644
--- a/tools/goctl/internal/version/version.go
+++ b/tools/goctl/internal/version/version.go
@@ -6,7 +6,7 @@ import (
 )
 
 // 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}
 
diff --git a/tools/goctl/model/sql/converter/types.go b/tools/goctl/model/sql/converter/types.go
index 42de8f66..296657f7 100644
--- a/tools/goctl/model/sql/converter/types.go
+++ b/tools/goctl/model/sql/converter/types.go
@@ -153,17 +153,18 @@ func ConvertDataType(dataBaseType int, isDefaultNull, unsigned, strict bool) (st
 }
 
 // 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)]
 	if !ok {
-		return "", fmt.Errorf("unsupported database type: %s", dataBaseType)
+		return "", false, fmt.Errorf("unsupported database type: %s", 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 {
diff --git a/tools/goctl/model/sql/gen/imports.go b/tools/goctl/model/sql/gen/imports.go
index a9842c9c..e17c3c84 100644
--- a/tools/goctl/model/sql/gen/imports.go
+++ b/tools/goctl/model/sql/gen/imports.go
@@ -14,8 +14,9 @@ func genImports(table Table, withCache, timeImport bool) (string, error) {
 		}
 
 		buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
-			"time": timeImport,
-			"data": table,
+			"time":       timeImport,
+			"containsPQ": table.ContainsPQ,
+			"data":       table,
 		})
 		if err != nil {
 			return "", err
@@ -30,8 +31,9 @@ func genImports(table Table, withCache, timeImport bool) (string, error) {
 	}
 
 	buffer, err := util.With("import").Parse(text).Execute(map[string]interface{}{
-		"time": timeImport,
-		"data": table,
+		"time":       timeImport,
+		"containsPQ": table.ContainsPQ,
+		"data":       table,
 	})
 	if err != nil {
 		return "", err
diff --git a/tools/goctl/model/sql/parser/parser.go b/tools/goctl/model/sql/parser/parser.go
index 1caab859..791bae8d 100644
--- a/tools/goctl/model/sql/parser/parser.go
+++ b/tools/goctl/model/sql/parser/parser.go
@@ -26,6 +26,7 @@ type (
 		PrimaryKey  Primary
 		UniqueIndex map[string][]*Field
 		Fields      []*Field
+		ContainsPQ  bool
 	}
 
 	// Primary describes a primary key
@@ -42,6 +43,7 @@ type (
 		Comment         string
 		SeqInIndex      int
 		OrdinalPosition int
+		ContainsPQ      bool
 	}
 
 	// KeyType types alias of int
@@ -268,12 +270,13 @@ func (t *Table) ContainsTime() bool {
 func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
 	isPrimaryDefaultNull := table.PrimaryKey.ColumnDefault == nil && table.PrimaryKey.IsNullAble == "YES"
 	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 {
 		return nil, err
 	}
 
 	var reply Table
+	reply.ContainsPQ = containsPQ
 	reply.UniqueIndex = map[string][]*Field{}
 	reply.Name = stringx.From(table.Table)
 	reply.Db = stringx.From(table.Db)
@@ -299,6 +302,9 @@ func ConvertDataType(table *model.Table, strict bool) (*Table, error) {
 	}
 
 	for _, each := range fieldM {
+		if each.ContainsPQ {
+			reply.ContainsPQ = true
+		}
 		reply.Fields = append(reply.Fields, each)
 	}
 	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 {
 		isDefaultNull := each.ColumnDefault == nil && each.IsNullAble == "YES"
 		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 {
 			return nil, err
 		}
@@ -364,6 +370,7 @@ func getTableFields(table *model.Table, strict bool) (map[string]*Field, error)
 			Comment:         each.Comment,
 			SeqInIndex:      columnSeqInIndex,
 			OrdinalPosition: each.OrdinalPosition,
+			ContainsPQ:      containsPQ,
 		}
 		fieldM[each.Name] = field
 	}
diff --git a/tools/goctl/model/sql/template/import.go b/tools/goctl/model/sql/template/import.go
index 0d92a818..d2f69899 100644
--- a/tools/goctl/model/sql/template/import.go
+++ b/tools/goctl/model/sql/template/import.go
@@ -9,6 +9,7 @@ const (
 	"strings"
 	{{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/cache"
 	"github.com/zeromicro/go-zero/core/stores/sqlc"
@@ -24,6 +25,7 @@ const (
 	"strings"
 	{{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/sqlc"
 	"github.com/zeromicro/go-zero/core/stores/sqlx"
diff --git a/tools/goctl/model/sql/template/model.go b/tools/goctl/model/sql/template/model.go
index 2e9ca10b..9c82262d 100644
--- a/tools/goctl/model/sql/template/model.go
+++ b/tools/goctl/model/sql/template/model.go
@@ -10,16 +10,12 @@ import (
 const ModelCustom = `package {{.pkg}}
 {{if .withCache}}
 import (
-	"github.com/lib/pq"
 	"github.com/zeromicro/go-zero/core/stores/cache"
 	"github.com/zeromicro/go-zero/core/stores/sqlx"
 )
 {{else}}
 
-import (
-	"github.com/lib/pq"
-	"github.com/zeromicro/go-zero/core/stores/sqlx"
-)
+import "github.com/zeromicro/go-zero/core/stores/sqlx"
 {{end}}
 var _ {{.upperStartCamelObject}}Model = (*custom{{.upperStartCamelObject}}Model)(nil)
 


Test patch:
diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go
index 97c27890..f445817d 100644
--- a/core/mapping/unmarshaler_test.go
+++ b/core/mapping/unmarshaler_test.go
@@ -10,6 +10,7 @@ import (
 
 	"github.com/google/uuid"
 	"github.com/stretchr/testify/assert"
+
 	"github.com/zeromicro/go-zero/core/stringx"
 )
 
@@ -539,7 +540,7 @@ func TestUnmarshalStringMapFromUnsupportedType(t *testing.T) {
 func TestUnmarshalStringMapFromNotSettableValue(t *testing.T) {
 	var v struct {
 		sort  map[string]string  `key:"sort"`
-		psort *map[string]string `key:"sort"`
+		psort *map[string]string `key:"psort"`
 	}
 	m := map[string]interface{}{
 		"sort":  `{"value":"ascend","emptyStr":""}`,