From 00d95203ba293f26ea1505d8285b72565fe86525 Mon Sep 17 00:00:00 2001 From: Asim Date: Wed, 2 Dec 2015 22:44:01 +0000 Subject: [PATCH] Add code generation example --- examples/client/codegen/codegen.go | 56 ++++++++ examples/server/proto/example/example.pb.go | 150 +++++++++++++++++--- examples/server/proto/example/example.proto | 7 + 3 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 examples/client/codegen/codegen.go diff --git a/examples/client/codegen/codegen.go b/examples/client/codegen/codegen.go new file mode 100644 index 00000000..6bceb605 --- /dev/null +++ b/examples/client/codegen/codegen.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + + "github.com/micro/go-micro/cmd" + example "github.com/micro/go-micro/examples/server/proto/example" + "golang.org/x/net/context" +) + +var ( + cl = example.NewExampleClient(nil) +) + +func call(i int) { + rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"}) + if err != nil { + fmt.Println("call err: ", err, rsp) + return + } + fmt.Println("Call:", i, "rsp:", rsp.Msg) +} + +func stream() { + stream, err := cl.Stream(context.Background(), &example.StreamingRequest{Count: int64(10)}) + if err != nil { + fmt.Println("err:", err) + return + } + for i := 0; i < 10; i++ { + rsp, err := stream.Next() + if err != nil { + fmt.Println("err:", err) + break + } + fmt.Println("Stream: rsp:", rsp.Count) + } + if stream.Error() != nil { + fmt.Println("stream err:", err) + return + } + if err := stream.Close(); err != nil { + fmt.Println("stream close err:", err) + } +} + +func main() { + cmd.Init() + + fmt.Println("\n--- Call example ---\n") + for i := 0; i < 10; i++ { + call(i) + } + fmt.Println("\n--- Streamer example ---\n") + stream() +} diff --git a/examples/server/proto/example/example.pb.go b/examples/server/proto/example/example.pb.go index ea9e3f36..d7c10258 100644 --- a/examples/server/proto/example/example.pb.go +++ b/examples/server/proto/example/example.pb.go @@ -1,12 +1,12 @@ // Code generated by protoc-gen-go. -// source: go-micro/examples/server/proto/example/example.proto +// source: example.proto // DO NOT EDIT! /* -Package example is a generated protocol buffer package. +Package go_micro_srv_example is a generated protocol buffer package. It is generated from these files: - go-micro/examples/server/proto/example/example.proto + example.proto It has these top-level messages: Message @@ -15,52 +15,164 @@ It has these top-level messages: StreamingRequest StreamingResponse */ -package example +package go_micro_srv_example import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + client "github.com/micro/go-micro/client" + server "github.com/micro/go-micro/server" +) // Reference imports to suppress errors if they are not otherwise used. var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf type Message struct { Say string `protobuf:"bytes,1,opt,name=say" json:"say,omitempty"` } -func (m *Message) Reset() { *m = Message{} } -func (m *Message) String() string { return proto.CompactTextString(m) } -func (*Message) ProtoMessage() {} +func (m *Message) Reset() { *m = Message{} } +func (m *Message) String() string { return proto.CompactTextString(m) } +func (*Message) ProtoMessage() {} +func (*Message) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } type Request struct { Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` } -func (m *Request) Reset() { *m = Request{} } -func (m *Request) String() string { return proto.CompactTextString(m) } -func (*Request) ProtoMessage() {} +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} +func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } type Response struct { Msg string `protobuf:"bytes,1,opt,name=msg" json:"msg,omitempty"` } -func (m *Response) Reset() { *m = Response{} } -func (m *Response) String() string { return proto.CompactTextString(m) } -func (*Response) ProtoMessage() {} +func (m *Response) Reset() { *m = Response{} } +func (m *Response) String() string { return proto.CompactTextString(m) } +func (*Response) ProtoMessage() {} +func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } type StreamingRequest struct { Count int64 `protobuf:"varint,1,opt,name=count" json:"count,omitempty"` } -func (m *StreamingRequest) Reset() { *m = StreamingRequest{} } -func (m *StreamingRequest) String() string { return proto.CompactTextString(m) } -func (*StreamingRequest) ProtoMessage() {} +func (m *StreamingRequest) Reset() { *m = StreamingRequest{} } +func (m *StreamingRequest) String() string { return proto.CompactTextString(m) } +func (*StreamingRequest) ProtoMessage() {} +func (*StreamingRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} } type StreamingResponse struct { Count int64 `protobuf:"varint,1,opt,name=count" json:"count,omitempty"` } -func (m *StreamingResponse) Reset() { *m = StreamingResponse{} } -func (m *StreamingResponse) String() string { return proto.CompactTextString(m) } -func (*StreamingResponse) ProtoMessage() {} +func (m *StreamingResponse) Reset() { *m = StreamingResponse{} } +func (m *StreamingResponse) String() string { return proto.CompactTextString(m) } +func (*StreamingResponse) ProtoMessage() {} +func (*StreamingResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} } func init() { + proto.RegisterType((*Message)(nil), "go.micro.srv.example.Message") + proto.RegisterType((*Request)(nil), "go.micro.srv.example.Request") + proto.RegisterType((*Response)(nil), "go.micro.srv.example.Response") + proto.RegisterType((*StreamingRequest)(nil), "go.micro.srv.example.StreamingRequest") + proto.RegisterType((*StreamingResponse)(nil), "go.micro.srv.example.StreamingResponse") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ client.Option +var _ server.Option + +// Client API for Example service + +type ExampleClient interface { + Call(ctx context.Context, in *Request) (*Response, error) + Stream(ctx context.Context, in *StreamingRequest) (Example_StreamClient, error) +} + +type exampleClient struct { + c client.Client +} + +func NewExampleClient(c client.Client) ExampleClient { + if c == nil { + c = client.NewClient() + } + return &exampleClient{ + c: c, + } +} + +func (c *exampleClient) Call(ctx context.Context, in *Request) (*Response, error) { + req := c.c.NewRequest("go.micro.srv.example", "Example.Call", in) + out := new(Response) + err := c.c.Call(ctx, req, out) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *exampleClient) Stream(ctx context.Context, in *StreamingRequest) (Example_StreamClient, error) { + req := c.c.NewRequest("go.micro.srv.example", "Example.Stream", in) + outCh := make(chan *StreamingResponse) + stream, err := c.c.Stream(ctx, req, outCh) + if err != nil { + return nil, err + } + return &exampleStreamClient{stream, outCh}, nil +} + +type Example_StreamClient interface { + Next() (*StreamingResponse, error) + client.Streamer +} + +type exampleStreamClient struct { + client.Streamer + next chan *StreamingResponse +} + +func (x *exampleStreamClient) Next() (*StreamingResponse, error) { + out, ok := <-x.next + if !ok { + return nil, fmt.Errorf(`chan closed`) + } + return out, nil +} + +// Server API for Example service + +type ExampleServer interface { + Call(context.Context, *Request) (*Response, error) + Stream(context.Context, *StreamingRequest, func(*StreamingResponse) error) error +} + +func RegisterExampleServer(s server.Server, srv ExampleServer) { + s.Handle(s.NewHandler(srv)) +} + +var fileDescriptor0 = []byte{ + // 211 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0xad, 0x48, 0xcc, + 0x2d, 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x49, 0xcf, 0xd7, 0xcb, 0xcd, + 0x4c, 0x2e, 0xca, 0xd7, 0x2b, 0x2e, 0x2a, 0xd3, 0x83, 0xca, 0x29, 0x89, 0x71, 0xb1, 0xfb, 0xa6, + 0x16, 0x17, 0x27, 0xa6, 0xa7, 0x0a, 0x71, 0x73, 0x31, 0x17, 0x27, 0x56, 0x4a, 0x30, 0x2a, 0x30, + 0x6a, 0x70, 0x2a, 0x89, 0x73, 0xb1, 0x07, 0xa5, 0x16, 0x96, 0xa6, 0x16, 0x97, 0x08, 0xf1, 0x70, + 0xb1, 0xe4, 0x25, 0xe6, 0xa6, 0xc2, 0x25, 0x38, 0x82, 0x52, 0x8b, 0x0b, 0xf2, 0xf3, 0x8a, 0xc1, + 0x3a, 0x72, 0x8b, 0xd3, 0xa1, 0x12, 0x8a, 0x5c, 0x02, 0xc1, 0x25, 0x45, 0xa9, 0x89, 0xb9, 0x99, + 0x79, 0xe9, 0x30, 0xad, 0xbc, 0x5c, 0xac, 0xc9, 0xf9, 0xa5, 0x79, 0x25, 0x60, 0x25, 0xcc, 0x4a, + 0x4a, 0x5c, 0x82, 0x48, 0x4a, 0xa0, 0x86, 0xa0, 0xaa, 0x31, 0xda, 0xc8, 0xc8, 0xc5, 0xee, 0x0a, + 0x71, 0x9c, 0x90, 0x3b, 0x17, 0x8b, 0x73, 0x62, 0x4e, 0x8e, 0x90, 0xac, 0x1e, 0x36, 0xb7, 0xeb, + 0x41, 0x6d, 0x91, 0x92, 0xc3, 0x25, 0x0d, 0xb1, 0x41, 0x89, 0x41, 0x28, 0x96, 0x8b, 0x0d, 0x62, + 0xb1, 0x90, 0x1a, 0x76, 0xb5, 0xe8, 0x2e, 0x97, 0x52, 0x27, 0xa8, 0x0e, 0x66, 0xb8, 0x01, 0x63, + 0x12, 0x1b, 0x38, 0x84, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x71, 0xe5, 0x8f, 0x25, 0x72, + 0x01, 0x00, 0x00, } diff --git a/examples/server/proto/example/example.proto b/examples/server/proto/example/example.proto index 23ad31bc..b7dd8748 100644 --- a/examples/server/proto/example/example.proto +++ b/examples/server/proto/example/example.proto @@ -1,5 +1,12 @@ syntax = "proto3"; +package go.micro.srv.example; + +service Example { + rpc Call(Request) returns (Response) {} + rpc Stream(StreamingRequest) returns (stream StreamingResponse) {} +} + message Message { string say = 1; }