Skip to content

Commit

Permalink
fix: change pkg var name / feat: project/service pkg add to rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
6xiaowu9 committed Jan 16, 2024
1 parent 54dccd2 commit 6058d3c
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func genConfig(dir, projectPkg, rootPkg string, cfg *config.Config, api *spec.Ap
builtinTemplate: configTemplate,
data: map[string]string{
"projectPkg": projectPkg,
"rootPkg": rootPkg,
"servicePkg": rootPkg,
"authImport": authImportStr,
"auth": strings.Join(auths, "\n"),
"jwtTrans": strings.Join(jwtTransList, "\n"),
Expand Down
4 changes: 2 additions & 2 deletions tools/goctl/api/gogen/genhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var handlerTemplate string

type handlerInfo struct {
ProjectPkg string
RootPkg string
ServicePkg string
PkgName string
ImportPackages string
ImportHttpxPackage string
Expand All @@ -44,7 +44,7 @@ func genHandler(dir, projectPkg, rootPkg string, cfg *config.Config, group spec.
}
return doGenToFile(dir, handler, cfg, group, route, handlerInfo{
ProjectPkg: projectPkg,
RootPkg: rootPkg,
ServicePkg: rootPkg,
PkgName: pkgName,
ImportPackages: genHandlerImports(group, route, rootPkg),
HandlerName: handler,
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func genMain(dir, projectPkg, rootPkg string, cfg *config.Config, api *spec.ApiS
builtinTemplate: mainTemplate,
data: map[string]string{
"projectPkg": projectPkg,
"rootPkg": rootPkg,
"servicePkg": rootPkg,
"importPackages": genMainImports(rootPkg),
"serviceName": configName,
},
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/genmiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func genMiddleware(dir, projectPkg, rootPkg string, cfg *config.Config, api *spe
builtinTemplate: middlewareImplementCode,
data: map[string]string{
"projectPkg": projectPkg,
"rootPkg": rootPkg,
"servicePkg": rootPkg,
"name": strings.Title(name),
},
})
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/genroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ rest.WithPrefix("%s"),`, g.prefix)
builtinTemplate: routesTemplate,
data: map[string]any{
"projectPkg": projectPkg,
"rootPkg": rootPkg,
"servicePkg": rootPkg,
"hasTimeout": hasTimeout,
"importPackages": genRouteImports(rootPkg, api),
"routesAdditions": strings.TrimSpace(builder.String()),
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/gensvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func genServiceContext(dir, projectPkg, rootPkg string, cfg *config.Config, api
builtinTemplate: contextTemplate,
data: map[string]string{
"projectPkg": projectPkg,
"rootPkg": rootPkg,
"servicePkg": rootPkg,
"configImport": configImport,
"config": "config.Config",
"middleware": middlewareStr,
Expand Down
2 changes: 1 addition & 1 deletion tools/goctl/api/gogen/gentypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func genTypes(dir, projectPkg, rootPkg string, cfg *config.Config, api *spec.Api
builtinTemplate: typesTemplate,
data: map[string]any{
"projectPkg": projectPkg,
"rootPkg": rootPkg,
"servicePkg": rootPkg,
"types": val,
"containsTime": false,
},
Expand Down
5 changes: 5 additions & 0 deletions tools/goctl/rpc/generator/gencall.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func (g *Generator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf.Config
}

func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
projectPkg := ctx.GetPath().Source()
servicePkg := ctx.GetMain().Package

dir := ctx.GetCall()
head := util.GetHead(proto.Name)
for _, service := range proto.Service {
Expand Down Expand Up @@ -105,6 +108,8 @@ func (g *Generator) genCallGroup(ctx DirContext, proto parser.Proto, cfg *conf.C
aliasKeys := alias.KeysStr()
sort.Strings(aliasKeys)
if err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]any{
"projectPkg": projectPkg,
"servicePkg": servicePkg,
"name": callFilename,
"alias": strings.Join(aliasKeys, pathx.NL),
"head": head,
Expand Down
5 changes: 5 additions & 0 deletions tools/goctl/rpc/generator/genlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (g *Generator) GenLogic(ctx DirContext, proto parser.Proto, cfg *conf.Confi

func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
cfg *conf.Config) error {
projectPkg := ctx.GetPath().Source()
servicePkg := ctx.GetMain().Package

dir := ctx.GetLogic()
service := proto.Service[0].Service.Name
for _, rpc := range proto.Service[0].RPC {
Expand All @@ -61,6 +64,8 @@ func (g *Generator) genLogicInCompatibility(ctx DirContext, proto parser.Proto,
return err
}
err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]any{
"projectPkg": projectPkg,
"servicePkg": servicePkg,
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
"functions": functions,
"packageName": "logic",
Expand Down
5 changes: 5 additions & 0 deletions tools/goctl/rpc/generator/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
return err
}

projectPkg := ctx.GetPath().Source()
servicePkg := ctx.GetMain().Package

fileName := filepath.Join(ctx.GetMain().Filename, fmt.Sprintf("%v.go", mainFilename))
imports := make([]string, 0)
pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
Expand Down Expand Up @@ -74,6 +77,8 @@ func (g *Generator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config
}

return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]any{
"projectPkg": projectPkg,
"servicePkg": servicePkg,
"serviceName": etcFileName,
"imports": strings.Join(imports, pathx.NL),
"pkg": proto.PbPackage,
Expand Down
7 changes: 6 additions & 1 deletion tools/goctl/rpc/generator/genserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (g *Generator) GenServer(ctx DirContext, proto parser.Proto, cfg *conf.Conf
}

func (g *Generator) genServerGroup(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
projectPkg := ctx.GetPath().Source()
servicePkg := ctx.GetMain().Package

dir := ctx.GetServer()
for _, service := range proto.Service {
var (
Expand Down Expand Up @@ -90,7 +93,9 @@ func (g *Generator) genServerGroup(ctx DirContext, proto parser.Proto, cfg *conf
}

if err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]any{
"head": head,
"projectPkg": projectPkg,
"servicePkg": servicePkg,
"head": head,
"unimplementedServer": fmt.Sprintf("%s.Unimplemented%sServer", proto.PbPackage,
stringx.From(service.Name).ToCamel()),
"server": stringx.From(service.Name).ToCamel(),
Expand Down
8 changes: 7 additions & 1 deletion tools/goctl/rpc/generator/gensvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ var svcTemplate string
// GenSvc generates the servicecontext.go file, which is the resource dependency of a service,
// such as rpc dependency, model dependency, etc.
func (g *Generator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
projectPkg := ctx.GetPath().Source()
servicePkg := ctx.GetMain().Package

dir := ctx.GetSvc()

svcFilename, err := format.FileNamingFormat(cfg.NamingFormat, "service_context")
if err != nil {
return err
Expand All @@ -31,6 +35,8 @@ 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),
"projectPkg": projectPkg,
"servicePkg": servicePkg,
"imports": fmt.Sprintf(`"%v"`, ctx.GetConfig().Package),
}, fileName, false)
}
10 changes: 8 additions & 2 deletions tools/goctl/rpc/generator/mkdir.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package generator

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

"github.com/zeromicro/go-zero/tools/goctl/util/format"

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"
Expand Down Expand Up @@ -39,6 +40,7 @@ type (
GetProtoGo() Dir
GetMain() Dir
GetServiceName() stringx.String
GetPath() stringx.String
SetPbDir(pbDir, grpcDir string)
}

Expand Down Expand Up @@ -91,7 +93,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, conf *conf.Config, c *ZR
if !c.Multiple {
callDir := filepath.Join(ctx.WorkDir,
strings.ToLower(stringx.From(proto.Service[0].Name).ToCamel()))
if strings.EqualFold(proto.Service[0].Name, filepath.Base(proto.GoPackage)) {
if strings.EqualFold(proto.Service[0].Name, stringx.From(filepath.Base(proto.GoPackage)).ToCamel()) {
var err error
clientDir, err = format.FileNamingFormat(conf.NamingFormat, proto.Service[0].Name+"_client")
if err != nil {
Expand Down Expand Up @@ -266,6 +268,10 @@ func (d *defaultDirContext) GetServiceName() stringx.String {
return d.serviceName
}

func (d *defaultDirContext) GetPath() stringx.String {
return stringx.From(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

0 comments on commit 6058d3c

Please sign in to comment.