Skip to content

Commit

Permalink
reactor rpc (#179)
Browse files Browse the repository at this point in the history
* reactor rpc generation

* update flag

* update command

* update command

* update unit test

* delete test file

* optimize code

* update doc

* update gen pb

* rename target dir

* update mysql data type convert rule

* add done flag

* optimize req/reply parameter

* optimize req/reply parameter

* remove waste code

* remove duplicate parameter

* format code

* format code

* optimize naming

* reactor rpcv2 to rpc

* remove new line

* format code

* rename underline to snake

* reactor getParentPackage

* remove debug log

* reactor background
  • Loading branch information
Keson authored Nov 5, 2020
1 parent c9ec22d commit e76f44a
Show file tree
Hide file tree
Showing 95 changed files with 2,698 additions and 3,291 deletions.
21 changes: 6 additions & 15 deletions tools/goctl/api/gogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,20 @@ import (
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
goctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/project"
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
)

func getParentPackage(dir string) (string, error) {
p, err := project.Prepare(dir, false)
abs, err := filepath.Abs(dir)
if err != nil {
return "", err
}

if len(p.GoMod.Path) > 0 {
goModePath := filepath.Clean(filepath.Dir(p.GoMod.Path))
absPath, err := filepath.Abs(dir)
if err != nil {
return "", err
}
parent := filepath.Clean(goctlutil.JoinPackages(p.GoMod.Module, absPath[len(goModePath):]))
parent = strings.ReplaceAll(parent, "\\", "/")
parent = strings.ReplaceAll(parent, `\`, "/")
return parent, nil
projectCtx, err := ctx.Prepare(abs)
if err != nil {
return "", err
}

return p.Package, nil
return filepath.ToSlash(filepath.Join(projectCtx.Path, strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir))), nil
}

func writeIndent(writer io.Writer, indent int) {
Expand Down
20 changes: 10 additions & 10 deletions tools/goctl/goctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/configgen"
"github.com/tal-tech/go-zero/tools/goctl/docker"
model "github.com/tal-tech/go-zero/tools/goctl/model/sql/command"
rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/command"
rpc "github.com/tal-tech/go-zero/tools/goctl/rpc/cli"
"github.com/tal-tech/go-zero/tools/goctl/tpl"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -211,7 +211,7 @@ var (
Flags: []cli.Flag{
cli.BoolFlag{
Name: "idea",
Usage: "whether the command execution environment is from idea plugin. [option]",
Usage: "whether the command execution environment is from idea plugin. [optional]",
},
},
Action: rpc.RpcNew,
Expand All @@ -226,7 +226,7 @@ var (
},
cli.BoolFlag{
Name: "idea",
Usage: "whether the command execution environment is from idea plugin. [option]",
Usage: "whether the command execution environment is from idea plugin. [optional]",
},
},
Action: rpc.RpcTemplate,
Expand All @@ -239,17 +239,17 @@ var (
Name: "src, s",
Usage: "the file path of the proto source file",
},
cli.StringFlag{
Name: "dir, d",
Usage: `the target path of the code,default path is "${pwd}". [option]`,
cli.StringSliceFlag{
Name: "proto_path, I",
Usage: `native command of protoc, specify the directory in which to search for imports. [optional]`,
},
cli.StringFlag{
Name: "service, srv",
Usage: `the name of rpc service. [option]`,
Name: "dir, d",
Usage: `the target path of the code`,
},
cli.BoolFlag{
Name: "idea",
Usage: "whether the command execution environment is from idea plugin. [option]",
Usage: "whether the command execution environment is from idea plugin. [optional]",
},
},
Action: rpc.Rpc,
Expand Down Expand Up @@ -313,7 +313,7 @@ var (
},
cli.StringFlag{
Name: "style",
Usage: "the file naming style, lower|camel|underline,default is lower",
Usage: "the file naming style, lower|camel|snake, default is lower",
},
cli.BoolFlag{
Name: "idea",
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func MysqlDDL(ctx *cli.Context) error {
}

switch namingStyle {
case gen.NamingLower, gen.NamingCamel, gen.NamingUnderline:
case gen.NamingLower, gen.NamingCamel, gen.NamingSnake:
case "":
namingStyle = gen.NamingLower
default:
Expand Down Expand Up @@ -87,7 +87,7 @@ func MyDataSource(ctx *cli.Context) error {
}

switch namingStyle {
case gen.NamingLower, gen.NamingCamel, gen.NamingUnderline:
case gen.NamingLower, gen.NamingCamel, gen.NamingSnake:
case "":
namingStyle = gen.NamingLower
default:
Expand Down
42 changes: 24 additions & 18 deletions tools/goctl/model/sql/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,36 @@ import (
var (
commonMysqlDataTypeMap = map[string]string{
// For consistency, all integer types are converted to int64
"tinyint": "int64",
"smallint": "int64",
"mediumint": "int64",
"int": "int64",
"integer": "int64",
"bigint": "int64",
"float": "float64",
"double": "float64",
"decimal": "float64",
"date": "time.Time",
"time": "string",
"year": "int64",
"datetime": "time.Time",
"timestamp": "time.Time",
// number
"bool": "int64",
"boolean": "int64",
"tinyint": "int64",
"smallint": "int64",
"mediumint": "int64",
"int": "int64",
"integer": "int64",
"bigint": "int64",
"float": "float64",
"double": "float64",
"decimal": "float64",
// date&time
"date": "time.Time",
"datetime": "time.Time",
"timestamp": "time.Time",
"time": "string",
"year": "int64",
// string
"char": "string",
"varchar": "string",
"tinyblob": "string",
"binary": "string",
"varbinary": "string",
"tinytext": "string",
"blob": "string",
"text": "string",
"mediumblob": "string",
"mediumtext": "string",
"longblob": "string",
"longtext": "string",
"enum": "string",
"set": "string",
"json": "string",
}
)

Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/model/sql/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
createTableFlag = `(?m)^(?i)CREATE\s+TABLE` // ignore case
NamingLower = "lower"
NamingCamel = "camel"
NamingUnderline = "underline"
NamingSnake = "snake"
)

type (
Expand Down Expand Up @@ -81,7 +81,7 @@ func (g *defaultGenerator) Start(withCache bool) error {
switch g.namingStyle {
case NamingCamel:
name = fmt.Sprintf("%sModel.go", tn.ToCamel())
case NamingUnderline:
case NamingSnake:
name = fmt.Sprintf("%s_model.go", tn.ToSnake())
}
filename := filepath.Join(dirAbs, name)
Expand Down
40 changes: 35 additions & 5 deletions tools/goctl/model/sql/gen/gen_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
package gen

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/logx"
)

var (
source = "CREATE TABLE `test_user_info` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `nanosecond` bigint NOT NULL DEFAULT '0',\n `data` varchar(255) DEFAULT '',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `nanosecond_unique` (`nanosecond`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"
source = "CREATE TABLE `test_user_info` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `nanosecond` bigint NOT NULL DEFAULT '0',\n `data` varchar(255) DEFAULT '',\n `content` json DEFAULT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `nanosecond_unique` (`nanosecond`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;"
)

func TestCacheModel(t *testing.T) {
logx.Disable()
_ = Clean()
g := NewDefaultGenerator(source, "./testmodel/cache", NamingLower)
dir, _ := filepath.Abs("./testmodel")
cacheDir := filepath.Join(dir, "cache")
noCacheDir := filepath.Join(dir, "nocache")
defer func() {
_ = os.RemoveAll(dir)
}()
g := NewDefaultGenerator(source, cacheDir, NamingLower)
err := g.Start(true)
assert.Nil(t, err)
g = NewDefaultGenerator(source, "./testmodel/nocache", NamingLower)
assert.True(t, func() bool {
_, err := os.Stat(filepath.Join(cacheDir, "testuserinfomodel.go"))
return err == nil
}())
g = NewDefaultGenerator(source, noCacheDir, NamingLower)
err = g.Start(false)
assert.Nil(t, err)
assert.True(t, func() bool {
_, err := os.Stat(filepath.Join(noCacheDir, "testuserinfomodel.go"))
return err == nil
}())
}

func TestNamingModel(t *testing.T) {
logx.Disable()
_ = Clean()
g := NewDefaultGenerator(source, "./testmodel/camel", NamingCamel)
dir, _ := filepath.Abs("./testmodel")
camelDir := filepath.Join(dir, "camel")
snakeDir := filepath.Join(dir, "snake")
defer func() {
_ = os.RemoveAll(dir)
}()
g := NewDefaultGenerator(source, camelDir, NamingCamel)
err := g.Start(true)
assert.Nil(t, err)
g = NewDefaultGenerator(source, "./testmodel/snake", NamingUnderline)
assert.True(t, func() bool {
_, err := os.Stat(filepath.Join(camelDir, "TestUserInfoModel.go"))
return err == nil
}())
g = NewDefaultGenerator(source, snakeDir, NamingSnake)
err = g.Start(true)
assert.Nil(t, err)
assert.True(t, func() bool {
_, err := os.Stat(filepath.Join(snakeDir, "test_user_info_model.go"))
return err == nil
}())
}
125 changes: 0 additions & 125 deletions tools/goctl/model/sql/gen/testmodel/cache/testuserinfomodel.go

This file was deleted.

5 changes: 0 additions & 5 deletions tools/goctl/model/sql/gen/testmodel/cache/vars.go

This file was deleted.

Loading

0 comments on commit e76f44a

Please sign in to comment.