diff --git a/tools/goctl/model/mongo/generate/generate.go b/tools/goctl/model/mongo/generate/generate.go index 9b9f09c4bfe2..1a75403b220c 100644 --- a/tools/goctl/model/mongo/generate/generate.go +++ b/tools/goctl/model/mongo/generate/generate.go @@ -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) } diff --git a/tools/goctl/model/mongo/mongo.go b/tools/goctl/model/mongo/mongo.go index fd7c37a21bf5..387bc9c10f59 100644 --- a/tools/goctl/model/mongo/mongo.go +++ b/tools/goctl/model/mongo/mongo.go @@ -71,11 +71,17 @@ func Action(_ *cobra.Command, _ []string) error { return err } + baseDir := filepath.Base(a) + if baseDir == "" || baseDir == "." { + baseDir = "model" // as default + } + return generate.Do(&generate.Context{ Types: tp, Cache: c, Easy: easy, Output: a, Cfg: cfg, + PackageName: baseDir, }) } diff --git a/tools/goctl/model/mongo/template/error.tpl b/tools/goctl/model/mongo/template/error.tpl index 27d9244976ae..b2f0f8dd7689 100644 --- a/tools/goctl/model/mongo/template/error.tpl +++ b/tools/goctl/model/mongo/template/error.tpl @@ -1,4 +1,4 @@ -package model +package {{.PackageName}} import ( "errors" diff --git a/tools/goctl/model/mongo/template/model.tpl b/tools/goctl/model/mongo/template/model.tpl index 6f8fa83d26d1..02b350155147 100644 --- a/tools/goctl/model/mongo/template/model.tpl +++ b/tools/goctl/model/mongo/template/model.tpl @@ -1,7 +1,7 @@ // Code generated by goctl. DO NOT EDIT. // goctl {{.version}} -package model +package {{.PackageName}} import ( "context" diff --git a/tools/goctl/model/mongo/template/model_custom.tpl b/tools/goctl/model/mongo/template/model_custom.tpl index 31fa86536925..82dc6431bc4e 100644 --- a/tools/goctl/model/mongo/template/model_custom.tpl +++ b/tools/goctl/model/mongo/template/model_custom.tpl @@ -1,4 +1,4 @@ -package model +package {{.PackageName}} {{if .Cache}}import ( "github.com/zeromicro/go-zero/core/stores/cache" diff --git a/tools/goctl/model/mongo/template/types.tpl b/tools/goctl/model/mongo/template/types.tpl index 8da006f44353..60993a7f6a31 100644 --- a/tools/goctl/model/mongo/template/types.tpl +++ b/tools/goctl/model/mongo/template/types.tpl @@ -1,4 +1,4 @@ -package model +package {{.PackageName}} import ( "time"