From 564e7b97ddeb085ba5f3d5afdd3f96ce5b6f1f55 Mon Sep 17 00:00:00 2001 From: nolouch Date: Thu, 31 May 2018 14:35:45 +0800 Subject: [PATCH] update grpc-gateway --- Gopkg.lock | 6 +- Gopkg.toml | 2 +- .../grpc-gateway/runtime/context.go | 6 +- .../grpc-gateway/runtime/convert.go | 11 +++ .../grpc-gateway/runtime/errors.go | 27 ++++-- .../grpc-gateway/runtime/handler.go | 12 ++- .../runtime/internal/stream_chunk.pb.go | 91 ++++++++++++------- .../grpc-gateway/runtime/marshal_json.go | 3 + .../grpc-gateway/runtime/marshal_jsonpb.go | 28 ++++-- .../runtime/marshaler_registry.go | 2 +- .../grpc-gateway/runtime/mux.go | 6 +- .../grpc-gateway/runtime/proto_errors.go | 2 +- .../grpc-gateway/runtime/query.go | 86 +++++++++++++++++- .../grpc-gateway/utilities/pattern.go | 2 +- 14 files changed, 219 insertions(+), 65 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index f8176e4546f..ff996176171 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -198,8 +198,8 @@ "runtime/internal", "utilities" ] - revision = "07f5e79768022f9a3265235f0db4ac8c3f675fec" - version = "v1.3.1" + revision = "92583770e3f01b09a0d3e9bdf64321d8bebd48f2" + version = "v1.4.1" [[projects]] name = "github.com/inconshreveable/mousetrap" @@ -494,6 +494,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "1cc633e92575e0431252f3a3d12043cb7525406974526931743d97ee1877e8bd" + inputs-digest = "da8bdc8144bcc6b679548cc3eb0de72258f9a9d480a789c2f43b77be8b5a0087" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 156bef1c34d..a60ac716d7e 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -13,7 +13,7 @@ [[override]] name = "github.com/grpc-ecosystem/grpc-gateway" - version = "~1.3" + version = "~1.4" [[constraint]] name = "google.golang.org/grpc" diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/context.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/context.go index 6e0eb27e285..a745074c961 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/context.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/context.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "golang.org/x/net/context" + "context" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/metadata" @@ -91,8 +91,8 @@ func AnnotateContext(ctx context.Context, mux *ServeMux, req *http.Request) (con return ctx, nil } md := metadata.Pairs(pairs...) - if mux.metadataAnnotator != nil { - md = metadata.Join(md, mux.metadataAnnotator(ctx, req)) + for _, mda := range mux.metadataAnnotators { + md = metadata.Join(md, mda(ctx, req)) } return metadata.NewOutgoingContext(ctx, md), nil } diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/convert.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/convert.go index f8931864100..903ae23407b 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/convert.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/convert.go @@ -1,6 +1,7 @@ package runtime import ( + "encoding/base64" "strconv" "github.com/golang/protobuf/jsonpb" @@ -61,6 +62,16 @@ func Uint32(val string) (uint32, error) { return uint32(i), nil } +// Bytes converts the given string representation of a byte sequence into a slice of bytes +// A bytes sequence is encoded in URL-safe base64 without padding +func Bytes(val string) ([]byte, error) { + b, err := base64.StdEncoding.DecodeString(val) + if err != nil { + return nil, err + } + return b, nil +} + // Timestamp converts the given RFC3339 formatted string into a timestamp.Timestamp. func Timestamp(val string) (*timestamp.Timestamp, error) { var r *timestamp.Timestamp diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/errors.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/errors.go index 8eebdcf49f4..0e2bdf4457d 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/errors.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/errors.go @@ -1,17 +1,20 @@ package runtime import ( + "context" "io" "net/http" "github.com/golang/protobuf/proto" - "golang.org/x/net/context" + "github.com/golang/protobuf/ptypes" + "github.com/golang/protobuf/ptypes/any" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/status" ) // HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status. +// See: https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto func HTTPStatusFromCode(code codes.Code) int { switch code { case codes.OK: @@ -23,7 +26,7 @@ func HTTPStatusFromCode(code codes.Code) int { case codes.InvalidArgument: return http.StatusBadRequest case codes.DeadlineExceeded: - return http.StatusRequestTimeout + return http.StatusGatewayTimeout case codes.NotFound: return http.StatusNotFound case codes.AlreadyExists: @@ -33,7 +36,7 @@ func HTTPStatusFromCode(code codes.Code) int { case codes.Unauthenticated: return http.StatusUnauthorized case codes.ResourceExhausted: - return http.StatusForbidden + return http.StatusTooManyRequests case codes.FailedPrecondition: return http.StatusPreconditionFailed case codes.Aborted: @@ -63,11 +66,12 @@ var ( ) type errorBody struct { - Error string `protobuf:"bytes,1,name=error" json:"error"` - Code int32 `protobuf:"varint,2,name=code" json:"code"` + Error string `protobuf:"bytes,1,name=error" json:"error"` + Code int32 `protobuf:"varint,2,name=code" json:"code"` + Details []*any.Any `protobuf:"bytes,3,rep,name=details" json:"details,omitempty"` } -//Make this also conform to proto.Message for builtin JSONPb Marshaler +// Make this also conform to proto.Message for builtin JSONPb Marshaler func (e *errorBody) Reset() { *e = errorBody{} } func (e *errorBody) String() string { return proto.CompactTextString(e) } func (*errorBody) ProtoMessage() {} @@ -94,6 +98,17 @@ func DefaultHTTPError(ctx context.Context, mux *ServeMux, marshaler Marshaler, w Code: int32(s.Code()), } + for _, detail := range s.Details() { + if det, ok := detail.(proto.Message); ok { + a, err := ptypes.MarshalAny(det) + if err != nil { + grpclog.Printf("Failed to marshal any: %v", err) + } else { + body.Details = append(body.Details, a) + } + } + } + buf, merr := marshaler.Marshal(body) if merr != nil { grpclog.Printf("Failed to marshal error message %q: %v", body, merr) diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/handler.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/handler.go index 1770b85344d..1b3c65035a4 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/handler.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/handler.go @@ -6,9 +6,10 @@ import ( "net/http" "net/textproto" + "context" "github.com/golang/protobuf/proto" + "github.com/golang/protobuf/ptypes/any" "github.com/grpc-ecosystem/grpc-gateway/runtime/internal" - "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/status" @@ -42,7 +43,7 @@ func ForwardResponseStream(ctx context.Context, mux *ServeMux, marshaler Marshal if d, ok := marshaler.(Delimited); ok { delimiter = d.Delimiter() } else { - delimiter = []byte("\n") + delimiter = []byte("\n") } var wroteHeader bool @@ -169,16 +170,21 @@ func handleForwardResponseStreamError(wroteHeader bool, marshaler Marshaler, w h func streamChunk(result proto.Message, err error) map[string]proto.Message { if err != nil { grpcCode := codes.Unknown + grpcMessage := err.Error() + var grpcDetails []*any.Any if s, ok := status.FromError(err); ok { grpcCode = s.Code() + grpcMessage = s.Message() + grpcDetails = s.Proto().GetDetails() } httpCode := HTTPStatusFromCode(grpcCode) return map[string]proto.Message{ "error": &internal.StreamError{ GrpcCode: int32(grpcCode), HttpCode: int32(httpCode), - Message: err.Error(), + Message: grpcMessage, HttpStatus: http.StatusText(httpCode), + Details: grpcDetails, }, } } diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/internal/stream_chunk.pb.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/internal/stream_chunk.pb.go index 44550f393b4..a06c722c167 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/internal/stream_chunk.pb.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/internal/stream_chunk.pb.go @@ -1,20 +1,12 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // source: runtime/internal/stream_chunk.proto -/* -Package internal is a generated protocol buffer package. - -It is generated from these files: - runtime/internal/stream_chunk.proto - -It has these top-level messages: - StreamError -*/ package internal import proto "github.com/golang/protobuf/proto" import fmt "fmt" import math "math" +import any "github.com/golang/protobuf/ptypes/any" // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal @@ -30,16 +22,39 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package // StreamError is a response type which is returned when // streaming rpc returns an error. type StreamError struct { - GrpcCode int32 `protobuf:"varint,1,opt,name=grpc_code,json=grpcCode" json:"grpc_code,omitempty"` - HttpCode int32 `protobuf:"varint,2,opt,name=http_code,json=httpCode" json:"http_code,omitempty"` - Message string `protobuf:"bytes,3,opt,name=message" json:"message,omitempty"` - HttpStatus string `protobuf:"bytes,4,opt,name=http_status,json=httpStatus" json:"http_status,omitempty"` + GrpcCode int32 `protobuf:"varint,1,opt,name=grpc_code,json=grpcCode" json:"grpc_code,omitempty"` + HttpCode int32 `protobuf:"varint,2,opt,name=http_code,json=httpCode" json:"http_code,omitempty"` + Message string `protobuf:"bytes,3,opt,name=message" json:"message,omitempty"` + HttpStatus string `protobuf:"bytes,4,opt,name=http_status,json=httpStatus" json:"http_status,omitempty"` + Details []*any.Any `protobuf:"bytes,5,rep,name=details" json:"details,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } -func (m *StreamError) Reset() { *m = StreamError{} } -func (m *StreamError) String() string { return proto.CompactTextString(m) } -func (*StreamError) ProtoMessage() {} -func (*StreamError) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } +func (m *StreamError) Reset() { *m = StreamError{} } +func (m *StreamError) String() string { return proto.CompactTextString(m) } +func (*StreamError) ProtoMessage() {} +func (*StreamError) Descriptor() ([]byte, []int) { + return fileDescriptor_stream_chunk_6adb10b40700458b, []int{0} +} +func (m *StreamError) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_StreamError.Unmarshal(m, b) +} +func (m *StreamError) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_StreamError.Marshal(b, m, deterministic) +} +func (dst *StreamError) XXX_Merge(src proto.Message) { + xxx_messageInfo_StreamError.Merge(dst, src) +} +func (m *StreamError) XXX_Size() int { + return xxx_messageInfo_StreamError.Size(m) +} +func (m *StreamError) XXX_DiscardUnknown() { + xxx_messageInfo_StreamError.DiscardUnknown(m) +} + +var xxx_messageInfo_StreamError proto.InternalMessageInfo func (m *StreamError) GetGrpcCode() int32 { if m != nil { @@ -69,24 +84,36 @@ func (m *StreamError) GetHttpStatus() string { return "" } +func (m *StreamError) GetDetails() []*any.Any { + if m != nil { + return m.Details + } + return nil +} + func init() { proto.RegisterType((*StreamError)(nil), "grpc.gateway.runtime.StreamError") } -func init() { proto.RegisterFile("runtime/internal/stream_chunk.proto", fileDescriptor0) } +func init() { + proto.RegisterFile("runtime/internal/stream_chunk.proto", fileDescriptor_stream_chunk_6adb10b40700458b) +} -var fileDescriptor0 = []byte{ - // 181 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x34, 0x8e, 0xbf, 0xee, 0x82, 0x30, - 0x14, 0x85, 0xd3, 0xdf, 0x1f, 0x85, 0xcb, 0x46, 0x1c, 0x9a, 0x38, 0x48, 0x74, 0x61, 0x82, 0xc1, - 0x37, 0xd0, 0xf8, 0x02, 0xb0, 0xb9, 0x90, 0x0a, 0x37, 0x40, 0x94, 0x96, 0xdc, 0x5e, 0x62, 0x5c, - 0x7d, 0x72, 0xd3, 0x22, 0xe3, 0xf9, 0xbe, 0x73, 0x92, 0x03, 0x07, 0x9a, 0x34, 0xf7, 0x03, 0xe6, - 0xbd, 0x66, 0x24, 0xad, 0x1e, 0xb9, 0x65, 0x42, 0x35, 0x54, 0x75, 0x37, 0xe9, 0x7b, 0x36, 0x92, - 0x61, 0x13, 0x6f, 0x5a, 0x1a, 0xeb, 0xac, 0x55, 0x8c, 0x4f, 0xf5, 0xca, 0xbe, 0x8b, 0xfd, 0x5b, - 0x40, 0x54, 0xfa, 0xf2, 0x85, 0xc8, 0x50, 0xbc, 0x85, 0xd0, 0xf5, 0xaa, 0xda, 0x34, 0x28, 0x45, - 0x22, 0xd2, 0xff, 0x22, 0x70, 0xe0, 0x6c, 0x1a, 0x74, 0xb2, 0x63, 0x1e, 0x67, 0xf9, 0x33, 0x4b, - 0x07, 0xbc, 0x94, 0xb0, 0x1e, 0xd0, 0x5a, 0xd5, 0xa2, 0xfc, 0x4d, 0x44, 0x1a, 0x16, 0x4b, 0x8c, - 0x77, 0x10, 0xf9, 0x99, 0x65, 0xc5, 0x93, 0x95, 0x7f, 0xde, 0x82, 0x43, 0xa5, 0x27, 0x27, 0xb8, - 0x06, 0xcb, 0xf3, 0xdb, 0xca, 0xbf, 0x3d, 0x7e, 0x02, 0x00, 0x00, 0xff, 0xff, 0xa9, 0x07, 0x92, - 0xb6, 0xd4, 0x00, 0x00, 0x00, +var fileDescriptor_stream_chunk_6adb10b40700458b = []byte{ + // 226 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x34, 0x90, 0xc1, 0x4e, 0x84, 0x30, + 0x10, 0x86, 0x83, 0xeb, 0xba, 0xbb, 0xc3, 0x8d, 0xec, 0xa1, 0xea, 0x41, 0xa2, 0x17, 0x4e, 0x25, + 0xd1, 0x27, 0x50, 0xe3, 0x0b, 0xb0, 0x37, 0x2f, 0x9b, 0x59, 0x18, 0x0b, 0x11, 0x5a, 0x32, 0x1d, + 0x62, 0x78, 0x2d, 0x9f, 0xd0, 0xb4, 0xc8, 0xb1, 0xdf, 0xf7, 0xff, 0x93, 0x3f, 0x85, 0x27, 0x9e, + 0xac, 0x74, 0x03, 0x95, 0x9d, 0x15, 0x62, 0x8b, 0x7d, 0xe9, 0x85, 0x09, 0x87, 0x73, 0xdd, 0x4e, + 0xf6, 0x5b, 0x8f, 0xec, 0xc4, 0x65, 0x47, 0xc3, 0x63, 0xad, 0x0d, 0x0a, 0xfd, 0xe0, 0xac, 0xff, + 0x1b, 0x77, 0xb7, 0xc6, 0x39, 0xd3, 0x53, 0x19, 0x33, 0x97, 0xe9, 0xab, 0x44, 0x3b, 0x2f, 0x85, + 0xc7, 0xdf, 0x04, 0xd2, 0x53, 0xbc, 0xf3, 0xc1, 0xec, 0x38, 0xbb, 0x87, 0x43, 0x38, 0x71, 0xae, + 0x5d, 0x43, 0x2a, 0xc9, 0x93, 0x62, 0x5b, 0xed, 0x03, 0x78, 0x77, 0x0d, 0x05, 0xd9, 0x8a, 0x8c, + 0x8b, 0xbc, 0x5a, 0x64, 0x00, 0x51, 0x2a, 0xd8, 0x0d, 0xe4, 0x3d, 0x1a, 0x52, 0x9b, 0x3c, 0x29, + 0x0e, 0xd5, 0xfa, 0xcc, 0x1e, 0x20, 0x8d, 0x35, 0x2f, 0x28, 0x93, 0x57, 0xd7, 0xd1, 0x42, 0x40, + 0xa7, 0x48, 0x32, 0x0d, 0xbb, 0x86, 0x04, 0xbb, 0xde, 0xab, 0x6d, 0xbe, 0x29, 0xd2, 0xe7, 0xa3, + 0x5e, 0x16, 0xeb, 0x75, 0xb1, 0x7e, 0xb5, 0x73, 0xb5, 0x86, 0xde, 0xe0, 0x73, 0xbf, 0x7e, 0xc2, + 0xe5, 0x26, 0x46, 0x5e, 0xfe, 0x02, 0x00, 0x00, 0xff, 0xff, 0x16, 0x75, 0x92, 0x08, 0x1f, 0x01, + 0x00, 0x00, } diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_json.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_json.go index b3a21418be4..f9d3a585a4c 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_json.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_json.go @@ -9,6 +9,9 @@ import ( // with the standard "encoding/json" package of Golang. // Although it is generally faster for simple proto messages than JSONPb, // it does not support advanced features of protobuf, e.g. map, oneof, .... +// +// The NewEncoder and NewDecoder types return *json.Encoder and +// *json.Decoder respectively. type JSONBuiltin struct{} // ContentType always Returns "application/json". diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_jsonpb.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_jsonpb.go index d42cc593e51..f56072a6f15 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_jsonpb.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshal_jsonpb.go @@ -14,6 +14,9 @@ import ( // JSONPb is a Marshaler which marshals/unmarshals into/from JSON // with the "github.com/golang/protobuf/jsonpb". // It supports fully functionality of protobuf unlike JSONBuiltin. +// +// The NewDecoder method returns a DecoderWrapper, so the underlying +// *json.Decoder methods can be used. type JSONPb jsonpb.Marshaler // ContentType always returns "application/json". @@ -21,9 +24,7 @@ func (*JSONPb) ContentType() string { return "application/json" } -// Marshal marshals "v" into JSON -// Currently it can marshal only proto.Message. -// TODO(yugui) Support fields of primitive types in a message. +// Marshal marshals "v" into JSON. func (j *JSONPb) Marshal(v interface{}) ([]byte, error) { if _, ok := v.(proto.Message); !ok { return j.marshalNonProtoField(v) @@ -50,11 +51,14 @@ func (j *JSONPb) marshalTo(w io.Writer, v interface{}) error { } // marshalNonProto marshals a non-message field of a protobuf message. -// This function does not correctly marshals arbitary data structure into JSON, +// This function does not correctly marshals arbitrary data structure into JSON, // but it is only capable of marshaling non-message field values of protobuf, // i.e. primitive types, enums; pointers to primitives or enums; maps from // integer/string types to primitives/enums/pointers to messages. func (j *JSONPb) marshalNonProtoField(v interface{}) ([]byte, error) { + if v == nil { + return []byte("null"), nil + } rv := reflect.ValueOf(v) for rv.Kind() == reflect.Ptr { if rv.IsNil() { @@ -84,8 +88,6 @@ func (j *JSONPb) marshalNonProtoField(v interface{}) ([]byte, error) { } // Unmarshal unmarshals JSON "data" into "v" -// Currently it can marshal only proto.Message. -// TODO(yugui) Support fields of primitive types in a message. func (j *JSONPb) Unmarshal(data []byte, v interface{}) error { return unmarshalJSONPb(data, v) } @@ -93,7 +95,19 @@ func (j *JSONPb) Unmarshal(data []byte, v interface{}) error { // NewDecoder returns a Decoder which reads JSON stream from "r". func (j *JSONPb) NewDecoder(r io.Reader) Decoder { d := json.NewDecoder(r) - return DecoderFunc(func(v interface{}) error { return decodeJSONPb(d, v) }) + return DecoderWrapper{Decoder: d} +} + +// DecoderWrapper is a wrapper around a *json.Decoder that adds +// support for protos to the Decode method. +type DecoderWrapper struct { + *json.Decoder +} + +// Decode wraps the embedded decoder's Decode method to support +// protos using a jsonpb.Unmarshaler. +func (d DecoderWrapper) Decode(v interface{}) error { + return decodeJSONPb(d.Decoder, v) } // NewEncoder returns an Encoder which writes JSON stream into "w". diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshaler_registry.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshaler_registry.go index 928f0733214..5cc53ae4f68 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshaler_registry.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/marshaler_registry.go @@ -68,7 +68,7 @@ func (m marshalerRegistry) add(mime string, marshaler Marshaler) error { // It allows for a mapping of case-sensitive Content-Type MIME type string to runtime.Marshaler interfaces. // // For example, you could allow the client to specify the use of the runtime.JSONPb marshaler -// with a "applicaton/jsonpb" Content-Type and the use of the runtime.JSONBuiltin marshaler +// with a "application/jsonpb" Content-Type and the use of the runtime.JSONBuiltin marshaler // with a "application/json" Content-Type. // "*" can be used to match any Content-Type. // This can be attached to a ServerMux with the marshaler option. diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/mux.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/mux.go index 205bc430921..1d4c75760fe 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/mux.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/mux.go @@ -6,8 +6,8 @@ import ( "net/textproto" "strings" + "context" "github.com/golang/protobuf/proto" - "golang.org/x/net/context" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" @@ -25,7 +25,7 @@ type ServeMux struct { marshalers marshalerRegistry incomingHeaderMatcher HeaderMatcherFunc outgoingHeaderMatcher HeaderMatcherFunc - metadataAnnotator func(context.Context, *http.Request) metadata.MD + metadataAnnotators []func(context.Context, *http.Request) metadata.MD protoErrorHandler ProtoErrorHandlerFunc } @@ -87,7 +87,7 @@ func WithOutgoingHeaderMatcher(fn HeaderMatcherFunc) ServeMuxOption { // is reading token from cookie and adding it in gRPC context. func WithMetadata(annotator func(context.Context, *http.Request) metadata.MD) ServeMuxOption { return func(serveMux *ServeMux) { - serveMux.metadataAnnotator = annotator + serveMux.metadataAnnotators = append(serveMux.metadataAnnotators, annotator) } } diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go index b1b089273b6..059928c2847 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/proto_errors.go @@ -4,7 +4,7 @@ import ( "io" "net/http" - "golang.org/x/net/context" + "context" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/status" diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go index c00e0b914e2..07d0ff8c5bf 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/runtime/query.go @@ -1,9 +1,11 @@ package runtime import ( + "encoding/base64" "fmt" "net/url" "reflect" + "regexp" "strconv" "strings" "time" @@ -17,6 +19,15 @@ import ( // A value is ignored if its key starts with one of the elements in "filter". func PopulateQueryParameters(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error { for key, values := range values { + re, err := regexp.Compile("^(.*)\\[(.*)\\]$") + if err != nil { + return err + } + match := re.FindStringSubmatch(key) + if len(match) == 3 { + key = match[1] + values = append([]string{match[2]}, values...) + } fieldPath := strings.Split(key, ".") if filter.HasCommonPrefix(fieldPath) { continue @@ -64,10 +75,14 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values [] } m = f case reflect.Slice: - // TODO(yugui) Support []byte if !isLast { return fmt.Errorf("unexpected repeated field in %s", strings.Join(fieldPath, ".")) } + // Handle []byte + if f.Type().Elem().Kind() == reflect.Uint8 { + m = f + break + } return populateRepeatedField(f, values, props) case reflect.Ptr: if f.IsNil() { @@ -79,6 +94,11 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values [] case reflect.Struct: m = f continue + case reflect.Map: + if !isLast { + return fmt.Errorf("unexpected nested field %s in %s", fieldPath[i+1], strings.Join(fieldPath[:i+1], ".")) + } + return populateMapField(f, values, props) default: return fmt.Errorf("unexpected type %s in %T", f.Type(), msg) } @@ -120,6 +140,41 @@ func fieldByProtoName(m reflect.Value, name string) (reflect.Value, *proto.Prope return reflect.Value{}, nil, nil } +func populateMapField(f reflect.Value, values []string, props *proto.Properties) error { + if len(values) != 2 { + return fmt.Errorf("more than one value provided for key %s in map %s", values[0], props.Name) + } + + key, value := values[0], values[1] + keyType := f.Type().Key() + valueType := f.Type().Elem() + if f.IsNil() { + f.Set(reflect.MakeMap(f.Type())) + } + + keyConv, ok := convFromType[keyType.Kind()] + if !ok { + return fmt.Errorf("unsupported key type %s in map %s", keyType, props.Name) + } + valueConv, ok := convFromType[valueType.Kind()] + if !ok { + return fmt.Errorf("unsupported value type %s in map %s", valueType, props.Name) + } + + keyV := keyConv.Call([]reflect.Value{reflect.ValueOf(key)}) + if err := keyV[1].Interface(); err != nil { + return err.(error) + } + valueV := valueConv.Call([]reflect.Value{reflect.ValueOf(value)}) + if err := valueV[1].Interface(); err != nil { + return err.(error) + } + + f.SetMapIndex(keyV[0].Convert(keyType), valueV[0].Convert(valueType)) + + return nil +} + func populateRepeatedField(f reflect.Value, values []string, props *proto.Properties) error { elemType := f.Type().Elem() @@ -144,11 +199,13 @@ func populateRepeatedField(f reflect.Value, values []string, props *proto.Proper } func populateField(f reflect.Value, value string, props *proto.Properties) error { - // Handle well known type + i := f.Addr().Interface() + + // Handle protobuf well known types type wkt interface { XXX_WellKnownType() string } - if wkt, ok := f.Addr().Interface().(wkt); ok { + if wkt, ok := i.(wkt); ok { switch wkt.XXX_WellKnownType() { case "Timestamp": if value == "null" { @@ -203,6 +260,27 @@ func populateField(f reflect.Value, value string, props *proto.Properties) error case "StringValue": f.Field(0).SetString(value) return nil + case "BytesValue": + bytesVal, err := base64.StdEncoding.DecodeString(value) + if err != nil { + return fmt.Errorf("bad BytesValue: %s", value) + } + f.Field(0).SetBytes(bytesVal) + return nil + } + } + + // Handle google well known types + if gwkt, ok := i.(proto.Message); ok { + switch proto.MessageName(gwkt) { + case "google.protobuf.FieldMask": + p := f.Field(0) + for _, v := range strings.Split(value, ",") { + if v != "" { + p.Set(reflect.Append(p, reflect.ValueOf(v))) + } + } + return nil } } @@ -274,6 +352,6 @@ var ( reflect.Int32: reflect.ValueOf(Int32), reflect.Uint64: reflect.ValueOf(Uint64), reflect.Uint32: reflect.ValueOf(Uint32), - // TODO(yugui) Support []byte + reflect.Slice: reflect.ValueOf(Bytes), } ) diff --git a/vendor/github.com/grpc-ecosystem/grpc-gateway/utilities/pattern.go b/vendor/github.com/grpc-ecosystem/grpc-gateway/utilities/pattern.go index 28ad9461f86..dfe7de4864a 100644 --- a/vendor/github.com/grpc-ecosystem/grpc-gateway/utilities/pattern.go +++ b/vendor/github.com/grpc-ecosystem/grpc-gateway/utilities/pattern.go @@ -17,6 +17,6 @@ const ( OpConcatN // OpCapture pops an item and binds it to the variable OpCapture - // OpEnd is the least postive invalid opcode. + // OpEnd is the least positive invalid opcode. OpEnd )