Skip to content
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

opt mongo model package name #4462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
opt mongo model package name
robinzhang authored and kevwan committed Nov 21, 2024
commit b73479efc15b20bfb7b80f725efa898fd59bd608
8 changes: 7 additions & 1 deletion tools/goctl/model/mongo/generate/generate.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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
}
@@ -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
@@ -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
}
@@ -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)
}
6 changes: 6 additions & 0 deletions tools/goctl/model/mongo/mongo.go
Original file line number Diff line number Diff line change
@@ -71,11 +71,17 @@ func Action(_ *cobra.Command, _ []string) error {
return err
}

baseDir := filepath.Base(a)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not friendly to past versions and is a destructive change. You can pass a package name from the command line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea! I'll make adjustments accordingly.

if baseDir == "" || baseDir == "." {
baseDir = "model" // as default
}

return generate.Do(&generate.Context{
Types: tp,
Cache: c,
Easy: easy,
Output: a,
Cfg: cfg,
PackageName: baseDir,
})
}
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"
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"
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"
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"