Skip to content

Based on #4462, it has been improved and is compatible with previous versions #4775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/goctl/internal/flags/default_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@
"cache": "Generate code with cache [optional]",
"easy": "Generate code with auto generated CollectionName for easy declare [optional]",
"dir": "{{.goctl.model.dir}}",
"auto-package": "Whether to automatically use the name of the directory where the build file is located as the package name [optional,default=false]\nIt is mainly used to be compatible with previous versions,If this parameter is set to false, the package name is set to model by default",
"package": "Manually specify the package name, which takes precedence over VarBoolPkgAuto [optional]",
Copy link
Preview

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation refers to 'VarBoolPkgAuto' but the actual variable name is 'VarBoolAutoPackage'. This inconsistency could confuse users.

Suggested change
"package": "Manually specify the package name, which takes precedence over VarBoolPkgAuto [optional]",
"package": "Manually specify the package name, which takes precedence over VarBoolAutoPackage [optional]",

Copilot uses AI. Check for mistakes.

"style": "{{.global.style}}",
"home": "{{.global.home}}",
"remote": "{{.global.remote}}",
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/model/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func init() {
mongoCmdFlags.BoolVarP(&mongo.VarBoolCache, "cache", "c")
mongoCmdFlags.BoolVarP(&mongo.VarBoolEasy, "easy", "e")
mongoCmdFlags.StringVarP(&mongo.VarStringDir, "dir", "d")
mongoCmdFlags.BoolVarP(&mongo.VarBoolAutoPackage, "auto-package", "a")
mongoCmdFlags.StringVarP(&mongo.VarStringPackage, "package", "p")
mongoCmdFlags.StringVar(&mongo.VarStringStyle, "style")
mongoCmdFlags.StringVar(&mongo.VarStringHome, "home")
mongoCmdFlags.StringVar(&mongo.VarStringRemote, "remote")
Expand Down
8 changes: 7 additions & 1 deletion tools/goctl/model/mongo/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Context struct {
Easy bool
Output string
Cfg *config.Config
PackageName string
}

// Do executes model template and output the result into the specified file path
Expand Down Expand Up @@ -61,6 +62,7 @@ func generateModel(ctx *Context) error {
"lowerType": stringx.From(t).Untitle(),
"Cache": ctx.Cache,
"version": version.BuildVersion,
"PackageName": ctx.PackageName,
}, output, true); err != nil {
return err
}
Expand Down Expand Up @@ -88,6 +90,7 @@ func generateCustomModel(ctx *Context) error {
"snakeType": stringx.From(t).ToSnake(),
"Cache": ctx.Cache,
"Easy": ctx.Easy,
"PackageName": ctx.PackageName,
}, output, false)
if err != nil {
return err
Expand All @@ -112,6 +115,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]any{
"Type": stringx.From(t).Title(),
"PackageName": ctx.PackageName,
}, output, false); err != nil {
return err
}
Expand All @@ -128,5 +132,7 @@ func generateError(ctx *Context) error {

output := filepath.Join(ctx.Output, "error.go")

return util.With("error").Parse(text).GoFmt(true).SaveTo(ctx, output, false)
return util.With("error").Parse(text).GoFmt(true).SaveTo(map[string]any{
"PackageName": ctx.PackageName,
}, output, false)
}
27 changes: 21 additions & 6 deletions tools/goctl/model/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var (
VarStringSliceType []string
// VarStringDir describes an output directory.
VarStringDir string
//VarBoolAutoPackage Default false;
// - Whether to automatically use the name of the directory where the build file is located as the package name
// - It is mainly used to be compatible with previous versions,If this parameter is set to false, the package name is set to model by default
VarBoolAutoPackage bool
//VarStringPackage Manually specify the package name, which takes precedence over VarBoolPkgAuto
Copy link
Preview

Copilot AI Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment refers to 'VarBoolPkgAuto' but the actual variable name is 'VarBoolAutoPackage'. This should be updated for consistency.

Suggested change
//VarStringPackage Manually specify the package name, which takes precedence over VarBoolPkgAuto
//VarStringPackage Manually specify the package name, which takes precedence over VarBoolAutoPackage

Copilot uses AI. Check for mistakes.

VarStringPackage string
// VarBoolCache describes whether cache is enabled.
VarBoolCache bool
// VarBoolEasy describes whether to generate Collection Name in the code for easy declare.
Expand Down Expand Up @@ -66,16 +72,25 @@ func Action(_ *cobra.Command, _ []string) error {
if err != nil {
return err
}

if err = pathx.MkdirIfNotExist(a); err != nil {
return err
}

var pkg string
if VarStringPackage != "" {
pkg = VarStringPackage
} else if VarBoolAutoPackage {
pkg = file.SafeString(filepath.Base(a))
} else {
pkg = "model"
}

return generate.Do(&generate.Context{
Types: tp,
Cache: c,
Easy: easy,
Output: a,
Cfg: cfg,
Types: tp,
Cache: c,
Easy: easy,
Output: a,
Cfg: cfg,
PackageName: pkg,
})
}
23 changes: 14 additions & 9 deletions tools/goctl/model/mongo/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,20 @@ Usage:
goctl model mongo [flags]

Flags:
--branch string The branch of the remote repo, it does work with --remote
-c, --cache Generate code with cache [optional]
-d, --dir string The target dir
-h, --help help for mongo
--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
--remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
--style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md]
-t, --type strings Specified model type name
-a, --auto-package Whether to automatically use the name of the directory where the build file is located as the package name [optional,default=false]
It is mainly used to be compatible with previous versions,If this parameter is set to false, the package name is set to model by default
--branch string The branch of the remote repo, it does work with --remote
-c, --cache Generate code with cache [optional]
-d, --dir string The target dir
-e, --easy Generate code with auto generated CollectionName for easy declare [optional]
-h, --help help for mongo
--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
-p, --package string Manually specify the package name, which takes precedence over VarBoolPkgAuto [optional]
--remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
--style string The file naming format, see [https://github.com/zeromicro/go-zero/blob/master/tools/goctl/config/readme.md]
-t, --type strings Specified model type name


```

Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/template/error.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package {{.PackageName}}

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/template/model.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Code generated by goctl. DO NOT EDIT.
// goctl {{.version}}

package model
package {{.PackageName}}

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/template/model_custom.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package {{.PackageName}}

{{if .Cache}}import (
"github.com/zeromicro/go-zero/core/stores/cache"
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/model/mongo/template/types.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package {{.PackageName}}

import (
"time"
Expand Down