mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
feat(cmd/gf): add controller comment support for command gf gen ctrl
(#4169)
This commit is contained in:
parent
63cb3285f8
commit
bda42d18ee
@ -6,7 +6,11 @@
|
|||||||
|
|
||||||
package genctrl
|
package genctrl
|
||||||
|
|
||||||
import "github.com/gogf/gf/v2/text/gstr"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
|
)
|
||||||
|
|
||||||
type apiItem struct {
|
type apiItem struct {
|
||||||
Import string `eg:"demo.com/api/user/v1"`
|
Import string `eg:"demo.com/api/user/v1"`
|
||||||
@ -14,6 +18,7 @@ type apiItem struct {
|
|||||||
Module string `eg:"user"`
|
Module string `eg:"user"`
|
||||||
Version string `eg:"v1"`
|
Version string `eg:"v1"`
|
||||||
MethodName string `eg:"GetList"`
|
MethodName string `eg:"GetList"`
|
||||||
|
Comment string `eg:"GetList get list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a apiItem) String() string {
|
func (a apiItem) String() string {
|
||||||
@ -21,3 +26,12 @@ func (a apiItem) String() string {
|
|||||||
a.Import, a.Module, a.Version, a.MethodName,
|
a.Import, a.Module, a.Version, a.MethodName,
|
||||||
}, ",")
|
}, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetComment returns the comment of apiItem.
|
||||||
|
func (a apiItem) GetComment() string {
|
||||||
|
if a.Comment == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
// format for handling comments
|
||||||
|
return fmt.Sprintf("\n// %s %s", a.MethodName, a.Comment)
|
||||||
|
}
|
||||||
|
@ -17,9 +17,14 @@ import (
|
|||||||
"github.com/gogf/gf/v2/text/gstr"
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getStructsNameInSrc retrieves all struct names
|
type structInfo struct {
|
||||||
|
structName string
|
||||||
|
comment string
|
||||||
|
}
|
||||||
|
|
||||||
|
// getStructsNameInSrc retrieves all struct names and comment
|
||||||
// that end in "Req" and have "g.Meta" in their body.
|
// that end in "Req" and have "g.Meta" in their body.
|
||||||
func (c CGenCtrl) getStructsNameInSrc(filePath string) (structsName []string, err error) {
|
func (c CGenCtrl) getStructsNameInSrc(filePath string) (structInfos []*structInfo, err error) {
|
||||||
var (
|
var (
|
||||||
fileContent = gfile.GetContents(filePath)
|
fileContent = gfile.GetContents(filePath)
|
||||||
fileSet = token.NewFileSet()
|
fileSet = token.NewFileSet()
|
||||||
@ -32,8 +37,8 @@ func (c CGenCtrl) getStructsNameInSrc(filePath string) (structsName []string, er
|
|||||||
|
|
||||||
ast.Inspect(node, func(n ast.Node) bool {
|
ast.Inspect(node, func(n ast.Node) bool {
|
||||||
if typeSpec, ok := n.(*ast.TypeSpec); ok {
|
if typeSpec, ok := n.(*ast.TypeSpec); ok {
|
||||||
methodName := typeSpec.Name.Name
|
structName := typeSpec.Name.Name
|
||||||
if !gstr.HasSuffix(methodName, "Req") {
|
if !gstr.HasSuffix(structName, "Req") {
|
||||||
// ignore struct name that do not end in "Req"
|
// ignore struct name that do not end in "Req"
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -46,7 +51,19 @@ func (c CGenCtrl) getStructsNameInSrc(filePath string) (structsName []string, er
|
|||||||
if !gstr.Contains(buf.String(), `g.Meta`) {
|
if !gstr.Contains(buf.String(), `g.Meta`) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
structsName = append(structsName, methodName)
|
|
||||||
|
comment := typeSpec.Doc.Text()
|
||||||
|
// remove the struct name from the comment
|
||||||
|
if gstr.HasPrefix(comment, structName) {
|
||||||
|
comment = gstr.TrimLeftStr(comment, structName, 1)
|
||||||
|
}
|
||||||
|
// remove the comment \n or space
|
||||||
|
comment = gstr.Trim(comment)
|
||||||
|
|
||||||
|
structInfos = append(structInfos, &structInfo{
|
||||||
|
structName: structName,
|
||||||
|
comment: comment,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -39,15 +39,16 @@ func (c CGenCtrl) getApiItemsInSrc(apiModuleFolderPath string) (items []apiItem,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, methodName := range structsInfo {
|
for _, s := range structsInfo {
|
||||||
// remove end "Req"
|
// remove end "Req"
|
||||||
methodName = gstr.TrimRightStr(methodName, "Req", 1)
|
methodName := gstr.TrimRightStr(s.structName, "Req", 1)
|
||||||
item := apiItem{
|
item := apiItem{
|
||||||
Import: gstr.Trim(importPath, `"`),
|
Import: gstr.Trim(importPath, `"`),
|
||||||
FileName: gfile.Name(apiFileFolderPath),
|
FileName: gfile.Name(apiFileFolderPath),
|
||||||
Module: gfile.Basename(apiModuleFolderPath),
|
Module: gfile.Basename(apiModuleFolderPath),
|
||||||
Version: gfile.Basename(apiVersionFolderPath),
|
Version: gfile.Basename(apiVersionFolderPath),
|
||||||
MethodName: methodName,
|
MethodName: methodName,
|
||||||
|
Comment: s.comment,
|
||||||
}
|
}
|
||||||
items = append(items, item)
|
items = append(items, item)
|
||||||
}
|
}
|
||||||
|
@ -138,10 +138,11 @@ func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, ite
|
|||||||
|
|
||||||
if gfile.Exists(methodFilePath) {
|
if gfile.Exists(methodFilePath) {
|
||||||
content = gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFuncMerge, g.MapStrStr{
|
content = gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFuncMerge, g.MapStrStr{
|
||||||
"{Module}": item.Module,
|
"{Module}": item.Module,
|
||||||
"{CtrlName}": ctrlName,
|
"{CtrlName}": ctrlName,
|
||||||
"{Version}": item.Version,
|
"{Version}": item.Version,
|
||||||
"{MethodName}": item.MethodName,
|
"{MethodName}": item.MethodName,
|
||||||
|
"{MethodComment}": item.GetComment(),
|
||||||
})
|
})
|
||||||
|
|
||||||
if gstr.Contains(gfile.GetContents(methodFilePath), fmt.Sprintf(`func (c *%v) %v(`, ctrlName, item.MethodName)) {
|
if gstr.Contains(gfile.GetContents(methodFilePath), fmt.Sprintf(`func (c *%v) %v(`, ctrlName, item.MethodName)) {
|
||||||
@ -152,11 +153,12 @@ func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, ite
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
content = gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFunc, g.MapStrStr{
|
content = gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFunc, g.MapStrStr{
|
||||||
"{Module}": item.Module,
|
"{Module}": item.Module,
|
||||||
"{ImportPath}": item.Import,
|
"{ImportPath}": item.Import,
|
||||||
"{CtrlName}": ctrlName,
|
"{CtrlName}": ctrlName,
|
||||||
"{Version}": item.Version,
|
"{Version}": item.Version,
|
||||||
"{MethodName}": item.MethodName,
|
"{MethodName}": item.MethodName,
|
||||||
|
"{MethodComment}": item.GetComment(),
|
||||||
})
|
})
|
||||||
if err = gfile.PutContents(methodFilePath, gstr.TrimLeft(content)); err != nil {
|
if err = gfile.PutContents(methodFilePath, gstr.TrimLeft(content)); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -192,10 +194,11 @@ func (c *controllerGenerator) doGenerateCtrlMergeItem(dstModuleFolderPath string
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctrl := gstr.TrimLeft(gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFuncMerge, g.MapStrStr{
|
ctrl := gstr.TrimLeft(gstr.ReplaceByMap(consts.TemplateGenCtrlControllerMethodFuncMerge, g.MapStrStr{
|
||||||
"{Module}": api.Module,
|
"{Module}": api.Module,
|
||||||
"{CtrlName}": fmt.Sprintf(`Controller%s`, gstr.UcFirst(api.Version)),
|
"{CtrlName}": fmt.Sprintf(`Controller%s`, gstr.UcFirst(api.Version)),
|
||||||
"{Version}": api.Version,
|
"{Version}": api.Version,
|
||||||
"{MethodName}": api.MethodName,
|
"{MethodName}": api.MethodName,
|
||||||
|
"{MethodComment}": api.GetComment(),
|
||||||
}))
|
}))
|
||||||
ctrlFileItem.controllers.WriteString(ctrl)
|
ctrlFileItem.controllers.WriteString(ctrl)
|
||||||
doneApiSet.Add(api.String())
|
doneApiSet.Add(api.String())
|
||||||
|
@ -180,6 +180,7 @@ func (c *apiSdkGenerator) doGenerateSdkImplementer(
|
|||||||
"{Version}": item.Version,
|
"{Version}": item.Version,
|
||||||
"{MethodName}": item.MethodName,
|
"{MethodName}": item.MethodName,
|
||||||
"{ImplementerName}": implementerName,
|
"{ImplementerName}": implementerName,
|
||||||
|
"{MethodComment}": item.GetComment(),
|
||||||
}))
|
}))
|
||||||
implementerFileContent += "\n"
|
implementerFileContent += "\n"
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ package v1
|
|||||||
import "github.com/gogf/gf/v2/frame/g"
|
import "github.com/gogf/gf/v2/frame/g"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// CreateReq add title.
|
||||||
CreateReq struct {
|
CreateReq struct {
|
||||||
g.Meta `path:"/article/create" method:"post" tags:"ArticleService"`
|
g.Meta `path:"/article/create" method:"post" tags:"ArticleService"`
|
||||||
Title string `v:"required"`
|
Title string `v:"required"`
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"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/api/article/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Create add title.
|
||||||
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
|
func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
|
||||||
return nil, gerror.NewCode(gcode.CodeNotImplemented)
|
return nil, gerror.NewCode(gcode.CodeNotImplemented)
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ import (
|
|||||||
|
|
||||||
"{ImportPath}"
|
"{ImportPath}"
|
||||||
)
|
)
|
||||||
|
{MethodComment}
|
||||||
func (c *{CtrlName}) {MethodName}(ctx context.Context, req *{Version}.{MethodName}Req) (res *{Version}.{MethodName}Res, err error) {
|
func (c *{CtrlName}) {MethodName}(ctx context.Context, req *{Version}.{MethodName}Req) (res *{Version}.{MethodName}Res, err error) {
|
||||||
return nil, gerror.NewCode(gcode.CodeNotImplemented)
|
return nil, gerror.NewCode(gcode.CodeNotImplemented)
|
||||||
}
|
}
|
||||||
@ -66,7 +66,7 @@ import (
|
|||||||
`
|
`
|
||||||
|
|
||||||
const TemplateGenCtrlControllerMethodFuncMerge = `
|
const TemplateGenCtrlControllerMethodFuncMerge = `
|
||||||
|
{MethodComment}
|
||||||
func (c *{CtrlName}) {MethodName}(ctx context.Context, req *{Version}.{MethodName}Req) (res *{Version}.{MethodName}Res, err error) {
|
func (c *{CtrlName}) {MethodName}(ctx context.Context, req *{Version}.{MethodName}Req) (res *{Version}.{MethodName}Res, err error) {
|
||||||
return nil, gerror.NewCode(gcode.CodeNotImplemented)
|
return nil, gerror.NewCode(gcode.CodeNotImplemented)
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ func (i *implementer) {ImplementerName}() {Module}.I{ImplementerName} {
|
|||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
const TemplateGenCtrlSdkImplementerFunc = `
|
const TemplateGenCtrlSdkImplementerFunc = `{MethodComment}
|
||||||
func (i *implementer{ImplementerName}) {MethodName}(ctx context.Context, req *{Version}.{MethodName}Req) (res *{Version}.{MethodName}Res, err error) {
|
func (i *implementer{ImplementerName}) {MethodName}(ctx context.Context, req *{Version}.{MethodName}Req) (res *{Version}.{MethodName}Res, err error) {
|
||||||
err = i.Request(ctx, req, &res)
|
err = i.Request(ctx, req, &res)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user