Skip to content
Merged
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
10 changes: 8 additions & 2 deletions internal/gengapic/client_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (g *generator) internalClientIntfInit(serv *descriptorpb.ServiceDescriptorP
}

// serviceDoc is a helper function similar to methodDoc that includes a deprecation notice for deprecated services.
func (g *generator) serviceDoc(serv *descriptorpb.ServiceDescriptorProto) {
func (g *generator) serviceDoc(serv *descriptorpb.ServiceDescriptorProto, includeAPIVersion bool) {
com := g.comments[serv]

// If there's no comment and the service is not deprecated, return.
Expand Down Expand Up @@ -184,6 +184,12 @@ func (g *generator) serviceDoc(serv *descriptorpb.ServiceDescriptorProto) {
// Prepend new line break before existing service comments.
g.printf("//")
g.comment(com)

apiVersion := proto.GetExtension(serv.Options, annotations.E_ApiVersion).(string)
if apiVersion != "" && includeAPIVersion {
g.printf("//")
g.comment(fmt.Sprintf("This client uses %s version %s.", serv.GetName(), apiVersion))
}
}

func (g *generator) clientInit(serv *descriptorpb.ServiceDescriptorProto, servName string, hasRPCForLRO bool) {
Expand All @@ -192,7 +198,7 @@ func (g *generator) clientInit(serv *descriptorpb.ServiceDescriptorProto, servNa
// client struct
p("// %sClient is a client for interacting with %s.", servName, g.apiName)
p("// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.")
g.serviceDoc(serv)
g.serviceDoc(serv, true) // include API version docs
p("type %sClient struct {", servName)

// Fields
Expand Down
23 changes: 16 additions & 7 deletions internal/gengapic/client_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,21 @@ func TestClientOpt(t *testing.T) {
}

func TestServiceDoc(t *testing.T) {
sOpts := &descriptorpb.ServiceOptions{}
proto.SetExtension(sOpts, annotations.E_ApiVersion, "2024-09-14")

serv := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("MyService"),
Name: proto.String("MyService"),
Options: sOpts,
}

var g generator
g.comments = make(map[protoiface.MessageV1]string)

for _, tst := range []struct {
in, want string
deprecated bool
in, want string
deprecated bool
includeAPIVersion bool
}{
{
in: "",
Expand All @@ -279,6 +284,11 @@ func TestServiceDoc(t *testing.T) {
in: "Does stuff.\n It also does other stuffs.",
want: "//\n// Does stuff.\n// It also does other stuffs.\n",
},
{
in: "Does stuff.\n It also does other stuffs.",
want: "//\n// Does stuff.\n// It also does other stuffs.\n//\n// This client uses MyService version 2024-09-14.\n",
includeAPIVersion: true,
},
{
in: "This is deprecated.\n It does not have a proper comment.",
want: "//\n// This is deprecated.\n// It does not have a proper comment.\n//\n// Deprecated: MyService may be removed in a future version.\n",
Expand All @@ -301,11 +311,10 @@ func TestServiceDoc(t *testing.T) {
},
} {
g.comments[serv] = tst.in
serv.Options = &descriptorpb.ServiceOptions{
Deprecated: proto.Bool(tst.deprecated),
}
serv.Options.Deprecated = proto.Bool(tst.deprecated)

g.pt.Reset()
g.serviceDoc(serv)
g.serviceDoc(serv, tst.includeAPIVersion)
if diff := cmp.Diff(g.pt.String(), tst.want); diff != "" {
t.Errorf("comment() got(-),want(+):\n%s", diff)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/gengapic/gengrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (g *generator) grpcClientUtilities(serv *descriptorpb.ServiceDescriptorProt
// Factory function
p("// New%sClient creates a new %s client based on gRPC.", servName, clientName)
p("// The returned client must be Closed when it is done being used to clean up its underlying connections.")
g.serviceDoc(serv)
g.serviceDoc(serv, false) // exclude API version docs
p("func New%[1]sClient(ctx context.Context, opts ...option.ClientOption) (*%[1]sClient, error) {", servName)
p(" clientOpts := default%[1]sGRPCClientOptions()", servName)

Expand Down
2 changes: 1 addition & 1 deletion internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (g *generator) restClientUtilities(serv *descriptorpb.ServiceDescriptorProt
opServ, hasCustomOp := g.customOpServices[serv]

p("// New%sRESTClient creates a new %s rest client.", servName, clientName)
g.serviceDoc(serv)
g.serviceDoc(serv, false) // exclude API version docs
p("func New%[1]sRESTClient(ctx context.Context, opts ...option.ClientOption) (*%[1]sClient, error) {", servName)
p(" clientOpts := append(default%sRESTClientOptions(), opts...)", servName)
p(" httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)")
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/empty_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type internalClient interface {
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
//
// Foo service does stuff.
//
// This client uses Foo version v1_20240425.
type Client struct {
// The internal transport-dependent client.
internalClient internalClient
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/foo_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type internalFooClient interface {
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
//
// Foo service does stuff.
//
// This client uses Foo version v1_20240425.
type FooClient struct {
// The internal transport-dependent client.
internalClient internalFooClient
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/foo_rest_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type internalFooClient interface {
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
//
// Foo service does stuff.
//
// This client uses Foo version v1_20240425.
type FooClient struct {
// The internal transport-dependent client.
internalClient internalFooClient
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/lro_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type internalFooClient interface {
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
//
// Foo service does stuff.
//
// This client uses Foo version v1_20240425.
type FooClient struct {
// The internal transport-dependent client.
internalClient internalFooClient
Expand Down