Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 116 additions & 18 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
apiFolder = gtest.DataPath("genctrl", "api")
apiFolder = gtest.DataPath("genctrl", "default", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: path,
Expand All @@ -39,7 +39,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {

err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
defer gfile.RemoveAll(path)

_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
Expand All @@ -49,7 +49,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
genApi = apiFolder + filepath.FromSlash("/article/article.go")
genApiExpect = apiFolder + filepath.FromSlash("/article/article_expect.go")
)
defer gfile.Remove(genApi)
defer gfile.RemoveAll(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))

// files
Expand All @@ -67,7 +67,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
})

// content
testPath := gtest.DataPath("genctrl", "controller")
testPath := gtest.DataPath("genctrl", "default", "controller")
expectFiles := []string{
testPath + filepath.FromSlash("/article/article.go"),
testPath + filepath.FromSlash("/article/article_new.go"),
Expand All @@ -84,6 +84,104 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
})
}

func Test_Gen_Ctrl_Default_Multi(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
apiFolder = gtest.DataPath("genctrl", "multi", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: path,
WatchFile: "",
SdkPath: "",
SdkStdVersion: false,
SdkNoV1: false,
Clear: false,
Merge: false,
}
)

err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)

err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.RemoveAll(path)

_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)

// apiInterface file
var (
genApiSlice = []string{
apiFolder + filepath.FromSlash("/admin/article/article.go"),
apiFolder + filepath.FromSlash("/admin/user/user.go"),
apiFolder + filepath.FromSlash("/app/user/user.go"),
apiFolder + filepath.FromSlash("/app/user/user_ext/user_ext.go"),
}
genApiSliceExpect = []string{
apiFolder + filepath.FromSlash("/admin/article/article_expect.go"),
apiFolder + filepath.FromSlash("/admin/user/user_expect.go"),
apiFolder + filepath.FromSlash("/app/user/user_expect.go"),
apiFolder + filepath.FromSlash("/app/user/user_ext/user_ext_expect.go"),
}
)

for i := range genApiSlice {
t.Assert(gfile.GetContents(genApiSlice[i]), gfile.GetContents(genApiSliceExpect[i]))
gfile.RemoveAll(genApiSlice[i])
}

// files
files, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(files, []string{
path + filepath.FromSlash("/admin/article/article.go"),
path + filepath.FromSlash("/admin/article/article_new.go"),
path + filepath.FromSlash("/admin/article/article_v1_create.go"),

path + filepath.FromSlash("/admin/user/user.go"),
path + filepath.FromSlash("/admin/user/user_new.go"),
path + filepath.FromSlash("/admin/user/user_v1_create.go"),

path + filepath.FromSlash("/app/user/user.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext_new.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext_v1_create.go"),
path + filepath.FromSlash("/app/user/user_ext/user_ext_v1_update.go"),

path + filepath.FromSlash("/app/user/user_new.go"),
path + filepath.FromSlash("/app/user/user_v1_create.go"),
path + filepath.FromSlash("/app/user/user_v1_update.go"),
})

// content
testPath := gtest.DataPath("genctrl", "multi", "controller")
expectFiles := []string{
testPath + filepath.FromSlash("/admin/article/article.go"),
testPath + filepath.FromSlash("/admin/article/article_new.go"),
testPath + filepath.FromSlash("/admin/article/article_v1_create.go"),

testPath + filepath.FromSlash("/admin/user/user.go"),
testPath + filepath.FromSlash("/admin/user/user_new.go"),
testPath + filepath.FromSlash("/admin/user/user_v1_create.go"),

testPath + filepath.FromSlash("/app/user/user.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext_new.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext_v1_create.go"),
testPath + filepath.FromSlash("/app/user/user_ext/user_ext_v1_update.go"),

testPath + filepath.FromSlash("/app/user/user_new.go"),
testPath + filepath.FromSlash("/app/user/user_v1_create.go"),
testPath + filepath.FromSlash("/app/user/user_v1_update.go"),
}
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})
}

