Skip to content

Commit 5c73cd9

Browse files
author
robinzhang
committed
opt mongo model package name
1 parent cbd118d commit 5c73cd9

File tree

6 files changed

+17
-5
lines changed

6 files changed

+17
-5
lines changed

tools/goctl/model/mongo/generate/generate.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Context struct {
2020
Easy bool
2121
Output string
2222
Cfg *config.Config
23+
PackageName string
2324
}
2425

2526
// Do executes model template and output the result into the specified file path
@@ -61,6 +62,7 @@ func generateModel(ctx *Context) error {
6162
"lowerType": stringx.From(t).Untitle(),
6263
"Cache": ctx.Cache,
6364
"version": version.BuildVersion,
65+
"PackageName": ctx.PackageName,
6466
}, output, true); err != nil {
6567
return err
6668
}
@@ -88,6 +90,7 @@ func generateCustomModel(ctx *Context) error {
8890
"snakeType": stringx.From(t).ToSnake(),
8991
"Cache": ctx.Cache,
9092
"Easy": ctx.Easy,
93+
"PackageName": ctx.PackageName,
9194
}, output, false)
9295
if err != nil {
9396
return err
@@ -112,6 +115,7 @@ func generateTypes(ctx *Context) error {
112115
output := filepath.Join(ctx.Output, fn+".go")
113116
if err = util.With("model").Parse(text).GoFmt(true).SaveTo(map[string]any{
114117
"Type": stringx.From(t).Title(),
118+
"PackageName": ctx.PackageName,
115119
}, output, false); err != nil {
116120
return err
117121
}
@@ -128,5 +132,7 @@ func generateError(ctx *Context) error {
128132

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

131-
return util.With("error").Parse(text).GoFmt(true).SaveTo(ctx, output, false)
135+
return util.With("error").Parse(text).GoFmt(true).SaveTo(map[string]any{
136+
"PackageName": ctx.PackageName,
137+
}, output, false)
132138
}

tools/goctl/model/mongo/mongo.go

+6
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ func Action(_ *cobra.Command, _ []string) error {
7171
return err
7272
}
7373

74+
baseDir := filepath.Base(a)
75+
if baseDir == "" || baseDir == "." {
76+
baseDir = "model" // as default
77+
}
78+
7479
return generate.Do(&generate.Context{
7580
Types: tp,
7681
Cache: c,
7782
Easy: easy,
7883
Output: a,
7984
Cfg: cfg,
85+
PackageName: baseDir,
8086
})
8187
}

tools/goctl/model/mongo/template/error.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package model
1+
package {{.PackageName}}
22

33
import (
44
"errors"

tools/goctl/model/mongo/template/model.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Code generated by goctl. DO NOT EDIT.
22
// goctl {{.version}}
33

4-
package model
4+
package {{.PackageName}}
55

66
import (
77
"context"

tools/goctl/model/mongo/template/model_custom.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package model
1+
package {{.PackageName}}
22

33
{{if .Cache}}import (
44
"github.com/zeromicro/go-zero/core/stores/cache"

tools/goctl/model/mongo/template/types.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package model
1+
package {{.PackageName}}
22

33
import (
44
"time"

0 commit comments

Comments
 (0)