From 063f96e3cfad9dc5c47cb6bf6bc17e8c087452d4 Mon Sep 17 00:00:00 2001 From: Mayank Date: Mon, 19 Nov 2018 16:39:37 +0530 Subject: [PATCH] Added support for more WKT (#816) * Added support for more WKT * Lint fix: added comments on exported functions * Lint fix: added comments on exported functions * Added end to end example to show support of StringValue in url option template * updated more example protos; removed server and proxy for now * Updated convert.go to fix float and other wkt * brazel build fix --- examples/pbwrappers/helloworld/BUILD.bazel | 39 + .../pbwrappers/helloworld/helloworld.pb.go | 288 ++++++++ .../pbwrappers/helloworld/helloworld.pb.gw.go | 664 ++++++++++++++++++ .../pbwrappers/helloworld/helloworld.proto | 58 ++ protoc-gen-grpc-gateway/descriptor/types.go | 9 + runtime/BUILD.bazel | 1 + runtime/convert.go | 58 ++ 7 files changed, 1117 insertions(+) create mode 100644 examples/pbwrappers/helloworld/BUILD.bazel create mode 100644 examples/pbwrappers/helloworld/helloworld.pb.go create mode 100644 examples/pbwrappers/helloworld/helloworld.pb.gw.go create mode 100644 examples/pbwrappers/helloworld/helloworld.proto diff --git a/examples/pbwrappers/helloworld/BUILD.bazel b/examples/pbwrappers/helloworld/BUILD.bazel new file mode 100644 index 00000000000..0b6c67f8074 --- /dev/null +++ b/examples/pbwrappers/helloworld/BUILD.bazel @@ -0,0 +1,39 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") + +proto_library( + name = "helloworld_proto", + srcs = ["helloworld.proto"], + visibility = ["//visibility:public"], + deps = [ + "@com_google_protobuf//:wrappers_proto", + "@go_googleapis//google/api:annotations_proto", + ], +) + +go_proto_library( + name = "helloworld_go_proto", + compilers = ["@io_bazel_rules_go//proto:go_grpc"], + importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/pbwrappers/helloworld", + proto = ":helloworld_proto", + visibility = ["//visibility:public"], + deps = ["@go_googleapis//google/api:annotations_go_proto"], +) + +go_library( + name = "go_default_library", + srcs = ["helloworld.pb.gw.go"], + embed = [":helloworld_go_proto"], + importpath = "github.com/grpc-ecosystem/grpc-gateway/examples/pbwrappers/helloworld", + visibility = ["//visibility:public"], + deps = [ + "//runtime:go_default_library", + "//utilities:go_default_library", + "@com_github_golang_protobuf//proto:go_default_library", + "@org_golang_google_grpc//:go_default_library", + "@org_golang_google_grpc//codes:go_default_library", + "@org_golang_google_grpc//grpclog:go_default_library", + "@org_golang_google_grpc//status:go_default_library", + "@org_golang_x_net//context:go_default_library", + ], +) diff --git a/examples/pbwrappers/helloworld/helloworld.pb.go b/examples/pbwrappers/helloworld/helloworld.pb.go new file mode 100644 index 00000000000..9225dfd1f88 --- /dev/null +++ b/examples/pbwrappers/helloworld/helloworld.pb.go @@ -0,0 +1,288 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: helloworld.proto + +package helloworld + +import ( + context "context" + fmt "fmt" + proto "github.com/golang/protobuf/proto" + wrappers "github.com/golang/protobuf/ptypes/wrappers" + _ "google.golang.org/genproto/googleapis/api/annotations" + grpc "google.golang.org/grpc" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type HelloRequest struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + StrVal *wrappers.StringValue `protobuf:"bytes,2,opt,name=strVal,proto3" json:"strVal,omitempty"` + FloatVal *wrappers.FloatValue `protobuf:"bytes,3,opt,name=floatVal,proto3" json:"floatVal,omitempty"` + DoubleVal *wrappers.DoubleValue `protobuf:"bytes,4,opt,name=doubleVal,proto3" json:"doubleVal,omitempty"` + BoolVal *wrappers.BoolValue `protobuf:"bytes,5,opt,name=boolVal,proto3" json:"boolVal,omitempty"` + BytesVal *wrappers.BytesValue `protobuf:"bytes,6,opt,name=bytesVal,proto3" json:"bytesVal,omitempty"` + Int32Val *wrappers.Int32Value `protobuf:"bytes,7,opt,name=int32Val,proto3" json:"int32Val,omitempty"` + Uint32Val *wrappers.UInt32Value `protobuf:"bytes,8,opt,name=uint32Val,proto3" json:"uint32Val,omitempty"` + Int64Val *wrappers.Int64Value `protobuf:"bytes,9,opt,name=int64Val,proto3" json:"int64Val,omitempty"` + Uint64Val *wrappers.UInt64Value `protobuf:"bytes,10,opt,name=uint64Val,proto3" json:"uint64Val,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *HelloRequest) Reset() { *m = HelloRequest{} } +func (m *HelloRequest) String() string { return proto.CompactTextString(m) } +func (*HelloRequest) ProtoMessage() {} +func (*HelloRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_17b8c58d586b62f2, []int{0} +} + +func (m *HelloRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_HelloRequest.Unmarshal(m, b) +} +func (m *HelloRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_HelloRequest.Marshal(b, m, deterministic) +} +func (m *HelloRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_HelloRequest.Merge(m, src) +} +func (m *HelloRequest) XXX_Size() int { + return xxx_messageInfo_HelloRequest.Size(m) +} +func (m *HelloRequest) XXX_DiscardUnknown() { + xxx_messageInfo_HelloRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_HelloRequest proto.InternalMessageInfo + +func (m *HelloRequest) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +func (m *HelloRequest) GetStrVal() *wrappers.StringValue { + if m != nil { + return m.StrVal + } + return nil +} + +func (m *HelloRequest) GetFloatVal() *wrappers.FloatValue { + if m != nil { + return m.FloatVal + } + return nil +} + +func (m *HelloRequest) GetDoubleVal() *wrappers.DoubleValue { + if m != nil { + return m.DoubleVal + } + return nil +} + +func (m *HelloRequest) GetBoolVal() *wrappers.BoolValue { + if m != nil { + return m.BoolVal + } + return nil +} + +func (m *HelloRequest) GetBytesVal() *wrappers.BytesValue { + if m != nil { + return m.BytesVal + } + return nil +} + +func (m *HelloRequest) GetInt32Val() *wrappers.Int32Value { + if m != nil { + return m.Int32Val + } + return nil +} + +func (m *HelloRequest) GetUint32Val() *wrappers.UInt32Value { + if m != nil { + return m.Uint32Val + } + return nil +} + +func (m *HelloRequest) GetInt64Val() *wrappers.Int64Value { + if m != nil { + return m.Int64Val + } + return nil +} + +func (m *HelloRequest) GetUint64Val() *wrappers.UInt64Value { + if m != nil { + return m.Uint64Val + } + return nil +} + +type HelloReply struct { + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *HelloReply) Reset() { *m = HelloReply{} } +func (m *HelloReply) String() string { return proto.CompactTextString(m) } +func (*HelloReply) ProtoMessage() {} +func (*HelloReply) Descriptor() ([]byte, []int) { + return fileDescriptor_17b8c58d586b62f2, []int{1} +} + +func (m *HelloReply) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_HelloReply.Unmarshal(m, b) +} +func (m *HelloReply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_HelloReply.Marshal(b, m, deterministic) +} +func (m *HelloReply) XXX_Merge(src proto.Message) { + xxx_messageInfo_HelloReply.Merge(m, src) +} +func (m *HelloReply) XXX_Size() int { + return xxx_messageInfo_HelloReply.Size(m) +} +func (m *HelloReply) XXX_DiscardUnknown() { + xxx_messageInfo_HelloReply.DiscardUnknown(m) +} + +var xxx_messageInfo_HelloReply proto.InternalMessageInfo + +func (m *HelloReply) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func init() { + proto.RegisterType((*HelloRequest)(nil), "helloworld.HelloRequest") + proto.RegisterType((*HelloReply)(nil), "helloworld.HelloReply") +} + +func init() { proto.RegisterFile("helloworld.proto", fileDescriptor_17b8c58d586b62f2) } + +var fileDescriptor_17b8c58d586b62f2 = []byte{ + // 468 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0xcd, 0x8a, 0xd4, 0x40, + 0x14, 0x85, 0xe9, 0xd8, 0xf6, 0x4f, 0x8d, 0x0b, 0x29, 0xa4, 0x09, 0xb1, 0x91, 0xa1, 0x17, 0x32, + 0xab, 0x04, 0x7a, 0x9a, 0x11, 0x5c, 0x0e, 0xe2, 0xcf, 0x36, 0x83, 0xb3, 0xe8, 0x5d, 0x85, 0xb9, + 0xd3, 0x06, 0x6a, 0x52, 0xb1, 0x7e, 0x1c, 0xc2, 0x90, 0x8d, 0x3b, 0x77, 0x82, 0xef, 0xe0, 0xeb, + 0xb8, 0x70, 0xe9, 0xd6, 0x07, 0x91, 0xba, 0xa9, 0x4a, 0x45, 0xc6, 0xb8, 0x4b, 0xe5, 0x9c, 0xef, + 0x9e, 0x4a, 0xee, 0x21, 0x8f, 0x3f, 0x00, 0xe7, 0xe2, 0x56, 0x48, 0x7e, 0x95, 0xd6, 0x52, 0x68, + 0x41, 0x49, 0x78, 0x93, 0xac, 0x0f, 0x42, 0x1c, 0x38, 0x64, 0xac, 0x2e, 0x33, 0x56, 0x55, 0x42, + 0x33, 0x5d, 0x8a, 0x4a, 0x75, 0xce, 0xe4, 0x99, 0x53, 0xf1, 0x54, 0x98, 0xeb, 0xec, 0x56, 0xb2, + 0xba, 0x06, 0xe9, 0xf4, 0xcd, 0xf7, 0x29, 0x79, 0xf4, 0xd6, 0x0e, 0xcb, 0xe1, 0xa3, 0x01, 0xa5, + 0x29, 0x25, 0xd3, 0x8a, 0xdd, 0x40, 0x3c, 0x39, 0x9e, 0x9c, 0x2c, 0x73, 0x7c, 0xa6, 0x3b, 0x32, + 0x53, 0x5a, 0x5e, 0x32, 0x1e, 0x47, 0xc7, 0x93, 0x93, 0xa3, 0xed, 0x3a, 0xed, 0xa6, 0xa6, 0x7e, + 0x6a, 0x7a, 0xa1, 0x65, 0x59, 0x1d, 0x2e, 0x19, 0x37, 0x90, 0x3b, 0x2f, 0x7d, 0x41, 0x16, 0xd7, + 0x5c, 0x30, 0x6d, 0xb9, 0x07, 0xc8, 0x3d, 0xbd, 0xc7, 0xbd, 0x76, 0x06, 0x03, 0x79, 0x6f, 0xa6, + 0x2f, 0xc9, 0xf2, 0x4a, 0x98, 0x82, 0x83, 0x25, 0xa7, 0x23, 0x89, 0xaf, 0xbc, 0xc3, 0x40, 0x1e, + 0xec, 0x74, 0x47, 0xe6, 0x85, 0x10, 0xdc, 0x92, 0x0f, 0x91, 0x4c, 0xee, 0x91, 0xe7, 0x9d, 0x6e, + 0x20, 0xf7, 0x56, 0x7b, 0xd5, 0xa2, 0xd1, 0xa0, 0x2c, 0x36, 0x1b, 0xb9, 0xea, 0xb9, 0x33, 0xd8, + 0xab, 0x7a, 0xb3, 0x05, 0xcb, 0x4a, 0x9f, 0x6e, 0x2d, 0x38, 0x1f, 0x01, 0xdf, 0x39, 0x83, 0x05, + 0xbd, 0xd9, 0x7e, 0xa3, 0xe9, 0xc9, 0xc5, 0xc8, 0x37, 0xbe, 0x1f, 0xa0, 0xc1, 0xee, 0x42, 0xcf, + 0x76, 0x16, 0x5d, 0x8e, 0x87, 0xa2, 0xc1, 0x85, 0xe2, 0xb3, 0x0f, 0xed, 0x48, 0xf2, 0x9f, 0x50, + 0x8f, 0x06, 0xfb, 0xe6, 0x39, 0x21, 0xae, 0x27, 0x35, 0x6f, 0x68, 0x4c, 0xe6, 0x37, 0xa0, 0x14, + 0x3b, 0xf8, 0xa2, 0xf8, 0xe3, 0xf6, 0x57, 0x44, 0xe6, 0x6f, 0x24, 0x80, 0x06, 0x49, 0x7f, 0x44, + 0x64, 0x71, 0xc1, 0x1a, 0xe4, 0x68, 0x9c, 0x0e, 0x6a, 0x3c, 0xac, 0x5c, 0xb2, 0xfa, 0x87, 0x52, + 0xf3, 0x66, 0xf3, 0x35, 0xfa, 0xfc, 0xf3, 0xf7, 0xb7, 0xe8, 0x4b, 0x44, 0x8f, 0x32, 0xc5, 0x9a, + 0xec, 0xce, 0x96, 0xb1, 0xdd, 0xaf, 0xe8, 0x13, 0x3c, 0x2a, 0x2d, 0x3f, 0x31, 0x9e, 0xdd, 0x75, + 0x75, 0x6b, 0xf7, 0x09, 0x8d, 0xf1, 0x3d, 0xf6, 0x08, 0x15, 0xdf, 0xa8, 0x76, 0xbf, 0xa6, 0x09, + 0x6a, 0x5d, 0x51, 0x50, 0xec, 0x3b, 0xd3, 0xee, 0x63, 0xba, 0x42, 0xd5, 0xd6, 0x01, 0x35, 0xd7, + 0x8b, 0x30, 0x13, 0x17, 0xde, 0x49, 0x6e, 0xf5, 0x41, 0xc3, 0xbd, 0xa0, 0xe6, 0x37, 0x14, 0xf2, + 0x4c, 0x10, 0x4d, 0x50, 0x07, 0xe4, 0xd9, 0xce, 0x93, 0xf8, 0x9b, 0xff, 0x26, 0x9d, 0xd8, 0x2f, + 0xa1, 0x2d, 0x66, 0xb8, 0xa6, 0xd3, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x65, 0xa5, 0x95, 0xa8, + 0x13, 0x04, 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// GreeterClient is the client API for Greeter service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type GreeterClient interface { + SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) +} + +type greeterClient struct { + cc *grpc.ClientConn +} + +func NewGreeterClient(cc *grpc.ClientConn) GreeterClient { + return &greeterClient{cc} +} + +func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) { + out := new(HelloReply) + err := c.cc.Invoke(ctx, "/helloworld.Greeter/SayHello", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GreeterServer is the server API for Greeter service. +type GreeterServer interface { + SayHello(context.Context, *HelloRequest) (*HelloReply, error) +} + +func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) { + s.RegisterService(&_Greeter_serviceDesc, srv) +} + +func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(HelloRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GreeterServer).SayHello(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/helloworld.Greeter/SayHello", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Greeter_serviceDesc = grpc.ServiceDesc{ + ServiceName: "helloworld.Greeter", + HandlerType: (*GreeterServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "SayHello", + Handler: _Greeter_SayHello_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "helloworld.proto", +} diff --git a/examples/pbwrappers/helloworld/helloworld.pb.gw.go b/examples/pbwrappers/helloworld/helloworld.pb.gw.go new file mode 100644 index 00000000000..c09fe741ece --- /dev/null +++ b/examples/pbwrappers/helloworld/helloworld.pb.gw.go @@ -0,0 +1,664 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: helloworld.proto + +/* +Package helloworld is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package helloworld + +import ( + "io" + "net/http" + + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray + +var ( + filter_Greeter_SayHello_0 = &utilities.DoubleArray{Encoding: map[string]int{"name": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_0(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["name"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "name") + } + + protoReq.Name, err = runtime.String(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "name", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_1 = &utilities.DoubleArray{Encoding: map[string]int{"strVal": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_1(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["strVal"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "strVal") + } + + protoReq.StrVal, err = runtime.StringValue(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "strVal", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_1); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_2 = &utilities.DoubleArray{Encoding: map[string]int{"floatVal": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_2(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["floatVal"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "floatVal") + } + + protoReq.FloatVal, err = runtime.FloatValue(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "floatVal", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_2); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_3 = &utilities.DoubleArray{Encoding: map[string]int{"doubleVal": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_3(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["doubleVal"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "doubleVal") + } + + protoReq.DoubleVal, err = runtime.DoubleValue(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "doubleVal", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_3); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_4 = &utilities.DoubleArray{Encoding: map[string]int{"boolVal": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_4(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["boolVal"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "boolVal") + } + + protoReq.BoolVal, err = runtime.BoolValue(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "boolVal", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_4); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_5 = &utilities.DoubleArray{Encoding: map[string]int{"bytesVal": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_5(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["bytesVal"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "bytesVal") + } + + protoReq.BytesVal, err = runtime.BytesValue(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "bytesVal", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_5); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_6 = &utilities.DoubleArray{Encoding: map[string]int{"int32Val": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_6(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["int32Val"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "int32Val") + } + + protoReq.Int32Val, err = runtime.Int32Value(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "int32Val", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_6); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_7 = &utilities.DoubleArray{Encoding: map[string]int{"uint32Val": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_7(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["uint32Val"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "uint32Val") + } + + protoReq.Uint32Val, err = runtime.UInt32Value(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uint32Val", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_7); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_8 = &utilities.DoubleArray{Encoding: map[string]int{"int64Val": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_8(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["int64Val"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "int64Val") + } + + protoReq.Int64Val, err = runtime.Int64Value(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "int64Val", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_8); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +var ( + filter_Greeter_SayHello_9 = &utilities.DoubleArray{Encoding: map[string]int{"uint64Val": 0}, Base: []int{1, 1, 0}, Check: []int{0, 1, 2}} +) + +func request_Greeter_SayHello_9(ctx context.Context, marshaler runtime.Marshaler, client GreeterClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HelloRequest + var metadata runtime.ServerMetadata + + var ( + val string + ok bool + err error + _ = err + ) + + val, ok = pathParams["uint64Val"] + if !ok { + return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", "uint64Val") + } + + protoReq.Uint64Val, err = runtime.UInt64Value(val) + + if err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", "uint64Val", err) + } + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Greeter_SayHello_9); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.SayHello(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +// RegisterGreeterHandlerFromEndpoint is same as RegisterGreeterHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterGreeterHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterGreeterHandler(ctx, mux, conn) +} + +// RegisterGreeterHandler registers the http handlers for service Greeter to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterGreeterHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterGreeterHandlerClient(ctx, mux, NewGreeterClient(conn)) +} + +// RegisterGreeterHandlerClient registers the http handlers for service Greeter +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "GreeterClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "GreeterClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "GreeterClient" to call the correct interceptors. +func RegisterGreeterHandlerClient(ctx context.Context, mux *runtime.ServeMux, client GreeterClient) error { + + mux.Handle("GET", pattern_Greeter_SayHello_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_1, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_1(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_1(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_2, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_2(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_2(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_3, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_3(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_3(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_4, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_4(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_4(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_5, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_5(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_5(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_6, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_6(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_6(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_7, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_7(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_7(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_8, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_8(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_8(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_Greeter_SayHello_9, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Greeter_SayHello_9(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Greeter_SayHello_9(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Greeter_SayHello_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 1, 0, 4, 1, 5, 1}, []string{"say", "name"}, "")) + + pattern_Greeter_SayHello_1 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "strval", "strVal"}, "")) + + pattern_Greeter_SayHello_2 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "floatval", "floatVal"}, "")) + + pattern_Greeter_SayHello_3 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "doubleval", "doubleVal"}, "")) + + pattern_Greeter_SayHello_4 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "boolval", "boolVal"}, "")) + + pattern_Greeter_SayHello_5 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "bytesval", "bytesVal"}, "")) + + pattern_Greeter_SayHello_6 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "int32val", "int32Val"}, "")) + + pattern_Greeter_SayHello_7 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "uint32val", "uint32Val"}, "")) + + pattern_Greeter_SayHello_8 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "int64val", "int64Val"}, "")) + + pattern_Greeter_SayHello_9 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"say", "uint64val", "uint64Val"}, "")) +) + +var ( + forward_Greeter_SayHello_0 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_1 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_2 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_3 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_4 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_5 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_6 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_7 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_8 = runtime.ForwardResponseMessage + + forward_Greeter_SayHello_9 = runtime.ForwardResponseMessage +) diff --git a/examples/pbwrappers/helloworld/helloworld.proto b/examples/pbwrappers/helloworld/helloworld.proto new file mode 100644 index 00000000000..a685c65b8bc --- /dev/null +++ b/examples/pbwrappers/helloworld/helloworld.proto @@ -0,0 +1,58 @@ +syntax = "proto3"; + +package helloworld; + +import "google/api/annotations.proto"; +import "google/protobuf/wrappers.proto"; + +service Greeter { + rpc SayHello (HelloRequest) returns (HelloReply) { + option (google.api.http) = { + get: "/say/{name}" + additional_bindings: { + get: "/say/strval/{strVal}", + } + additional_bindings: { + get: "/say/floatval/{floatVal}", + } + additional_bindings: { + get: "/say/doubleval/{doubleVal}", + } + additional_bindings: { + get: "/say/boolval/{boolVal}", + } + additional_bindings: { + get: "/say/bytesval/{bytesVal}", + } + additional_bindings: { + get: "/say/int32val/{int32Val}", + } + additional_bindings: { + get: "/say/uint32val/{uint32Val}", + } + additional_bindings: { + get: "/say/int64val/{int64Val}", + } + additional_bindings: { + get: "/say/uint64val/{uint64Val}", + } + }; + } +} + +message HelloRequest { + string name = 1; + google.protobuf.StringValue strVal = 2; + google.protobuf.FloatValue floatVal = 3; + google.protobuf.DoubleValue doubleVal = 4; + google.protobuf.BoolValue boolVal = 5; + google.protobuf.BytesValue bytesVal = 6; + google.protobuf.Int32Value int32Val = 7; + google.protobuf.UInt32Value uint32Val = 8; + google.protobuf.Int64Value int64Val = 9; + google.protobuf.UInt64Value uint64Val = 10; +} + +message HelloReply { + string message = 1; +} diff --git a/protoc-gen-grpc-gateway/descriptor/types.go b/protoc-gen-grpc-gateway/descriptor/types.go index a412b2bcf67..4aa75f85dbf 100644 --- a/protoc-gen-grpc-gateway/descriptor/types.go +++ b/protoc-gen-grpc-gateway/descriptor/types.go @@ -453,5 +453,14 @@ var ( wellKnownTypeConv = map[string]string{ ".google.protobuf.Timestamp": "runtime.Timestamp", ".google.protobuf.Duration": "runtime.Duration", + ".google.protobuf.StringValue": "runtime.StringValue", + ".google.protobuf.FloatValue": "runtime.FloatValue", + ".google.protobuf.DoubleValue": "runtime.DoubleValue", + ".google.protobuf.BoolValue": "runtime.BoolValue", + ".google.protobuf.BytesValue": "runtime.BytesValue", + ".google.protobuf.Int32Value": "runtime.Int32Value", + ".google.protobuf.UInt32Value": "runtime.UInt32Value", + ".google.protobuf.Int64Value": "runtime.Int64Value", + ".google.protobuf.UInt64Value": "runtime.UInt64Value", } ) diff --git a/runtime/BUILD.bazel b/runtime/BUILD.bazel index 66a8999f2a2..252c4527853 100644 --- a/runtime/BUILD.bazel +++ b/runtime/BUILD.bazel @@ -30,6 +30,7 @@ go_library( "@io_bazel_rules_go//proto/wkt:any_go_proto", "@io_bazel_rules_go//proto/wkt:duration_go_proto", "@io_bazel_rules_go//proto/wkt:timestamp_go_proto", + "@io_bazel_rules_go//proto/wkt:wrappers_go_proto", "@org_golang_google_grpc//codes:go_default_library", "@org_golang_google_grpc//grpclog:go_default_library", "@org_golang_google_grpc//metadata:go_default_library", diff --git a/runtime/convert.go b/runtime/convert.go index bd54ba12bec..a5b3bd6a792 100644 --- a/runtime/convert.go +++ b/runtime/convert.go @@ -9,6 +9,7 @@ import ( "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/ptypes/duration" "github.com/golang/protobuf/ptypes/timestamp" + "github.com/golang/protobuf/ptypes/wrappers" ) // String just returns the given string. @@ -252,3 +253,60 @@ func EnumSlice(val, sep string, enumValMap map[string]int32) ([]int32, error) { } return values, nil } + +/* + Support fot google.protobuf.wrappers on top of primitive types +*/ + +// StringValue well-known type support as wrapper around string type +func StringValue(val string) (*wrappers.StringValue, error) { + return &wrappers.StringValue{Value: val}, nil +} + +// FloatValue well-known type support as wrapper around float32 type +func FloatValue(val string) (*wrappers.FloatValue, error) { + parsedVal, err := Float32(val) + return &wrappers.FloatValue{Value: parsedVal}, err +} + +// DoubleValue well-known type support as wrapper around float64 type +func DoubleValue(val string) (*wrappers.DoubleValue, error) { + parsedVal, err := Float64(val) + return &wrappers.DoubleValue{Value: parsedVal}, err +} + +// BoolValue well-known type support as wrapper around bool type +func BoolValue(val string) (*wrappers.BoolValue, error) { + parsedVal, err := Bool(val) + return &wrappers.BoolValue{Value: parsedVal}, err +} + +// Int32Value well-known type support as wrapper around int32 type +func Int32Value(val string) (*wrappers.Int32Value, error) { + parsedVal, err := Int32(val) + return &wrappers.Int32Value{Value: parsedVal}, err +} + +// UInt32Value well-known type support as wrapper around uint32 type +func UInt32Value(val string) (*wrappers.UInt32Value, error) { + parsedVal, err := Uint32(val) + return &wrappers.UInt32Value{Value: parsedVal}, err +} + +// Int64Value well-known type support as wrapper around int64 type +func Int64Value(val string) (*wrappers.Int64Value, error) { + parsedVal, err := Int64(val) + return &wrappers.Int64Value{Value: parsedVal}, err +} + +// UInt64Value well-known type support as wrapper around uint64 type +func UInt64Value(val string) (*wrappers.UInt64Value, error) { + parsedVal, err := Uint64(val) + return &wrappers.UInt64Value{Value: parsedVal}, err +} + +// BytesValue well-known type support as wrapper around bytes[] type +func BytesValue(val string) (*wrappers.BytesValue, error) { + parsedVal, err := Bytes(val) + return &wrappers.BytesValue{Value: parsedVal}, err +}