Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
go: use new metadata methods (#1262)
Browse files Browse the repository at this point in the history
Updates googleapis/google-cloud-go#624.

metadata.FromContext and metdata.NewContext
are replaced by metadata.{New,From}{Incoming,Outgoing}Context.

Generated unit tests now verify that x-goog-api-client header
is inserted.

Additionally, clients now save a string slice containing x-goog-api-client
header value instead of the value by itself.
This is a small optimization so that we don't create a new slice every time.
  • Loading branch information
pongad authored May 12, 2017
1 parent bf3ef68 commit abbc03a
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/main/resources/com/google/api/codegen/go/doc.snip
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"google.golang.org/grpc/metadata"
)

func insertXGoog(ctx context.Context, val string) context.Context {
md, _ := metadata.FromContext(ctx)
func insertXGoog(ctx context.Context, val []string) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
md = md.Copy()
md["x-goog-api-client"] = []string{val}
return metadata.NewContext(ctx, md)
md["x-goog-api-client"] = val
return metadata.NewOutgoingContext(ctx, md)
}

func DefaultAuthScopes() []string {
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/com/google/api/codegen/go/main.snip
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
CallOptions *{@view.callOptionsTypeName}

// The metadata to be sent with each request.
xGoogHeader string
xGoogHeader []string
}

// {@view.clientConstructorName} creates a new {@view.servicePhraseName} client.
Expand Down Expand Up @@ -113,7 +113,7 @@
func (c *{@view.clientTypeName}) SetGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", version.Go()}, keyval...)
kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeader = gax.XGoogHeader(kv...)
c.xGoogHeader = []string{gax.XGoogHeader(kv...)}
}

@join getter : view.pathTemplateGetters
Expand Down Expand Up @@ -338,7 +338,7 @@
lro *longrunning.Operation

// The metadata to be sent with each request.
xGoogHeader string
xGoogHeader []string
}

// {@lro.constructorName} returns a new {@lro.clientReturnTypeName} from a given name.
Expand Down
19 changes: 18 additions & 1 deletion src/main/resources/com/google/api/codegen/go/mock.snip
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"testing"

"github.com/golang/protobuf/proto"
Expand All @@ -25,6 +27,7 @@
status "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
)

var _ = io.EOF
Expand Down Expand Up @@ -104,7 +107,11 @@
@end

@private simpleMethod(impl, method)
func (s *{@impl.name}) {@method.name}(_ context.Context, req {@method.requestTypeName}) ({@method.responseTypeName}, error) {
func (s *{@impl.name}) {@method.name}(ctx context.Context, req {@method.requestTypeName}) ({@method.responseTypeName}, error) {
md, _ := metadata.FromIncomingContext(ctx)
if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
}
s.reqs = append(s.reqs, req)
if s.err != nil {
return nil, s.err
Expand All @@ -113,8 +120,16 @@
}
@end

@private streamAssertXGoog()
md, _ := metadata.FromIncomingContext(stream.Context())
if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
return fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
}
@end

@private serverStreamMethod(impl, method)
func (s *{@impl.name}) {@method.name}(req {@method.requestTypeName}, stream {@method.streamHandleTypeName}) error {
{@streamAssertXGoog()}
s.reqs = append(s.reqs, req)
if s.err != nil {
return s.err
Expand All @@ -130,6 +145,7 @@

@private clientStreamMethod(impl, method)
func (s *{@impl.name}) {@method.name}(stream {@method.streamHandleTypeName}) error {
{@streamAssertXGoog()}
for {
if req, err := stream.Recv(); err == io.EOF {
break
Expand All @@ -148,6 +164,7 @@

@private bidiMethod(impl, method)
func (s *{@impl.name}) {@method.name}(stream {@method.streamHandleTypeName}) error {
{@streamAssertXGoog()}
for {
if req, err := stream.Recv(); err == io.EOF {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"google.golang.org/grpc/metadata"
)

func insertXGoog(ctx context.Context, val string) context.Context {
md, _ := metadata.FromContext(ctx)
func insertXGoog(ctx context.Context, val []string) context.Context {
md, _ := metadata.FromOutgoingContext(ctx)
md = md.Copy()
md["x-goog-api-client"] = []string{val}
return metadata.NewContext(ctx, md)
md["x-goog-api-client"] = val
return metadata.NewOutgoingContext(ctx, md)
}

func DefaultAuthScopes() []string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type Client struct {
CallOptions *CallOptions

// The metadata to be sent with each request.
xGoogHeader string
xGoogHeader []string
}

// NewClient creates a new library service client.
Expand Down Expand Up @@ -191,7 +191,7 @@ func (c *Client) Close() error {
func (c *Client) SetGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", version.Go()}, keyval...)
kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeader = gax.XGoogHeader(kv...)
c.xGoogHeader = []string{gax.XGoogHeader(kv...)}
}

// LibraryShelfPath returns the path for the shelf resource.
Expand Down Expand Up @@ -858,7 +858,7 @@ type GetBigBookOperation struct {
lro *longrunning.Operation

// The metadata to be sent with each request.
xGoogHeader string
xGoogHeader []string
}

// GetBigBookOperation returns a new GetBigBookOperation from a given name.
Expand Down Expand Up @@ -943,7 +943,7 @@ type GetBigNothingOperation struct {
lro *longrunning.Operation

// The metadata to be sent with each request.
xGoogHeader string
xGoogHeader []string
}

// GetBigNothingOperation returns a new GetBigNothingOperation from a given name.
Expand Down
Loading

0 comments on commit abbc03a

Please sign in to comment.