Documentation
¶
Index ¶
- Variables
- func Execute[TRequest any, TResult any](ctx context.Context, req TRequest, resultHint *TResult) (TResult, error)
- func MockCommand[TRequest any, TResult any]() *mockcommand[TRequest, TResult]
- func MockCommandError[TRequest any, TResult any](err error) *mockcommand[TRequest, TResult]
- func MockCommandResult[TRequest any, TResult any](result TResult) *mockcommand[TRequest, TResult]
- func MockCommandValidationError[TRequest any, TResult any](err error) *mockcommand[TRequest, TResult]
- func RegisterCommand[TRequest any, TResult any](ctx context.Context, cmd CommandHandler[TRequest, TResult]) error
- func RegisterMockCommand[TRequest any, TResult any](ctx context.Context, mock CommandHandler[TRequest, TResult]) func()
- type CommandAlreadyRegisteredError
- type CommandHandler
- type ConfigurationChecker
- type NoCommandForRequestTypeError
- type NoResultType
- type ResultTypeError
- type ValidationError
- type Validator
Constants ¶
This section is empty.
Variables ¶
var NoResult = new(NoResultType)
NoResult may be used in Execute calls to commands that do not return a result:
// calling the command _, err := mediator.Execute(ctx, MyCommand.Request{}, mediator.NoResult)
Since NoResultType is itself a pointer, when implementing a command that does not return a result simply return a nil result:
// implementing a command returning NoResultType func (c Command) Execute(ctx context.Context, req Request) (mediator.NoResultType, error) { if err := c.doSomething(); err != nil { return nil, err } return nil, nil }
Functions ¶
func Execute ¶
func Execute[TRequest any, TResult any](ctx context.Context, req TRequest, resultHint *TResult) (TResult, error)
Execute sends the specified request to the registered command for the request type. The result parameter is a type-hint, providing a pointer to a value of the expected result type. The result itself is returned by the function along with any error. The type-hint result pointer is otherwise ignored.
The result type-hint enables the GoLang compiler to infer both the request and result type for the generic function.
The type-hint pointer is not de-referenced so may be nil; alternatively the `new()` function may be used to create a pointer to the result type:
// call the Foo.Request command and capture the result in foo foo, err := mediator.Execute(ctx, Foo.Request{}, new(Foo.Result))
In the event of an error, the result will be the zero-value of the result type, otherwise it will be the value returned by the command.
A special case is when a command does not return any result.
Such a command would typically be registered with a result type of 'mediator.NoResultType' and called with a type-hint parameter of `mediator.NoResult`. The value returned by the Execute function is discarded in this case:
// call the Bar.Request command which returns only an error _, err := mediator.Execute(ctx, Bar.Request{}, mediator.NoResult)
If the command implements Validator and the validator returns an error, then the command Execute() function is not called and the error returned will be a ValidationError wrapping the error.
func MockCommand ¶
MockCommand registers a mock command for the specified request and result type modelling successful execution of the command returning a zero-value result and no error.
func MockCommandError ¶
MockCommandError registers a mock command for the specified request and result type modelling a failed execution of the command, returning the specified error and a zero-value result.
func MockCommandResult ¶
MockCommandResult registers a mock command for the specified request and result type modelling successful execution of the command returning the specified result and a nil error.
func MockCommandValidationError ¶
func MockCommandValidationError[TRequest any, TResult any](err error) *mockcommand[TRequest, TResult]
MockCommandValidationError registers a mock command for the specified request and result type modelling a failed validation of the request.
The specified error will be returned by the validator of the mocked command (and will therefore be wrapped in a ValidationError).
func RegisterCommand ¶
func RegisterCommand[TRequest any, TResult any](ctx context.Context, cmd CommandHandler[TRequest, TResult]) error
RegisterCommand[TRequest, TResult] registers a command returning a specific result type for the specified request type.
If a command is already registered for the request type the function will return a CommandAlreadyRegisteredError.
If the command being registered implements the ConfigurationChecker interface, this is called and any error returned.
If the command does not implementation ConfigurationChecker or the configuration check returns no error, then the command is registered.
func RegisterMockCommand ¶
func RegisterMockCommand[TRequest any, TResult any](ctx context.Context, mock CommandHandler[TRequest, TResult]) func()
RegisterMockCommand registers a custom mock command, returning a function to unregister the mock when no longer required:
unreg := mediator.RegisterMockCommand[MyRequest, MyResult](myMock) defer unreg()
Custom mocks are useful when you want to mock a command that has a custom validator or executor. If a custom mock implements CheckConfiguration, it must not return any error from the configuration check.
If a custom mock fails configuration checks the registration will panic.
Types ¶
type CommandAlreadyRegisteredError ¶
type CommandAlreadyRegisteredError struct {
// contains filtered or unexported fields
}
NoCommandForRequestTypeError is returned by Execute if there is no command registered for the request and result type involved.
func (CommandAlreadyRegisteredError) Error ¶
func (e CommandAlreadyRegisteredError) Error() string
func (CommandAlreadyRegisteredError) Is ¶
func (e CommandAlreadyRegisteredError) Is(target error) bool
type CommandHandler ¶
type CommandHandler[TRequest any, TResult any] interface { Execute(context.Context, TRequest) (TResult, error) }
CommandHandler[TRequest, TResult] is the one interface that MUST be implemented by a command.
It provides the Execute function that is called by the mediator.
type ConfigurationChecker ¶
ConfigurationChecker is an optional interface that may be implemented by a command to separate any configuration checks from validation of any specific request or the execution of the command.
If implemented, CheckConfiguration is called when registering a command. if an error is returned, command registration fails and the error is returned from RegisterCommand.
type NoCommandForRequestTypeError ¶
type NoCommandForRequestTypeError struct {
// contains filtered or unexported fields
}
NoCommandForRequestTypeError is returned by Execute if there is no command registered for the request and result type involved.
func (NoCommandForRequestTypeError) Error ¶
func (e NoCommandForRequestTypeError) Error() string
func (NoCommandForRequestTypeError) Is ¶
func (e NoCommandForRequestTypeError) Is(target error) bool
type NoResultType ¶
type NoResultType *int
NoResultType may be used as the TResult of a command when the command does not return a result. The NoResult value may then be used in Execute calls.
For example:
// to register a command with no result mediator.RegisterCommand[MyCommand.Request, mediator.NoResultType](MyCommand.Handler{}) // calling the command _, err := mediator.Execute(ctx, MyCommand.Request{}, mediator.NoResult)
type ResultTypeError ¶
type ResultTypeError struct {
// contains filtered or unexported fields
}
ResultTypeError is returned if the command registered for the specified request type does not return the result type expected by the caller.
func (ResultTypeError) Error ¶
func (e ResultTypeError) Error() string
func (ResultTypeError) Is ¶
func (e ResultTypeError) Is(target error) bool
type ValidationError ¶
type ValidationError struct {
E error
}
ValidationError is returned by a command when it is unable to process a request due to the request itself being invalid. The ValidationError wraps a specific error that identifies the problem with the request.
"request validation error: <specific error>"
func (ValidationError) Error ¶
func (e ValidationError) Error() string
func (ValidationError) Unwrap ¶
func (e ValidationError) Unwrap() error
type Validator ¶
Validator[TRequest] is an optional interface that may be implemented by a command to separate the validation of the request from the execution of the command.
If implemented, the mediator will the Validate function before passing a request to the Execute function; any error returned by Validate is returned (wrapped in a ValidationError).