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

feat: add service renaming #1580

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion internal/gengapic/doc_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (g *generator) genDocFile(year int, scopes []string, serv *descriptorpb.Ser
p("//")
p("// To get started with this package, create a client.")
// Code block for client creation
servName := pbinfo.ReduceServName(serv.GetName(), g.opts.pkgName)
override := g.getServiceNameOverride(serv)
servName := pbinfo.ReduceServNameWithOverride(serv.GetName(), g.opts.pkgName, override)
tmpClient := g.pt
g.pt = printer.P{}
g.exampleInitClient(g.opts.pkgName, servName)
Expand Down
6 changes: 4 additions & 2 deletions internal/gengapic/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (

func (g *generator) genExampleFile(serv *descriptorpb.ServiceDescriptorProto) error {
pkgName := g.opts.pkgName
servName := pbinfo.ReduceServName(serv.GetName(), pkgName)
override := g.getServiceNameOverride(serv)
servName := pbinfo.ReduceServNameWithOverride(serv.GetName(), pkgName, override)

g.exampleClientFactory(pkgName, servName)

Expand All @@ -42,7 +43,8 @@ func (g *generator) genExampleFile(serv *descriptorpb.ServiceDescriptorProto) er

func (g *generator) genExampleIteratorFile(serv *descriptorpb.ServiceDescriptorProto) error {
pkgName := g.opts.pkgName
servName := pbinfo.ReduceServName(serv.GetName(), pkgName)
override := g.getServiceNameOverride(serv)
servName := pbinfo.ReduceServNameWithOverride(serv.GetName(), pkgName, override)
methods := append(serv.GetMethod(), g.getMixinMethods()...)
for _, m := range methods {
// Don't need streaming RPCs
Expand Down
17 changes: 17 additions & 0 deletions internal/gengapic/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,20 @@ func (g *generator) autoPopulatedFields(_ string, m *descriptorpb.MethodDescript
}
return validated
}

// getServiceNameOverride checks to see if the service has a defined service name override.
func (g *generator) getServiceNameOverride(s *descriptorpb.ServiceDescriptorProto) string {
// TODO(hongalex): replace the manual map with service config
// ls := g.serviceConfig.GetPublishing().GetLibrarySettings()[0]
// renamedServices := ls.GetGoSettings().GetRenamedServices()

renamedServices := make(map[string]string)
renamedServices["Publisher"] = "TopicAdmin"
renamedServices["Subscriber"] = "SubscriptionAdmin"

if v, ok := renamedServices[s.GetName()]; ok {
return v
}

return ""
}
7 changes: 5 additions & 2 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func gen(genReq *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse
// so even though the client for LoggingServiceV2 is just "Client"
// the file name is "logging_client.go".
// Keep the current behavior for now, but we could revisit this later.
servName := pbinfo.ReduceServName(s.GetName(), "")
override := g.getServiceNameOverride(s)
servName := pbinfo.ReduceServNameWithOverride(s.GetName(), "", override)
outFile := camelToSnake(servName)
outFile = filepath.Join(g.opts.outDir, outFile)

Expand Down Expand Up @@ -200,7 +201,9 @@ func (g *generator) collectServices(genReq *pluginpb.CodeGeneratorRequest) (genS

// gen generates client for the given service.
func (g *generator) gen(serv *descriptorpb.ServiceDescriptorProto) error {
servName := pbinfo.ReduceServName(serv.GetName(), g.opts.pkgName)
// If using service name overrides, use that directly for the rest of generation.
override := g.getServiceNameOverride(serv)
servName := pbinfo.ReduceServNameWithOverride(serv.GetName(), g.opts.pkgName, override)

g.clientHook(servName)
if err := g.clientOptions(serv, servName); err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/gengapic/gengrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ func (g *generator) emptyUnaryGRPCCall(servName string, m *descriptorpb.MethodDe
}

func (g *generator) grpcStubCall(method *descriptorpb.MethodDescriptorProto) string {
service := g.descInfo.ParentElement[method]
stub := pbinfo.ReduceServName(service.GetName(), g.opts.pkgName)
service := g.descInfo.ParentElement[method].(*descriptorpb.ServiceDescriptorProto)
override := g.getServiceNameOverride(service)
stub := pbinfo.ReduceServNameWithOverride(service.GetName(), g.opts.pkgName, override)
return fmt.Sprintf("c.%s.%s(ctx, req, settings.GRPC...)", grpcClientField(stub), method.GetName())
}

Expand Down
9 changes: 9 additions & 0 deletions internal/pbinfo/pbinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ func (in *Info) ImportSpec(e ProtoType) (ImportSpec, error) {
return imp, err
}

// ReduceServNameWithOverride returns the override string if present,
// otherwise calls ReduceServName.
func ReduceServNameWithOverride(svc, pkg, override string) string {
if override != "" {
return override
}
return ReduceServName(svc, pkg)
}

// ReduceServName removes redundant components from the service name.
// For example, FooServiceV2 -> Foo.
// The returned name is used as part of longer names, like FooClient.
Expand Down