Repository: zeromicro/go-zero
Pull request #1569 (2022-02-23 08:25:54)

feat: supports `importValue` for more path formats
`importValueRegex` now can match more path formats

Resolves: #1568


Patch:
diff --git a/tools/goctl/api/parser/g4/ast/api.go b/tools/goctl/api/parser/g4/ast/api.go
index d657b9f4..cc747eaf 100644
--- a/tools/goctl/api/parser/g4/ast/api.go
+++ b/tools/goctl/api/parser/g4/ast/api.go
@@ -8,8 +8,10 @@ import (
 	"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
 )
 
-const prefixKey = "prefix"
-const groupKey = "group"
+const (
+	prefixKey = "prefix"
+	groupKey  = "group"
+)
 
 // Api describes syntax for api
 type Api struct {
diff --git a/tools/goctl/api/parser/g4/ast/apiparser.go b/tools/goctl/api/parser/g4/ast/apiparser.go
index 29d69da3..2cb0e21d 100644
--- a/tools/goctl/api/parser/g4/ast/apiparser.go
+++ b/tools/goctl/api/parser/g4/ast/apiparser.go
@@ -114,13 +114,16 @@ func (p *Parser) parse(filename, content string) (*Api, error) {
 	apiAstList = append(apiAstList, root)
 	for _, imp := range root.Import {
 		dir := filepath.Dir(p.src)
-		imp := filepath.Join(dir, imp.Value.Text())
-		data, err := p.readContent(imp)
+		impPath := strings.ReplaceAll(imp.Value.Text(), "\"", "")
+		if !filepath.IsAbs(impPath) {
+			impPath = filepath.Join(dir, impPath)
+		}
+		data, err := p.readContent(impPath)
 		if err != nil {
 			return nil, err
 		}
 
-		nestedApi, err := p.invoke(imp, data)
+		nestedApi, err := p.invoke(impPath, data)
 		if err != nil {
 			return nil, err
 		}
diff --git a/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go b/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go
index 82cd78d2..8599337c 100755
--- a/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go
+++ b/tools/goctl/api/parser/g4/gen/api/apiparser_parser.go
@@ -634,4 +634,3 @@ func NewSyntaxLitContext(parser antlr.Parser, parent antlr.ParserRuleContext, in
 
 	return p
 }
-
diff --git a/tools/goctl/api/parser/g4/gen/api/baseparser.go b/tools/goctl/api/parser/g4/gen/api/baseparser.go
index 3d6772e5..32b53093 100644
--- a/tools/goctl/api/parser/g4/gen/api/baseparser.go
+++ b/tools/goctl/api/parser/g4/gen/api/baseparser.go
@@ -12,7 +12,7 @@ import (
 
 const (
 	versionRegex     = `(?m)"v[1-9][0-9]*"`
-	importValueRegex = `(?m)"(/?[a-zA-Z0-9_#-])+\.api"`
+	importValueRegex = `(?m)"\/?(([a-zA-Z0-9.]+)+(\/?){1})+([a-zA-Z0-9]+)+\.api"`
 	tagRegex         = `(?m)\x60[a-z]+:".+"\x60`
 )
 
diff --git a/tools/goctl/completion/const.go b/tools/goctl/completion/const.go
index 2dd95714..4cf81bc0 100644
--- a/tools/goctl/completion/const.go
+++ b/tools/goctl/completion/const.go
@@ -1,7 +1,9 @@
 package completion
 
-const BashCompletionFlag = `generate-goctl-completion`
-const defaultCompletionFilename = "goctl_autocomplete"
+const (
+	BashCompletionFlag        = `generate-goctl-completion`
+	defaultCompletionFilename = "goctl_autocomplete"
+)
 const (
 	magic = 1 << iota
 	flagZsh
diff --git a/tools/goctl/env/check.go b/tools/goctl/env/check.go
index 723e9b96..c6e21d76 100644
--- a/tools/goctl/env/check.go
+++ b/tools/goctl/env/check.go
@@ -44,7 +44,7 @@ func Check(ctx *cli.Context) error {
 }
 
 func check(install, force bool) error {
-	var pending = true
+	pending := true
 	console.Info("[goctl-env]: preparing to check env")
 	defer func() {
 		if p := recover(); p != nil {
diff --git a/tools/goctl/go.mod b/tools/goctl/go.mod
index f43d8f30..bd61025f 100644
--- a/tools/goctl/go.mod
+++ b/tools/goctl/go.mod
@@ -1,6 +1,6 @@
 module github.com/zeromicro/go-zero/tools/goctl
 
-go 1.15
+go 1.16
 
 require (
 	github.com/DATA-DOG/go-sqlmock v1.5.0
diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go
index 304387c7..6cc7869c 100644
--- a/tools/goctl/goctl.go
+++ b/tools/goctl/goctl.go
@@ -690,7 +690,7 @@ var commands = []cli.Command{
 						Flags: []cli.Flag{
 							cli.StringFlag{
 								Name:  "url",
-								Usage: `the data source of database,like "postgres://root:password@127.0.0.1:54332/database?sslmode=disable"`,
+								Usage: `the data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"`,
 							},
 							cli.StringFlag{
 								Name:  "table, t",
diff --git a/tools/goctl/migrate/proxy.go b/tools/goctl/migrate/proxy.go
index b5218940..610f2bab 100644
--- a/tools/goctl/migrate/proxy.go
+++ b/tools/goctl/migrate/proxy.go
@@ -9,8 +9,10 @@ import (
 	"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
 )
 
-var defaultProxy = "https://goproxy.cn"
-var defaultProxies = []string{defaultProxy}
+var (
+	defaultProxy   = "https://goproxy.cn"
+	defaultProxies = []string{defaultProxy}
+)
 
 func goProxy() []string {
 	wd, err := os.Getwd()
diff --git a/tools/goctl/model/sql/model/informationschemamodel.go b/tools/goctl/model/sql/model/infoschemamodel.go
similarity index 100%
rename from tools/goctl/model/sql/model/informationschemamodel.go
rename to tools/goctl/model/sql/model/infoschemamodel.go
diff --git a/tools/goctl/model/sql/model/postgresqlmodel.go b/tools/goctl/model/sql/model/postgresqlmodel.go
index ad20ee49..95b0c7cf 100644
--- a/tools/goctl/model/sql/model/postgresqlmodel.go
+++ b/tools/goctl/model/sql/model/postgresqlmodel.go
@@ -108,6 +108,7 @@ func (m *PostgreSqlModel) getColumns(schema, table string, in []*PostgreColumn)
 	if err != nil {
 		return nil, err
 	}
+
 	var list []*Column
 	for _, e := range in {
 		var dft interface{}
@@ -120,9 +121,15 @@ func (m *PostgreSqlModel) getColumns(schema, table string, in []*PostgreColumn)
 			isNullAble = "NO"
 		}
 
-		extra := "auto_increment"
-		if e.IdentityIncrement.Int32 != 1 {
-			extra = ""
+		var extra string
+		// when identity is true, the column is auto increment
+		if e.IdentityIncrement.Int32 == 1 {
+			extra = "auto_increment"
+		}
+
+		// when type is serial, it's auto_increment. and the default value is tablename_columnname_seq
+		if strings.Contains(e.ColumnDefault.String, table+"_"+e.Field.String+"_seq") {
+			extra = "auto_increment"
 		}
 
 		if len(index[e.Field.String]) > 0 {
@@ -172,6 +179,7 @@ func (m *PostgreSqlModel) getIndex(schema, table string) (map[string][]*DbIndex,
 	if err != nil {
 		return nil, err
 	}
+
 	index := make(map[string][]*DbIndex)
 	for _, e := range indexes {
 		if e.IsPrimary.Bool {
@@ -193,6 +201,7 @@ func (m *PostgreSqlModel) getIndex(schema, table string) (map[string][]*DbIndex,
 			SeqInIndex: int(e.IndexSort.Int32),
 		})
 	}
+
 	return index, nil
 }
 
diff --git a/tools/goctl/pkg/collection/sortedmap.go b/tools/goctl/pkg/collection/sortedmap.go
index 90ed2ab1..456170a5 100644
--- a/tools/goctl/pkg/collection/sortedmap.go
+++ b/tools/goctl/pkg/collection/sortedmap.go
@@ -9,8 +9,10 @@ import (
 	"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
 )
 
-var ErrInvalidKVExpression = errors.New(`invalid key-value expression`)
-var ErrInvalidKVS = errors.New("the length of kv must be a even number")
+var (
+	ErrInvalidKVExpression = errors.New(`invalid key-value expression`)
+	ErrInvalidKVS          = errors.New("the length of kv must be a even number")
+)
 
 type KV []interface{}
 
@@ -193,7 +195,7 @@ func (m *SortedMap) Copy() *SortedMap {
 }
 
 func (m *SortedMap) Format() []string {
-	var format = make([]string, 0)
+	format := make([]string, 0)
 	m.Range(func(key, value interface{}) {
 		format = append(format, fmt.Sprintf("%s=%s", key, value))
 	})
diff --git a/tools/goctl/pkg/env/env.go b/tools/goctl/pkg/env/env.go
index 32dea269..0e97e9e6 100644
--- a/tools/goctl/pkg/env/env.go
+++ b/tools/goctl/pkg/env/env.go
@@ -143,5 +143,5 @@ func WriteEnv(kv []string) error {
 		return err
 	}
 	envFile := filepath.Join(defaultGoctlHome, envFileDir)
-	return ioutil.WriteFile(envFile, []byte(strings.Join(goctlEnv.Format(), "\n")), 0777)
+	return ioutil.WriteFile(envFile, []byte(strings.Join(goctlEnv.Format(), "\n")), 0o777)
 }
diff --git a/tools/goctl/rpc/cli/zrpc.go b/tools/goctl/rpc/cli/zrpc.go
index 37b6e3de..f48bd87e 100644
--- a/tools/goctl/rpc/cli/zrpc.go
+++ b/tools/goctl/rpc/cli/zrpc.go
@@ -80,7 +80,7 @@ func ZRPC(c *cli.Context) error {
 		return err
 	}
 
-	var isGooglePlugin = len(grpcOut) > 0
+	isGooglePlugin := len(grpcOut) > 0
 	// If grpcOut is not empty means that user generates grpc code by
 	// https://google.golang.org/protobuf/cmd/protoc-gen-go and
 	// https://google.golang.org/grpc/cmd/protoc-gen-go-grpc,


Test patch:
diff --git a/core/mapping/yamlunmarshaler_test.go b/core/mapping/yamlunmarshaler_test.go
index a44c6c36..3a4d5066 100644
--- a/core/mapping/yamlunmarshaler_test.go
+++ b/core/mapping/yamlunmarshaler_test.go
@@ -937,7 +937,6 @@ func TestUnmarshalYamlReaderError(t *testing.T) {
 	reader = strings.NewReader("chenquan")
 	err = UnmarshalYamlReader(reader, &v)
 	assert.ErrorIs(t, err, ErrUnsupportedType)
-
 }
 
 func TestUnmarshalYamlBadReader(t *testing.T) {
diff --git a/tools/goctl/api/parser/g4/gen/api/baseparser_test.go b/tools/goctl/api/parser/g4/gen/api/baseparser_test.go
index d50ec323..9cd29ca7 100644
--- a/tools/goctl/api/parser/g4/gen/api/baseparser_test.go
+++ b/tools/goctl/api/parser/g4/gen/api/baseparser_test.go
@@ -9,3 +9,24 @@ import (
 func TestMatch(t *testing.T) {
 	assert.False(t, matchRegex("v1ddd", versionRegex))
 }
+
+func TestImportRegex(t *testing.T) {
+	tests := []struct {
+		value   string
+		matched bool
+	}{
+		{`"bar.api"`, true},
+		{`"foo/bar.api"`, true},
+		{`"/foo/bar.api"`, true},
+		{`"../foo/bar.api"`, true},
+		{`"../../foo/bar.api"`, true},
+
+		{`"bar..api"`, false},
+		{`"//bar.api"`, false},
+	}
+	for _, tt := range tests {
+		t.Run(tt.value, func(t *testing.T) {
+			assert.Equal(t, tt.matched, matchRegex(tt.value, importValueRegex))
+		})
+	}
+}
diff --git a/tools/goctl/rpc/cli/zrpc_test.go b/tools/goctl/rpc/cli/zrpc_test.go
index e809f841..54ce6ff5 100644
--- a/tools/goctl/rpc/cli/zrpc_test.go
+++ b/tools/goctl/rpc/cli/zrpc_test.go
@@ -23,7 +23,7 @@ func Test_GetSourceProto(t *testing.T) {
 		return
 	}
 
-	var testData = []test{
+	testData := []test{
 		{
 			source:   []string{"a.proto"},
 			expected: filepath.Join(pwd, "a.proto"),
@@ -54,7 +54,7 @@ func Test_GetSourceProto(t *testing.T) {
 }
 
 func Test_RemoveGoctlFlag(t *testing.T) {
-	var testData = []test{
+	testData := []test{
 		{
 			source:   strings.Fields("protoc foo.proto --go_out=. --go_opt=bar --zrpc_out=. --style go-zero --home=foo"),
 			expected: "protoc foo.proto --go_out=. --go_opt=bar",
@@ -87,7 +87,7 @@ func Test_RemoveGoctlFlag(t *testing.T) {
 }
 
 func Test_RemovePluginFlag(t *testing.T) {
-	var testData = []test{
+	testData := []test{
 		{
 			source:   strings.Fields("plugins=grpc:."),
 			expected: ".",