diff --git a/tools/goctl/rpc/generator/genetc.go b/tools/goctl/rpc/generator/genetc.go index f6c9f867..caeffb99 100644 --- a/tools/goctl/rpc/generator/genetc.go +++ b/tools/goctl/rpc/generator/genetc.go @@ -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 } @@ -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) } diff --git a/tools/goctl/rpc/generator/genmain.go b/tools/goctl/rpc/generator/genmain.go index 5e5d39dc..b62eb4e4 100644 --- a/tools/goctl/rpc/generator/genmain.go +++ b/tools/goctl/rpc/generator/genmain.go @@ -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) @@ -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(), diff --git a/tools/goctl/rpc/generator/mkdir.go b/tools/goctl/rpc/generator/mkdir.go index d43ae099..8be924d1 100644 --- a/tools/goctl/rpc/generator/mkdir.go +++ b/tools/goctl/rpc/generator/mkdir.go @@ -33,6 +33,7 @@ type ( GetSvc() Dir GetPb() Dir GetMain() Dir + GetServiceName() stringx.String } Dir struct { @@ -41,7 +42,8 @@ type ( Package string } defaultDirContext struct { - inner map[string]Dir + inner map[string]Dir + serviceName stringx.String } ) @@ -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 } @@ -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 }