feat: add cmdline argument to control whether generate package name from proto filename (#5387)

This commit is contained in:
Kevin Wan
2026-01-24 19:47:14 +08:00
committed by GitHub
parent 6e1af75635
commit 173f76acf9
9 changed files with 221 additions and 24 deletions

View File

@@ -199,7 +199,9 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, conf *conf.Config, c *ZR
return nil, err
}
}
serviceName := proto.Package.Name
serviceName := determineServiceName(proto, c)
return &defaultDirContext{
ctx: ctx,
inner: inner,
@@ -270,3 +272,16 @@ func (d *defaultDirContext) GetServiceName() stringx.String {
func (d *Dir) Valid() bool {
return len(d.Filename) > 0 && len(d.Package) > 0
}
// determineServiceName returns the service name based on the proto file and context.
// By default, it uses the proto package name (supports multi-proto files).
// Falls back to filename if --name-from-filename flag is set or package name is empty.
func determineServiceName(proto parser.Proto, c *ZRpcContext) string {
if c != nil && c.NameFromFilename {
return strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
}
if proto.Package.Package != nil && len(proto.Package.Name) > 0 {
return proto.Package.Name
}
return strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
}