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

inject go module variable for go template #3798

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions tools/goctl/api/gogen/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/vars"
)
Expand Down Expand Up @@ -48,6 +49,11 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
}
authImportStr := fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceURL)

projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}

return genFile(fileGenConfig{
dir: dir,
subdir: configDir,
Expand All @@ -60,6 +66,7 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
"authImport": authImportStr,
"auth": strings.Join(auths, "\n"),
"jwtTrans": strings.Join(jwtTransList, "\n"),
"goModule": projectCtx.Path,
},
})
}
7 changes: 7 additions & 0 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
Expand All @@ -29,6 +30,7 @@ type handlerInfo struct {
Call string
HasResp bool
HasRequest bool
GoModule string
}

func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route spec.Route) error {
Expand All @@ -41,6 +43,10 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
logicName = pkgName
}

projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
PkgName: pkgName,
ImportPackages: genHandlerImports(group, route, rootPkg),
Expand All @@ -51,6 +57,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route
Call: strings.Title(strings.TrimSuffix(handler, "Handler")),
HasResp: len(route.ResponseTypeName()) > 0,
HasRequest: len(route.RequestTypeName()) > 0,
GoModule: projectCtx.Path,
})
}

Expand Down
7 changes: 7 additions & 0 deletions tools/goctl/api/gogen/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/api/parser/g4/gen/api"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/vars"
Expand Down Expand Up @@ -54,6 +55,11 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
}

subDir := getLogicFolderPath(group, route)
projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}

return genFile(fileGenConfig{
dir: dir,
subdir: subDir,
Expand All @@ -70,6 +76,7 @@ func genLogicByRoute(dir, rootPkg string, cfg *config.Config, group spec.Group,
"responseType": responseString,
"returnString": returnString,
"request": requestString,
"goModule": projectCtx.Path,
},
})
}
Expand Down
7 changes: 7 additions & 0 deletions tools/goctl/api/gogen/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/vars"
Expand All @@ -27,6 +28,11 @@ func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
filename = strings.ReplaceAll(filename, "-api", "")
}

projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}

return genFile(fileGenConfig{
dir: dir,
subdir: "",
Expand All @@ -38,6 +44,7 @@ func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
data: map[string]string{
"importPackages": genMainImports(rootPkg),
"serviceName": configName,
"goModule": projectCtx.Path,
},
})
}
Expand Down
9 changes: 8 additions & 1 deletion tools/goctl/api/gogen/genmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)

Expand All @@ -14,6 +15,11 @@ var middlewareImplementCode string

func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
middlewares := getMiddleware(api)
projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}

