command

package module
v0.0.0-...-faf17a6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2016 License: MIT Imports: 4 Imported by: 0

README

GoDoc Codeship Codecov Go Report Card

Command

Command pattern for Go with thread safe serial and parallel dispatcher.

Installation

go get -u github.com/txgruppi/command

Tests

go get -u -t github.com/txgruppi/command
cd $GOPATH/src/github.com/txgruppi/command
go test ./...

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher interface {
	// Dispatch dispatches the given command to all known handlers.
	//
	// If no handler returns `true` for `CanHandle` call it will return a
	// `NoHandlerFoundError`.
	Dispatch(cmd interface{}) error

	// DispatchOptional works the same way as `Dispatch` but it will not return an
	// error if all handler's `CanHandle` call returns `false`.
	DispatchOptional(cmd interface{}) error

	// AppendHandlers append the given handler to the list of known handlers.
	//
	// This is useful if you want to add more handler after creating a dispatcher.
	AppendHandlers(handlers ...Handler)
}

Dispatcher defines the interface for a command dispatcher.

func NewParallelDispatcher

func NewParallelDispatcher(handlers []Handler) Dispatcher

NewParallelDispatcher creates a new PrallelDispatcher with the given handlers

func NewSerialDispatcher

func NewSerialDispatcher(handlers []Handler) Dispatcher

NewSerialDispatcher creates a new PrallelDispatcher with the given handlers

type Handler

type Handler interface {
	// CanHandle should return `true` whenever the given command can be handled
	// by this Handler, otherwise it should return `false`
	CanHandle(cmd interface{}) bool

	// Handle does all the *work* realted to the given command.
	//
	// It will only be called if CanHandle returns `true` for the given command.
	Handle(cmd interface{}, dispatcher Dispatcher) error
}

Handler defines the interface for a command handler.

type NoHandlerFoundError

type NoHandlerFoundError struct {
	Command interface{}
}

NoHandlerFoundError is returned when no handler returns `true` for the `CanHandle` call.

func (*NoHandlerFoundError) Error

func (e *NoHandlerFoundError) Error() string

type ParallelDispatcher

type ParallelDispatcher struct {
	// contains filtered or unexported fields
}

ParallelDispatcher is a command dispatcher wich will run all handlers in parallel and wait all handlers to finish before returning.

All errors returned by the handlers will be grouped in a `errorgroup.ErrorGroup`.

This dispatcher is *thread safe*.

func (*ParallelDispatcher) AppendHandlers

func (d *ParallelDispatcher) AppendHandlers(handlers ...Handler)

AppendHandlers implements `Dispatcher.AppendHandlers`

func (*ParallelDispatcher) Dispatch

func (d *ParallelDispatcher) Dispatch(cmd interface{}) (err error)

Dispatch implements `Dispatcher.Dispatch`

func (*ParallelDispatcher) DispatchOptional

func (d *ParallelDispatcher) DispatchOptional(cmd interface{}) (err error)

DispatchOptional implements `Dispatcher.DispatchOptional`

type SerialDispatcher

type SerialDispatcher struct {
	// contains filtered or unexported fields
}

SerialDispatcher is a command dispatcher wich will run all handlers in parallel and wait all handlers to finish before returning.

If any handler returns an error the dispatcher will stop execution and will return that error.

This dispatcher is *thread safe*.

func (*SerialDispatcher) AppendHandlers

func (d *SerialDispatcher) AppendHandlers(handlers ...Handler)

AppendHandlers implements `Dispatcher.AppendHandlers`

func (*SerialDispatcher) Dispatch

func (d *SerialDispatcher) Dispatch(cmd interface{}) (err error)

Dispatch implements `Dispatcher.Dispatch`

func (*SerialDispatcher) DispatchOptional

func (d *SerialDispatcher) DispatchOptional(cmd interface{}) (err error)

DispatchOptional implements `Dispatcher.DispatchOptional`

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL