diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml new file mode 100644 index 00000000..e01104ac --- /dev/null +++ b/.github/workflows/integration-test.yml @@ -0,0 +1,30 @@ +name: Integration test +on: + pull_request: + branches: + master + + +jobs: + build: + name: Run example + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@master + - name: Run Simple Example + uses: ./ + with: + entrypoint: .github/workflows/run-example.sh + args: simple + - name: Run Stream Example + uses: ./ + with: + entrypoint: .github/workflows/run-example.sh + args: stream + - name: Run WKT Example + uses: ./ + with: + entrypoint: .github/workflows/run-example.sh + args: well_known_types + diff --git a/.github/workflows/run-example.sh b/.github/workflows/run-example.sh new file mode 100755 index 00000000..43453e4f --- /dev/null +++ b/.github/workflows/run-example.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env sh + +gripmock --stub=example/$1/stub example/$1/$1.proto & + +go run example/$1/client/*.go \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 3d2682d3..945048c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,8 +28,6 @@ RUN mv /protobuf-repo/src/ /protobuf/ RUN rm -rf /protobuf-repo -RUN apk del git - RUN mkdir -p /go/src/github.com/tokopedia/gripmock COPY . /go/src/github.com/tokopedia/gripmock @@ -48,8 +46,6 @@ WORKDIR /go/src/github.com/tokopedia/gripmock # install gripmock RUN go install -v -RUN rm -rf * - EXPOSE 4770 4771 ENTRYPOINT ["gripmock"] \ No newline at end of file diff --git a/build.sh b/build.sh index 23beb438..91dd9f0d 100755 --- a/build.sh +++ b/build.sh @@ -7,4 +7,4 @@ fi go build ../. -docker build -t "ahmadmuzakki/gripmock:$1" . \ No newline at end of file +docker build -t "tkpd/gripmock:$1" . \ No newline at end of file diff --git a/example/client/go/hello.pb.go b/example/client/go/hello.pb.go deleted file mode 100644 index f94d04b3..00000000 --- a/example/client/go/hello.pb.go +++ /dev/null @@ -1,440 +0,0 @@ -// Code generated by protoc-gen-go. DO NOT EDIT. -// source: pb/hello.proto - -package main - -import proto "github.com/golang/protobuf/proto" -import fmt "fmt" -import math "math" -import empty "github.com/golang/protobuf/ptypes/empty" -import wrappers "github.com/golang/protobuf/ptypes/wrappers" - -import ( - context "golang.org/x/net/context" - grpc "google.golang.org/grpc" -) - -// 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 - -// The request message containing the user's name. -type Request struct { - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` -} - -func (m *Request) Reset() { *m = Request{} } -func (m *Request) String() string { return proto.CompactTextString(m) } -func (*Request) ProtoMessage() {} -func (*Request) Descriptor() ([]byte, []int) { - return fileDescriptor_hello_64609aa425866106, []int{0} -} -func (m *Request) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_Request.Unmarshal(m, b) -} -func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_Request.Marshal(b, m, deterministic) -} -func (dst *Request) XXX_Merge(src proto.Message) { - xxx_messageInfo_Request.Merge(dst, src) -} -func (m *Request) XXX_Size() int { - return xxx_messageInfo_Request.Size(m) -} -func (m *Request) XXX_DiscardUnknown() { - xxx_messageInfo_Request.DiscardUnknown(m) -} - -var xxx_messageInfo_Request proto.InternalMessageInfo - -func (m *Request) GetName() string { - if m != nil { - return m.Name - } - return "" -} - -// The response message containing the greetings -type Reply 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 *Reply) Reset() { *m = Reply{} } -func (m *Reply) String() string { return proto.CompactTextString(m) } -func (*Reply) ProtoMessage() {} -func (*Reply) Descriptor() ([]byte, []int) { - return fileDescriptor_hello_64609aa425866106, []int{1} -} -func (m *Reply) XXX_Unmarshal(b []byte) error { - return xxx_messageInfo_Reply.Unmarshal(m, b) -} -func (m *Reply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - return xxx_messageInfo_Reply.Marshal(b, m, deterministic) -} -func (dst *Reply) XXX_Merge(src proto.Message) { - xxx_messageInfo_Reply.Merge(dst, src) -} -func (m *Reply) XXX_Size() int { - return xxx_messageInfo_Reply.Size(m) -} -func (m *Reply) XXX_DiscardUnknown() { - xxx_messageInfo_Reply.DiscardUnknown(m) -} - -var xxx_messageInfo_Reply proto.InternalMessageInfo - -func (m *Reply) GetMessage() string { - if m != nil { - return m.Message - } - return "" -} - -func init() { - proto.RegisterType((*Request)(nil), "main.Request") - proto.RegisterType((*Reply)(nil), "main.Reply") -} - -// 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 - -// GripmockClient is the client API for Gripmock service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. -type GripmockClient interface { - // standard grpc method - SayHello(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) - // server to client sreaming - ServerStream(ctx context.Context, in *Request, opts ...grpc.CallOption) (Gripmock_ServerStreamClient, error) - // client to server streaming - ClientStream(ctx context.Context, opts ...grpc.CallOption) (Gripmock_ClientStreamClient, error) - // bidirectional streaming - Bidirectional(ctx context.Context, opts ...grpc.CallOption) (Gripmock_BidirectionalClient, error) - // using well-known-type of Empty - HealthCheck(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*wrappers.StringValue, error) -} - -type gripmockClient struct { - cc *grpc.ClientConn -} - -func NewGripmockClient(cc *grpc.ClientConn) GripmockClient { - return &gripmockClient{cc} -} - -func (c *gripmockClient) SayHello(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { - out := new(Reply) - err := c.cc.Invoke(ctx, "/main.Gripmock/SayHello", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *gripmockClient) ServerStream(ctx context.Context, in *Request, opts ...grpc.CallOption) (Gripmock_ServerStreamClient, error) { - stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[0], "/main.Gripmock/serverStream", opts...) - if err != nil { - return nil, err - } - x := &gripmockServerStreamClient{stream} - if err := x.ClientStream.SendMsg(in); err != nil { - return nil, err - } - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - return x, nil -} - -type Gripmock_ServerStreamClient interface { - Recv() (*Reply, error) - grpc.ClientStream -} - -type gripmockServerStreamClient struct { - grpc.ClientStream -} - -func (x *gripmockServerStreamClient) Recv() (*Reply, error) { - m := new(Reply) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func (c *gripmockClient) ClientStream(ctx context.Context, opts ...grpc.CallOption) (Gripmock_ClientStreamClient, error) { - stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[1], "/main.Gripmock/clientStream", opts...) - if err != nil { - return nil, err - } - x := &gripmockClientStreamClient{stream} - return x, nil -} - -type Gripmock_ClientStreamClient interface { - Send(*Request) error - CloseAndRecv() (*Reply, error) - grpc.ClientStream -} - -type gripmockClientStreamClient struct { - grpc.ClientStream -} - -func (x *gripmockClientStreamClient) Send(m *Request) error { - return x.ClientStream.SendMsg(m) -} - -func (x *gripmockClientStreamClient) CloseAndRecv() (*Reply, error) { - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - m := new(Reply) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func (c *gripmockClient) Bidirectional(ctx context.Context, opts ...grpc.CallOption) (Gripmock_BidirectionalClient, error) { - stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[2], "/main.Gripmock/bidirectional", opts...) - if err != nil { - return nil, err - } - x := &gripmockBidirectionalClient{stream} - return x, nil -} - -type Gripmock_BidirectionalClient interface { - Send(*Request) error - Recv() (*Reply, error) - grpc.ClientStream -} - -type gripmockBidirectionalClient struct { - grpc.ClientStream -} - -func (x *gripmockBidirectionalClient) Send(m *Request) error { - return x.ClientStream.SendMsg(m) -} - -func (x *gripmockBidirectionalClient) Recv() (*Reply, error) { - m := new(Reply) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func (c *gripmockClient) HealthCheck(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*wrappers.StringValue, error) { - out := new(wrappers.StringValue) - err := c.cc.Invoke(ctx, "/main.Gripmock/HealthCheck", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// GripmockServer is the server API for Gripmock service. -type GripmockServer interface { - // standard grpc method - SayHello(context.Context, *Request) (*Reply, error) - // server to client sreaming - ServerStream(*Request, Gripmock_ServerStreamServer) error - // client to server streaming - ClientStream(Gripmock_ClientStreamServer) error - // bidirectional streaming - Bidirectional(Gripmock_BidirectionalServer) error - // using well-known-type of Empty - HealthCheck(context.Context, *empty.Empty) (*wrappers.StringValue, error) -} - -func RegisterGripmockServer(s *grpc.Server, srv GripmockServer) { - s.RegisterService(&_Gripmock_serviceDesc, srv) -} - -func _Gripmock_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(Request) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(GripmockServer).SayHello(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/main.Gripmock/SayHello", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(GripmockServer).SayHello(ctx, req.(*Request)) - } - return interceptor(ctx, in, info, handler) -} - -func _Gripmock_ServerStream_Handler(srv interface{}, stream grpc.ServerStream) error { - m := new(Request) - if err := stream.RecvMsg(m); err != nil { - return err - } - return srv.(GripmockServer).ServerStream(m, &gripmockServerStreamServer{stream}) -} - -type Gripmock_ServerStreamServer interface { - Send(*Reply) error - grpc.ServerStream -} - -type gripmockServerStreamServer struct { - grpc.ServerStream -} - -func (x *gripmockServerStreamServer) Send(m *Reply) error { - return x.ServerStream.SendMsg(m) -} - -func _Gripmock_ClientStream_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(GripmockServer).ClientStream(&gripmockClientStreamServer{stream}) -} - -type Gripmock_ClientStreamServer interface { - SendAndClose(*Reply) error - Recv() (*Request, error) - grpc.ServerStream -} - -type gripmockClientStreamServer struct { - grpc.ServerStream -} - -func (x *gripmockClientStreamServer) SendAndClose(m *Reply) error { - return x.ServerStream.SendMsg(m) -} - -func (x *gripmockClientStreamServer) Recv() (*Request, error) { - m := new(Request) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func _Gripmock_Bidirectional_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(GripmockServer).Bidirectional(&gripmockBidirectionalServer{stream}) -} - -type Gripmock_BidirectionalServer interface { - Send(*Reply) error - Recv() (*Request, error) - grpc.ServerStream -} - -type gripmockBidirectionalServer struct { - grpc.ServerStream -} - -func (x *gripmockBidirectionalServer) Send(m *Reply) error { - return x.ServerStream.SendMsg(m) -} - -func (x *gripmockBidirectionalServer) Recv() (*Request, error) { - m := new(Request) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func _Gripmock_HealthCheck_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(empty.Empty) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(GripmockServer).HealthCheck(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/main.Gripmock/HealthCheck", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(GripmockServer).HealthCheck(ctx, req.(*empty.Empty)) - } - return interceptor(ctx, in, info, handler) -} - -var _Gripmock_serviceDesc = grpc.ServiceDesc{ - ServiceName: "main.Gripmock", - HandlerType: (*GripmockServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "SayHello", - Handler: _Gripmock_SayHello_Handler, - }, - { - MethodName: "HealthCheck", - Handler: _Gripmock_HealthCheck_Handler, - }, - }, - Streams: []grpc.StreamDesc{ - { - StreamName: "serverStream", - Handler: _Gripmock_ServerStream_Handler, - ServerStreams: true, - }, - { - StreamName: "clientStream", - Handler: _Gripmock_ClientStream_Handler, - ClientStreams: true, - }, - { - StreamName: "bidirectional", - Handler: _Gripmock_Bidirectional_Handler, - ServerStreams: true, - ClientStreams: true, - }, - }, - Metadata: "pb/hello.proto", -} - -func init() { proto.RegisterFile("pb/hello.proto", fileDescriptor_hello_64609aa425866106) } - -var fileDescriptor_hello_64609aa425866106 = []byte{ - // 257 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x90, 0x4f, 0x4b, 0xc3, 0x40, - 0x14, 0xc4, 0x59, 0xa9, 0xb6, 0xbe, 0x5a, 0x0f, 0x7b, 0x90, 0x12, 0xff, 0xa0, 0x3d, 0x48, 0x0f, - 0xb2, 0x29, 0xfa, 0x11, 0x8a, 0xd8, 0x73, 0x02, 0xde, 0x37, 0xf1, 0x99, 0x2c, 0xdd, 0x7f, 0xee, - 0x6e, 0x94, 0xdc, 0xfd, 0xe0, 0xb2, 0x89, 0xb9, 0x28, 0xa4, 0xb7, 0xf7, 0x66, 0x7e, 0x0c, 0xc3, - 0xc0, 0xb9, 0x2d, 0xd2, 0x1a, 0xa5, 0x34, 0xcc, 0x3a, 0x13, 0x0c, 0x9d, 0x28, 0x2e, 0x74, 0x72, - 0x59, 0x19, 0x53, 0x49, 0x4c, 0x3b, 0xad, 0x68, 0xde, 0x53, 0x54, 0x36, 0xb4, 0x3d, 0x92, 0xdc, - 0xfc, 0x35, 0xbf, 0x1c, 0xb7, 0x16, 0x9d, 0xef, 0xfd, 0xd5, 0x35, 0x4c, 0x33, 0xfc, 0x68, 0xd0, - 0x07, 0x4a, 0x61, 0xa2, 0xb9, 0xc2, 0x25, 0xb9, 0x25, 0xeb, 0xd3, 0xac, 0xbb, 0x57, 0x77, 0x70, - 0x9c, 0xa1, 0x95, 0x2d, 0x5d, 0xc2, 0x54, 0xa1, 0xf7, 0xbc, 0x1a, 0xfc, 0xe1, 0x7d, 0xfc, 0x3e, - 0x82, 0xd9, 0x8b, 0x13, 0x56, 0x99, 0x72, 0x4f, 0xef, 0x61, 0x96, 0xf3, 0x76, 0x17, 0x3b, 0xd2, - 0x05, 0x8b, 0xf5, 0xd8, 0x6f, 0x7c, 0x32, 0x1f, 0xde, 0x18, 0xf7, 0x00, 0x67, 0x1e, 0xdd, 0x27, - 0xba, 0x3c, 0x38, 0xe4, 0x6a, 0x8c, 0xdd, 0x90, 0x48, 0x97, 0x52, 0xa0, 0x0e, 0x87, 0xe9, 0x35, - 0xa1, 0x29, 0x2c, 0x0a, 0xf1, 0x26, 0x1c, 0x96, 0x41, 0x18, 0xcd, 0xe5, 0x38, 0xbe, 0x21, 0x74, - 0x0b, 0xf3, 0x1d, 0x72, 0x19, 0xea, 0x6d, 0x8d, 0xe5, 0x9e, 0x5e, 0xb0, 0x7e, 0x33, 0x36, 0x6c, - 0xc6, 0x9e, 0xe3, 0xa0, 0xc9, 0xd5, 0x3f, 0x3d, 0x0f, 0x4e, 0xe8, 0xea, 0x95, 0xcb, 0x06, 0x8b, - 0x93, 0x4e, 0x7d, 0xfa, 0x09, 0x00, 0x00, 0xff, 0xff, 0x37, 0x73, 0xb5, 0x17, 0xa4, 0x01, 0x00, - 0x00, -} diff --git a/example/server/generated_server.go b/example/server/generated_server.go deleted file mode 100644 index 357796c1..00000000 --- a/example/server/generated_server.go +++ /dev/null @@ -1,140 +0,0 @@ -// Code generated by GripMock. DO NOT EDIT. -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "io/ioutil" - "log" - "net" - "net/http" - - "github.com/mitchellh/mapstructure" - "golang.org/x/net/context" - "google.golang.org/grpc" - "google.golang.org/grpc/reflection" -) - -const ( - TCP_ADDRESS = "" - HTTP_PORT = ":" -) - -type Gripmock struct{} - -func (s *Gripmock) SayHello(ctx context.Context, in *Request) (*Reply, error) { - out := &Reply{} - err := findStub("Gripmock", "SayHello", in, out) - return out, err -} - -func (s *Gripmock) ServerStream(in *Request, stream Gripmock_ServerStreamServer) error { - out := &Reply{} - err := findStub("Gripmock", "ServerStream", in, out) - if err != nil { - return err - } - - return stream.Send(out) -} - -func (s *Gripmock) ClientStream(stream Gripmock_ClientStreamServer) error { - out := &Reply{} - for { - input, err := stream.Recv() - if err == io.EOF { - return stream.SendAndClose(out) - } - err = findStub("Gripmock", "ClientStream", input, out) - if err != nil { - return err - } - } -} - -func (s *Gripmock) Bidirectional(stream Gripmock_BidirectionalServer) error { - for { - in, err := stream.Recv() - if err == io.EOF { - return nil - } - if err != nil { - return err - } - - out := &Reply{} - err = findStub("Gripmock", "Bidirectional", in, out) - if err != nil { - return err - } - - if err := stream.Send(out); err != nil { - return err - } - } -} - -func main() { - lis, err := net.Listen("tcp", TCP_ADDRESS) - if err != nil { - log.Fatalf("failed to listen: %v", err) - } - - s := grpc.NewServer() - - RegisterGripmockServer(s, &Gripmock{}) - - reflection.Register(s) - fmt.Println("Serving gRPC on tcp://" + TCP_ADDRESS) - if err := s.Serve(lis); err != nil { - log.Fatalf("failed to serve: %v", err) - } -} - -type payload struct { - Service string `json:"service"` - Method string `json:"method"` - Data interface{} `json:"data"` -} - -type response struct { - Data interface{} `json:"data"` - Error string `json:"error"` -} - -func findStub(service, method string, in, out interface{}) error { - url := fmt.Sprintf("http://localhost%s/find", HTTP_PORT) - pyl := payload{ - Service: service, - Method: method, - Data: in, - } - byt, err := json.Marshal(pyl) - if err != nil { - return err - } - reader := bytes.NewReader(byt) - resp, err := http.DefaultClient.Post(url, "application/json", reader) - if err != nil { - return fmt.Errorf("Error request to stub server %v", err) - } - - if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) - return fmt.Errorf(string(body)) - } - - respRPC := new(response) - err = json.NewDecoder(resp.Body).Decode(respRPC) - if err != nil { - return fmt.Errorf("decoding json response %v", err) - } - - if respRPC.Error != "" { - return fmt.Errorf(respRPC.Error) - } - - return mapstructure.Decode(respRPC.Data, out) -} diff --git a/example/simple/client/main.go b/example/simple/client/main.go new file mode 100644 index 00000000..990985d7 --- /dev/null +++ b/example/simple/client/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "context" + "log" + "os" + "time" + + pb "github.com/tokopedia/gripmock/example/simple" + "google.golang.org/grpc" +) + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + // Set up a connection to the server. + conn, err := grpc.DialContext(ctx, "localhost:4770", grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + + c := pb.NewGripmockClient(conn) + + // Contact the server and print out its response. + name := "tokopedia" + if len(os.Args) > 1 { + name = os.Args[1] + } + r, err := c.SayHello(context.Background(), &pb.Request{Name: name}) + if err != nil { + log.Fatalf("error from grpc: %v", err) + } + log.Printf("Greeting: %s", r.Message) +} diff --git a/example/simple/simple.pb.go b/example/simple/simple.pb.go new file mode 100644 index 00000000..ccb8fa77 --- /dev/null +++ b/example/simple/simple.pb.go @@ -0,0 +1,196 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: simple/simple.proto + +package simple + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// 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 + +// The request message containing the user's name. +type Request struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} +func (*Request) Descriptor() ([]byte, []int) { + return fileDescriptor_simple_e0ad890e6f6d32ea, []int{0} +} +func (m *Request) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Request.Unmarshal(m, b) +} +func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Request.Marshal(b, m, deterministic) +} +func (dst *Request) XXX_Merge(src proto.Message) { + xxx_messageInfo_Request.Merge(dst, src) +} +func (m *Request) XXX_Size() int { + return xxx_messageInfo_Request.Size(m) +} +func (m *Request) XXX_DiscardUnknown() { + xxx_messageInfo_Request.DiscardUnknown(m) +} + +var xxx_messageInfo_Request proto.InternalMessageInfo + +func (m *Request) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +// The response message containing the greetings +type Reply 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 *Reply) Reset() { *m = Reply{} } +func (m *Reply) String() string { return proto.CompactTextString(m) } +func (*Reply) ProtoMessage() {} +func (*Reply) Descriptor() ([]byte, []int) { + return fileDescriptor_simple_e0ad890e6f6d32ea, []int{1} +} +func (m *Reply) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Reply.Unmarshal(m, b) +} +func (m *Reply) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Reply.Marshal(b, m, deterministic) +} +func (dst *Reply) XXX_Merge(src proto.Message) { + xxx_messageInfo_Reply.Merge(dst, src) +} +func (m *Reply) XXX_Size() int { + return xxx_messageInfo_Reply.Size(m) +} +func (m *Reply) XXX_DiscardUnknown() { + xxx_messageInfo_Reply.DiscardUnknown(m) +} + +var xxx_messageInfo_Reply proto.InternalMessageInfo + +func (m *Reply) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func init() { + proto.RegisterType((*Request)(nil), "simple.Request") + proto.RegisterType((*Reply)(nil), "simple.Reply") +} + +// 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 + +// GripmockClient is the client API for Gripmock service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type GripmockClient interface { + // simple unary method + SayHello(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) +} + +type gripmockClient struct { + cc *grpc.ClientConn +} + +func NewGripmockClient(cc *grpc.ClientConn) GripmockClient { + return &gripmockClient{cc} +} + +func (c *gripmockClient) SayHello(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { + out := new(Reply) + err := c.cc.Invoke(ctx, "/simple.Gripmock/SayHello", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GripmockServer is the server API for Gripmock service. +type GripmockServer interface { + // simple unary method + SayHello(context.Context, *Request) (*Reply, error) +} + +func RegisterGripmockServer(s *grpc.Server, srv GripmockServer) { + s.RegisterService(&_Gripmock_serviceDesc, srv) +} + +func _Gripmock_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Request) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GripmockServer).SayHello(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/simple.Gripmock/SayHello", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GripmockServer).SayHello(ctx, req.(*Request)) + } + return interceptor(ctx, in, info, handler) +} + +var _Gripmock_serviceDesc = grpc.ServiceDesc{ + ServiceName: "simple.Gripmock", + HandlerType: (*GripmockServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "SayHello", + Handler: _Gripmock_SayHello_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "simple/simple.proto", +} + +func init() { proto.RegisterFile("simple/simple.proto", fileDescriptor_simple_e0ad890e6f6d32ea) } + +var fileDescriptor_simple_e0ad890e6f6d32ea = []byte{ + // 138 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0xce, 0xcc, 0x2d, + 0xc8, 0x49, 0xd5, 0x87, 0x50, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0x6c, 0x10, 0x9e, 0x92, + 0x2c, 0x17, 0x7b, 0x50, 0x6a, 0x61, 0x69, 0x6a, 0x71, 0x89, 0x90, 0x10, 0x17, 0x4b, 0x5e, 0x62, + 0x6e, 0xaa, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x98, 0xad, 0xa4, 0xc8, 0xc5, 0x1a, 0x94, + 0x5a, 0x90, 0x53, 0x29, 0x24, 0xc1, 0xc5, 0x9e, 0x9b, 0x5a, 0x5c, 0x9c, 0x98, 0x0e, 0x93, 0x87, + 0x71, 0x8d, 0xcc, 0xb8, 0x38, 0xdc, 0x8b, 0x32, 0x0b, 0x72, 0xf3, 0x93, 0xb3, 0x85, 0xb4, 0xb8, + 0x38, 0x82, 0x13, 0x2b, 0x3d, 0x52, 0x73, 0x72, 0xf2, 0x85, 0xf8, 0xf5, 0xa0, 0x16, 0x42, 0xcd, + 0x97, 0xe2, 0x45, 0x08, 0x14, 0xe4, 0x54, 0x26, 0xb1, 0x81, 0x1d, 0x62, 0x0c, 0x08, 0x00, 0x00, + 0xff, 0xff, 0x75, 0x88, 0x72, 0x2a, 0x9f, 0x00, 0x00, 0x00, +} diff --git a/example/simple/simple.proto b/example/simple/simple.proto new file mode 100644 index 00000000..158f29e2 --- /dev/null +++ b/example/simple/simple.proto @@ -0,0 +1,19 @@ +syntax = "proto3"; + +package simple; + +// The Gripmock service definition. +service Gripmock { + // simple unary method + rpc SayHello (Request) returns (Reply); +} + +// The request message containing the user's name. +message Request { + string name = 1; +} + +// The response message containing the greetings +message Reply { + string message = 1; +} \ No newline at end of file diff --git a/example/stubs/standard.json b/example/simple/stub/simple.json similarity index 100% rename from example/stubs/standard.json rename to example/simple/stub/simple.json diff --git a/example/client/go/main.go b/example/stream/client/main.go similarity index 67% rename from example/client/go/main.go rename to example/stream/client/main.go index e22b9bb7..d94d7233 100644 --- a/example/client/go/main.go +++ b/example/stream/client/main.go @@ -4,32 +4,25 @@ import ( "context" "io" "log" - "os" "sync" + "time" - "github.com/golang/protobuf/ptypes/empty" + pb "github.com/tokopedia/gripmock/example/stream" "google.golang.org/grpc" ) func main() { // Set up a connection to the server. - conn, err := grpc.Dial("localhost:4770", grpc.WithInsecure()) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + // Set up a connection to the server. + conn, err := grpc.DialContext(ctx, "localhost:4770", grpc.WithInsecure(), grpc.WithBlock()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() - c := NewGripmockClient(conn) - - // Contact the server and print out its response. - name := "tokopedia" - if len(os.Args) > 1 { - name = os.Args[1] - } - r, err := c.SayHello(context.Background(), &Request{Name: name}) - if err != nil { - log.Fatalf("error from grpc: %v", err) - } - log.Printf("Greeting: %s", r.Message) + c := pb.NewGripmockClient(conn) wg := &sync.WaitGroup{} wg.Add(1) @@ -43,18 +36,12 @@ func main() { wg.Wait() - ctx := context.Background() - resp, err := c.HealthCheck(ctx, &empty.Empty{}) - if err != nil { - log.Fatalf("error call HealthCheck %v", err) - } - log.Printf("Healthcheck: %s", resp.GetValue()) } // server to client streaming -func serverStream(c GripmockClient, wg *sync.WaitGroup) { +func serverStream(c pb.GripmockClient, wg *sync.WaitGroup) { defer wg.Done() - req := &Request{ + req := &pb.Request{ Name: "server-to-client-streaming", } stream, err := c.ServerStream(context.Background(), req) @@ -77,14 +64,14 @@ func serverStream(c GripmockClient, wg *sync.WaitGroup) { } // client to server streaming -func clientStream(c GripmockClient, wg *sync.WaitGroup) { +func clientStream(c pb.GripmockClient, wg *sync.WaitGroup) { defer wg.Done() stream, err := c.ClientStream(context.Background()) if err != nil { log.Fatalf("c2s error: %v", err) } - requests := []Request{ + requests := []pb.Request{ { Name: "c2s-1", }, { @@ -106,13 +93,13 @@ func clientStream(c GripmockClient, wg *sync.WaitGroup) { } // bidirectional stream -func bidirectionalStream(c GripmockClient, wg *sync.WaitGroup) { +func bidirectionalStream(c pb.GripmockClient, wg *sync.WaitGroup) { stream, err := c.Bidirectional(context.Background()) if err != nil { log.Fatalf("2ds error: %v", err) } - requests := []Request{ + requests := []pb.Request{ { Name: "2ds-message1", }, { diff --git a/example/server/hello.pb.go b/example/stream/stream.pb.go similarity index 74% rename from example/server/hello.pb.go rename to example/stream/stream.pb.go index 8c15ba7e..57d7a7b9 100644 --- a/example/server/hello.pb.go +++ b/example/stream/stream.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. -// source: hello.proto +// source: stream.proto -package main +package stream import proto "github.com/golang/protobuf/proto" import fmt "fmt" @@ -35,7 +35,7 @@ func (m *Request) Reset() { *m = Request{} } func (m *Request) String() string { return proto.CompactTextString(m) } func (*Request) ProtoMessage() {} func (*Request) Descriptor() ([]byte, []int) { - return fileDescriptor_hello_6f78bf933483ca19, []int{0} + return fileDescriptor_stream_b89e769b981cf265, []int{0} } func (m *Request) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Request.Unmarshal(m, b) @@ -74,7 +74,7 @@ func (m *Reply) Reset() { *m = Reply{} } func (m *Reply) String() string { return proto.CompactTextString(m) } func (*Reply) ProtoMessage() {} func (*Reply) Descriptor() ([]byte, []int) { - return fileDescriptor_hello_6f78bf933483ca19, []int{1} + return fileDescriptor_stream_b89e769b981cf265, []int{1} } func (m *Reply) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_Reply.Unmarshal(m, b) @@ -102,8 +102,8 @@ func (m *Reply) GetMessage() string { } func init() { - proto.RegisterType((*Request)(nil), "main.Request") - proto.RegisterType((*Reply)(nil), "main.Reply") + proto.RegisterType((*Request)(nil), "stream.Request") + proto.RegisterType((*Reply)(nil), "stream.Reply") } // Reference imports to suppress errors if they are not otherwise used. @@ -118,8 +118,6 @@ const _ = grpc.SupportPackageIsVersion4 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. type GripmockClient interface { - // standard grpc method - SayHello(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) // server to client sreaming ServerStream(ctx context.Context, in *Request, opts ...grpc.CallOption) (Gripmock_ServerStreamClient, error) // client to server streaming @@ -136,17 +134,8 @@ func NewGripmockClient(cc *grpc.ClientConn) GripmockClient { return &gripmockClient{cc} } -func (c *gripmockClient) SayHello(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Reply, error) { - out := new(Reply) - err := c.cc.Invoke(ctx, "/main.Gripmock/SayHello", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - func (c *gripmockClient) ServerStream(ctx context.Context, in *Request, opts ...grpc.CallOption) (Gripmock_ServerStreamClient, error) { - stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[0], "/main.Gripmock/serverStream", opts...) + stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[0], "/stream.Gripmock/serverStream", opts...) if err != nil { return nil, err } @@ -178,7 +167,7 @@ func (x *gripmockServerStreamClient) Recv() (*Reply, error) { } func (c *gripmockClient) ClientStream(ctx context.Context, opts ...grpc.CallOption) (Gripmock_ClientStreamClient, error) { - stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[1], "/main.Gripmock/clientStream", opts...) + stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[1], "/stream.Gripmock/clientStream", opts...) if err != nil { return nil, err } @@ -212,7 +201,7 @@ func (x *gripmockClientStreamClient) CloseAndRecv() (*Reply, error) { } func (c *gripmockClient) Bidirectional(ctx context.Context, opts ...grpc.CallOption) (Gripmock_BidirectionalClient, error) { - stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[2], "/main.Gripmock/bidirectional", opts...) + stream, err := c.cc.NewStream(ctx, &_Gripmock_serviceDesc.Streams[2], "/stream.Gripmock/bidirectional", opts...) if err != nil { return nil, err } @@ -244,8 +233,6 @@ func (x *gripmockBidirectionalClient) Recv() (*Reply, error) { // GripmockServer is the server API for Gripmock service. type GripmockServer interface { - // standard grpc method - SayHello(context.Context, *Request) (*Reply, error) // server to client sreaming ServerStream(*Request, Gripmock_ServerStreamServer) error // client to server streaming @@ -258,24 +245,6 @@ func RegisterGripmockServer(s *grpc.Server, srv GripmockServer) { s.RegisterService(&_Gripmock_serviceDesc, srv) } -func _Gripmock_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(Request) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(GripmockServer).SayHello(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/main.Gripmock/SayHello", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(GripmockServer).SayHello(ctx, req.(*Request)) - } - return interceptor(ctx, in, info, handler) -} - func _Gripmock_ServerStream_Handler(srv interface{}, stream grpc.ServerStream) error { m := new(Request) if err := stream.RecvMsg(m); err != nil { @@ -350,14 +319,9 @@ func (x *gripmockBidirectionalServer) Recv() (*Request, error) { } var _Gripmock_serviceDesc = grpc.ServiceDesc{ - ServiceName: "main.Gripmock", + ServiceName: "stream.Gripmock", HandlerType: (*GripmockServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "SayHello", - Handler: _Gripmock_SayHello_Handler, - }, - }, + Methods: []grpc.MethodDesc{}, Streams: []grpc.StreamDesc{ { StreamName: "serverStream", @@ -376,26 +340,22 @@ var _Gripmock_serviceDesc = grpc.ServiceDesc{ ClientStreams: true, }, }, - Metadata: "hello.proto", -} - -func init() { proto.RegisterFile("hello.proto", fileDescriptor_hello_6f78bf933483ca19) } - -var fileDescriptor_hello_6f78bf933483ca19 = []byte{ - // 225 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x84, 0x90, 0xcd, 0x4a, 0x03, 0x31, - 0x14, 0x85, 0x09, 0x54, 0x5b, 0x6f, 0x2d, 0x42, 0x56, 0x45, 0x11, 0xb4, 0x0b, 0xe9, 0x42, 0xe2, - 0xa0, 0x6f, 0xd0, 0x8d, 0x2e, 0x87, 0x99, 0x85, 0xeb, 0x4c, 0xe6, 0x32, 0x06, 0xf3, 0xe7, 0x4d, - 0xfc, 0x99, 0xa7, 0xf3, 0xd5, 0x24, 0xc1, 0xd9, 0x4e, 0x77, 0xe7, 0x70, 0x3f, 0xce, 0xe1, 0x1e, - 0x58, 0xbf, 0xa1, 0x31, 0x5e, 0x04, 0xf2, 0xc9, 0xf3, 0x85, 0x95, 0xda, 0xed, 0xae, 0x61, 0xd9, - 0xe0, 0xc7, 0x27, 0xc6, 0xc4, 0x39, 0x2c, 0x9c, 0xb4, 0xb8, 0x65, 0x37, 0x6c, 0x7f, 0xd6, 0x14, - 0xbd, 0xbb, 0x85, 0x93, 0x06, 0x83, 0x19, 0xf9, 0x16, 0x96, 0x16, 0x63, 0x94, 0xc3, 0x74, 0x9f, - 0xec, 0xe3, 0x2f, 0x83, 0xd5, 0x33, 0xe9, 0x60, 0xbd, 0x7a, 0xe7, 0x77, 0xb0, 0x6a, 0xe5, 0xf8, - 0x92, 0x6b, 0xf8, 0x46, 0xe4, 0x06, 0xf1, 0x1f, 0x7f, 0xb9, 0x9e, 0x6c, 0x8e, 0xbb, 0x87, 0xf3, - 0x88, 0xf4, 0x85, 0xd4, 0x26, 0x42, 0x69, 0xe7, 0xd8, 0x8a, 0x65, 0x5a, 0x19, 0x8d, 0x2e, 0x1d, - 0xa7, 0xf7, 0x8c, 0x3f, 0xc0, 0xa6, 0xd3, 0xbd, 0x26, 0x54, 0x49, 0x7b, 0x27, 0xcd, 0x3c, 0x5e, - 0xb1, 0x43, 0x05, 0x57, 0xda, 0x8b, 0x81, 0x82, 0x12, 0xf8, 0x23, 0x6d, 0x30, 0x18, 0x45, 0x59, - 0xea, 0xdb, 0x93, 0xe9, 0x0f, 0x17, 0xe5, 0x9d, 0xd7, 0xac, 0xeb, 0xbc, 0x5c, 0xcd, 0xba, 0xd3, - 0x32, 0xe1, 0xd3, 0x5f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x17, 0xbc, 0x43, 0x51, 0x01, 0x00, - 0x00, + Metadata: "stream.proto", +} + +func init() { proto.RegisterFile("stream.proto", fileDescriptor_stream_b89e769b981cf265) } + +var fileDescriptor_stream_b89e769b981cf265 = []byte{ + // 173 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x2e, 0x29, 0x4a, + 0x4d, 0xcc, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x83, 0xf0, 0x94, 0x64, 0xb9, 0xd8, + 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x84, 0xb8, 0x58, 0xf2, 0x12, 0x73, 0x53, 0x25, + 0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xc0, 0x6c, 0x25, 0x45, 0x2e, 0xd6, 0xa0, 0xd4, 0x82, 0x9c, + 0x4a, 0x21, 0x09, 0x2e, 0xf6, 0xdc, 0xd4, 0xe2, 0xe2, 0xc4, 0x74, 0x98, 0x3c, 0x8c, 0x6b, 0xb4, + 0x98, 0x91, 0x8b, 0xc3, 0xbd, 0x28, 0xb3, 0x20, 0x37, 0x3f, 0x39, 0x5b, 0xc8, 0x80, 0x8b, 0xa7, + 0x38, 0xb5, 0xa8, 0x2c, 0xb5, 0x28, 0x18, 0x6c, 0xbc, 0x10, 0xbf, 0x1e, 0xd4, 0x56, 0xa8, 0x25, + 0x52, 0xbc, 0x08, 0x81, 0x82, 0x9c, 0x4a, 0x03, 0x46, 0x90, 0x8e, 0xe4, 0x9c, 0xcc, 0xd4, 0xbc, + 0x12, 0xe2, 0x74, 0x68, 0x30, 0x0a, 0x19, 0x73, 0xf1, 0x26, 0x65, 0xa6, 0x64, 0x16, 0xa5, 0x26, + 0x97, 0x64, 0xe6, 0xe7, 0x25, 0xe6, 0x10, 0xd6, 0x62, 0xc0, 0x98, 0xc4, 0x06, 0xf6, 0xb6, 0x31, + 0x20, 0x00, 0x00, 0xff, 0xff, 0x62, 0x90, 0x1d, 0x75, 0x06, 0x01, 0x00, 0x00, } diff --git a/example/pb/hello.proto b/example/stream/stream.proto similarity index 61% rename from example/pb/hello.proto rename to example/stream/stream.proto index 275ba6a4..502ab571 100644 --- a/example/pb/hello.proto +++ b/example/stream/stream.proto @@ -1,22 +1,15 @@ syntax = "proto3"; -package main; +package stream; -import "google/protobuf/empty.proto"; -import "google/protobuf/wrappers.proto"; - -// The greeting service definition. +// The Gripmock service definition. service Gripmock { - // standard grpc method - rpc SayHello (Request) returns (Reply); // server to client sreaming rpc serverStream (Request) returns (stream Reply); // client to server streaming rpc clientStream (stream Request) returns (Reply); // bidirectional streaming rpc bidirectional (stream Request) returns (stream Reply); - // using well-known-type - rpc HealthCheck(google.protobuf.Empty) returns (google.protobuf.StringValue); } // The request message containing the user's name. diff --git a/example/stubs/bidirectional-stream.json b/example/stream/stub/bidirectional-stream.json similarity index 100% rename from example/stubs/bidirectional-stream.json rename to example/stream/stub/bidirectional-stream.json diff --git a/example/stubs/client2server-stream.json b/example/stream/stub/client2server-stream.json similarity index 100% rename from example/stubs/client2server-stream.json rename to example/stream/stub/client2server-stream.json diff --git a/example/stubs/server2client-stream.json b/example/stream/stub/server2client-stream.json similarity index 100% rename from example/stubs/server2client-stream.json rename to example/stream/stub/server2client-stream.json diff --git a/example/well_known_types/client/main.go b/example/well_known_types/client/main.go new file mode 100644 index 00000000..f26fec55 --- /dev/null +++ b/example/well_known_types/client/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "context" + "log" + "time" + + "github.com/golang/protobuf/ptypes/empty" + pb "github.com/tokopedia/gripmock/example/well_known_types" + "google.golang.org/grpc" +) + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + // Set up a connection to the server. + conn, err := grpc.DialContext(ctx, "localhost:4770", grpc.WithInsecure(), grpc.WithBlock()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + + c := pb.NewGripmockClient(conn) + + r, err := c.ApiInfo(context.Background(), &empty.Empty{}) + if err != nil { + log.Fatalf("error from grpc: %v", err) + } + + if r.Name != "Gripmock" { + log.Fatalf("expecting api name: Gripmock, but got '%v' instead", r.Name) + } + + log.Printf("Api Name: %v", r.Name) +} diff --git a/example/stubs/healthcheck.json b/example/well_known_types/stub/wkt.json similarity index 57% rename from example/stubs/healthcheck.json rename to example/well_known_types/stub/wkt.json index 868278bd..b87167d5 100644 --- a/example/stubs/healthcheck.json +++ b/example/well_known_types/stub/wkt.json @@ -1,12 +1,13 @@ { "service":"Gripmock", - "method":"HealthCheck", + "method":"ApiInfo", "input":{ "equals":{} }, "output":{ "data":{ - "value": "OK" + "name": "Gripmock", + "version": "1.0" } } } \ No newline at end of file diff --git a/example/well_known_types/well_known_types.pb.go b/example/well_known_types/well_known_types.pb.go new file mode 100644 index 00000000..215acf97 --- /dev/null +++ b/example/well_known_types/well_known_types.pb.go @@ -0,0 +1,115 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: well_known_types/well_known_types.proto + +package well_known_types + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" +import empty "github.com/golang/protobuf/ptypes/empty" +import api "google.golang.org/genproto/protobuf/api" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// 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 + +// 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 + +// GripmockClient is the client API for Gripmock service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type GripmockClient interface { + ApiInfo(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*api.Api, error) +} + +type gripmockClient struct { + cc *grpc.ClientConn +} + +func NewGripmockClient(cc *grpc.ClientConn) GripmockClient { + return &gripmockClient{cc} +} + +func (c *gripmockClient) ApiInfo(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*api.Api, error) { + out := new(api.Api) + err := c.cc.Invoke(ctx, "/well_known_types.Gripmock/ApiInfo", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// GripmockServer is the server API for Gripmock service. +type GripmockServer interface { + ApiInfo(context.Context, *empty.Empty) (*api.Api, error) +} + +func RegisterGripmockServer(s *grpc.Server, srv GripmockServer) { + s.RegisterService(&_Gripmock_serviceDesc, srv) +} + +func _Gripmock_ApiInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(empty.Empty) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(GripmockServer).ApiInfo(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/well_known_types.Gripmock/ApiInfo", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(GripmockServer).ApiInfo(ctx, req.(*empty.Empty)) + } + return interceptor(ctx, in, info, handler) +} + +var _Gripmock_serviceDesc = grpc.ServiceDesc{ + ServiceName: "well_known_types.Gripmock", + HandlerType: (*GripmockServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "ApiInfo", + Handler: _Gripmock_ApiInfo_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "well_known_types/well_known_types.proto", +} + +func init() { + proto.RegisterFile("well_known_types/well_known_types.proto", fileDescriptor_well_known_types_79f0e9b76c3d9f98) +} + +var fileDescriptor_well_known_types_79f0e9b76c3d9f98 = []byte{ + // 133 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x2f, 0x4f, 0xcd, 0xc9, + 0x89, 0xcf, 0xce, 0xcb, 0x2f, 0xcf, 0x8b, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x47, 0x17, 0xd0, + 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x40, 0x17, 0x97, 0x92, 0x4e, 0xcf, 0xcf, 0x4f, 0xcf, + 0x49, 0xd5, 0x07, 0xcb, 0x27, 0x95, 0xa6, 0xe9, 0xa7, 0xe6, 0x16, 0x94, 0x54, 0x42, 0x94, 0x4b, + 0x49, 0xa2, 0x4b, 0x26, 0x16, 0x64, 0x42, 0xa4, 0x8c, 0x9c, 0xb9, 0x38, 0xdc, 0x8b, 0x32, 0x0b, + 0x72, 0xf3, 0x93, 0xb3, 0x85, 0xcc, 0xb9, 0xd8, 0x1d, 0x0b, 0x32, 0x3d, 0xf3, 0xd2, 0xf2, 0x85, + 0xc4, 0xf4, 0x20, 0x5a, 0xf4, 0x60, 0x5a, 0xf4, 0x5c, 0x41, 0xe6, 0x49, 0x89, 0x60, 0x88, 0x3b, + 0x16, 0x64, 0x26, 0xb1, 0x81, 0x79, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x4e, 0x55, 0xb9, + 0x1e, 0xc0, 0x00, 0x00, 0x00, +} diff --git a/example/well_known_types/well_known_types.proto b/example/well_known_types/well_known_types.proto new file mode 100644 index 00000000..9c5ccce0 --- /dev/null +++ b/example/well_known_types/well_known_types.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package well_known_types; + +import "google/protobuf/empty.proto"; +import "google/protobuf/api.proto"; + +service Gripmock { + // this shows us example on using WKT as dependency + // api.proto in particular has go_package alias with semicolon + // "google.golang.org/genproto/protobuf/api;api" + rpc ApiInfo(google.protobuf.Empty) returns (google.protobuf.Api); +} diff --git a/gripmock.go b/gripmock.go index 6aaf49fb..5ee625cf 100644 --- a/gripmock.go +++ b/gripmock.go @@ -20,6 +20,7 @@ func main() { adminport := flag.String("admin-port", "4771", "Port of stub admin server") adminBindAddr := flag.String("admin-listen", "", "Adress the admin server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine") stubPath := flag.String("stub", "", "Path where the stub files are (Optional)") + wktDir := flag.String("wkt-dir", "/protobuf", "Directory of well-known-types protos.") // for backwards compatibility if os.Args[1] == "gripmock" { os.Args = append(os.Args[:1], os.Args[2:]...) @@ -63,6 +64,7 @@ func main() { grpcAddress: *grpcBindAddr, grpcPort: *grpcPort, output: output, + wktPath: *wktDir, }) // build the server @@ -94,6 +96,7 @@ type protocParam struct { grpcAddress string grpcPort string output string + wktPath string } func generateProtoc(param protocParam) { @@ -105,7 +108,7 @@ func generateProtoc(param protocParam) { args := []string{"-I", protodir} // include well-known-types - args = append(args, "-I", "/protobuf") + args = append(args, "-I", param.wktPath) args = append(args, param.protoPath...) args = append(args, "--go_out=plugins=grpc:"+param.output) args = append(args, fmt.Sprintf("--gripmock_out=admin-port=%s,grpc-address=%s,grpc-port=%s:%s", diff --git a/protoc-gen-gripmock/generator.go b/protoc-gen-gripmock/generator.go index 01c70c34..0304886f 100644 --- a/protoc-gen-gripmock/generator.go +++ b/protoc-gen-gripmock/generator.go @@ -141,12 +141,12 @@ func generateServer(protos []*descriptor.FileDescriptorProto, opt *Options) erro } byt := buf.Bytes() - byt, err = imports.Process("", byt, nil) + bytProcessed, err := imports.Process("", byt, nil) if err != nil { - return fmt.Errorf("formatting %v", err) + return fmt.Errorf("formatting: %v \n%s", err, string(byt)) } - _, err = opt.writer.Write(byt) + _, err = opt.writer.Write(bytProcessed) return err } @@ -161,7 +161,7 @@ func resolveDependencies(protos []*descriptor.FileDescriptorProto) map[string]st aliasNum := 1 for _, dep := range depsFile { for _, proto := range protos { - pkg := proto.GetOptions().GetGoPackage() + alias, pkg := getGoPackage(proto) // skip whether its not intended deps // or has empty Go package @@ -169,7 +169,6 @@ func resolveDependencies(protos []*descriptor.FileDescriptorProto) map[string]st continue } - alias := getAlias(proto.GetName()) // in case of found same alias if ok := aliases[alias]; ok { alias = fmt.Sprintf("%s%d", alias, aliasNum) @@ -184,10 +183,23 @@ func resolveDependencies(protos []*descriptor.FileDescriptorProto) map[string]st return deps } -func getAlias(protoName string) string { - splitSlash := strings.Split(protoName, "/") - split := strings.Split(splitSlash[len(splitSlash)-1], ".") - return split[0] +func getGoPackage(proto *descriptor.FileDescriptorProto) (alias string, goPackage string) { + goPackage = proto.GetOptions().GetGoPackage() + if goPackage == "" { + return + } + + // support go_package alias declaration + // https://github.com/golang/protobuf/issues/139 + if splits := strings.Split(goPackage, ";"); len(splits) > 1 { + goPackage = splits[0] + alias = splits[1] + } else { + splitSlash := strings.Split(proto.GetName(), "/") + split := strings.Split(splitSlash[len(splitSlash)-1], ".") + alias = split[0] + } + return } // change the structure also translate method type @@ -234,7 +246,7 @@ func getMessageType(protos []*descriptor.FileDescriptorProto, deps []string, tip for _, msg := range proto.GetMessageType() { if msg.GetName() == targetType { - alias := getAlias(proto.GetName()) + alias, _ := getGoPackage(proto) return fmt.Sprintf("%s.%s", alias, msg.GetName()) } }