for _, item := range middlewares {
middlewareFilename := strings.TrimSuffix(strings.ToLower(item), "middleware") + "_middleware"
filename, err := format.FileNamingFormat(cfg.NamingFormat, middlewareFilename)
Expand All @@ -31,7 +37,8 @@ func genMiddleware(dir string, cfg *config.Config, api *spec.ApiSpec) error {
templateFile: middlewareImplementCodeFile,
builtinTemplate: middlewareImplementCode,
data: map[string]string{
"name": strings.Title(name),
"name": strings.Title(name),
"goModule": projectCtx.Path,
},
})
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/zeromicro/go-zero/core/collection"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/vars"
Expand Down Expand Up @@ -158,13 +159,19 @@ rest.WithPrefix("%s"),`, g.prefix)
routes = strings.TrimSpace(gbuilder.String())
}

projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}

if err := gt.Execute(&builder, map[string]string{
"routes": routes,
"jwt": jwt,
"signature": signature,
"prefix": prefix,
"timeout": timeout,
"maxBytes": maxBytes,
"goModule": projectCtx.Path,
}); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions tools/goctl/api/gogen/gensvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/zeromicro/go-zero/tools/goctl/api/spec"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/vars"
Expand Down Expand Up @@ -40,6 +41,10 @@ func genServiceContext(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpe
configImport += fmt.Sprintf("\n\t\"%s/rest\"", vars.ProjectOpenSourceURL)
}

projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}
return genFile(fileGenConfig{
dir: dir,
subdir: contextDir,
Expand All @@ -53,6 +58,7 @@ func genServiceContext(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpe
"config": "config.Config",
"middleware": middlewareStr,
"middlewareAssignment": middlewareAssignment,
"goModule": projectCtx.Path,
},
})
}
6 changes: 6 additions & 0 deletions tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util"
"github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/util"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
)

Expand Down Expand Up @@ -53,6 +54,10 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
filename := path.Join(dir, typesDir, typeFilename)
os.Remove(filename)

projectCtx, err := ctx.Prepare(dir)
if err != nil {
return err
}
return genFile(fileGenConfig{
dir: dir,
subdir: typesDir,
Expand All @@ -64,6 +69,7 @@ func genTypes(dir string, cfg *config.Config, api *spec.ApiSpec) error {
data: map[string]any{
"types": val,
"containsTime": false,
"goModule": projectCtx.Path,
},
})
}
Expand Down
1 change: 1 addition & 0 deletions tools/goctl/rpc/generator/gencall.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.C
"serviceName": serviceName,
"functions": strings.Join(functions, pathx.NL),
"interface": strings.Join(iFunctions, pathx.NL),
"goModule": ctx.GetModule(),
}, filename, true); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions tools/goctl/rpc/generator/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
"functions": functions,
"packageName": "logic",
"imports": strings.Join(imports.KeysStr(), pathx.NL),
"goModule": ctx.GetModule(),
}, filename, false)
if err != nil {
return err
Expand Down Expand Up @@ -119,6 +120,7 @@ func (g *Generator) genLogicGroup(ctx DirContext, proto parser.Proto, cfg *conf.
"functions": functions,
"packageName": packageName,
"imports": strings.Join(imports.KeysStr(), pathx.NL),
"goModule": ctx.GetModule(),
}, filename, false); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions tools/goctl/rpc/generator/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
"imports": strings.Join(imports, pathx.NL),
"pkg": proto.PbPackage,
"serviceNames": serviceNames,
"goModule": ctx.GetModule(),
}, fileName, false)
}
2 changes: 2 additions & 0 deletions tools/goctl/rpc/generator/genserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (g *Generator) genServerGroup(ctx DirContext, proto parser.Proto, cfg *conf
"imports": strings.Join(imports.KeysStr(), pathx.NL),
"funcs": strings.Join(funcList, pathx.NL),
"notStream": notStream,
"goModule": ctx.GetModule(),
}, serverFile, true); err != nil {
return err
}
Expand Down Expand Up @@ -148,6 +149,7 @@ func (g *Generator) genServerInCompatibility(ctx DirContext, proto parser.Proto,
"imports": strings.Join(imports.KeysStr(), pathx.NL),
"funcs": strings.Join(funcList, pathx.NL),
"notStream": notStream,
"goModule": ctx.GetModule(),
}, serverFile, true)
}

Expand Down
3 changes: 2 additions & 1 deletion tools/goctl/rpc/generator/gensvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (g *Generator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config) err
}

return util.With("svc").GoFmt(true).Parse(text).SaveTo(map[string]any{
"imports": fmt.Sprintf(`"%v"`, ctx.GetConfig().Package),
"imports": fmt.Sprintf(`"%v"`, ctx.GetConfig().Package),
"goModule": ctx.GetModule(),
}, fileName, false)
}
7 changes: 6 additions & 1 deletion tools/goctl/rpc/generator/mkdir.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package generator

import (
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"path/filepath"
"strings"

conf "github.com/zeromicro/go-zero/tools/goctl/config"
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
"github.com/zeromicro/go-zero/tools/goctl/util/format"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
"github.com/zeromicro/go-zero/tools/goctl/util/stringx"
)
Expand Down Expand Up @@ -40,6 +40,7 @@ type (
GetMain() Dir
GetServiceName() stringx.String
SetPbDir(pbDir, grpcDir string)
GetModule() string
}

// Dir defines a directory
Expand Down Expand Up @@ -266,6 +267,10 @@ func (d *defaultDirContext) GetServiceName() stringx.String {
return d.serviceName
}

func (d *defaultDirContext) GetModule() string {
return d.ctx.Path
}

// Valid returns true if the directory is valid
func (d *Dir) Valid() bool {
return len(d.Filename) > 0 && len(d.Package) > 0
Expand Down