Skip to content

Commit

Permalink
feat(goctl): update api handler comment during generation
Browse files Browse the repository at this point in the history
  • Loading branch information
WqyJh authored and kevwan committed Feb 3, 2024
1 parent 97cf242 commit 259a941
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type handlerInfo struct {
LogicName string
LogicType string
Call string
HandlerComment string
HasResp bool
HasRequest bool
}
Expand All @@ -49,6 +50,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
LogicName: logicName,
LogicType: strings.Title(getLogicName(route)),
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
HandlerComment: getHandlerDoc(route),
HasResp: len(route.ResponseTypeName()) > 0,
HasRequest: len(route.RequestTypeName()) > 0,
})
Expand All @@ -61,6 +63,11 @@ func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
if err != nil {
return err
}
filename = filename + ".go"
targetFile := path.Join(dir, getHandlerFolderPath(group, route), filename)
if pathx.FileExists(targetFile) {
return updateHandlerComments(targetFile, handler, route.HandlerDoc)
}

return genFile(fileGenConfig{
dir: dir,
Expand Down Expand Up @@ -139,3 +146,8 @@ func getLogicName(route spec.Route) string {

return handler + "Logic"
}

func getHandlerDoc(route spec.Route) string {
doc := strings.Join(route.HandlerDoc, "\n")
return doc
}
1 change: 1 addition & 0 deletions tools/goctl/api/gogen/handler.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
{{.ImportPackages}}
)

{{.HandlerComment}}
func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
{{if .HasRequest}}var req types.{{.RequestType}}
Expand Down
57 changes: 57 additions & 0 deletions tools/goctl/api/gogen/updatehandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gogen

import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
)

func updateHandlerComments(filename string, handlerName string, comments []string) error {
// Parse the code file
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return err
}

// Find the UserSelfHandler function
var fn *ast.FuncDecl
for i := range f.Decls {
if fd, ok := f.Decls[i].(*ast.FuncDecl); ok && fd.Name.Name == handlerName {
fn = fd
break
}
}
if fn == nil {
return fmt.Errorf("handler %s not found", handlerName)
}

// Update the function's comments
var list []*ast.Comment
for i, comment := range comments {
if i == 0 {
list = append(list, &ast.Comment{Slash: fn.Pos() - 1, Text: comment})
} else {
list = append(list, &ast.Comment{Text: comment})
}
}
if fn.Doc == nil {
fn.Doc = &ast.CommentGroup{}
}
fn.Doc.List = list

// Format and write the updated code to file
var buf bytes.Buffer
if err := format.Node(&buf, fset, f); err != nil {
return err
}
if err := os.WriteFile(filename, buf.Bytes(), 0644); err != nil {
return err
}

return nil
}

0 comments on commit 259a941

Please sign in to comment.