Add String method to all interfaces

This commit is contained in:
Asim 2015-12-19 21:56:14 +00:00
parent d7b3765c71
commit be43d827c7
17 changed files with 73 additions and 0 deletions

View File

@ -7,6 +7,7 @@ type Broker interface {
Init() error Init() error
Publish(string, *Message) error Publish(string, *Message) error
Subscribe(string, Handler) (Subscriber, error) Subscribe(string, Handler) (Subscriber, error)
String() string
} }
type Handler func(*Message) type Handler func(*Message)
@ -52,3 +53,7 @@ func Publish(topic string, msg *Message) error {
func Subscribe(topic string, handler Handler) (Subscriber, error) { func Subscribe(topic string, handler Handler) (Subscriber, error) {
return DefaultBroker.Subscribe(topic, handler) return DefaultBroker.Subscribe(topic, handler)
} }
func String() string {
return DefaultBroker.String()
}

View File

@ -237,3 +237,7 @@ func (h *httpBroker) Subscribe(topic string, handler Handler) (Subscriber, error
return subscriber, nil return subscriber, nil
} }
func (h *httpBroker) String() string {
return "http"
}

View File

@ -35,6 +35,7 @@ type Client interface {
Stream(ctx context.Context, req Request, opts ...CallOption) (Streamer, error) Stream(ctx context.Context, req Request, opts ...CallOption) (Streamer, error)
StreamRemote(ctx context.Context, addr string, req Request, opts ...CallOption) (Streamer, error) StreamRemote(ctx context.Context, addr string, req Request, opts ...CallOption) (Streamer, error)
Publish(ctx context.Context, p Publication, opts ...PublishOption) error Publish(ctx context.Context, p Publication, opts ...PublishOption) error
String() string
} }
type Publication interface { type Publication interface {
@ -122,3 +123,7 @@ func NewProtoRequest(service, method string, request interface{}, reqOpts ...Req
func NewJsonRequest(service, method string, request interface{}, reqOpts ...RequestOption) Request { func NewJsonRequest(service, method string, request interface{}, reqOpts ...RequestOption) Request {
return DefaultClient.NewJsonRequest(service, method, request, reqOpts...) return DefaultClient.NewJsonRequest(service, method, request, reqOpts...)
} }
func String() string {
return DefaultClient.String()
}

View File

@ -258,3 +258,7 @@ func (r *rpcClient) NewProtoRequest(service, method string, request interface{},
func (r *rpcClient) NewJsonRequest(service, method string, request interface{}, reqOpts ...RequestOption) Request { func (r *rpcClient) NewJsonRequest(service, method string, request interface{}, reqOpts ...RequestOption) Request {
return newRpcRequest(service, method, request, "application/json", reqOpts...) return newRpcRequest(service, method, request, "application/json", reqOpts...)
} }
func (r *rpcClient) String() string {
return "rpc"
}

View File

@ -76,6 +76,10 @@ func (n *dcSelector) Close() error {
return nil return nil
} }
func (n *dcSelector) String() string {
return "dc"
}
// Return a new first node selector // Return a new first node selector
func DCSelector(opts ...selector.Option) selector.Selector { func DCSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options var sopts selector.Options

View File

@ -66,6 +66,10 @@ func (n *firstNodeSelector) Close() error {
return nil return nil
} }
func (n *firstNodeSelector) String() string {
return "first"
}
// Return a new first node selector // Return a new first node selector
func FirstNodeSelector(opts ...selector.Option) selector.Selector { func FirstNodeSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options var sopts selector.Options

View File

@ -212,3 +212,7 @@ func (c *consulRegistry) ListServices() ([]*Service, error) {
func (c *consulRegistry) Watch() (Watcher, error) { func (c *consulRegistry) Watch() (Watcher, error) {
return newConsulWatcher(c) return newConsulWatcher(c)
} }
func (c *consulRegistry) String() string {
return "consul"
}

View File

@ -65,6 +65,10 @@ func (m *MockRegistry) Watch() (registry.Watcher, error) {
return nil, nil return nil, nil
} }
func (m *MockRegistry) String() string {
return "mock"
}
func NewRegistry() *MockRegistry { func NewRegistry() *MockRegistry {
return &MockRegistry{} return &MockRegistry{}
} }

View File

@ -6,6 +6,7 @@ type Registry interface {
GetService(string) ([]*Service, error) GetService(string) ([]*Service, error)
ListServices() ([]*Service, error) ListServices() ([]*Service, error)
Watch() (Watcher, error) Watch() (Watcher, error)
String() string
} }
type Option func(*Options) type Option func(*Options)
@ -37,3 +38,7 @@ func ListServices() ([]*Service, error) {
func Watch() (Watcher, error) { func Watch() (Watcher, error) {
return DefaultRegistry.Watch() return DefaultRegistry.Watch()
} }
func String() string {
return DefaultRegistry.String()
}

View File

@ -143,6 +143,10 @@ func (r *blackListSelector) Close() error {
return nil return nil
} }
func (r *blackListSelector) String() string {
return "blacklist"
}
func NewSelector(opts ...selector.Option) selector.Selector { func NewSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options var sopts selector.Options

View File

@ -74,6 +74,10 @@ func (r *randomSelector) Close() error {
return nil return nil
} }
func (r *randomSelector) String() string {
return "random"
}
func newRandomSelector(opts ...Option) Selector { func newRandomSelector(opts ...Option) Selector {
var sopts Options var sopts Options

View File

@ -73,6 +73,10 @@ func (r *roundRobinSelector) Close() error {
return nil return nil
} }
func (r *roundRobinSelector) String() string {
return "roundrobin"
}
func NewSelector(opts ...selector.Option) selector.Selector { func NewSelector(opts ...selector.Option) selector.Selector {
var sopts selector.Options var sopts selector.Options

View File

@ -72,6 +72,8 @@ type Selector interface {
Reset(service string) Reset(service string)
// Close renders the selector unusable // Close renders the selector unusable
Close() error Close() error
// Name of the selector
String() string
} }
// Next is a function that returns the next node // Next is a function that returns the next node

View File

@ -185,6 +185,8 @@ func (s *rpcServer) Register() error {
Metadata: config.Metadata(), Metadata: config.Metadata(),
} }
node.Metadata["transport"] = config.transport.String()
s.RLock() s.RLock()
var endpoints []*registry.Endpoint var endpoints []*registry.Endpoint
for _, e := range s.handlers { for _, e := range s.handlers {
@ -309,3 +311,7 @@ func (s *rpcServer) Stop() error {
s.exit <- ch s.exit <- ch
return <-ch return <-ch
} }
func (s *rpcServer) String() string {
return "rpc"
}

View File

@ -49,6 +49,7 @@ type Server interface {
Deregister() error Deregister() error
Start() error Start() error
Stop() error Stop() error
String() string
} }
type Publication interface { type Publication interface {
@ -183,3 +184,7 @@ func Stop() error {
log.Infof("Stopping server") log.Infof("Stopping server")
return DefaultServer.Stop() return DefaultServer.Stop()
} }
func String() string {
return DefaultServer.String()
}

View File

@ -308,6 +308,10 @@ func (h *httpTransport) Listen(addr string) (Listener, error) {
}, nil }, nil
} }
func (h *httpTransport) String() string {
return "http"
}
func newHttpTransport(addrs []string, opt ...Option) *httpTransport { func newHttpTransport(addrs []string, opt ...Option) *httpTransport {
return &httpTransport{} return &httpTransport{}
} }

View File

@ -26,6 +26,7 @@ type Listener interface {
type Transport interface { type Transport interface {
Dial(addr string, opts ...DialOption) (Client, error) Dial(addr string, opts ...DialOption) (Client, error)
Listen(addr string) (Listener, error) Listen(addr string) (Listener, error)
String() string
} }
type options struct{} type options struct{}
@ -59,3 +60,7 @@ func Dial(addr string, opts ...DialOption) (Client, error) {
func Listen(addr string) (Listener, error) { func Listen(addr string) (Listener, error) {
return DefaultTransport.Listen(addr) return DefaultTransport.Listen(addr)
} }
func String() string {
return DefaultTransport.String()
}