From eae7c5260a262a4e27cd003688ab7514e4a4826e Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Mon, 25 Jan 2016 21:55:08 +0900 Subject: [PATCH 01/11] get header and trailer metadata from server and set as ServerMetadata context --- .../gengateway/template.go | 42 ++++++++++++------- .../gengateway/template_test.go | 8 ++-- runtime/context.go | 18 ++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index 6eaff63bb7e..c731be72190 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -127,17 +127,18 @@ var _ = utilities.NewDoubleArray _ = template.Must(handlerTemplate.New("request-func-signature").Parse(strings.Replace(` {{if .Method.GetServerStreaming}} -func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx context.Context, client {{.Method.Service.GetName}}Client, req *http.Request, pathParams map[string]string) ({{.Method.Service.GetName}}_{{.Method.GetName}}Client, error) +func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx context.Context, client {{.Method.Service.GetName}}Client, req *http.Request, pathParams map[string]string) ({{.Method.Service.GetName}}_{{.Method.GetName}}Client, runtime.ServerMetadata, error) {{else}} -func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx context.Context, client {{.Method.Service.GetName}}Client, req *http.Request, pathParams map[string]string) (proto.Message, error) +func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx context.Context, client {{.Method.Service.GetName}}Client, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {{end}}`, "\n", "", -1))) _ = template.Must(handlerTemplate.New("client-streaming-request-func").Parse(` {{template "request-func-signature" .}} { + var metadata runtime.ServerMetadata stream, err := client.{{.Method.GetName}}(ctx) if err != nil { glog.Errorf("Failed to start streaming: %v", err) - return nil, err + return nil, metadata, err } dec := json.NewDecoder(req.Body) for { @@ -148,21 +149,24 @@ func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx cont } if err != nil { glog.Errorf("Failed to decode request: %v", err) - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err = stream.Send(&protoReq); err != nil { glog.Errorf("Failed to send request: %v", err) - return nil, err + return nil, metadata, err } } {{if .Method.GetServerStreaming}} if err = stream.CloseSend(); err != nil { glog.Errorf("Failed to terminate client stream: %v", err) - return nil, err + return nil, metadata, err } - return stream, nil + metadata.TrailerMD = stream.Trailer() + return stream, metadata, nil {{else}} - return stream.CloseAndRecv() + msg, err := stream.CloseAndRecv() + metadata.TrailerMD = stream.Trailer() + return msg, metadata, err {{end}} } `)) @@ -175,9 +179,10 @@ var ( {{end}} {{template "request-func-signature" .}} { var protoReq {{.Method.RequestType.GoType .Method.Service.File.GoPkg.Path}} + var metadata runtime.ServerMetadata {{if .Body}} if err := json.NewDecoder(req.Body).Decode(&{{.Body.RHS "protoReq"}}); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } {{end}} {{if .PathParams}} @@ -190,7 +195,7 @@ var ( {{range $param := .PathParams}} val, ok = pathParams[{{$param | printf "%q"}}] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", {{$param | printf "%q"}}) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", {{$param | printf "%q"}}) } {{if $param.IsNestedProto3 }} err = runtime.PopulateFieldFromPath(&protoReq, {{$param | printf "%q"}}, val) @@ -198,17 +203,23 @@ var ( {{$param.RHS "protoReq"}}, err = {{$param.ConvertFuncExpr}}(val) {{end}} if err != nil { - return nil, err + return nil, metadata, err } {{end}} {{end}} {{if .HasQueryParam}} if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } {{end}} - - return client.{{.Method.GetName}}(ctx, &protoReq) +{{if .Method.GetServerStreaming}} + stream, err := client.{{.Method.GetName}}(ctx, &protoReq) + metadata.TrailerMD = stream.Trailer() + return stream, metadata, err +{{else}} + msg, err := client.{{.Method.GetName}}(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err +{{end}} }`)) trailerTemplate = template.Must(template.New("trailer").Parse(` @@ -253,7 +264,8 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return diff --git a/protoc-gen-grpc-gateway/gengateway/template_test.go b/protoc-gen-grpc-gateway/gengateway/template_test.go index 77e8d50fbb3..c53d51e0eeb 100644 --- a/protoc-gen-grpc-gateway/gengateway/template_test.go +++ b/protoc-gen-grpc-gateway/gengateway/template_test.go @@ -133,11 +133,11 @@ func TestApplyTemplateRequestWithoutClientStreaming(t *testing.T) { }{ { serverStreaming: false, - sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) {`, + sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {`, }, { serverStreaming: true, - sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (ExampleService_EchoClient, error) {`, + sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (ExampleService_EchoClient, runtime.ServerMetadata, error) {`, }, } { meth.ServerStreaming = proto.Bool(spec.serverStreaming) @@ -294,11 +294,11 @@ func TestApplyTemplateRequestWithClientStreaming(t *testing.T) { }{ { serverStreaming: false, - sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) {`, + sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {`, }, { serverStreaming: true, - sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (ExampleService_EchoClient, error) {`, + sigWant: `func request_ExampleService_Echo_0(ctx context.Context, client ExampleServiceClient, req *http.Request, pathParams map[string]string) (ExampleService_EchoClient, runtime.ServerMetadata, error) {`, }, } { meth.ServerStreaming = proto.Bool(spec.serverStreaming) diff --git a/runtime/context.go b/runtime/context.go index cde43136867..8c77befd521 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -35,3 +35,21 @@ func AnnotateContext(ctx context.Context, req *http.Request) context.Context { } return metadata.NewContext(ctx, metadata.Pairs(pairs...)) } + +type ServerMetadata struct { + HeaderMD metadata.MD + TrailerMD metadata.MD +} + +type serverMetadataKey struct{} + +// NewServerMetadataContext creates a new context with ServerMetadata +func NewServerMetadataContext(ctx context.Context, md *ServerMetadata) context.Context { + return context.WithValue(ctx, serverMetadataKey{}, md) +} + +// ServerMetadataFromContext returns the ServerMetadata in ctx +func ServerMetadataFromContext(ctx context.Context) (md *ServerMetadata, ok bool) { + md, ok = ctx.Value(serverMetadataKey{}).(*ServerMetadata) + return +} From 8efc3513f13cb25c8d7c9f0c0a33d09df761e8ad Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Sun, 31 Jan 2016 17:05:45 +0900 Subject: [PATCH 02/11] add tests for metadata --- examples/integration_test.go | 45 +++++++++++++++++++++++++- examples/server/a_bit_of_everything.go | 25 ++++++++++++++ examples/server/echo.go | 10 ++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/examples/integration_test.go b/examples/integration_test.go index 1472efec6ef..97c09b1c7ed 100644 --- a/examples/integration_test.go +++ b/examples/integration_test.go @@ -34,7 +34,26 @@ func TestIntegration(t *testing.T) { } }() go func() { - if err := Run(":8080"); err != nil { + if err := Run( + ":8080", + runtime.WithForwardResponseOption( + func(ctx context.Context, w http.ResponseWriter, _ proto.Message) error { + if md, ok := runtime.ServerMetadataFromContext(ctx); ok { + for k, vs := range md.HeaderMD { + for i := range vs { + w.Header().Add(fmt.Sprintf("Grpc-Header-%s", k), vs[i]) + } + } + for k, vs := range md.TrailerMD { + for i := range vs { + w.Header().Add(fmt.Sprintf("Grpc-Trailer-%s", k), vs[i]) + } + } + } + return nil + }, + ), + ); err != nil { t.Errorf("gw.Run() failed with %v; want success", err) return } @@ -135,6 +154,22 @@ func testEchoBody(t *testing.T) { if got, want := received, sent; !reflect.DeepEqual(got, want) { t.Errorf("msg.Id = %q; want %q", got, want) } + + if value := resp.Header.Get("Grpc-Header-foo"); value != "foo1" { + t.Errorf("Grpc-Header-foo was %s, wanted %s", value, "foo1") + } + + if value := resp.Header.Get("Grpc-Header-bar"); value != "bar1" { + t.Errorf("Grpc-Header-bar was %s, wanted %s", value, "bar1") + } + + if value := resp.Header.Get("Grpc-Trailer-foo"); value != "foo2" { + t.Errorf("Grpc-Trailer-foo was %s, wanted %s", value, "foo2") + } + + if value := resp.Header.Get("Grpc-Trailer-bar"); value != "bar2" { + t.Errorf("Grpc-Trailer-bar was %s, wanted %s", value, "bar2") + } } func testABECreate(t *testing.T) { @@ -327,6 +362,14 @@ func testABEBulkCreate(t *testing.T) { t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success", buf, err) return } + + if value := resp.Header.Get("Grpc-Trailer-foo"); value != "foo2" { + t.Errorf("Grpc-Trailer-foo was %q, wanted %q", value, "foo2") + } + + if value := resp.Header.Get("Grpc-Trailer-bar"); value != "bar2" { + t.Errorf("Grpc-Trailer-bar was %q, wanted %q", value, "bar2") + } } func testABELookup(t *testing.T) { diff --git a/examples/server/a_bit_of_everything.go b/examples/server/a_bit_of_everything.go index 49d52223bcd..2dedc5fecf3 100644 --- a/examples/server/a_bit_of_everything.go +++ b/examples/server/a_bit_of_everything.go @@ -12,6 +12,7 @@ import ( "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" ) // Implements of ABitOfEverythingServiceServer @@ -52,6 +53,7 @@ func (s *_ABitOfEverythingServer) CreateBody(ctx context.Context, msg *examples. } func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingService_BulkCreateServer) error { + ctx := stream.Context() for { msg, err := stream.Recv() @@ -66,6 +68,15 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingSer return err } } + + stream.SendHeader(metadata.New(map[string]string{ + "foo": "foo1", + "bar": "bar1", + })) + stream.SetTrailer(metadata.New(map[string]string{ + "foo": "foo2", + "bar": "bar2", + })) return stream.SendAndClose(new(examples.EmptyMessage)) } @@ -137,11 +148,25 @@ func (s *_ABitOfEverythingServer) BulkEcho(stream examples.ABitOfEverythingServi } msgs = append(msgs, msg) } + + hmd := metadata.New(map[string]string{ + "foo": "foo1", + "bar": "bar1", + }) + if err := stream.SendHeader(hmd); err != nil { + return err + } + for _, msg := range msgs { glog.Info(msg) if err := stream.Send(msg); err != nil { return err } } + + stream.SetTrailer(metadata.New(map[string]string{ + "foo": "foo2", + "bar": "bar2", + })) return nil } diff --git a/examples/server/echo.go b/examples/server/echo.go index 040a9e5da68..0f07fbbd496 100644 --- a/examples/server/echo.go +++ b/examples/server/echo.go @@ -4,6 +4,8 @@ import ( examples "github.com/gengo/grpc-gateway/examples/examplepb" "github.com/golang/glog" "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" ) // Implements of EchoServiceServer @@ -21,5 +23,13 @@ func (s *echoServer) Echo(ctx context.Context, msg *examples.SimpleMessage) (*ex func (s *echoServer) EchoBody(ctx context.Context, msg *examples.SimpleMessage) (*examples.SimpleMessage, error) { glog.Info(msg) + grpc.SendHeader(ctx, metadata.New(map[string]string{ + "foo": "foo1", + "bar": "bar1", + })) + grpc.SetTrailer(ctx, metadata.New(map[string]string{ + "foo": "foo2", + "bar": "bar2", + })) return msg, nil } From ee784097e7da972d93fc583abda1304af2f6f14f Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Sat, 6 Feb 2016 17:58:08 +0900 Subject: [PATCH 03/11] return trailer metadata in streaming rpc --- examples/integration_test.go | 66 ++++++++++++++++++- examples/server/a_bit_of_everything.go | 11 ++++ .../gengateway/template.go | 4 +- runtime/handler.go | 51 +++++++++----- 4 files changed, 113 insertions(+), 19 deletions(-) diff --git a/examples/integration_test.go b/examples/integration_test.go index 97c09b1c7ed..bbb9c0be462 100644 --- a/examples/integration_test.go +++ b/examples/integration_test.go @@ -21,6 +21,11 @@ import ( "github.com/golang/protobuf/proto" ) +type aBitOfEverything struct { + Result gw.ABitOfEverything `json:"result,omitempty"` + Error *runtime.ResponseStreamError `json:"error,omitempty"` +} + func TestIntegration(t *testing.T) { if testing.Short() { t.Skip() @@ -67,6 +72,7 @@ func TestIntegration(t *testing.T) { testABEBulkCreate(t) testABELookup(t) testABEList(t) + testABEListError(t) testAdditionalBindings(t) go func() { @@ -435,7 +441,7 @@ func testABEList(t *testing.T) { dec := json.NewDecoder(resp.Body) var i int for i = 0; ; i++ { - var msg gw.ABitOfEverything + var msg aBitOfEverything err := dec.Decode(&msg) if err == io.EOF { break @@ -449,6 +455,64 @@ func testABEList(t *testing.T) { } } +func testABEListError(t *testing.T) { + url := "http://localhost:8080/v1/example/a_bit_of_everything" + req, err := http.NewRequest("GET", url, nil) + if err != nil { + t.Errorf("http.NewReuest(%q) failed with %v; want success", url, err) + return + } + req.Header.Set("Grpc-Metadata-error", "foo") + + client := new(http.Client) + resp, err := client.Do(req) + if err != nil { + t.Errorf("client.Do failed with %v; want success", err) + return + } + defer resp.Body.Close() + + dec := json.NewDecoder(resp.Body) + var i int + var lastMsg aBitOfEverything + for i = 0; ; i++ { + var msg aBitOfEverything + err := dec.Decode(&msg) + if err == io.EOF { + break + } + if err != nil { + t.Errorf("dec.Decode(&msg) failed with %v; want success; i = %d", err, i) + } + lastMsg = msg + } + if i <= 0 { + t.Errorf("i == %d; want > 0", i) + } + + if got, want := lastMsg.Error.HTTPCode, http.StatusBadRequest; got != want { + t.Errorf("lastMsg.Error.HTTPCode = %d; want %d", got, want) + return + } + + md := lastMsg.Error.Trailer + v, ok := md["foo"] + if !ok || len(v) == 0 { + t.Errorf("Trailer doesn't contain %q", "foo") + } + if !ok && v[0] != "foo1" { + t.Errorf("Trailer %q = %q; want %q", "foo", v[0], "foo2") + } + + v, ok = md["bar"] + if !ok || len(v) == 0 { + t.Errorf("Trailer doesn't contain %q", "bar") + } + if !ok && v[0] != "bar1" { + t.Errorf("Trailer %q = %q; want %q", "bar", v[0], "bar2") + } +} + func testAdditionalBindings(t *testing.T) { for i, f := range []func() *http.Response{ func() *http.Response { diff --git a/examples/server/a_bit_of_everything.go b/examples/server/a_bit_of_everything.go index 2dedc5fecf3..5166b2134e4 100644 --- a/examples/server/a_bit_of_everything.go +++ b/examples/server/a_bit_of_everything.go @@ -99,6 +99,17 @@ func (s *_ABitOfEverythingServer) List(_ *examples.EmptyMessage, stream examples return err } } + + // return error when metadata includes error header + if header, ok := metadata.FromContext(stream.Context()); ok { + if v, ok := header["error"]; ok { + stream.SetTrailer(metadata.New(map[string]string{ + "foo": "foo2", + "bar": "bar2", + })) + return grpc.Errorf(codes.InvalidArgument, "error metadata: %v", v) + } + } return nil } diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index c731be72190..ee1255f2967 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -161,7 +161,6 @@ func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx cont glog.Errorf("Failed to terminate client stream: %v", err) return nil, metadata, err } - metadata.TrailerMD = stream.Trailer() return stream, metadata, nil {{else}} msg, err := stream.CloseAndRecv() @@ -214,7 +213,6 @@ var ( {{end}} {{if .Method.GetServerStreaming}} stream, err := client.{{.Method.GetName}}(ctx, &protoReq) - metadata.TrailerMD = stream.Trailer() return stream, metadata, err {{else}} msg, err := client.{{.Method.GetName}}(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) @@ -271,7 +269,7 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, return } {{if $m.GetServerStreaming}} - forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, func() (proto.Message, error) { return resp.Recv() }, mux.GetForwardResponseOptions()...) + forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, resp, func() (proto.Message, error) { return resp.Recv() }, mux.GetForwardResponseOptions()...) {{else}} forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, resp, mux.GetForwardResponseOptions()...) {{end}} diff --git a/runtime/handler.go b/runtime/handler.go index 02d7c92c349..1be324cafdd 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -10,22 +10,24 @@ import ( "github.com/golang/protobuf/proto" "golang.org/x/net/context" "google.golang.org/grpc" + "google.golang.org/grpc/metadata" ) type responseStreamChunk struct { - Result proto.Message `json:"result,omitempty"` - Error *responseStreamError `json:"error,omitempty"` + Result proto.Message `json:"result,omitempty"` + Error *ResponseStreamError `json:"error,omitempty"` } -type responseStreamError struct { - GrpcCode int `json:"grpc_code, omitempty"` - HTTPCode int `json:"http_code, omitempty"` - Message string `json:"message, omitempty"` - HTTPStatus string `json:"http_status, omitempty"` +type ResponseStreamError struct { + GrpcCode int `json:"grpc_code, omitempty"` + HTTPCode int `json:"http_code, omitempty"` + Message string `json:"message, omitempty"` + HTTPStatus string `json:"http_status, omitempty"` + Trailer metadata.MD `json:"trailer, omitempty"` } // ForwardResponseStream forwards the stream from gRPC server to REST client. -func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http.Request, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { +func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http.Request, stream grpc.ClientStream, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { f, ok := w.(http.Flusher) if !ok { glog.Errorf("Flush not supported in %T", w) @@ -33,6 +35,13 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http return } + md, ok := ServerMetadataFromContext(ctx) + if !ok { + glog.Errorf("Failed to extract ServerMetadata from context") + http.Error(w, "unexpected error", http.StatusInternalServerError) + return + } + w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("Content-Type", "application/json") if err := handleForwardResponseOptions(ctx, w, nil, opts); err != nil { @@ -47,11 +56,13 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http return } if err != nil { - handleForwardResponseStreamError(w, err) + md.TrailerMD = stream.Trailer() + handleForwardResponseStreamError(ctx, w, err) return } if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { - handleForwardResponseStreamError(w, err) + md.TrailerMD = stream.Trailer() + handleForwardResponseStreamError(ctx, w, err) return } buf, err := json.Marshal(responseStreamChunk{Result: resp}) @@ -100,13 +111,23 @@ func handleForwardResponseOptions(ctx context.Context, w http.ResponseWriter, re return nil } -func handleForwardResponseStreamError(w http.ResponseWriter, err error) { +func handleForwardResponseStreamError(ctx context.Context, w http.ResponseWriter, err error) { + md, ok := ServerMetadataFromContext(ctx) + if !ok { + glog.Errorf("Failed to extract ServerMetadata from context") + return + } + grpcCode := grpc.Code(err) httpCode := HTTPStatusFromCode(grpcCode) - resp := responseStreamChunk{Error: &responseStreamError{GrpcCode: int(grpcCode), - HTTPCode: httpCode, - Message: err.Error(), - HTTPStatus: http.StatusText(httpCode)}} + resp := responseStreamChunk{ + Error: &ResponseStreamError{ + GrpcCode: int(grpcCode), + HTTPCode: httpCode, + Message: err.Error(), + HTTPStatus: http.StatusText(httpCode), + Trailer: md.TrailerMD, + }} buf, merr := json.Marshal(resp) if merr != nil { glog.Errorf("Failed to marshal an error: %v", merr) From cb54dbdf9a6e3db6b1dedcfdd59ef19099c71a2e Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Sat, 6 Feb 2016 18:39:29 +0900 Subject: [PATCH 04/11] return trailer metadata in unary rpc --- examples/integration_test.go | 68 +++++++++++++++++++++++--- examples/server/a_bit_of_everything.go | 11 ++++- runtime/errors.go | 23 +++++++-- runtime/handler.go | 14 ++++++ 4 files changed, 104 insertions(+), 12 deletions(-) diff --git a/examples/integration_test.go b/examples/integration_test.go index bbb9c0be462..24f482ef73c 100644 --- a/examples/integration_test.go +++ b/examples/integration_test.go @@ -19,6 +19,7 @@ import ( sub "github.com/gengo/grpc-gateway/examples/sub" "github.com/gengo/grpc-gateway/runtime" "github.com/golang/protobuf/proto" + "google.golang.org/grpc/codes" ) type aBitOfEverything struct { @@ -44,11 +45,6 @@ func TestIntegration(t *testing.T) { runtime.WithForwardResponseOption( func(ctx context.Context, w http.ResponseWriter, _ proto.Message) error { if md, ok := runtime.ServerMetadataFromContext(ctx); ok { - for k, vs := range md.HeaderMD { - for i := range vs { - w.Header().Add(fmt.Sprintf("Grpc-Header-%s", k), vs[i]) - } - } for k, vs := range md.TrailerMD { for i := range vs { w.Header().Add(fmt.Sprintf("Grpc-Trailer-%s", k), vs[i]) @@ -71,6 +67,7 @@ func TestIntegration(t *testing.T) { testABECreateBody(t) testABEBulkCreate(t) testABELookup(t) + testABELookupNotFound(t) testABEList(t) testABEListError(t) testAdditionalBindings(t) @@ -161,11 +158,11 @@ func testEchoBody(t *testing.T) { t.Errorf("msg.Id = %q; want %q", got, want) } - if value := resp.Header.Get("Grpc-Header-foo"); value != "foo1" { + if value := resp.Header.Get("Grpc-Metadata-foo"); value != "foo1" { t.Errorf("Grpc-Header-foo was %s, wanted %s", value, "foo1") } - if value := resp.Header.Get("Grpc-Header-bar"); value != "bar1" { + if value := resp.Header.Get("Grpc-Metadata-bar"); value != "bar1" { t.Errorf("Grpc-Header-bar was %s, wanted %s", value, "bar1") } @@ -427,6 +424,63 @@ func testABELookup(t *testing.T) { if got := msg; !reflect.DeepEqual(got, want) { t.Errorf("msg= %v; want %v", &got, &want) } + + if got, want := resp.Header.Get("Grpc-Metadata-uuid"), want.Uuid; got != want { + t.Errorf("Grpc-Metadata-foo was %s, wanted %s", got, want) + } +} + +func testABELookupNotFound(t *testing.T) { + url := "http://localhost:8080/v1/example/a_bit_of_everything" + uuid := "not_exist" + url = fmt.Sprintf("%s/%s", url, uuid) + resp, err := http.Get(url) + if err != nil { + t.Errorf("http.Get(%q) failed with %v; want success", url, err) + return + } + defer resp.Body.Close() + + buf, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Errorf("ioutil.ReadAll(resp.Body) failed with %v; want success", err) + return + } + + if got, want := resp.StatusCode, http.StatusNotFound; got != want { + t.Errorf("resp.StatusCode = %d; want %d", got, want) + t.Logf("%s", buf) + return + } + + var msg runtime.ErrorBody + if err := json.Unmarshal(buf, &msg); err != nil { + t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success", buf, err) + return + } + + if got, want := msg.Code, int(codes.NotFound); got != want { + t.Errorf("msg.Code = %d; want %d", got, want) + return + } + + if got, want := resp.Header.Get("Grpc-Metadata-uuid"), uuid; got != want { + t.Errorf("Grpc-Metadata-foo was %s, wanted %s", got, want) + } + + md := msg.Trailer + if md.Len() == 0 { + t.Errorf("no trailer is set") + return + } + + if got, want := md["foo"], []string{"foo2"}; !reflect.DeepEqual(got, want) { + t.Errorf("msg.Trailer[%q] = %v; want %v", "foo", got, want) + } + + if got, want := md["bar"], []string{"bar2"}; !reflect.DeepEqual(got, want) { + t.Errorf("msg.Trailer[%q] = %v; want %v", "bar", got, want) + } } func testABEList(t *testing.T) { diff --git a/examples/server/a_bit_of_everything.go b/examples/server/a_bit_of_everything.go index 5166b2134e4..a5d492df287 100644 --- a/examples/server/a_bit_of_everything.go +++ b/examples/server/a_bit_of_everything.go @@ -83,11 +83,20 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingSer func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMessage) (*examples.ABitOfEverything, error) { s.m.Lock() defer s.m.Unlock() - glog.Info(msg) + + grpc.SendHeader(ctx, metadata.New(map[string]string{ + "uuid": msg.Uuid, + })) + if a, ok := s.v[msg.Uuid]; ok { return a, nil } + + grpc.SetTrailer(ctx, metadata.New(map[string]string{ + "foo": "foo2", + "bar": "bar2", + })) return nil, grpc.Errorf(codes.NotFound, "not found") } diff --git a/runtime/errors.go b/runtime/errors.go index 98c1950d4de..30e2dc85dd5 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -2,6 +2,7 @@ package runtime import ( "encoding/json" + "fmt" "io" "net/http" @@ -9,6 +10,7 @@ import ( "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" ) // HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status. @@ -62,9 +64,10 @@ var ( OtherErrorHandler = DefaultOtherErrorHandler ) -type errorBody struct { - Error string `json:"error"` - Code int `json:"code"` +type ErrorBody struct { + Error string `json:"error"` + Code int `json:"code"` + Trailer metadata.MD `json:"trailer,omitempty"` } // DefaultHTTPError is the default implementation of HTTPError. @@ -77,7 +80,19 @@ func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, _ *http.Reques const fallback = `{"error": "failed to marshal error message"}` w.Header().Set("Content-Type", "application/json") - body := errorBody{Error: grpc.ErrorDesc(err), Code: int(grpc.Code(err))} + body := ErrorBody{ + Error: grpc.ErrorDesc(err), + Code: int(grpc.Code(err)), + } + if md, ok := ServerMetadataFromContext(ctx); ok { + for k, vs := range md.HeaderMD { + hKey := fmt.Sprintf("%s%s", metadataHeaderPrefix, k) + for i := range vs { + w.Header().Add(hKey, vs[i]) + } + } + body.Trailer = md.TrailerMD + } buf, merr := json.Marshal(body) if merr != nil { glog.Errorf("Failed to marshal error message %q: %v", body, merr) diff --git a/runtime/handler.go b/runtime/handler.go index 1be324cafdd..517cf6575b1 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -80,6 +80,20 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http // ForwardResponseMessage forwards the message "resp" from gRPC server to REST client. func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { + md, ok := ServerMetadataFromContext(ctx) + if !ok { + glog.Errorf("Failed to extract ServerMetadata from context") + } + + if md != nil { + for k, vs := range md.HeaderMD { + hKey := fmt.Sprintf("%s%s", metadataHeaderPrefix, k) + for i := range vs { + w.Header().Add(hKey, vs[i]) + } + } + } + w.Header().Set("Content-Type", "application/json") if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { HTTPError(ctx, w, req, err) From 07b73580b8c458b36b3695d2d5af189257032370 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 11 Feb 2016 17:45:12 +0900 Subject: [PATCH 05/11] use HTTP 1.1 trailer to return grpc trailer for unary RPC --- runtime/context.go | 1 + runtime/errors.go | 19 ++++++++--------- runtime/handler.go | 51 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/runtime/context.go b/runtime/context.go index 8c77befd521..f75a3946533 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -9,6 +9,7 @@ import ( ) const metadataHeaderPrefix = "Grpc-Metadata-" +const metadataTrailerPrefix = "Grpc-Trailer-" /* AnnotateContext adds context information such as metadata from the request. diff --git a/runtime/errors.go b/runtime/errors.go index 30e2dc85dd5..97cfddcd0c7 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -2,7 +2,6 @@ package runtime import ( "encoding/json" - "fmt" "io" "net/http" @@ -79,20 +78,12 @@ type ErrorBody struct { func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, _ *http.Request, err error) { const fallback = `{"error": "failed to marshal error message"}` + w.Header().Del("Trailer") w.Header().Set("Content-Type", "application/json") body := ErrorBody{ Error: grpc.ErrorDesc(err), Code: int(grpc.Code(err)), } - if md, ok := ServerMetadataFromContext(ctx); ok { - for k, vs := range md.HeaderMD { - hKey := fmt.Sprintf("%s%s", metadataHeaderPrefix, k) - for i := range vs { - w.Header().Add(hKey, vs[i]) - } - } - body.Trailer = md.TrailerMD - } buf, merr := json.Marshal(body) if merr != nil { glog.Errorf("Failed to marshal error message %q: %v", body, merr) @@ -103,11 +94,19 @@ func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, _ *http.Reques return } + md, ok := ServerMetadataFromContext(ctx) + if !ok { + glog.Errorf("Failed to extract ServerMetadata from context") + } + + handleForwardResponseServerMetadata(w, md) st := HTTPStatusFromCode(grpc.Code(err)) w.WriteHeader(st) if _, err := w.Write(buf); err != nil { glog.Errorf("Failed to write response: %v", err) } + + handleForwardResponseTrailer(w, md) } func DefaultOtherErrorHandler(w http.ResponseWriter, _ *http.Request, error string, code int) { diff --git a/runtime/handler.go b/runtime/handler.go index 517cf6575b1..fab9bcd43b2 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "net/textproto" "github.com/golang/glog" "github.com/golang/protobuf/proto" @@ -78,6 +79,43 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http } } +func handleForwardResponseServerMetadata(w http.ResponseWriter, md *ServerMetadata) { + if md == nil { + return + } + + for k, vs := range md.HeaderMD { + hKey := fmt.Sprintf("%s%s", metadataHeaderPrefix, k) + for i := range vs { + w.Header().Add(hKey, vs[i]) + } + } +} + +func handleForwardResponseTrailerHeader(w http.ResponseWriter, md *ServerMetadata) { + if md == nil { + return + } + + for k := range md.TrailerMD { + tKey := textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", metadataTrailerPrefix, k)) + w.Header().Add("Trailer", tKey) + } +} + +func handleForwardResponseTrailer(w http.ResponseWriter, md *ServerMetadata) { + if md == nil { + return + } + + for k, vs := range md.TrailerMD { + tKey := fmt.Sprintf("%s%s", metadataTrailerPrefix, k) + for i := range vs { + w.Header().Add(tKey, vs[i]) + } + } +} + // ForwardResponseMessage forwards the message "resp" from gRPC server to REST client. func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, req *http.Request, resp proto.Message, opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { md, ok := ServerMetadataFromContext(ctx) @@ -85,15 +123,8 @@ func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, req *htt glog.Errorf("Failed to extract ServerMetadata from context") } - if md != nil { - for k, vs := range md.HeaderMD { - hKey := fmt.Sprintf("%s%s", metadataHeaderPrefix, k) - for i := range vs { - w.Header().Add(hKey, vs[i]) - } - } - } - + handleForwardResponseServerMetadata(w, md) + handleForwardResponseTrailerHeader(w, md) w.Header().Set("Content-Type", "application/json") if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { HTTPError(ctx, w, req, err) @@ -110,6 +141,8 @@ func ForwardResponseMessage(ctx context.Context, w http.ResponseWriter, req *htt if _, err = w.Write(buf); err != nil { glog.Errorf("Failed to write response: %v", err) } + + handleForwardResponseTrailer(w, md) } func handleForwardResponseOptions(ctx context.Context, w http.ResponseWriter, resp proto.Message, opts []func(context.Context, http.ResponseWriter, proto.Message) error) error { From 5c1ef44be794fe0c3f6e9284961223381f179076 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 18 Feb 2016 00:39:08 +0900 Subject: [PATCH 06/11] return Header metadata for streaming --- .../gengateway/template.go | 23 ++++++++++--- runtime/handler.go | 34 +++++++------------ 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index ee1255f2967..9a515411c2a 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -156,11 +156,18 @@ func request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx cont return nil, metadata, err } } -{{if .Method.GetServerStreaming}} - if err = stream.CloseSend(); err != nil { + + if err := stream.CloseSend(); err != nil { glog.Errorf("Failed to terminate client stream: %v", err) return nil, metadata, err } + header, err := stream.Header() + if err != nil { + glog.Errorf("Failed to get header from client: %v", err) + return nil, metadata, err + } + metadata.HeaderMD = header +{{if .Method.GetServerStreaming}} return stream, metadata, nil {{else}} msg, err := stream.CloseAndRecv() @@ -213,7 +220,15 @@ var ( {{end}} {{if .Method.GetServerStreaming}} stream, err := client.{{.Method.GetName}}(ctx, &protoReq) - return stream, metadata, err + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil {{else}} msg, err := client.{{.Method.GetName}}(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) return msg, metadata, err @@ -269,7 +284,7 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, return } {{if $m.GetServerStreaming}} - forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, resp, func() (proto.Message, error) { return resp.Recv() }, mux.GetForwardResponseOptions()...) + forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, func() (proto.Message, error) { return resp.Recv() }, mux.GetForwardResponseOptions()...) {{else}} forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(ctx, w, req, resp, mux.GetForwardResponseOptions()...) {{end}} diff --git a/runtime/handler.go b/runtime/handler.go index fab9bcd43b2..674f4e779ad 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -11,24 +11,22 @@ import ( "github.com/golang/protobuf/proto" "golang.org/x/net/context" "google.golang.org/grpc" - "google.golang.org/grpc/metadata" ) type responseStreamChunk struct { Result proto.Message `json:"result,omitempty"` - Error *ResponseStreamError `json:"error,omitempty"` + Error *responseStreamError `json:"error,omitempty"` } -type ResponseStreamError struct { - GrpcCode int `json:"grpc_code, omitempty"` - HTTPCode int `json:"http_code, omitempty"` - Message string `json:"message, omitempty"` - HTTPStatus string `json:"http_status, omitempty"` - Trailer metadata.MD `json:"trailer, omitempty"` +type responseStreamError struct { + GrpcCode int `json:"grpc_code, omitempty"` + HTTPCode int `json:"http_code, omitempty"` + Message string `json:"message, omitempty"` + HTTPStatus string `json:"http_status, omitempty"` } // ForwardResponseStream forwards the stream from gRPC server to REST client. -func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http.Request, stream grpc.ClientStream, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { +func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http.Request, recv func() (proto.Message, error), opts ...func(context.Context, http.ResponseWriter, proto.Message) error) { f, ok := w.(http.Flusher) if !ok { glog.Errorf("Flush not supported in %T", w) @@ -42,6 +40,7 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http http.Error(w, "unexpected error", http.StatusInternalServerError) return } + handleForwardResponseServerMetadata(w, md) w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("Content-Type", "application/json") @@ -57,13 +56,11 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http return } if err != nil { - md.TrailerMD = stream.Trailer() - handleForwardResponseStreamError(ctx, w, err) + handleForwardResponseStreamError(w, err) return } if err := handleForwardResponseOptions(ctx, w, resp, opts); err != nil { - md.TrailerMD = stream.Trailer() - handleForwardResponseStreamError(ctx, w, err) + handleForwardResponseStreamError(w, err) return } buf, err := json.Marshal(responseStreamChunk{Result: resp}) @@ -158,22 +155,15 @@ func handleForwardResponseOptions(ctx context.Context, w http.ResponseWriter, re return nil } -func handleForwardResponseStreamError(ctx context.Context, w http.ResponseWriter, err error) { - md, ok := ServerMetadataFromContext(ctx) - if !ok { - glog.Errorf("Failed to extract ServerMetadata from context") - return - } - +func handleForwardResponseStreamError(w http.ResponseWriter, err error) { grpcCode := grpc.Code(err) httpCode := HTTPStatusFromCode(grpcCode) resp := responseStreamChunk{ - Error: &ResponseStreamError{ + Error: &responseStreamError{ GrpcCode: int(grpcCode), HTTPCode: httpCode, Message: err.Error(), HTTPStatus: http.StatusText(httpCode), - Trailer: md.TrailerMD, }} buf, merr := json.Marshal(resp) if merr != nil { From 380a7092e681c6ea61c80f2351581de481444090 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 18 Feb 2016 00:39:50 +0900 Subject: [PATCH 07/11] test Header metadata for streaming --- examples/integration_test.go | 138 ++++++------------------- examples/server/a_bit_of_everything.go | 25 ++++- 2 files changed, 52 insertions(+), 111 deletions(-) diff --git a/examples/integration_test.go b/examples/integration_test.go index 24f482ef73c..324acb81c9e 100644 --- a/examples/integration_test.go +++ b/examples/integration_test.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net/http" "reflect" + "strconv" "strings" "testing" "time" @@ -22,11 +23,6 @@ import ( "google.golang.org/grpc/codes" ) -type aBitOfEverything struct { - Result gw.ABitOfEverything `json:"result,omitempty"` - Error *runtime.ResponseStreamError `json:"error,omitempty"` -} - func TestIntegration(t *testing.T) { if testing.Short() { t.Skip() @@ -40,21 +36,7 @@ func TestIntegration(t *testing.T) { } }() go func() { - if err := Run( - ":8080", - runtime.WithForwardResponseOption( - func(ctx context.Context, w http.ResponseWriter, _ proto.Message) error { - if md, ok := runtime.ServerMetadataFromContext(ctx); ok { - for k, vs := range md.TrailerMD { - for i := range vs { - w.Header().Add(fmt.Sprintf("Grpc-Trailer-%s", k), vs[i]) - } - } - } - return nil - }, - ), - ); err != nil { + if err := Run(":8080"); err != nil { t.Errorf("gw.Run() failed with %v; want success", err) return } @@ -69,7 +51,6 @@ func TestIntegration(t *testing.T) { testABELookup(t) testABELookupNotFound(t) testABEList(t) - testABEListError(t) testAdditionalBindings(t) go func() { @@ -158,20 +139,18 @@ func testEchoBody(t *testing.T) { t.Errorf("msg.Id = %q; want %q", got, want) } - if value := resp.Header.Get("Grpc-Metadata-foo"); value != "foo1" { - t.Errorf("Grpc-Header-foo was %s, wanted %s", value, "foo1") + if got, want := resp.Header.Get("Grpc-Metadata-Foo"), "foo1"; got != want { + t.Errorf("Grpc-Header-Foo was %q, wanted %q", got, want) } - - if value := resp.Header.Get("Grpc-Metadata-bar"); value != "bar1" { - t.Errorf("Grpc-Header-bar was %s, wanted %s", value, "bar1") + if got, want := resp.Header.Get("Grpc-Metadata-Bar"), "bar1"; got != want { + t.Errorf("Grpc-Header-Bar was %q, wanted %q", got, want) } - if value := resp.Header.Get("Grpc-Trailer-foo"); value != "foo2" { - t.Errorf("Grpc-Trailer-foo was %s, wanted %s", value, "foo2") + if got, want := resp.Trailer.Get("Grpc-Trailer-Foo"), "foo2"; got != want { + t.Errorf("Grpc-Trailer-Foo was %q, wanted %q", got, want) } - - if value := resp.Header.Get("Grpc-Trailer-bar"); value != "bar2" { - t.Errorf("Grpc-Trailer-bar was %s, wanted %s", value, "bar2") + if got, want := resp.Trailer.Get("Grpc-Trailer-Bar"), "bar2"; got != want { + t.Errorf("Grpc-Trailer-Bar was %q, wanted %q", got, want) } } @@ -291,6 +270,7 @@ func testABECreateBody(t *testing.T) { } func testABEBulkCreate(t *testing.T) { + count := 0 r, w := io.Pipe() go func(w io.WriteCloser) { defer func() { @@ -340,6 +320,7 @@ func testABEBulkCreate(t *testing.T) { t.Errorf("w.Write(%s) failed with %v; want success", buf, err) return } + count++ } }(w) url := "http://localhost:8080/v1/example/a_bit_of_everything/bulk" @@ -366,12 +347,15 @@ func testABEBulkCreate(t *testing.T) { return } - if value := resp.Header.Get("Grpc-Trailer-foo"); value != "foo2" { - t.Errorf("Grpc-Trailer-foo was %q, wanted %q", value, "foo2") + if got, want := resp.Header.Get("Grpc-Metadata-Count"), fmt.Sprintf("%d", count); got != want { + t.Errorf("Grpc-Header-Count was %q, wanted %q", got, want) } - if value := resp.Header.Get("Grpc-Trailer-bar"); value != "bar2" { - t.Errorf("Grpc-Trailer-bar was %q, wanted %q", value, "bar2") + if got, want := resp.Trailer.Get("Grpc-Trailer-Foo"), "foo2"; got != want { + t.Errorf("Grpc-Trailer-Foo was %q, wanted %q", got, want) + } + if got, want := resp.Trailer.Get("Grpc-Trailer-Bar"), "bar2"; got != want { + t.Errorf("Grpc-Trailer-Bar was %q, wanted %q", got, want) } } @@ -425,8 +409,8 @@ func testABELookup(t *testing.T) { t.Errorf("msg= %v; want %v", &got, &want) } - if got, want := resp.Header.Get("Grpc-Metadata-uuid"), want.Uuid; got != want { - t.Errorf("Grpc-Metadata-foo was %s, wanted %s", got, want) + if got, want := resp.Header.Get("Grpc-Metadata-Uuid"), want.Uuid; got != want { + t.Errorf("Grpc-Metadata-Uuid was %s, wanted %s", got, want) } } @@ -464,22 +448,8 @@ func testABELookupNotFound(t *testing.T) { return } - if got, want := resp.Header.Get("Grpc-Metadata-uuid"), uuid; got != want { - t.Errorf("Grpc-Metadata-foo was %s, wanted %s", got, want) - } - - md := msg.Trailer - if md.Len() == 0 { - t.Errorf("no trailer is set") - return - } - - if got, want := md["foo"], []string{"foo2"}; !reflect.DeepEqual(got, want) { - t.Errorf("msg.Trailer[%q] = %v; want %v", "foo", got, want) - } - - if got, want := md["bar"], []string{"bar2"}; !reflect.DeepEqual(got, want) { - t.Errorf("msg.Trailer[%q] = %v; want %v", "bar", got, want) + if got, want := resp.Header.Get("Grpc-Metadata-Uuid"), uuid; got != want { + t.Errorf("Grpc-Metadata-Uuid was %s, wanted %s", got, want) } } @@ -495,7 +465,7 @@ func testABEList(t *testing.T) { dec := json.NewDecoder(resp.Body) var i int for i = 0; ; i++ { - var msg aBitOfEverything + var msg gw.ABitOfEverything err := dec.Decode(&msg) if err == io.EOF { break @@ -507,63 +477,19 @@ func testABEList(t *testing.T) { if i <= 0 { t.Errorf("i == %d; want > 0", i) } -} -func testABEListError(t *testing.T) { - url := "http://localhost:8080/v1/example/a_bit_of_everything" - req, err := http.NewRequest("GET", url, nil) - if err != nil { - t.Errorf("http.NewReuest(%q) failed with %v; want success", url, err) - return + value := resp.Header.Get("Grpc-Metadata-Count") + if value == "" { + t.Errorf("Grpc-Header-Count should not be empty") } - req.Header.Set("Grpc-Metadata-error", "foo") - client := new(http.Client) - resp, err := client.Do(req) + count, err := strconv.Atoi(value) if err != nil { - t.Errorf("client.Do failed with %v; want success", err) - return + t.Errorf("failed to Atoi %q: %v", value, err) } - defer resp.Body.Close() - dec := json.NewDecoder(resp.Body) - var i int - var lastMsg aBitOfEverything - for i = 0; ; i++ { - var msg aBitOfEverything - err := dec.Decode(&msg) - if err == io.EOF { - break - } - if err != nil { - t.Errorf("dec.Decode(&msg) failed with %v; want success; i = %d", err, i) - } - lastMsg = msg - } - if i <= 0 { - t.Errorf("i == %d; want > 0", i) - } - - if got, want := lastMsg.Error.HTTPCode, http.StatusBadRequest; got != want { - t.Errorf("lastMsg.Error.HTTPCode = %d; want %d", got, want) - return - } - - md := lastMsg.Error.Trailer - v, ok := md["foo"] - if !ok || len(v) == 0 { - t.Errorf("Trailer doesn't contain %q", "foo") - } - if !ok && v[0] != "foo1" { - t.Errorf("Trailer %q = %q; want %q", "foo", v[0], "foo2") - } - - v, ok = md["bar"] - if !ok || len(v) == 0 { - t.Errorf("Trailer doesn't contain %q", "bar") - } - if !ok && v[0] != "bar1" { - t.Errorf("Trailer %q = %q; want %q", "bar", v[0], "bar2") + if count <= 0 { + t.Errorf("count == %d; want > 0", count) } } @@ -615,7 +541,7 @@ func testAdditionalBindings(t *testing.T) { var msg sub.StringMessage if err := json.Unmarshal(buf, &msg); err != nil { - t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success; %i", buf, err, i) + t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success; %d", buf, err, i) return } if got, want := msg.GetValue(), "hello"; got != want { diff --git a/examples/server/a_bit_of_everything.go b/examples/server/a_bit_of_everything.go index a5d492df287..3fdecd1edca 100644 --- a/examples/server/a_bit_of_everything.go +++ b/examples/server/a_bit_of_everything.go @@ -53,7 +53,7 @@ func (s *_ABitOfEverythingServer) CreateBody(ctx context.Context, msg *examples. } func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingService_BulkCreateServer) error { - + count := 0 ctx := stream.Context() for { msg, err := stream.Recv() @@ -63,16 +63,20 @@ func (s *_ABitOfEverythingServer) BulkCreate(stream examples.ABitOfEverythingSer if err != nil { return err } + count++ glog.Error(msg) if _, err = s.Create(ctx, msg); err != nil { return err } } - stream.SendHeader(metadata.New(map[string]string{ - "foo": "foo1", - "bar": "bar1", + err := stream.SendHeader(metadata.New(map[string]string{ + "count": fmt.Sprintf("%d", count), })) + if err != nil { + return nil + } + stream.SetTrailer(metadata.New(map[string]string{ "foo": "foo2", "bar": "bar2", @@ -85,9 +89,12 @@ func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMe defer s.m.Unlock() glog.Info(msg) - grpc.SendHeader(ctx, metadata.New(map[string]string{ + err := grpc.SendHeader(ctx, metadata.New(map[string]string{ "uuid": msg.Uuid, })) + if err != nil { + return nil, err + } if a, ok := s.v[msg.Uuid]; ok { return a, nil @@ -103,6 +110,14 @@ func (s *_ABitOfEverythingServer) Lookup(ctx context.Context, msg *examples.IdMe func (s *_ABitOfEverythingServer) List(_ *examples.EmptyMessage, stream examples.ABitOfEverythingService_ListServer) error { s.m.Lock() defer s.m.Unlock() + + err := stream.SendHeader(metadata.New(map[string]string{ + "count": fmt.Sprintf("%d", len(s.v)), + })) + if err != nil { + return nil + } + for _, msg := range s.v { if err := stream.Send(msg); err != nil { return err From ef85ed58bc988853097ca5fd85182e7327074403 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 18 Feb 2016 00:46:20 +0900 Subject: [PATCH 08/11] regenerate examples --- .../examplepb/a_bit_of_everything.pb.gw.go | 232 +++++--- examples/examplepb/echo_service.pb.gw.go | 26 +- examples/examplepb/flow_combination.pb.gw.go | 540 ++++++++++++------ 3 files changed, 545 insertions(+), 253 deletions(-) diff --git a/examples/examplepb/a_bit_of_everything.pb.gw.go b/examples/examplepb/a_bit_of_everything.pb.gw.go index 75cf12cc89a..b96fea0ae8d 100644 --- a/examples/examplepb/a_bit_of_everything.pb.gw.go +++ b/examples/examplepb/a_bit_of_everything.pb.gw.go @@ -34,8 +34,9 @@ var ( filter_ABitOfEverythingService_Create_0 = &utilities.DoubleArray{Encoding: map[string]int{"float_value": 0, "double_value": 1, "int64_value": 2, "uint64_value": 3, "int32_value": 4, "fixed64_value": 5, "fixed32_value": 6, "bool_value": 7, "string_value": 8, "uint32_value": 9, "sfixed32_value": 10, "sfixed64_value": 11, "sint32_value": 12, "sint64_value": 13}, Base: []int{1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, Check: []int{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}} ) -func request_ABitOfEverythingService_Create_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Create_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq ABitOfEverything + var metadata runtime.ServerMetadata var ( val string @@ -46,180 +47,186 @@ func request_ABitOfEverythingService_Create_0(ctx context.Context, client ABitOf val, ok = pathParams["float_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "float_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "float_value") } protoReq.FloatValue, err = runtime.Float32(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["double_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "double_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "double_value") } protoReq.DoubleValue, err = runtime.Float64(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["int64_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "int64_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "int64_value") } protoReq.Int64Value, err = runtime.Int64(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["uint64_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uint64_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uint64_value") } protoReq.Uint64Value, err = runtime.Uint64(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["int32_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "int32_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "int32_value") } protoReq.Int32Value, err = runtime.Int32(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["fixed64_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "fixed64_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "fixed64_value") } protoReq.Fixed64Value, err = runtime.Uint64(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["fixed32_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "fixed32_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "fixed32_value") } protoReq.Fixed32Value, err = runtime.Uint32(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["bool_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "bool_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "bool_value") } protoReq.BoolValue, err = runtime.Bool(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["string_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "string_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "string_value") } protoReq.StringValue, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["uint32_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uint32_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uint32_value") } protoReq.Uint32Value, err = runtime.Uint32(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["sfixed32_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sfixed32_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sfixed32_value") } protoReq.Sfixed32Value, err = runtime.Int32(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["sfixed64_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sfixed64_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sfixed64_value") } protoReq.Sfixed64Value, err = runtime.Int64(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["sint32_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sint32_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sint32_value") } protoReq.Sint32Value, err = runtime.Int32(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["sint64_value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sint64_value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "sint64_value") } protoReq.Sint64Value, err = runtime.Int64(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_ABitOfEverythingService_Create_0); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.Create(ctx, &protoReq) + msg, err := client.Create(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_CreateBody_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_CreateBody_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq ABitOfEverything + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.CreateBody(ctx, &protoReq) + msg, err := client.CreateBody(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_BulkCreate_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_BulkCreate_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var metadata runtime.ServerMetadata stream, err := client.BulkCreate(ctx) if err != nil { glog.Errorf("Failed to start streaming: %v", err) - return nil, err + return nil, metadata, err } dec := json.NewDecoder(req.Body) for { @@ -230,20 +237,34 @@ func request_ABitOfEverythingService_BulkCreate_0(ctx context.Context, client AB } if err != nil { glog.Errorf("Failed to decode request: %v", err) - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err = stream.Send(&protoReq); err != nil { glog.Errorf("Failed to send request: %v", err) - return nil, err + return nil, metadata, err } } - return stream.CloseAndRecv() + if err := stream.CloseSend(); err != nil { + glog.Errorf("Failed to terminate client stream: %v", err) + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + glog.Errorf("Failed to get header from client: %v", err) + return nil, metadata, err + } + metadata.HeaderMD = header + + msg, err := stream.CloseAndRecv() + metadata.TrailerMD = stream.Trailer() + return msg, metadata, err } -func request_ABitOfEverythingService_Lookup_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Lookup_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq IdMessage + var metadata runtime.ServerMetadata var ( val string @@ -254,29 +275,43 @@ func request_ABitOfEverythingService_Lookup_0(ctx context.Context, client ABitOf val, ok = pathParams["uuid"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uuid") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uuid") } protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.Lookup(ctx, &protoReq) + msg, err := client.Lookup(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_List_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (ABitOfEverythingService_ListClient, error) { +func request_ABitOfEverythingService_List_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (ABitOfEverythingService_ListClient, runtime.ServerMetadata, error) { var protoReq EmptyMessage + var metadata runtime.ServerMetadata + + stream, err := client.List(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil - return client.List(ctx, &protoReq) } -func request_ABitOfEverythingService_Update_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Update_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq ABitOfEverything + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -288,20 +323,23 @@ func request_ABitOfEverythingService_Update_0(ctx context.Context, client ABitOf val, ok = pathParams["uuid"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uuid") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uuid") } protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.Update(ctx, &protoReq) + msg, err := client.Update(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_Delete_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Delete_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq IdMessage + var metadata runtime.ServerMetadata var ( val string @@ -312,20 +350,23 @@ func request_ABitOfEverythingService_Delete_0(ctx context.Context, client ABitOf val, ok = pathParams["uuid"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uuid") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "uuid") } protoReq.Uuid, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.Delete(ctx, &protoReq) + msg, err := client.Delete(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_Echo_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Echo_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq sub.StringMessage + var metadata runtime.ServerMetadata var ( val string @@ -336,47 +377,56 @@ func request_ABitOfEverythingService_Echo_0(ctx context.Context, client ABitOfEv val, ok = pathParams["value"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "value") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "value") } protoReq.Value, err = runtime.StringP(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.Echo(ctx, &protoReq) + msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_Echo_1(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Echo_1(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq sub.StringMessage + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.Value); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.Echo(ctx, &protoReq) + msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_ABitOfEverythingService_Echo_2 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} ) -func request_ABitOfEverythingService_Echo_2(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_ABitOfEverythingService_Echo_2(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq sub.StringMessage + var metadata runtime.ServerMetadata if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_ABitOfEverythingService_Echo_2); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.Echo(ctx, &protoReq) + msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_ABitOfEverythingService_BulkEcho_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (ABitOfEverythingService_BulkEchoClient, error) { +func request_ABitOfEverythingService_BulkEcho_0(ctx context.Context, client ABitOfEverythingServiceClient, req *http.Request, pathParams map[string]string) (ABitOfEverythingService_BulkEchoClient, runtime.ServerMetadata, error) { + var metadata runtime.ServerMetadata stream, err := client.BulkEcho(ctx) if err != nil { glog.Errorf("Failed to start streaming: %v", err) - return nil, err + return nil, metadata, err } dec := json.NewDecoder(req.Body) for { @@ -387,19 +437,26 @@ func request_ABitOfEverythingService_BulkEcho_0(ctx context.Context, client ABit } if err != nil { glog.Errorf("Failed to decode request: %v", err) - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err = stream.Send(&protoReq); err != nil { glog.Errorf("Failed to send request: %v", err) - return nil, err + return nil, metadata, err } } - if err = stream.CloseSend(); err != nil { + if err := stream.CloseSend(); err != nil { glog.Errorf("Failed to terminate client stream: %v", err) - return nil, err + return nil, metadata, err } - return stream, nil + header, err := stream.Header() + if err != nil { + glog.Errorf("Failed to get header from client: %v", err) + return nil, metadata, err + } + metadata.HeaderMD = header + + return stream, metadata, nil } @@ -442,7 +499,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Create_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Create_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -461,7 +519,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_CreateBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_CreateBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -480,7 +539,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_BulkCreate_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_BulkCreate_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -499,7 +559,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Lookup_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Lookup_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -518,7 +579,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_List_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_List_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -537,7 +599,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Update_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Update_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -556,7 +619,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Delete_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Delete_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -575,7 +639,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -594,7 +659,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Echo_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Echo_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -613,7 +679,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_Echo_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_Echo_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -632,7 +699,8 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se cancel() }() } - resp, err := request_ABitOfEverythingService_BulkEcho_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_ABitOfEverythingService_BulkEcho_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return diff --git a/examples/examplepb/echo_service.pb.gw.go b/examples/examplepb/echo_service.pb.gw.go index 1e7f121f023..6f28b97d209 100644 --- a/examples/examplepb/echo_service.pb.gw.go +++ b/examples/examplepb/echo_service.pb.gw.go @@ -29,8 +29,9 @@ var _ = runtime.String var _ = json.Marshal var _ = utilities.NewDoubleArray -func request_EchoService_Echo_0(ctx context.Context, client EchoServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_EchoService_Echo_0(ctx context.Context, client EchoServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq SimpleMessage + var metadata runtime.ServerMetadata var ( val string @@ -41,26 +42,31 @@ func request_EchoService_Echo_0(ctx context.Context, client EchoServiceClient, r val, ok = pathParams["id"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "id") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "id") } protoReq.Id, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.Echo(ctx, &protoReq) + msg, err := client.Echo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_EchoService_EchoBody_0(ctx context.Context, client EchoServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_EchoService_EchoBody_0(ctx context.Context, client EchoServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq SimpleMessage + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.EchoBody(ctx, &protoReq) + msg, err := client.EchoBody(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } // RegisterEchoServiceHandlerFromEndpoint is same as RegisterEchoServiceHandler but @@ -102,7 +108,8 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn cancel() }() } - resp, err := request_EchoService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_EchoService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -121,7 +128,8 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn cancel() }() } - resp, err := request_EchoService_EchoBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_EchoService_EchoBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return diff --git a/examples/examplepb/flow_combination.pb.gw.go b/examples/examplepb/flow_combination.pb.gw.go index 3839f631998..60deabbdeb9 100644 --- a/examples/examplepb/flow_combination.pb.gw.go +++ b/examples/examplepb/flow_combination.pb.gw.go @@ -29,23 +29,38 @@ var _ = runtime.String var _ = json.Marshal var _ = utilities.NewDoubleArray -func request_FlowCombination_RpcEmptyRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcEmptyRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq EmptyProto + var metadata runtime.ServerMetadata + + msg, err := client.RpcEmptyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err - return client.RpcEmptyRpc(ctx, &protoReq) } -func request_FlowCombination_RpcEmptyStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcEmptyStreamClient, error) { +func request_FlowCombination_RpcEmptyStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcEmptyStreamClient, runtime.ServerMetadata, error) { var protoReq EmptyProto + var metadata runtime.ServerMetadata + + stream, err := client.RpcEmptyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcEmptyStream(ctx, &protoReq) } -func request_FlowCombination_StreamEmptyRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_StreamEmptyRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var metadata runtime.ServerMetadata stream, err := client.StreamEmptyRpc(ctx) if err != nil { glog.Errorf("Failed to start streaming: %v", err) - return nil, err + return nil, metadata, err } dec := json.NewDecoder(req.Body) for { @@ -56,23 +71,37 @@ func request_FlowCombination_StreamEmptyRpc_0(ctx context.Context, client FlowCo } if err != nil { glog.Errorf("Failed to decode request: %v", err) - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err = stream.Send(&protoReq); err != nil { glog.Errorf("Failed to send request: %v", err) - return nil, err + return nil, metadata, err } } - return stream.CloseAndRecv() + if err := stream.CloseSend(); err != nil { + glog.Errorf("Failed to terminate client stream: %v", err) + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + glog.Errorf("Failed to get header from client: %v", err) + return nil, metadata, err + } + metadata.HeaderMD = header + + msg, err := stream.CloseAndRecv() + metadata.TrailerMD = stream.Trailer() + return msg, metadata, err } -func request_FlowCombination_StreamEmptyStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_StreamEmptyStreamClient, error) { +func request_FlowCombination_StreamEmptyStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_StreamEmptyStreamClient, runtime.ServerMetadata, error) { + var metadata runtime.ServerMetadata stream, err := client.StreamEmptyStream(ctx) if err != nil { glog.Errorf("Failed to start streaming: %v", err) - return nil, err + return nil, metadata, err } dec := json.NewDecoder(req.Body) for { @@ -83,34 +112,45 @@ func request_FlowCombination_StreamEmptyStream_0(ctx context.Context, client Flo } if err != nil { glog.Errorf("Failed to decode request: %v", err) - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err = stream.Send(&protoReq); err != nil { glog.Errorf("Failed to send request: %v", err) - return nil, err + return nil, metadata, err } } - if err = stream.CloseSend(); err != nil { + if err := stream.CloseSend(); err != nil { glog.Errorf("Failed to terminate client stream: %v", err) - return nil, err + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + glog.Errorf("Failed to get header from client: %v", err) + return nil, metadata, err } - return stream, nil + metadata.HeaderMD = header + + return stream, metadata, nil } -func request_FlowCombination_RpcBodyRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_FlowCombination_RpcBodyRpc_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata var ( val string @@ -121,59 +161,65 @@ func request_FlowCombination_RpcBodyRpc_1(ctx context.Context, client FlowCombin val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["b"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") } protoReq.B, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["c"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "c") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "c") } protoReq.C, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcBodyRpc_2 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} ) -func request_FlowCombination_RpcBodyRpc_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyRpc_2); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_FlowCombination_RpcBodyRpc_3(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_3(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -185,56 +231,62 @@ func request_FlowCombination_RpcBodyRpc_3(ctx context.Context, client FlowCombin val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["b"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") } protoReq.B, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcBodyRpc_4 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} ) -func request_FlowCombination_RpcBodyRpc_4(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_4(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyRpc_4); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcBodyRpc_5 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0, "a": 1}, Base: []int{1, 1, 2, 0, 0}, Check: []int{0, 1, 1, 2, 3}} ) -func request_FlowCombination_RpcBodyRpc_5(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_5(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -246,28 +298,31 @@ func request_FlowCombination_RpcBodyRpc_5(ctx context.Context, client FlowCombin val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyRpc_5); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcBodyRpc_6 = &utilities.DoubleArray{Encoding: map[string]int{"a": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} ) -func request_FlowCombination_RpcBodyRpc_6(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcBodyRpc_6(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata var ( val string @@ -278,28 +333,31 @@ func request_FlowCombination_RpcBodyRpc_6(ctx context.Context, client FlowCombin val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyRpc_6); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcBodyRpc(ctx, &protoReq) + msg, err := client.RpcBodyRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcPathSingleNestedRpc_0 = &utilities.DoubleArray{Encoding: map[string]int{"a": 0, "str": 1}, Base: []int{1, 1, 1, 0}, Check: []int{0, 1, 2, 3}} ) -func request_FlowCombination_RpcPathSingleNestedRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcPathSingleNestedRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq SingleNestedProto + var metadata runtime.ServerMetadata var ( val string @@ -310,31 +368,34 @@ func request_FlowCombination_RpcPathSingleNestedRpc_0(ctx context.Context, clien val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathSingleNestedRpc_0); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcPathSingleNestedRpc(ctx, &protoReq) + msg, err := client.RpcPathSingleNestedRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcPathNestedRpc_0 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0, "a": 1, "str": 2, "b": 3}, Base: []int{1, 1, 1, 2, 3, 0, 0, 0}, Check: []int{0, 1, 1, 3, 1, 2, 4, 5}} ) -func request_FlowCombination_RpcPathNestedRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcPathNestedRpc_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NestedProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -346,39 +407,42 @@ func request_FlowCombination_RpcPathNestedRpc_0(ctx context.Context, client Flow val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["b"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") } protoReq.B, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedRpc_0); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcPathNestedRpc(ctx, &protoReq) + msg, err := client.RpcPathNestedRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcPathNestedRpc_1 = &utilities.DoubleArray{Encoding: map[string]int{"a": 0, "str": 1}, Base: []int{1, 1, 1, 0}, Check: []int{0, 1, 2, 3}} ) -func request_FlowCombination_RpcPathNestedRpc_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcPathNestedRpc_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NestedProto + var metadata runtime.ServerMetadata var ( val string @@ -389,31 +453,34 @@ func request_FlowCombination_RpcPathNestedRpc_1(ctx context.Context, client Flow val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedRpc_1); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcPathNestedRpc(ctx, &protoReq) + msg, err := client.RpcPathNestedRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } var ( filter_FlowCombination_RpcPathNestedRpc_2 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0, "a": 1, "str": 2}, Base: []int{1, 1, 1, 2, 0, 0}, Check: []int{0, 1, 1, 3, 2, 4}} ) -func request_FlowCombination_RpcPathNestedRpc_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, error) { +func request_FlowCombination_RpcPathNestedRpc_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq NestedProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -425,34 +492,48 @@ func request_FlowCombination_RpcPathNestedRpc_2(ctx context.Context, client Flow val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedRpc_2); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcPathNestedRpc(ctx, &protoReq) + msg, err := client.RpcPathNestedRpc(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + } -func request_FlowCombination_RpcBodyStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) + } + + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcBodyStream(ctx, &protoReq) } -func request_FlowCombination_RpcBodyStream_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata var ( val string @@ -463,59 +544,81 @@ func request_FlowCombination_RpcBodyStream_1(ctx context.Context, client FlowCom val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["b"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") } protoReq.B, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["c"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "c") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "c") } protoReq.C, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.RpcBodyStream(ctx, &protoReq) + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil + } var ( filter_FlowCombination_RpcBodyStream_2 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} ) -func request_FlowCombination_RpcBodyStream_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyStream_2); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) + } + + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcBodyStream(ctx, &protoReq) } -func request_FlowCombination_RpcBodyStream_3(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_3(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -527,56 +630,78 @@ func request_FlowCombination_RpcBodyStream_3(ctx context.Context, client FlowCom val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["b"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") } protoReq.B, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } - return client.RpcBodyStream(ctx, &protoReq) + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil + } var ( filter_FlowCombination_RpcBodyStream_4 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} ) -func request_FlowCombination_RpcBodyStream_4(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_4(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyStream_4); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) + } + + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcBodyStream(ctx, &protoReq) } var ( filter_FlowCombination_RpcBodyStream_5 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0, "a": 1}, Base: []int{1, 1, 2, 0, 0}, Check: []int{0, 1, 1, 2, 3}} ) -func request_FlowCombination_RpcBodyStream_5(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_5(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -588,28 +713,39 @@ func request_FlowCombination_RpcBodyStream_5(ctx context.Context, client FlowCom val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyStream_5); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcBodyStream(ctx, &protoReq) + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil + } var ( filter_FlowCombination_RpcBodyStream_6 = &utilities.DoubleArray{Encoding: map[string]int{"a": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} ) -func request_FlowCombination_RpcBodyStream_6(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, error) { +func request_FlowCombination_RpcBodyStream_6(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcBodyStreamClient, runtime.ServerMetadata, error) { var protoReq NonEmptyProto + var metadata runtime.ServerMetadata var ( val string @@ -620,28 +756,39 @@ func request_FlowCombination_RpcBodyStream_6(ctx context.Context, client FlowCom val, ok = pathParams["a"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a") } protoReq.A, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcBodyStream_6); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) + } + + stream, err := client.RpcBodyStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcBodyStream(ctx, &protoReq) } var ( filter_FlowCombination_RpcPathSingleNestedStream_0 = &utilities.DoubleArray{Encoding: map[string]int{"a": 0, "str": 1}, Base: []int{1, 1, 1, 0}, Check: []int{0, 1, 2, 3}} ) -func request_FlowCombination_RpcPathSingleNestedStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathSingleNestedStreamClient, error) { +func request_FlowCombination_RpcPathSingleNestedStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathSingleNestedStreamClient, runtime.ServerMetadata, error) { var protoReq SingleNestedProto + var metadata runtime.ServerMetadata var ( val string @@ -652,31 +799,42 @@ func request_FlowCombination_RpcPathSingleNestedStream_0(ctx context.Context, cl val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathSingleNestedStream_0); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcPathSingleNestedStream(ctx, &protoReq) + stream, err := client.RpcPathSingleNestedStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil + } var ( filter_FlowCombination_RpcPathNestedStream_0 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0, "a": 1, "str": 2, "b": 3}, Base: []int{1, 1, 1, 2, 3, 0, 0, 0}, Check: []int{0, 1, 1, 3, 1, 2, 4, 5}} ) -func request_FlowCombination_RpcPathNestedStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathNestedStreamClient, error) { +func request_FlowCombination_RpcPathNestedStream_0(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathNestedStreamClient, runtime.ServerMetadata, error) { var protoReq NestedProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -688,39 +846,50 @@ func request_FlowCombination_RpcPathNestedStream_0(ctx context.Context, client F val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } val, ok = pathParams["b"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "b") } protoReq.B, err = runtime.String(val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedStream_0); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) + } + + stream, err := client.RpcPathNestedStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcPathNestedStream(ctx, &protoReq) } var ( filter_FlowCombination_RpcPathNestedStream_1 = &utilities.DoubleArray{Encoding: map[string]int{"a": 0, "str": 1}, Base: []int{1, 1, 1, 0}, Check: []int{0, 1, 2, 3}} ) -func request_FlowCombination_RpcPathNestedStream_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathNestedStreamClient, error) { +func request_FlowCombination_RpcPathNestedStream_1(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathNestedStreamClient, runtime.ServerMetadata, error) { var protoReq NestedProto + var metadata runtime.ServerMetadata var ( val string @@ -731,31 +900,42 @@ func request_FlowCombination_RpcPathNestedStream_1(ctx context.Context, client F val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedStream_1); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } - return client.RpcPathNestedStream(ctx, &protoReq) + stream, err := client.RpcPathNestedStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err + } + metadata.HeaderMD = header + return stream, metadata, nil + } var ( filter_FlowCombination_RpcPathNestedStream_2 = &utilities.DoubleArray{Encoding: map[string]int{"c": 0, "a": 1, "str": 2}, Base: []int{1, 1, 1, 2, 0, 0}, Check: []int{0, 1, 1, 3, 2, 4}} ) -func request_FlowCombination_RpcPathNestedStream_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathNestedStreamClient, error) { +func request_FlowCombination_RpcPathNestedStream_2(ctx context.Context, client FlowCombinationClient, req *http.Request, pathParams map[string]string) (FlowCombination_RpcPathNestedStreamClient, runtime.ServerMetadata, error) { var protoReq NestedProto + var metadata runtime.ServerMetadata if err := json.NewDecoder(req.Body).Decode(&protoReq.C); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) } var ( @@ -767,20 +947,30 @@ func request_FlowCombination_RpcPathNestedStream_2(ctx context.Context, client F val, ok = pathParams["a.str"] if !ok { - return nil, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "missing parameter %s", "a.str") } err = runtime.PopulateFieldFromPath(&protoReq, "a.str", val) if err != nil { - return nil, err + return nil, metadata, err } if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_FlowCombination_RpcPathNestedStream_2); err != nil { - return nil, grpc.Errorf(codes.InvalidArgument, "%v", err) + return nil, metadata, grpc.Errorf(codes.InvalidArgument, "%v", err) + } + + stream, err := client.RpcPathNestedStream(ctx, &protoReq) + if err != nil { + return nil, metadata, err + } + header, err := stream.Header() + if err != nil { + return nil, metadata, err } + metadata.HeaderMD = header + return stream, metadata, nil - return client.RpcPathNestedStream(ctx, &protoReq) } // RegisterFlowCombinationHandlerFromEndpoint is same as RegisterFlowCombinationHandler but @@ -822,7 +1012,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -841,7 +1032,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -860,7 +1052,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_StreamEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_StreamEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -879,7 +1072,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_StreamEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_StreamEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -898,7 +1092,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -917,7 +1112,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -936,7 +1132,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -955,7 +1152,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -974,7 +1172,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -993,7 +1192,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1012,7 +1212,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyRpc_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyRpc_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1031,7 +1232,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathSingleNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathSingleNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1050,7 +1252,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1069,7 +1272,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathNestedRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathNestedRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1088,7 +1292,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathNestedRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathNestedRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1107,7 +1312,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1126,7 +1332,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1145,7 +1352,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1164,7 +1372,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1183,7 +1392,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1202,7 +1412,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1221,7 +1432,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcBodyStream_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcBodyStream_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1240,7 +1452,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathSingleNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathSingleNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1259,7 +1472,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1278,7 +1492,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathNestedStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathNestedStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1297,7 +1512,8 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, cancel() }() } - resp, err := request_FlowCombination_RpcPathNestedStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + resp, md, err := request_FlowCombination_RpcPathNestedStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, &md) if err != nil { runtime.HTTPError(ctx, w, req, err) return From e6a7a733fecd1017036e89c95645c5c25e0aa61b Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 18 Feb 2016 19:32:02 +0900 Subject: [PATCH 09/11] not passing pointer to context --- protoc-gen-grpc-gateway/gengateway/template.go | 2 +- runtime/context.go | 7 ++++--- runtime/errors.go | 1 - runtime/handler.go | 18 +++--------------- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index 9a515411c2a..34838c3907c 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -278,7 +278,7 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return diff --git a/runtime/context.go b/runtime/context.go index f75a3946533..83557fa72fc 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -37,6 +37,7 @@ func AnnotateContext(ctx context.Context, req *http.Request) context.Context { return metadata.NewContext(ctx, metadata.Pairs(pairs...)) } +// ServerMetadata consists of metadata sent from gRPC server. type ServerMetadata struct { HeaderMD metadata.MD TrailerMD metadata.MD @@ -45,12 +46,12 @@ type ServerMetadata struct { type serverMetadataKey struct{} // NewServerMetadataContext creates a new context with ServerMetadata -func NewServerMetadataContext(ctx context.Context, md *ServerMetadata) context.Context { +func NewServerMetadataContext(ctx context.Context, md ServerMetadata) context.Context { return context.WithValue(ctx, serverMetadataKey{}, md) } // ServerMetadataFromContext returns the ServerMetadata in ctx -func ServerMetadataFromContext(ctx context.Context) (md *ServerMetadata, ok bool) { - md, ok = ctx.Value(serverMetadataKey{}).(*ServerMetadata) +func ServerMetadataFromContext(ctx context.Context) (md ServerMetadata, ok bool) { + md, ok = ctx.Value(serverMetadataKey{}).(ServerMetadata) return } diff --git a/runtime/errors.go b/runtime/errors.go index 97cfddcd0c7..ea96e153fac 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -9,7 +9,6 @@ import ( "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" - "google.golang.org/grpc/metadata" ) // HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status. diff --git a/runtime/handler.go b/runtime/handler.go index 674f4e779ad..c1f7ce16c0b 100644 --- a/runtime/handler.go +++ b/runtime/handler.go @@ -76,11 +76,7 @@ func ForwardResponseStream(ctx context.Context, w http.ResponseWriter, req *http } } -func handleForwardResponseServerMetadata(w http.ResponseWriter, md *ServerMetadata) { - if md == nil { - return - } - +func handleForwardResponseServerMetadata(w http.ResponseWriter, md ServerMetadata) { for k, vs := range md.HeaderMD { hKey := fmt.Sprintf("%s%s", metadataHeaderPrefix, k) for i := range vs { @@ -89,22 +85,14 @@ func handleForwardResponseServerMetadata(w http.ResponseWriter, md *ServerMetada } } -func handleForwardResponseTrailerHeader(w http.ResponseWriter, md *ServerMetadata) { - if md == nil { - return - } - +func handleForwardResponseTrailerHeader(w http.ResponseWriter, md ServerMetadata) { for k := range md.TrailerMD { tKey := textproto.CanonicalMIMEHeaderKey(fmt.Sprintf("%s%s", metadataTrailerPrefix, k)) w.Header().Add("Trailer", tKey) } } -func handleForwardResponseTrailer(w http.ResponseWriter, md *ServerMetadata) { - if md == nil { - return - } - +func handleForwardResponseTrailer(w http.ResponseWriter, md ServerMetadata) { for k, vs := range md.TrailerMD { tKey := fmt.Sprintf("%s%s", metadataTrailerPrefix, k) for i := range vs { From 21081c005d63b5d5c1d39a47eef03c0574fe8fe5 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 18 Feb 2016 19:32:48 +0900 Subject: [PATCH 10/11] unexport struct --- examples/integration_test.go | 7 ++++++- runtime/errors.go | 9 ++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/integration_test.go b/examples/integration_test.go index 324acb81c9e..fd3c2ff6300 100644 --- a/examples/integration_test.go +++ b/examples/integration_test.go @@ -23,6 +23,11 @@ import ( "google.golang.org/grpc/codes" ) +type errorBody struct { + Error string `json:"error"` + Code int `json:"code"` +} + func TestIntegration(t *testing.T) { if testing.Short() { t.Skip() @@ -437,7 +442,7 @@ func testABELookupNotFound(t *testing.T) { return } - var msg runtime.ErrorBody + var msg errorBody if err := json.Unmarshal(buf, &msg); err != nil { t.Errorf("json.Unmarshal(%s, &msg) failed with %v; want success", buf, err) return diff --git a/runtime/errors.go b/runtime/errors.go index ea96e153fac..d26eac37df2 100644 --- a/runtime/errors.go +++ b/runtime/errors.go @@ -62,10 +62,9 @@ var ( OtherErrorHandler = DefaultOtherErrorHandler ) -type ErrorBody struct { - Error string `json:"error"` - Code int `json:"code"` - Trailer metadata.MD `json:"trailer,omitempty"` +type errorBody struct { + Error string `json:"error"` + Code int `json:"code"` } // DefaultHTTPError is the default implementation of HTTPError. @@ -79,7 +78,7 @@ func DefaultHTTPError(ctx context.Context, w http.ResponseWriter, _ *http.Reques w.Header().Del("Trailer") w.Header().Set("Content-Type", "application/json") - body := ErrorBody{ + body := errorBody{ Error: grpc.ErrorDesc(err), Code: int(grpc.Code(err)), } From bf9efa4fbc4442c9856856e908c78d9120224297 Mon Sep 17 00:00:00 2001 From: Masahiro Sano Date: Thu, 18 Feb 2016 19:33:01 +0900 Subject: [PATCH 11/11] regenerate examples --- .../examplepb/a_bit_of_everything.pb.gw.go | 22 ++++---- examples/examplepb/echo_service.pb.gw.go | 4 +- examples/examplepb/flow_combination.pb.gw.go | 52 +++++++++---------- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/examples/examplepb/a_bit_of_everything.pb.gw.go b/examples/examplepb/a_bit_of_everything.pb.gw.go index b96fea0ae8d..b906c01fc57 100644 --- a/examples/examplepb/a_bit_of_everything.pb.gw.go +++ b/examples/examplepb/a_bit_of_everything.pb.gw.go @@ -500,7 +500,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Create_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -520,7 +520,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_CreateBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -540,7 +540,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_BulkCreate_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -560,7 +560,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Lookup_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -580,7 +580,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_List_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -600,7 +600,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Update_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -620,7 +620,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Delete_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -640,7 +640,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -660,7 +660,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Echo_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -680,7 +680,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_Echo_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -700,7 +700,7 @@ func RegisterABitOfEverythingServiceHandler(ctx context.Context, mux *runtime.Se }() } resp, md, err := request_ABitOfEverythingService_BulkEcho_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return diff --git a/examples/examplepb/echo_service.pb.gw.go b/examples/examplepb/echo_service.pb.gw.go index 6f28b97d209..c16cc444a74 100644 --- a/examples/examplepb/echo_service.pb.gw.go +++ b/examples/examplepb/echo_service.pb.gw.go @@ -109,7 +109,7 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn }() } resp, md, err := request_EchoService_Echo_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -129,7 +129,7 @@ func RegisterEchoServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn }() } resp, md, err := request_EchoService_EchoBody_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return diff --git a/examples/examplepb/flow_combination.pb.gw.go b/examples/examplepb/flow_combination.pb.gw.go index 60deabbdeb9..f83c88fa5d9 100644 --- a/examples/examplepb/flow_combination.pb.gw.go +++ b/examples/examplepb/flow_combination.pb.gw.go @@ -1013,7 +1013,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1033,7 +1033,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1053,7 +1053,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_StreamEmptyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1073,7 +1073,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_StreamEmptyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1093,7 +1093,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1113,7 +1113,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1133,7 +1133,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1153,7 +1153,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1173,7 +1173,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1193,7 +1193,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1213,7 +1213,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyRpc_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1233,7 +1233,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathSingleNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1253,7 +1253,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathNestedRpc_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1273,7 +1273,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathNestedRpc_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1293,7 +1293,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathNestedRpc_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1313,7 +1313,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1333,7 +1333,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1353,7 +1353,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1373,7 +1373,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_3(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1393,7 +1393,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_4(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1413,7 +1413,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_5(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1433,7 +1433,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcBodyStream_6(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1453,7 +1453,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathSingleNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1473,7 +1473,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathNestedStream_0(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1493,7 +1493,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathNestedStream_1(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return @@ -1513,7 +1513,7 @@ func RegisterFlowCombinationHandler(ctx context.Context, mux *runtime.ServeMux, }() } resp, md, err := request_FlowCombination_RpcPathNestedStream_2(runtime.AnnotateContext(ctx, req), client, req, pathParams) - ctx = runtime.NewServerMetadataContext(ctx, &md) + ctx = runtime.NewServerMetadataContext(ctx, md) if err != nil { runtime.HTTPError(ctx, w, req, err) return