-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add service renaming * fix other uses of ReduceServName * fix comments * fix comments and style * switch to package relative renaming * switch to official genproto package * check for zero length lang settings * override rest client naming * add test for grpc client renaming * call proper rest client name
- Loading branch information
Showing
9 changed files
with
265 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gengapic | ||
|
||
import ( | ||
"bytes" | ||
"path/filepath" | ||
"testing" | ||
|
||
conf "github.com/googleapis/gapic-generator-go/internal/grpc_service_config" | ||
"github.com/googleapis/gapic-generator-go/internal/pbinfo" | ||
"github.com/googleapis/gapic-generator-go/internal/txtdiff" | ||
"google.golang.org/genproto/googleapis/api/annotations" | ||
"google.golang.org/genproto/googleapis/api/serviceconfig" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/descriptorpb" | ||
duration "google.golang.org/protobuf/types/known/durationpb" | ||
) | ||
|
||
func TestServiceRenaming(t *testing.T) { | ||
inputType := &descriptorpb.DescriptorProto{ | ||
Name: proto.String("InputType"), | ||
} | ||
outputType := &descriptorpb.DescriptorProto{ | ||
Name: proto.String("OutputType"), | ||
} | ||
metadataType := &descriptorpb.DescriptorProto{ | ||
Name: proto.String("MetadataType"), | ||
} | ||
|
||
file := &descriptorpb.FileDescriptorProto{ | ||
Package: proto.String("my.pkg"), | ||
Options: &descriptorpb.FileOptions{ | ||
GoPackage: proto.String("mypackage"), | ||
}, | ||
} | ||
serv := &descriptorpb.ServiceDescriptorProto{ | ||
Name: proto.String("Foo"), | ||
} | ||
|
||
var g generator | ||
g.imports = map[pbinfo.ImportSpec]bool{} | ||
g.opts = &options{ | ||
pkgName: "pkg", | ||
transports: []transport{grpc}, | ||
} | ||
cpb := &conf.ServiceConfig{ | ||
MethodConfig: []*conf.MethodConfig{ | ||
{ | ||
Name: []*conf.MethodConfig_Name{ | ||
{ | ||
Service: "my.pkg.Foo", | ||
}, | ||
}, | ||
Timeout: &duration.Duration{Seconds: 10}, | ||
}, | ||
}, | ||
} | ||
data, err := protojson.Marshal(cpb) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
in := bytes.NewReader(data) | ||
g.grpcConf, err = conf.New(in) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
commonTypes(&g) | ||
for _, typ := range []*descriptorpb.DescriptorProto{ | ||
inputType, outputType, metadataType, | ||
} { | ||
g.descInfo.Type[".my.pkg."+*typ.Name] = typ | ||
g.descInfo.ParentFile[typ] = file | ||
} | ||
g.descInfo.ParentFile[serv] = file | ||
g.descInfo.ParentElement = map[pbinfo.ProtoType]pbinfo.ProtoType{} | ||
g.serviceConfig = &serviceconfig.Service{ | ||
Publishing: &annotations.Publishing{ | ||
LibrarySettings: []*annotations.ClientLibrarySettings{ | ||
{ | ||
GoSettings: &annotations.GoSettings{ | ||
RenamedServices: map[string]string{"Foo": "Bar"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
m := &descriptorpb.MethodDescriptorProto{ | ||
Name: proto.String("Baz"), | ||
InputType: proto.String(".my.pkg.InputType"), | ||
OutputType: proto.String(emptyType), | ||
} | ||
|
||
serv.Method = []*descriptorpb.MethodDescriptorProto{ | ||
m, | ||
} | ||
g.descInfo.ParentFile[serv] = file | ||
g.descInfo.ParentElement[m] = serv | ||
imp, err := g.descInfo.ImportSpec(serv) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Test the generation of the client boilerplate and single gRPC method rename. | ||
g.grpcClientInit(serv, "Bar", imp, false) | ||
if err := g.genGRPCMethod("Bar", serv, m); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
txtdiff.Diff(t, g.pt.String(), filepath.Join("testdata", "service_rename.want")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// barGRPCClient is a client for interacting with over gRPC transport. | ||
// | ||
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. | ||
type barGRPCClient struct { | ||
// Connection pool of gRPC connections to the service. | ||
connPool gtransport.ConnPool | ||
|
||
// Points back to the CallOptions field of the containing BarClient | ||
CallOptions **BarCallOptions | ||
|
||
// The gRPC API client. | ||
barClient mypackagepb.FooClient | ||
|
||
// The x-goog-* metadata to be sent with each request. | ||
xGoogHeaders []string | ||
|
||
logger *slog.Logger | ||
} | ||
|
||
// NewBarClient creates a new bar client based on gRPC. | ||
// The returned client must be Closed when it is done being used to clean up its underlying connections. | ||
func NewBarClient(ctx context.Context, opts ...option.ClientOption) (*BarClient, error) { | ||
clientOpts := defaultBarGRPCClientOptions() | ||
if newBarClientHook != nil { | ||
hookOpts, err := newBarClientHook(ctx, clientHookParams{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientOpts = append(clientOpts, hookOpts...) | ||
} | ||
|
||
connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
client := BarClient{CallOptions: defaultBarCallOptions()} | ||
|
||
c := &barGRPCClient{ | ||
connPool: connPool, | ||
barClient: mypackagepb.NewFooClient(connPool), | ||
CallOptions: &client.CallOptions, | ||
logger: internaloption.GetLogger(opts), | ||
|
||
} | ||
c.setGoogleClientInfo() | ||
|
||
client.internalClient = c | ||
|
||
return &client, nil | ||
} | ||
|
||
// Connection returns a connection to the API service. | ||
// | ||
// Deprecated: Connections are now pooled so this method does not always | ||
// return the same resource. | ||
func (c *barGRPCClient) Connection() *grpc.ClientConn { | ||
return c.connPool.Conn() | ||
} | ||
|
||
// setGoogleClientInfo sets the name and version of the application in | ||
// the `x-goog-api-client` header passed on each request. Intended for | ||
// use by Google-written clients. | ||
func (c *barGRPCClient) setGoogleClientInfo(keyval ...string) { | ||
kv := append([]string{"gl-go", gax.GoVersion}, keyval...) | ||
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version) | ||
c.xGoogHeaders = []string{ | ||
"x-goog-api-client", gax.XGoogHeader(kv...), | ||
} | ||
} | ||
|
||
// Close closes the connection to the API service. The user should invoke this when | ||
// the client is no longer required. | ||
func (c *barGRPCClient) Close() error { | ||
return c.connPool.Close() | ||
} | ||
|
||
func (c *barGRPCClient) Baz(ctx context.Context, req *mypackagepb.InputType, opts ...gax.CallOption) error { | ||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...) | ||
opts = append((*c.CallOptions).Baz[0:len((*c.CallOptions).Baz):len((*c.CallOptions).Baz)], opts...) | ||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { | ||
var err error | ||
_, err = executeRPC(ctx, c.barClient.Baz, req, settings.GRPC, c.logger, "Baz") | ||
return err | ||
}, opts...) | ||
return err | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters