From 4c57511bc78210ee6e2ccd7a1a4eea6ff1423daf Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 9 Jul 2022 15:46:07 +0800 Subject: [PATCH] chore: refactor --- tools/goctl/api/docgen/doc.go | 3 +++ tools/goctl/model/cmd.go | 2 +- tools/goctl/model/mongo/template/model_custom.tpl | 6 ++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/goctl/api/docgen/doc.go b/tools/goctl/api/docgen/doc.go index 9fe77c8e1729..2ef66959fb94 100644 --- a/tools/goctl/api/docgen/doc.go +++ b/tools/goctl/api/docgen/doc.go @@ -21,11 +21,13 @@ 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() @@ -61,6 +63,7 @@ func genDoc(api *spec.ApiSpec, dir, filename string) error { builder.Write(tmplBytes.Bytes()) } + _, err = fp.WriteString(strings.Replace(builder.String(), """, `"`, -1)) return err } diff --git a/tools/goctl/model/cmd.go b/tools/goctl/model/cmd.go index ff1479592982..99de7c401e8b 100644 --- a/tools/goctl/model/cmd.go +++ b/tools/goctl/model/cmd.go @@ -83,7 +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().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/template/model_custom.tpl b/tools/goctl/model/mongo/template/model_custom.tpl index 9cb5de532226..31fa86536925 100644 --- a/tools/goctl/model/mongo/template/model_custom.tpl +++ b/tools/goctl/model/mongo/template/model_custom.tpl @@ -6,7 +6,7 @@ package model ){{else}}import "github.com/zeromicro/go-zero/core/stores/mon"{{end}} {{if .Easy}} - const {{.Type}}CollectionName = "{{.snakeType}}" +const {{.Type}}CollectionName = "{{.snakeType}}" {{end}} var _ {{.Type}}Model = (*custom{{.Type}}Model)(nil) @@ -30,11 +30,9 @@ type ( 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 { +}{{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}} -