mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
feat:goctl model mongo
add easy
flag for easy declare. (#2073)
* fix:typo in readme.md * feat:`goctl model mongo ` add `easy` flag to generate code with Auto generated CollectionName for easy declare. * fix:`goctl api doc ` when referenced api file contains no route,will generate an empty markdown file. * code: adjust code. Co-authored-by: 虫子樱桃 <czyt@w.cn>
This commit is contained in:
parent
1410f7dc20
commit
725e6056e1
@ -18,12 +18,14 @@ import (
|
|||||||
var markdownTemplate string
|
var markdownTemplate string
|
||||||
|
|
||||||
func genDoc(api *spec.ApiSpec, dir, filename string) error {
|
func genDoc(api *spec.ApiSpec, dir, filename string) error {
|
||||||
|
if len(api.Service.Routes()) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
fp, _, err := util.MaybeCreateFile(dir, "", filename)
|
fp, _, err := util.MaybeCreateFile(dir, "", filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer fp.Close()
|
defer fp.Close()
|
||||||
|
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
for index, route := range api.Service.Routes() {
|
for index, route := range api.Service.Routes() {
|
||||||
routeComment := route.JoinedDoc()
|
routeComment := route.JoinedDoc()
|
||||||
|
@ -83,6 +83,7 @@ func init() {
|
|||||||
|
|
||||||
mongoCmd.Flags().StringSliceVarP(&mongo.VarStringSliceType, "type", "t", nil, "Specified model type name")
|
mongoCmd.Flags().StringSliceVarP(&mongo.VarStringSliceType, "type", "t", nil, "Specified model type name")
|
||||||
mongoCmd.Flags().BoolVarP(&mongo.VarBoolCache, "cache", "c", false, "Generate code with cache [optional]")
|
mongoCmd.Flags().BoolVarP(&mongo.VarBoolCache, "cache", "c", false, "Generate code with cache [optional]")
|
||||||
|
mongoCmd.Flags().BoolVarP(&mongo.VarBoolEasy, "easy", "e", false, "Generate code with Auto generated CollectionName for easy declare [optional]")
|
||||||
mongoCmd.Flags().StringVarP(&mongo.VarStringDir, "dir", "d", "", "The target dir")
|
mongoCmd.Flags().StringVarP(&mongo.VarStringDir, "dir", "d", "", "The target dir")
|
||||||
mongoCmd.Flags().StringVar(&mongo.VarStringStyle, "style", "", "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
|
mongoCmd.Flags().StringVar(&mongo.VarStringStyle, "style", "", "The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]")
|
||||||
mongoCmd.Flags().StringVar(&mongo.VarStringHome, "home", "", "The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority")
|
mongoCmd.Flags().StringVar(&mongo.VarStringHome, "home", "", "The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority")
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
type Context struct {
|
type Context struct {
|
||||||
Types []string
|
Types []string
|
||||||
Cache bool
|
Cache bool
|
||||||
|
Easy bool
|
||||||
Output string
|
Output string
|
||||||
Cfg *config.Config
|
Cfg *config.Config
|
||||||
}
|
}
|
||||||
@ -82,7 +83,9 @@ func generateCustomModel(ctx *Context) error {
|
|||||||
err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{
|
err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{
|
||||||
"Type": stringx.From(t).Title(),
|
"Type": stringx.From(t).Title(),
|
||||||
"lowerType": stringx.From(t).Untitle(),
|
"lowerType": stringx.From(t).Untitle(),
|
||||||
|
"snakeType": stringx.From(t).ToSnake(),
|
||||||
"Cache": ctx.Cache,
|
"Cache": ctx.Cache,
|
||||||
|
"Easy": ctx.Easy,
|
||||||
}, output, false)
|
}, output, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -19,6 +19,8 @@ var (
|
|||||||
VarStringDir string
|
VarStringDir string
|
||||||
// VarBoolCache describes whether cache is enabled.
|
// VarBoolCache describes whether cache is enabled.
|
||||||
VarBoolCache bool
|
VarBoolCache bool
|
||||||
|
// VarBoolEasy describes whether to generate Collection Name in the code for easy declare.
|
||||||
|
VarBoolEasy bool
|
||||||
// VarStringStyle describes the style.
|
// VarStringStyle describes the style.
|
||||||
VarStringStyle string
|
VarStringStyle string
|
||||||
// VarStringHome describes the goctl home.
|
// VarStringHome describes the goctl home.
|
||||||
@ -33,6 +35,7 @@ var (
|
|||||||
func Action(_ *cobra.Command, _ []string) error {
|
func Action(_ *cobra.Command, _ []string) error {
|
||||||
tp := VarStringSliceType
|
tp := VarStringSliceType
|
||||||
c := VarBoolCache
|
c := VarBoolCache
|
||||||
|
easy := VarBoolEasy
|
||||||
o := strings.TrimSpace(VarStringDir)
|
o := strings.TrimSpace(VarStringDir)
|
||||||
s := VarStringStyle
|
s := VarStringStyle
|
||||||
home := VarStringHome
|
home := VarStringHome
|
||||||
@ -71,6 +74,7 @@ func Action(_ *cobra.Command, _ []string) error {
|
|||||||
return generate.Do(&generate.Context{
|
return generate.Do(&generate.Context{
|
||||||
Types: tp,
|
Types: tp,
|
||||||
Cache: c,
|
Cache: c,
|
||||||
|
Easy: easy,
|
||||||
Output: a,
|
Output: a,
|
||||||
Cfg: cfg,
|
Cfg: cfg,
|
||||||
})
|
})
|
||||||
|
@ -5,6 +5,10 @@ package model
|
|||||||
"github.com/zeromicro/go-zero/core/stores/monc"
|
"github.com/zeromicro/go-zero/core/stores/monc"
|
||||||
){{else}}import "github.com/zeromicro/go-zero/core/stores/mon"{{end}}
|
){{else}}import "github.com/zeromicro/go-zero/core/stores/mon"{{end}}
|
||||||
|
|
||||||
|
{{if .Easy}}
|
||||||
|
const {{.Type}}CollectionName = "{{.snakeType}}"
|
||||||
|
{{end}}
|
||||||
|
|
||||||
var _ {{.Type}}Model = (*custom{{.Type}}Model)(nil)
|
var _ {{.Type}}Model = (*custom{{.Type}}Model)(nil)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -19,10 +23,18 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
// New{{.Type}}Model returns a model for the mongo.
|
// New{{.Type}}Model returns a model for the mongo.
|
||||||
func New{{.Type}}Model(url, db, collection string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model {
|
{{if .Easy}}func New{{.Type}}Model(url, db string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model {
|
||||||
conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, collection{{if .Cache}}, c{{end}})
|
conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, {{.Type}}CollectionName{{if .Cache}}, c{{end}})
|
||||||
return &custom{{.Type}}Model{
|
return &custom{{.Type}}Model{
|
||||||
default{{.Type}}Model: newDefault{{.Type}}Model(conn),
|
default{{.Type}}Model: newDefault{{.Type}}Model(conn),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
{{else}}func New{{.Type}}Model(url, db, collection string{{if .Cache}}, c cache.CacheConf{{end}}) {{.Type}}Model {
|
||||||
|
conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, collection{{if .Cache}}, c{{end}})
|
||||||
|
return &custom{{.Type}}Model{
|
||||||
|
default{{.Type}}Model: newDefault{{.Type}}Model(conn),
|
||||||
|
}
|
||||||
|
}{{end}}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user