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

modify the service name from proto #230

Merged
merged 1 commit into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions tools/goctl/rpc/generator/genetc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Etcd:

func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
dir := ctx.GetEtc()
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetMain().Base)
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil {
return err
}
Expand All @@ -35,6 +35,6 @@ func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Conf
}

return util.With("etc").Parse(text).SaveTo(map[string]interface{}{
"serviceName": strings.ToLower(stringx.From(ctx.GetMain().Base).ToCamel()),
"serviceName": strings.ToLower(stringx.From(ctx.GetServiceName().Source()).ToCamel()),
}, fileName, false)
}
7 changes: 3 additions & 4 deletions tools/goctl/rpc/generator/genmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ func main() {
`

func (g *defaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
dir := ctx.GetMain()
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetMain().Base)
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil {
return err
}

fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.go", mainFilename))
fileName := filepath.Join(ctx.GetMain().Filename, fmt.Sprintf("%v.go", mainFilename))
imports := make([]string, 0)
pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().Package)
Expand All @@ -69,7 +68,7 @@ func (g *defaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf

return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"head": head,
"serviceName": strings.ToLower(stringx.From(ctx.GetMain().Base).ToCamel()),
"serviceName": strings.ToLower(ctx.GetServiceName().ToCamel()),
"imports": strings.Join(imports, util.NL),
"pkg": proto.PbPackage,
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),
Expand Down
12 changes: 10 additions & 2 deletions tools/goctl/rpc/generator/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type (
GetSvc() Dir
GetPb() Dir
GetMain() Dir
GetServiceName() stringx.String
}

Dir struct {
Expand All @@ -41,7 +42,8 @@ type (
Package string
}
defaultDirContext struct {
inner map[string]Dir
inner map[string]Dir
serviceName stringx.String
}
)

Expand Down Expand Up @@ -110,8 +112,10 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto) (DirContext, error) {
return nil, err
}
}
serviceName := strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
return &defaultDirContext{
inner: inner,
inner: inner,
serviceName: stringx.From(strings.ReplaceAll(serviceName, "-", "")),
}, nil
}

Expand Down Expand Up @@ -151,6 +155,10 @@ func (d *defaultDirContext) GetMain() Dir {
return d.inner[wd]
}

func (d *defaultDirContext) GetServiceName() stringx.String {
return d.serviceName
}

func (d *Dir) Valid() bool {
return len(d.Filename) > 0 && len(d.Package) > 0
}