Skip to content

Commit 0d1dfda

Browse files
committed
fix: Fix fill router prefix
1 parent 936270c commit 0d1dfda

7 files changed

Lines changed: 49 additions & 35 deletions

File tree

cmd/generate.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/gin-admin/gin-admin-cli/v10/internal/actions"
8+
"github.com/gin-admin/gin-admin-cli/v10/internal/schema"
89
"github.com/gin-admin/gin-admin-cli/v10/internal/tfs"
910
"github.com/urfave/cli/v2"
1011
)
@@ -49,14 +50,17 @@ func Generate() *cli.Command {
4950
Usage: "The config file or directory to generate the struct from (JSON/YAML)",
5051
},
5152
&cli.StringFlag{
52-
Name: "structs",
53-
Aliases: []string{"s"},
54-
Usage: "The struct to generate (multiple structs can be separated by a comma)",
53+
Name: "structs",
54+
Usage: "The struct name to generate",
5555
},
5656
&cli.StringFlag{
5757
Name: "structs-comment",
5858
Usage: "Specify the struct comment",
5959
},
60+
&cli.BoolFlag{
61+
Name: "structs-router-prefix",
62+
Usage: "Use module name as router prefix",
63+
},
6064
&cli.StringFlag{
6165
Name: "structs-output",
6266
Usage: "Specify the packages to generate the struct (default: schema,dal,biz,api)",
@@ -92,8 +96,17 @@ func Generate() *cli.Command {
9296

9397
if c.String("config") != "" {
9498
return gen.RunWithConfig(c.Context, c.String("config"))
95-
} else if c.String("structs") != "" {
96-
return gen.RunWithStruct(c.Context, c.String("structs"), c.String("structs-comment"), strings.TrimSpace(c.String("structs-output")))
99+
} else if name := c.String("structs"); name != "" {
100+
var outputs []string
101+
if v := c.String("structs-output"); v != "" {
102+
outputs = strings.Split(v, ",")
103+
}
104+
return gen.RunWithStruct(c.Context, &schema.S{
105+
Name: name,
106+
Comment: c.String("structs-comment"),
107+
Outputs: outputs,
108+
FillRouterPrefix: c.Bool("structs-router-prefix"),
109+
})
97110
} else {
98111
return errors.New("structs or config must be specified")
99112
}

internal/actions/generate.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"strings"
98

109
"github.com/gin-admin/gin-admin-cli/v10/internal/parser"
1110
"github.com/gin-admin/gin-admin-cli/v10/internal/schema"
@@ -81,17 +80,8 @@ func (a *GenerateAction) RunWithConfig(ctx context.Context, cfgName string) erro
8180
return parseFile(cfgName)
8281
}
8382

84-
func (a *GenerateAction) RunWithStruct(ctx context.Context, structName, comment, output string) error {
85-
var outputs []string
86-
if output != "" {
87-
outputs = strings.Split(output, ",")
88-
}
89-
90-
input := []*schema.S{
91-
{Name: structName, Comment: comment, Outputs: outputs},
92-
}
93-
94-
return a.run(ctx, input)
83+
func (a *GenerateAction) RunWithStruct(ctx context.Context, s *schema.S) error {
84+
return a.run(ctx, []*schema.S{s})
9585
}
9686

9787
func (a *GenerateAction) run(ctx context.Context, data []*schema.S) error {
@@ -234,12 +224,13 @@ func (a *GenerateAction) generate(ctx context.Context, dataItem *schema.S) error
234224
}
235225

236226
basicArgs := parser.BasicArgs{
237-
Dir: a.cfg.Dir,
238-
ModuleName: dataItem.ModuleName,
239-
ModulePath: a.cfg.ModulePath,
240-
StructName: dataItem.Name,
241-
GenPackages: genPackages,
242-
Flag: parser.AstFlagGen,
227+
Dir: a.cfg.Dir,
228+
ModuleName: dataItem.ModuleName,
229+
ModulePath: a.cfg.ModulePath,
230+
StructName: dataItem.Name,
231+
GenPackages: genPackages,
232+
Flag: parser.AstFlagGen,
233+
FillRouterPrefix: dataItem.FillRouterPrefix,
243234
}
244235
moduleMainTplData, err := parser.ModifyModuleMainFile(ctx, basicArgs)
245236
if err != nil {

internal/parser/parser.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"go/token"
1010
"path/filepath"
1111
"strings"
12+
"text/template"
1213

1314
"github.com/gin-admin/gin-admin-cli/v10/internal/utils"
1415
)
@@ -27,7 +28,12 @@ func ModifyModuleMainFile(ctx context.Context, args BasicArgs) ([]byte, error) {
2728
tplData := strings.ReplaceAll(tplModuleMain, "$$LowerModuleName$$", GetModuleImportName(args.ModuleName))
2829
tplData = strings.ReplaceAll(tplData, "$$ModuleName$$", args.ModuleName)
2930
tplData = strings.ReplaceAll(tplData, "$$ModuleImportPath$$", GetModuleImportPath(args.Dir, args.ModulePath, args.ModuleName))
30-
if err := utils.WriteFile(fullname, []byte(tplData)); err != nil {
31+
buf := new(bytes.Buffer)
32+
err := template.Must(template.New("").Parse(tplData)).Execute(buf, args)
33+
if err != nil {
34+
return nil, err
35+
}
36+
if err := utils.WriteFile(fullname, buf.Bytes()); err != nil {
3137
return nil, err
3238
}
3339
}

internal/parser/tpl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func (a *$$ModuleName$$) Init(ctx context.Context) error {
2828
}
2929
3030
func (a *$$ModuleName$$) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
31+
{{- if .FillRouterPrefix}}
3132
v1 = v1.Group("$$LowerModuleName$$")
33+
{{- end}}
3234
return nil
3335
}
3436

internal/parser/type.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ const (
1818
)
1919

2020
type BasicArgs struct {
21-
Dir string
22-
ModuleName string
23-
ModulePath string
24-
StructName string
25-
GenPackages []string
26-
Flag AstFlag
21+
Dir string
22+
ModuleName string
23+
ModulePath string
24+
StructName string
25+
GenPackages []string
26+
Flag AstFlag
27+
FillRouterPrefix bool
2728
}

internal/schema/struct.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type S struct {
2828
DisablePagination bool `yaml:"disable_pagination,omitempty" json:"disable_pagination,omitempty"`
2929
DisableDefaultFields bool `yaml:"disable_default_fields,omitempty" json:"disable_default_fields,omitempty"`
3030
FillGormCommit bool `yaml:"fill_gorm_commit,omitempty" json:"fill_gorm_commit,omitempty"`
31+
FillRouterPrefix bool `yaml:"fill_router_module_name,omitempty" json:"fill_router_module_name,omitempty"`
3132
Fields []*Field `yaml:"fields,omitempty" json:"fields,omitempty"`
3233
GenerateFE bool `yaml:"generate_fe,omitempty" json:"generate_fe,omitempty"`
3334
FETpl string `yaml:"fe_tpl,omitempty" json:"fe_tpl,omitempty"` // react/react-v5-i18n

tpls/default/api.go.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type {{$name}} struct {
3232
// @Success 200 {object} util.ResponseResult{data=[]schema.{{$name}}}
3333
// @Failure 401 {object} util.ResponseResult
3434
// @Failure 500 {object} util.ResponseResult
35-
// @Router /api/v1/{{lower .ModuleName}}/{{lowerHyphensPlural .Name}} [get]
35+
// @Router /api/v1/{{if .FillRouterPrefix}}{{lower .ModuleName}}/{{end}}{{lowerHyphensPlural .Name}} [get]
3636
func (a *{{$name}}) Query(c *gin.Context) {
3737
ctx := c.Request.Context()
3838
var params schema.{{$name}}QueryParam
@@ -57,7 +57,7 @@ func (a *{{$name}}) Query(c *gin.Context) {
5757
// @Success 200 {object} util.ResponseResult{data=schema.{{$name}}}
5858
// @Failure 401 {object} util.ResponseResult
5959
// @Failure 500 {object} util.ResponseResult
60-
// @Router /api/v1/{{lower .ModuleName}}/{{lowerHyphensPlural .Name}}/{id} [get]
60+
// @Router /api/v1/{{if .FillRouterPrefix}}{{lower .ModuleName}}/{{end}}{{lowerHyphensPlural .Name}}/{id} [get]
6161
func (a *{{$name}}) Get(c *gin.Context) {
6262
ctx := c.Request.Context()
6363
item, err := a.{{$name}}BIZ.Get(ctx, c.Param("id"))
@@ -77,7 +77,7 @@ func (a *{{$name}}) Get(c *gin.Context) {
7777
// @Failure 400 {object} util.ResponseResult
7878
// @Failure 401 {object} util.ResponseResult
7979
// @Failure 500 {object} util.ResponseResult
80-
// @Router /api/v1/{{lower .ModuleName}}/{{lowerHyphensPlural .Name}} [post]
80+
// @Router /api/v1/{{if .FillRouterPrefix}}{{lower .ModuleName}}/{{end}}{{lowerHyphensPlural .Name}} [post]
8181
func (a *{{$name}}) Create(c *gin.Context) {
8282
ctx := c.Request.Context()
8383
item := new(schema.{{$name}}Form)
@@ -107,7 +107,7 @@ func (a *{{$name}}) Create(c *gin.Context) {
107107
// @Failure 400 {object} util.ResponseResult
108108
// @Failure 401 {object} util.ResponseResult
109109
// @Failure 500 {object} util.ResponseResult
110-
// @Router /api/v1/{{lower .ModuleName}}/{{lowerHyphensPlural .Name}}/{id} [put]
110+
// @Router /api/v1/{{if .FillRouterPrefix}}{{lower .ModuleName}}/{{end}}{{lowerHyphensPlural .Name}}/{id} [put]
111111
func (a *{{$name}}) Update(c *gin.Context) {
112112
ctx := c.Request.Context()
113113
item := new(schema.{{$name}}Form)
@@ -135,7 +135,7 @@ func (a *{{$name}}) Update(c *gin.Context) {
135135
// @Success 200 {object} util.ResponseResult
136136
// @Failure 401 {object} util.ResponseResult
137137
// @Failure 500 {object} util.ResponseResult
138-
// @Router /api/v1/{{lower .ModuleName}}/{{lowerHyphensPlural .Name}}/{id} [delete]
138+
// @Router /api/v1/{{if .FillRouterPrefix}}{{lower .ModuleName}}/{{end}}{{lowerHyphensPlural .Name}}/{id} [delete]
139139
func (a *{{$name}}) Delete(c *gin.Context) {
140140
ctx := c.Request.Context()
141141
err := a.{{$name}}BIZ.Delete(ctx, c.Param("id"))

0 commit comments

Comments
 (0)