modify the service name from proto (#230)

master
Keson 4 years ago committed by GitHub
parent 3ebb1e0221
commit 2dfecda465
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,7 +22,7 @@ Etcd:
func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error { func (g *defaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Config) error {
dir := ctx.GetEtc() dir := ctx.GetEtc()
etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetMain().Base) etcFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
if err != nil { if err != nil {
return err 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{}{ 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) }, fileName, false)
} }

@ -48,13 +48,12 @@ func main() {
` `
func (g *defaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error { func (g *defaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf.Config) error {
dir := ctx.GetMain() mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetServiceName().Source())
mainFilename, err := format.FileNamingFormat(cfg.NamingFormat, ctx.GetMain().Base)
if err != nil { if err != nil {
return err 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) imports := make([]string, 0)
pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package) pbImport := fmt.Sprintf(`"%v"`, ctx.GetPb().Package)
svcImport := fmt.Sprintf(`"%v"`, ctx.GetSvc().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{}{ return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"head": head, "head": head,
"serviceName": strings.ToLower(stringx.From(ctx.GetMain().Base).ToCamel()), "serviceName": strings.ToLower(ctx.GetServiceName().ToCamel()),
"imports": strings.Join(imports, util.NL), "imports": strings.Join(imports, util.NL),
"pkg": proto.PbPackage, "pkg": proto.PbPackage,
"serviceNew": stringx.From(proto.Service.Name).ToCamel(), "serviceNew": stringx.From(proto.Service.Name).ToCamel(),

@ -33,6 +33,7 @@ type (
GetSvc() Dir GetSvc() Dir
GetPb() Dir GetPb() Dir
GetMain() Dir GetMain() Dir
GetServiceName() stringx.String
} }
Dir struct { Dir struct {
@ -41,7 +42,8 @@ type (
Package string Package string
} }
defaultDirContext struct { 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 return nil, err
} }
} }
serviceName := strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
return &defaultDirContext{ return &defaultDirContext{
inner: inner, inner: inner,
serviceName: stringx.From(strings.ReplaceAll(serviceName, "-", "")),
}, nil }, nil
} }
@ -151,6 +155,10 @@ func (d *defaultDirContext) GetMain() Dir {
return d.inner[wd] return d.inner[wd]
} }
func (d *defaultDirContext) GetServiceName() stringx.String {
return d.serviceName
}
func (d *Dir) Valid() bool { func (d *Dir) Valid() bool {
return len(d.Filename) > 0 && len(d.Package) > 0 return len(d.Filename) > 0 && len(d.Package) > 0
} }

Loading…
Cancel
Save