func expectFilesContent(t *gtest.T, paths []string, expectPaths []string) {
for i, expectFile := range expectPaths {
val := gfile.GetContents(paths[i])
Expand All @@ -98,8 +196,8 @@ func Test_Gen_Ctrl_UseMerge_AddNewFile(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctrlPath = gfile.Temp(guid.S())
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl-merge", "add_new_file", "api")
// ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl", "merge", "add_new_file", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: ctrlPath,
Expand All @@ -118,7 +216,7 @@ type DictTypeAddRes struct {

err := gfile.Mkdir(ctrlPath)
t.AssertNil(err)
defer gfile.Remove(ctrlPath)
defer gfile.RemoveAll(ctrlPath)

_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
Expand All @@ -127,7 +225,7 @@ type DictTypeAddRes struct {
genApi = filepath.Join(apiFolder, "/dict/dict.go")
genApiExpect = filepath.Join(apiFolder, "/dict/dict_expect.go")
)
defer gfile.Remove(genApi)
defer gfile.RemoveAll(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))

genCtrlFiles, err := gfile.ScanDir(ctrlPath, "*.go", true)
Expand All @@ -138,7 +236,7 @@ type DictTypeAddRes struct {
filepath.Join(ctrlPath, "/dict/dict_v1_dict_type.go"),
})

expectCtrlPath := gtest.DataPath("genctrl-merge", "add_new_file", "controller")
expectCtrlPath := gtest.DataPath("genctrl", "merge", "add_new_file", "controller")
expectFiles := []string{
filepath.Join(expectCtrlPath, "/dict/dict.go"),
filepath.Join(expectCtrlPath, "/dict/dict_new.go"),
Expand All @@ -152,7 +250,7 @@ type DictTypeAddRes struct {
newApiFilePath := filepath.Join(apiFolder, "/dict/v1/test_new.go")
err = gfile.PutContents(newApiFilePath, testNewApiFile)
t.AssertNil(err)
defer gfile.Remove(newApiFilePath)
defer gfile.RemoveAll(newApiFilePath)

// Then execute the command
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
Expand All @@ -179,8 +277,8 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctrlPath = gfile.Temp(guid.S())
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl-merge", "add_new_ctrl", "api")
// ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("genctrl", "merge", "add_new_ctrl", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
DstFolder: ctrlPath,
Expand All @@ -190,7 +288,7 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {

err := gfile.Mkdir(ctrlPath)
t.AssertNil(err)
defer gfile.Remove(ctrlPath)
defer gfile.RemoveAll(ctrlPath)

_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
Expand All @@ -199,7 +297,7 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
genApi = filepath.Join(apiFolder, "/dict/dict.go")
genApiExpect = filepath.Join(apiFolder, "/dict/dict_expect.go")
)
defer gfile.Remove(genApi)
defer gfile.RemoveAll(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))

genCtrlFiles, err := gfile.ScanDir(ctrlPath, "*.go", true)
Expand All @@ -210,7 +308,7 @@ func Test_Gen_Ctrl_UseMerge_AddNewCtrl(t *testing.T) {
filepath.Join(ctrlPath, "/dict/dict_v1_dict_type.go"),
})

expectCtrlPath := gtest.DataPath("genctrl-merge", "add_new_ctrl", "controller")
expectCtrlPath := gtest.DataPath("genctrl", "merge", "add_new_ctrl", "controller")
expectFiles := []string{
filepath.Join(expectCtrlPath, "/dict/dict.go"),
filepath.Join(expectCtrlPath, "/dict/dict_new.go"),
Expand All @@ -236,7 +334,7 @@ type DictTypeAddRes struct {
err = gfile.PutContentsAppend(dictModuleFileName, testNewApiFile)
t.AssertNil(err)

//==================================
// ==================================
// Then execute the command
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
Expand All @@ -262,7 +360,7 @@ func Test_Gen_Ctrl_UseMerge_Issue3460(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
ctrlPath = gfile.Temp(guid.S())
//ctrlPath = gtest.DataPath("issue", "3460", "controller")
// ctrlPath = gtest.DataPath("issue", "3460", "controller")
apiFolder = gtest.DataPath("issue", "3460", "api")
in = genctrl.CGenCtrlInput{
SrcFolder: apiFolder,
Expand All @@ -278,7 +376,7 @@ func Test_Gen_Ctrl_UseMerge_Issue3460(t *testing.T) {

err := gfile.Mkdir(ctrlPath)
t.AssertNil(err)
defer gfile.Remove(ctrlPath)
defer gfile.RemoveAll(ctrlPath)

_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
t.AssertNil(err)
Expand Down
71 changes: 52 additions & 19 deletions cmd/gf/internal/cmd/genctrl/genctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,11 @@ func (c CGenCtrl) Ctrl(ctx context.Context, in CGenCtrlInput) (out *CGenCtrlOutp
if !gfile.Exists(in.SrcFolder) {
mlog.Fatalf(`source folder path "%s" does not exist`, in.SrcFolder)
}
// retrieve all api modules.
apiModuleFolderPaths, err := gfile.ScanDir(in.SrcFolder, "*", false)

err = c.generateByModules(in)
if err != nil {
return nil, err
}
for _, apiModuleFolderPath := range apiModuleFolderPaths {
if !gfile.IsDir(apiModuleFolderPath) {
continue
}
// generate go files by api module.
var (
module = gfile.Basename(apiModuleFolderPath)
dstModuleFolderPath = gfile.Join(in.DstFolder, module)
)
err = c.generateByModule(
apiModuleFolderPath, dstModuleFolderPath, in.SdkPath,
in.SdkStdVersion, in.SdkNoV1, in.Clear, in.Merge,
)
if err != nil {
return nil, err
}
}

mlog.Print(`done!`)
return
Expand Down Expand Up @@ -163,6 +146,56 @@ func (c CGenCtrl) generateByWatchFile(watchFile, sdkPath string, sdkStdVersion,
)
}

// generateByModules recursively calls generateByModule for multi-level modules generation.
func (c CGenCtrl) generateByModules(in CGenCtrlInput) (err error) {
// read root folder, example: api/user or api/app
moduleFolderPaths, err := gfile.ScanDir(in.SrcFolder, "*", false)
if err != nil {
return err
}
for _, moduleFolder := range moduleFolderPaths {
if !gfile.IsDir(moduleFolder) {
continue
}

// read children folder, example: api/user/v1 or api/app/user
childrenFolderPaths, err := gfile.ScanDir(moduleFolder, "*", false)
if err != nil {
return err
}
for _, childrenFolderPath := range childrenFolderPaths {
if !gfile.IsDir(childrenFolderPath) {
continue
}

var (
inCopy = in
module = gfile.Basename(moduleFolder)
)
inCopy.SrcFolder = gfile.Join(in.SrcFolder, module)
inCopy.DstFolder = gfile.Join(in.DstFolder, module)
err = c.generateByModules(inCopy)
if err != nil {
return err
}
}

// generate go files by api module.
var (
module = gfile.Basename(moduleFolder)
dstModuleFolderPath = gfile.Join(in.DstFolder, module)
)
err = c.generateByModule(
moduleFolder, dstModuleFolderPath, in.SdkPath,
in.SdkStdVersion, in.SdkNoV1, in.Clear, in.Merge,
)
if err != nil {
return err
}
}
return
}

// parseApiModule parses certain api and generate associated go files by certain module, not all api modules.
func (c CGenCtrl) generateByModule(
apiModuleFolderPath, dstModuleFolderPath, sdkPath string,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package article

import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article"
)

type ControllerV1 struct{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"

"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
)

// Create add title.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"

"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
)

func (c *ControllerV1) GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"

"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
)

func (c *ControllerV1) GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"

"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/api/article/v1"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genctrl/default/api/article/v1"
)

func (c *ControllerV1) Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error) {
Expand Down
Loading
Loading