Skip to content

Commit

Permalink
feat(api): export GenRoutesString function
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Sep 26, 2024
1 parent 5b8126c commit 3ab293f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
47 changes: 35 additions & 12 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,42 @@ type (
}
)

func GenRoutesString(rootPkg string, cfg *config.Config, api *spec.ApiSpec) (string, error) {
fgc, err := genRoutesConfig("", rootPkg, cfg, api)
if err != nil {
return "", err
}
return genFileString(*fgc)
}

func genRoutes(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
fgc, err := genRoutesConfig(dir, rootPkg, cfg, api)
if err != nil {
return err
}

routeFilename, err := format.FileNamingFormat(cfg.NamingFormat, routesFilename)
if err != nil {
return err
}

routeFilename = routeFilename + ".go"
filename := path.Join(dir, handlerDir, routeFilename)
_ = os.Remove(filename)

return genFile(*fgc)
}

func genRoutesConfig(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) (*fileGenConfig, error) {
var builder strings.Builder
groups, err := getRoutes(api)
if err != nil {
return err
return nil, err
}

templateText, err := pathx.LoadTemplate(category, routesAdditionTemplateFile, routesAdditionTemplate)
if err != nil {
return err
return nil, err
}

var hasTimeout bool
Expand Down Expand Up @@ -136,12 +162,12 @@ rest.WithPrefix("%s"),`, g.prefix)
if len(g.timeout) > 0 {
duration, err := time.ParseDuration(g.timeout)
if err != nil {
return err
return nil, err
}

// why we check this, maybe some users set value 1, it's 1ns, not 1s.
if duration < timeoutThreshold {
return fmt.Errorf("timeout should not less than 1ms, now %v", duration)
return nil, fmt.Errorf("timeout should not less than 1ms, now %v", duration)
}

timeout = fmt.Sprintf("\n rest.WithTimeout(%d * time.Millisecond),", duration.Milliseconds())
Expand All @@ -152,7 +178,7 @@ rest.WithPrefix("%s"),`, g.prefix)
if len(g.maxBytes) > 0 {
_, err := strconv.ParseInt(g.maxBytes, 10, 64)
if err != nil {
return fmt.Errorf("maxBytes %s parse error,it is an invalid number", g.maxBytes)
return nil, fmt.Errorf("maxBytes %s parse error,it is an invalid number", g.maxBytes)
}

maxBytes = fmt.Sprintf("\n rest.WithMaxBytes(%s),", g.maxBytes)
Expand Down Expand Up @@ -181,20 +207,17 @@ rest.WithPrefix("%s"),`, g.prefix)
"timeout": timeout,
"maxBytes": maxBytes,
}); err != nil {
return err
return nil, err
}
}

routeFilename, err := format.FileNamingFormat(cfg.NamingFormat, routesFilename)
if err != nil {
return err
return nil, err
}

routeFilename = routeFilename + ".go"
filename := path.Join(dir, handlerDir, routeFilename)
os.Remove(filename)

return genFile(fileGenConfig{
return &fileGenConfig{
dir: dir,
subdir: handlerDir,
filename: routeFilename,
Expand All @@ -208,7 +231,7 @@ rest.WithPrefix("%s"),`, g.prefix)
"routesAdditions": strings.TrimSpace(builder.String()),
"version": version.BuildVersion,
},
})
}, nil
}

func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
Expand Down
22 changes: 22 additions & 0 deletions tools/goctl/api/gogen/genroutes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gogen

import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
"path/filepath"
"testing"
)

func TestGenRoutesString(t *testing.T) {
parse, err := parser.Parse(filepath.Join("testdata", "example.api"), nil)
assert.Nil(t, err)

routesString, err := GenRoutesString("example", &config.Config{
NamingFormat: "gozero",
}, parse)
assert.NotNil(t, routesString)
assert.Nil(t, err)
fmt.Println(routesString)
}
25 changes: 25 additions & 0 deletions tools/goctl/api/gogen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ func genFile(c fileGenConfig) error {
return err
}

func genFileString(c fileGenConfig) (string, error) {
var (
text string
err error
)
if len(c.category) == 0 || len(c.templateFile) == 0 {
text = c.builtinTemplate
} else {
text, err = pathx.LoadTemplate(c.category, c.templateFile, c.builtinTemplate)
if err != nil {
return "", err
}
}

t := template.Must(template.New(c.templateName).Parse(text))
buffer := new(bytes.Buffer)
err = t.Execute(buffer, c.data)
if err != nil {
return "", err
}

code := golang.FormatCode(buffer.String())
return code, nil
}

func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error {
util.WriteIndent(writer, indent)
var (
Expand Down

0 comments on commit 3ab293f

Please sign in to comment.