fix linting (#4)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-11-03 01:08:23 +03:00
committed by GitHub
parent e6ab6d50eb
commit 40b0870cf8
36 changed files with 218 additions and 188 deletions

View File

@@ -4,7 +4,7 @@ package broker
import "context"
var (
DefaultBroker Broker = &NoopBroker{opts: NewOptions()}
DefaultBroker Broker = NewBroker()
)
// Broker is an interface used for asynchronous messaging.

View File

@@ -2,7 +2,7 @@ package broker
import "context"
type NoopBroker struct {
type noopBroker struct {
opts Options
}
@@ -11,7 +11,13 @@ type noopSubscriber struct {
opts SubscribeOptions
}
func (n *NoopBroker) Init(opts ...Option) error {
// NewBroker returns new noop broker
func NewBroker(opts ...Option) Broker {
return &noopBroker{opts: NewOptions(opts...)}
}
// Init initialize broker
func (n *noopBroker) Init(opts ...Option) error {
for _, o := range opts {
o(&n.opts)
}
@@ -19,48 +25,53 @@ func (n *NoopBroker) Init(opts ...Option) error {
return nil
}
func (n *NoopBroker) Options() Options {
// Options returns broker Options
func (n *noopBroker) Options() Options {
return n.opts
}
func (n *NoopBroker) Address() string {
// Address returns broker address
func (n *noopBroker) Address() string {
return ""
}
func (n *NoopBroker) Connect(ctx context.Context) error {
// Connect connects to broker
func (n *noopBroker) Connect(ctx context.Context) error {
return nil
}
func (n *NoopBroker) Disconnect(ctx context.Context) error {
// Disconnect disconnects from broker
func (n *noopBroker) Disconnect(ctx context.Context) error {
return nil
}
func (n *NoopBroker) Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error {
// Publish publishes message to broker
func (n *noopBroker) Publish(ctx context.Context, topic string, m *Message, opts ...PublishOption) error {
return nil
}
func (n *NoopBroker) Subscribe(ctx context.Context, topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) {
options := NewSubscribeOptions()
for _, o := range opts {
o(&options)
}
// Subscribe subscribes to broker topic
func (n *noopBroker) Subscribe(ctx context.Context, topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) {
options := NewSubscribeOptions(opts...)
return &noopSubscriber{topic: topic, opts: options}, nil
}
func (n *NoopBroker) String() string {
// String return broker string representation
func (n *noopBroker) String() string {
return "noop"
}
// Options returns subscriber options
func (n *noopSubscriber) Options() SubscribeOptions {
return n.opts
}
// TOpic returns subscriber topic
func (n *noopSubscriber) Topic() string {
return n.topic
}
// Unsubscribe unsbscribes from broker topic
func (n *noopSubscriber) Unsubscribe(ctx context.Context) error {
return nil
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/unistack-org/micro/v3/registry"
)
// Options struct
type Options struct {
Addrs []string
Secure bool
@@ -27,6 +28,7 @@ type Options struct {
Context context.Context
}
// NewOptions create new Options
func NewOptions(opts ...Option) Options {
options := Options{
Registry: registry.DefaultRegistry,
@@ -39,30 +41,34 @@ func NewOptions(opts ...Option) Options {
return options
}
// Context sets the context option
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// PublishOptions struct
type PublishOptions struct {
// Other options for implementations of the interface
// can be stored in a context
Context context.Context
}
// NewPublishOptions creates PublishOptions struct
func NewPublishOptions(opts ...PublishOption) PublishOptions {
opt := PublishOptions{
options := PublishOptions{
Context: context.Background(),
}
for _, o := range opts {
o(&opt)
o(&options)
}
return opt
return options
}
// SubscribeOptions struct
type SubscribeOptions struct {
// AutoAck ack messages if handler returns nil err
AutoAck bool
@@ -80,30 +86,34 @@ type SubscribeOptions struct {
Context context.Context
}
// Option func
type Option func(*Options)
// PublishOption func
type PublishOption func(*PublishOptions)
// PublishContext set context
// PublishContext sets the context
func PublishContext(ctx context.Context) PublishOption {
return func(o *PublishOptions) {
o.Context = ctx
}
}
// SubscribeOption func
type SubscribeOption func(*SubscribeOptions)
// NewSubscribeOptions creates new SubscribeOptions
func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
opt := SubscribeOptions{
options := SubscribeOptions{
AutoAck: true,
Context: context.Background(),
}
for _, o := range opts {
o(&opt)
o(&options)
}
return opt
return options
}
// Addrs sets the host addresses to be used by the broker
@@ -121,6 +131,7 @@ func Codec(c codec.Marshaler) Option {
}
}
// DisableAutoAck disables auto ack
func DisableAutoAck() SubscribeOption {
return func(o *SubscribeOptions) {
o.AutoAck = false
@@ -151,6 +162,7 @@ func SubscribeErrorHandler(h Handler) SubscribeOption {
}
}
// Queue sets the subscribers sueue
func Queue(name string) SubscribeOption {
return func(o *SubscribeOptions) {
o.Group = name
@@ -164,6 +176,7 @@ func SubscribeGroup(name string) SubscribeOption {
}
}
// Registry sets registry option
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r