feature: file namestyle (#223)

* add api filename style

* new feature: config.yaml

* optimize

* optimize logic generation

* check hanlder valid

* optimize

* reactor naming style

* optimize

* optimize test

* optimize gen middleware

* format

Co-authored-by: anqiansong <anqiansong@xiaoheiban.cn>
Co-authored-by: kim <xutao@xiaoheiban.cn>
This commit is contained in:
kingxt
2020-11-24 15:11:18 +08:00
committed by GitHub
parent 702e8d79ce
commit b9ac51b6c3
40 changed files with 896 additions and 296 deletions

View File

@@ -2,7 +2,6 @@ package command
import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
@@ -10,6 +9,7 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/core/stores/sqlx"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
@@ -24,9 +24,9 @@ const (
flagDir = "dir"
flagCache = "cache"
flagIdea = "idea"
flagStyle = "style"
flagUrl = "url"
flagTable = "table"
flagStyle = "style"
)
func MysqlDDL(ctx *cli.Context) error {
@@ -34,8 +34,13 @@ func MysqlDDL(ctx *cli.Context) error {
dir := ctx.String(flagDir)
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
namingStyle := strings.TrimSpace(ctx.String(flagStyle))
return fromDDl(src, dir, namingStyle, cache, idea)
style := ctx.String(flagStyle)
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
return fromDDl(src, dir, cfg, cache, idea)
}
func MyDataSource(ctx *cli.Context) error {
@@ -43,26 +48,23 @@ func MyDataSource(ctx *cli.Context) error {
dir := strings.TrimSpace(ctx.String(flagDir))
cache := ctx.Bool(flagCache)
idea := ctx.Bool(flagIdea)
namingStyle := strings.TrimSpace(ctx.String(flagStyle))
style := ctx.String(flagStyle)
pattern := strings.TrimSpace(ctx.String(flagTable))
return fromDataSource(url, pattern, dir, namingStyle, cache, idea)
cfg, err := config.NewConfig(style)
if err != nil {
return err
}
return fromDataSource(url, pattern, dir, cfg, cache, idea)
}
func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
func fromDDl(src, dir string, cfg *config.Config, cache, idea bool) error {
log := console.NewConsole(idea)
src = strings.TrimSpace(src)
if len(src) == 0 {
return errors.New("expected path or path globbing patterns, but nothing found")
}
switch namingStyle {
case gen.NamingLower, gen.NamingCamel, gen.NamingSnake:
case "":
namingStyle = gen.NamingLower
default:
return fmt.Errorf("unexpected naming style: %s", namingStyle)
}
files, err := util.MatchFiles(src)
if err != nil {
return err
@@ -81,7 +83,7 @@ func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
source = append(source, string(data))
}
generator, err := gen.NewDefaultGenerator(dir, namingStyle, gen.WithConsoleOption(log))
generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
if err != nil {
return err
}
@@ -90,7 +92,7 @@ func fromDDl(src, dir, namingStyle string, cache, idea bool) error {
return err
}
func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) error {
func fromDataSource(url, pattern, dir string, cfg *config.Config, cache, idea bool) error {
log := console.NewConsole(idea)
if len(url) == 0 {
log.Error("%v", "expected data source of mysql, but nothing found")
@@ -102,25 +104,17 @@ func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) err
return nil
}
switch namingStyle {
case gen.NamingLower, gen.NamingCamel, gen.NamingSnake:
case "":
namingStyle = gen.NamingLower
default:
return fmt.Errorf("unexpected naming style: %s", namingStyle)
}
cfg, err := mysql.ParseDSN(url)
dsn, err := mysql.ParseDSN(url)
if err != nil {
return err
}
logx.Disable()
databaseSource := strings.TrimSuffix(url, "/"+cfg.DBName) + "/information_schema"
databaseSource := strings.TrimSuffix(url, "/"+dsn.DBName) + "/information_schema"
db := sqlx.NewMysql(databaseSource)
im := model.NewInformationSchemaModel(db)
tables, err := im.GetAllTables(cfg.DBName)
tables, err := im.GetAllTables(dsn.DBName)
if err != nil {
return err
}
@@ -135,7 +129,7 @@ func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) err
if !match {
continue
}
columns, err := im.FindByTableName(cfg.DBName, item)
columns, err := im.FindByTableName(dsn.DBName, item)
if err != nil {
return err
}
@@ -146,11 +140,11 @@ func fromDataSource(url, pattern, dir, namingStyle string, cache, idea bool) err
return errors.New("no tables matched")
}
generator, err := gen.NewDefaultGenerator(dir, namingStyle, gen.WithConsoleOption(log))
generator, err := gen.NewDefaultGenerator(dir, cfg, gen.WithConsoleOption(log))
if err != nil {
return err
}
err = generator.StartFromInformationSchema(cfg.DBName, matchTables, cache)
err = generator.StartFromInformationSchema(dsn.DBName, matchTables, cache)
return err
}

View File

@@ -7,19 +7,22 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/util"
)
var sql = "-- 用户表 --\nCREATE TABLE `user` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\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 `name_index` (`name`),\n UNIQUE KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;\n\n"
var cfg = &config.Config{
NamingFormat: "gozero",
}
func TestFromDDl(t *testing.T) {
err := fromDDl("./user.sql", t.TempDir(), gen.NamingCamel, true, false)
err := fromDDl("./user.sql", t.TempDir(), cfg, true, false)
assert.Equal(t, errNotMatched, err)
// case dir is not exists
unknownDir := filepath.Join(t.TempDir(), "test", "user.sql")
err = fromDDl(unknownDir, t.TempDir(), gen.NamingCamel, true, false)
err = fromDDl(unknownDir, t.TempDir(), cfg, true, false)
assert.True(t, func() bool {
switch err.(type) {
case *os.PathError:
@@ -30,18 +33,11 @@ func TestFromDDl(t *testing.T) {
}())
// case empty src
err = fromDDl("", t.TempDir(), gen.NamingCamel, true, false)
err = fromDDl("", t.TempDir(), cfg, true, false)
if err != nil {
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
}
// case unknown naming style
tmp := filepath.Join(t.TempDir(), "user.sql")
err = fromDDl(tmp, t.TempDir(), "lower1", true, false)
if err != nil {
assert.Equal(t, "unexpected naming style: lower1", err.Error())
}
tempDir := filepath.Join(t.TempDir(), "test")
err = util.MkdirIfNotExist(tempDir)
if err != nil {
@@ -67,7 +63,7 @@ func TestFromDDl(t *testing.T) {
_, err = os.Stat(user2Sql)
assert.Nil(t, err)
err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, gen.NamingLower, true, false)
err = fromDDl(filepath.Join(tempDir, "user*.sql"), tempDir, cfg, true, false)
assert.Nil(t, err)
_, err = os.Stat(filepath.Join(tempDir, "usermodel.go"))