From 725e6056e1449b10ec7f97418198c18775632199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=99=AB=E5=AD=90=E6=A8=B1=E6=A1=83?= Date: Sat, 9 Jul 2022 15:34:01 +0800 Subject: [PATCH] feat:`goctl model mongo ` add `easy` flag for easy declare. (#2073) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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: 虫子樱桃 --- tools/goctl/api/docgen/doc.go | 4 +++- tools/goctl/model/cmd.go | 1 + tools/goctl/model/mongo/generate/generate.go | 5 ++++- tools/goctl/model/mongo/mongo.go | 8 ++++++-- .../goctl/model/mongo/template/model_custom.tpl | 16 ++++++++++++++-- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/tools/goctl/api/docgen/doc.go b/tools/goctl/api/docgen/doc.go index 6189d5db..9fe77c8e 100644 --- a/tools/goctl/api/docgen/doc.go +++ b/tools/goctl/api/docgen/doc.go @@ -18,12 +18,14 @@ import ( var markdownTemplate string func genDoc(api *spec.ApiSpec, dir, filename string) error { + if len(api.Service.Routes()) == 0 { + return nil + } fp, _, err := util.MaybeCreateFile(dir, "", filename) if err != nil { return err } defer fp.Close() - var builder strings.Builder for index, route := range api.Service.Routes() { routeComment := route.JoinedDoc() diff --git a/tools/goctl/model/cmd.go b/tools/goctl/model/cmd.go index 9e32e97f..ff147959 100644 --- a/tools/goctl/model/cmd.go +++ b/tools/goctl/model/cmd.go @@ -83,6 +83,7 @@ func init() { 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.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().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") diff --git a/tools/goctl/model/mongo/generate/generate.go b/tools/goctl/model/mongo/generate/generate.go index bbdf9123..cdbfe3ed 100644 --- a/tools/goctl/model/mongo/generate/generate.go +++ b/tools/goctl/model/mongo/generate/generate.go @@ -16,6 +16,7 @@ import ( type Context struct { Types []string Cache bool + Easy bool Output string Cfg *config.Config } @@ -82,7 +83,9 @@ func generateCustomModel(ctx *Context) error { err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{ "Type": stringx.From(t).Title(), "lowerType": stringx.From(t).Untitle(), + "snakeType": stringx.From(t).ToSnake(), "Cache": ctx.Cache, + "Easy": ctx.Easy, }, output, false) if err != nil { return err @@ -107,7 +110,7 @@ func generateTypes(ctx *Context) error { output := filepath.Join(ctx.Output, fn+".go") if err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]interface{}{ "Type": stringx.From(t).Title(), - }, output, false);err!=nil{ + }, output, false); err != nil { return err } } diff --git a/tools/goctl/model/mongo/mongo.go b/tools/goctl/model/mongo/mongo.go index 022c47b0..fd7c37a2 100644 --- a/tools/goctl/model/mongo/mongo.go +++ b/tools/goctl/model/mongo/mongo.go @@ -19,6 +19,8 @@ var ( VarStringDir string // VarBoolCache describes whether cache is enabled. VarBoolCache bool + // VarBoolEasy describes whether to generate Collection Name in the code for easy declare. + VarBoolEasy bool // VarStringStyle describes the style. VarStringStyle string // VarStringHome describes the goctl home. @@ -33,19 +35,20 @@ var ( func Action(_ *cobra.Command, _ []string) error { tp := VarStringSliceType c := VarBoolCache + easy := VarBoolEasy o := strings.TrimSpace(VarStringDir) s := VarStringStyle home := VarStringHome remote := VarStringRemote branch := VarStringBranch - + if len(remote) > 0 { repo, _ := file.CloneIntoGitHome(remote, branch) if len(repo) > 0 { home = repo } } - + if len(home) > 0 { pathx.RegisterGoctlHome(home) } @@ -71,6 +74,7 @@ func Action(_ *cobra.Command, _ []string) error { return generate.Do(&generate.Context{ Types: tp, Cache: c, + Easy: easy, Output: a, Cfg: cfg, }) diff --git a/tools/goctl/model/mongo/template/model_custom.tpl b/tools/goctl/model/mongo/template/model_custom.tpl index 244e5c5a..9cb5de53 100644 --- a/tools/goctl/model/mongo/template/model_custom.tpl +++ b/tools/goctl/model/mongo/template/model_custom.tpl @@ -5,6 +5,10 @@ package model "github.com/zeromicro/go-zero/core/stores/monc" ){{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) type ( @@ -19,10 +23,18 @@ type ( } ) + // 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 { - conn := {{if .Cache}}monc{{else}}mon{{end}}.MustNewModel(url, db, collection{{if .Cache}}, c{{end}}) +{{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, {{.Type}}CollectionName{{if .Cache}}, c{{end}}) return &custom{{.Type}}Model{ 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}} +