Update the code generator initialisation

This commit is contained in:
Asim 2015-12-06 22:44:17 +00:00
parent 15d5299997
commit 2f02119e21
3 changed files with 63 additions and 48 deletions

View File

@ -52,6 +52,8 @@ hello.proto
syntax = "proto3"; syntax = "proto3";
// package name is used as the service name for discovery // package name is used as the service name for discovery
// if service name is not passed in when initialising the
// client
package go.micro.srv.greeter; package go.micro.srv.greeter;
service Say { service Say {
@ -82,40 +84,45 @@ protoc --go_out=plugins=micro:. hello.proto
// Client API for Say service // Client API for Say service
type SayClient interface { type SayClient interface {
Hello(ctx context.Context, in *Request) (*Response, error) Hello(ctx context.Context, in *Request) (*Response, error)
} }
type sayClient struct { type sayClient struct {
c client.Client c client.Client
serviceName string
} }
func NewSayClient(c client.Client) SayClient { func NewSayClient(serviceName string, c client.Client) SayClient {
if c == nil { if c == nil {
c = client.NewClient() c = client.NewClient()
} }
return &sayClient{ if len(serviceName) == 0 {
c: c, serviceName = "go.micro.srv.greeter"
} }
return &sayClient{
c: c,
serviceName: serviceName,
}
} }
func (c *sayClient) Hello(ctx context.Context, in *Request) (*Response, error) { func (c *sayClient) Hello(ctx context.Context, in *Request) (*Response, error) {
req := c.c.NewRequest("go.micro.srv.greeter", "Say.Hello", in) req := c.c.NewRequest(c.serviceName, "Say.Hello", in)
out := new(Response) out := new(Response)
err := c.c.Call(ctx, req, out) err := c.c.Call(ctx, req, out)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return out, nil return out, nil
} }
// Server API for Say service // Server API for Say service
type SayServer interface { type SayHandler interface {
Hello(context.Context, *Request, *Response) error Hello(context.Context, *Request, *Response) error
} }
func RegisterSayServer(s server.Server, srv SayServer) { func RegisterSayHandler(s server.Server, hdlr SayHandler) {
s.Handle(s.NewHandler(srv)) s.Handle(s.NewHandler(hdlr))
} }
``` ```
@ -131,7 +138,9 @@ import (
) )
func main() { func main() {
cl := hello.NewSayClient(client.DefaultClient) cl := hello.NewSayClient("go.micro.srv.greeter", client.DefaultClient)
// alternative initialisation
// cl := hello.NewSayClient("", nil)
rsp, err := cl.Hello(contex.Background(), &hello.Request{"Name": "John"}) rsp, err := cl.Hello(contex.Background(), &hello.Request{"Name": "John"})
if err != nil { if err != nil {

View File

@ -9,7 +9,7 @@ import (
) )
var ( var (
cl = example.NewExampleClient(nil) cl = example.NewExampleClient("go.micro.srv.example", nil)
) )
func call(i int) { func call(i int) {

View File

@ -1,12 +1,12 @@
// Code generated by protoc-gen-go. // Code generated by protoc-gen-go.
// source: example.proto // source: go-micro/examples/server/proto/example/example.proto
// DO NOT EDIT! // DO NOT EDIT!
/* /*
Package go_micro_srv_example is a generated protocol buffer package. Package go_micro_srv_example is a generated protocol buffer package.
It is generated from these files: It is generated from these files:
example.proto go-micro/examples/server/proto/example/example.proto
It has these top-level messages: It has these top-level messages:
Message Message
@ -22,9 +22,9 @@ import fmt "fmt"
import math "math" import math "math"
import ( import (
context "golang.org/x/net/context"
client "github.com/micro/go-micro/client" client "github.com/micro/go-micro/client"
server "github.com/micro/go-micro/server" server "github.com/micro/go-micro/server"
context "golang.org/x/net/context"
) )
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
@ -98,20 +98,25 @@ type ExampleClient interface {
} }
type exampleClient struct { type exampleClient struct {
c client.Client c client.Client
serviceName string
} }
func NewExampleClient(c client.Client) ExampleClient { func NewExampleClient(serviceName string, c client.Client) ExampleClient {
if c == nil { if c == nil {
c = client.NewClient() c = client.NewClient()
} }
if len(serviceName) == 0 {
serviceName = "go.micro.srv.example"
}
return &exampleClient{ return &exampleClient{
c: c, c: c,
serviceName: serviceName,
} }
} }
func (c *exampleClient) Call(ctx context.Context, in *Request) (*Response, error) { func (c *exampleClient) Call(ctx context.Context, in *Request) (*Response, error) {
req := c.c.NewRequest("go.micro.srv.example", "Example.Call", in) req := c.c.NewRequest(c.serviceName, "Example.Call", in)
out := new(Response) out := new(Response)
err := c.c.Call(ctx, req, out) err := c.c.Call(ctx, req, out)
if err != nil { if err != nil {
@ -121,7 +126,7 @@ func (c *exampleClient) Call(ctx context.Context, in *Request) (*Response, error
} }
func (c *exampleClient) Stream(ctx context.Context, in *StreamingRequest) (Example_StreamClient, error) { func (c *exampleClient) Stream(ctx context.Context, in *StreamingRequest) (Example_StreamClient, error) {
req := c.c.NewRequest("go.micro.srv.example", "Example.Stream", in) req := c.c.NewRequest(c.serviceName, "Example.Stream", in)
outCh := make(chan *StreamingResponse) outCh := make(chan *StreamingResponse)
stream, err := c.c.Stream(ctx, req, outCh) stream, err := c.c.Stream(ctx, req, outCh)
if err != nil { if err != nil {
@ -150,29 +155,30 @@ func (x *exampleStreamClient) Next() (*StreamingResponse, error) {
// Server API for Example service // Server API for Example service
type ExampleServer interface { type ExampleHandler interface {
Call(context.Context, *Request, *Response) error Call(context.Context, *Request, *Response) error
Stream(context.Context, func(*StreamingResponse) error) error Stream(context.Context, func(*StreamingResponse) error) error
} }
func RegisterExampleServer(s server.Server, srv ExampleServer) { func RegisterExampleHandler(s server.Server, hdlr ExampleHandler) {
s.Handle(s.NewHandler(srv)) s.Handle(s.NewHandler(hdlr))
} }
var fileDescriptor0 = []byte{ var fileDescriptor0 = []byte{
// 211 bytes of a gzipped FileDescriptorProto // 230 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0xad, 0x48, 0xcc, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x90, 0xcd, 0x4a, 0xc5, 0x30,
0x2d, 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x49, 0xcf, 0xd7, 0xcb, 0xcd, 0x10, 0x85, 0x0d, 0xf7, 0x7a, 0xab, 0xa3, 0x82, 0x06, 0x51, 0x29, 0x28, 0x9a, 0x85, 0xba, 0x31,
0x4c, 0x2e, 0xca, 0xd7, 0x2b, 0x2e, 0x2a, 0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