Documentation
¶
Overview ¶
Package interactor provides a flexible library for running use cases.
This package allows registering use cases for different request types, efficiently dispatching requests to the appropriate handlers.
Example usage:
func ExampleDispatcher() {
// arrange
useCaseRunner := &ConcreteUseCase{res: 42}
dispatcher := interactor.NewDispatcher()
dispatcher.RegisterFn(TestRequest{}, interactor.Must(interactor.Adapt(useCaseRunner)))
// act
var res TestResponse
if err := dispatcher.Run(context.Background(), TestRequest{}, &res); err != nil {
log.Fatal(err)
}
fmt.Printf("The answer to life the universe and everything: %d\n", res.result)
// Output:
// The answer to life the universe and everything: 42
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUseCaseRunnerNotFound = errors.New("use case runner not registered for the given request type") ErrInvalidUseCaseRunnerSignature = errors.New("useCaseRunner must have 3 input params") ErrUseCaseRunnerIsNotAFunction = errors.New("useCaseRunner is not a function") ErrUseCaseRunnerHasNoRunMethod = errors.New("useCaseRunner has no valid Run method") ErrFirstArgHasInvalidType = errors.New("first input argument must have context.Context type") ErrSecondArgHasInvalidType = errors.New("second input argument must implement Request interface") ErrThirdArgHasInvalidType = errors.New("third input argument must implement Response interface") ErrResultTypeMismatch = errors.New("result type mismatch") )
Guard errors.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher manages registered UseCaseRunners and dispatches requests to the appropriate UseCaseRunner.
Example ¶
// arrange
useCaseRunner := &ConcreteUseCase{}
dispatcher := interactor.NewDispatcher()
dispatcher.Register(TestRequest{}, interactor.MustAdapt(useCaseRunner))
// act
var res TestResponse
if err := dispatcher.Run(context.Background(), TestRequest{id: 42}, &res); err != nil {
log.Fatal(err)
}
fmt.Printf("The answer to life the universe and everything: %d\n", res.result)
Output: The answer to life the universe and everything: 42
func NewDispatcher ¶
func NewDispatcher() *Dispatcher
NewDispatcher creates a new Dispatcher instance.
func (*Dispatcher) Register ¶
func (d *Dispatcher) Register(request Request, runner UseCaseRunnerFn)
Register registers the given UseCaseRunner for the provided request type.
type Request ¶
type Request interface{}
Request is an interface representing the input for the use case.
type Response ¶
type Response interface{}
Response is an interface representing the output from the use case.
type UseCaseRunner ¶
type UseCaseRunner interface {
// Run executes the given request and writes the result to the provided response.
Run(ctx context.Context, req Request, resp Response) error
}
UseCaseRunner is an interface that defines a contract for running a use case.
type UseCaseRunnerFn ¶
UseCaseRunnerFn allows using pure functions as a UseCaseRunner.
func Adapt ¶
func Adapt(runner interface{}) (UseCaseRunnerFn, error)
Adapt is a helper function that converts a struct with a Run method into a UseCaseRunnerFn.
The function `Run` must have the following signature:
Have 3 arguments:
- ctx context.Context,
- req a struct which implements Request interface,
- res a pointer to a struct which implements Response interface.
Return an error
An example signature may look like as follows:
func (uc *UseCase) Run(ctx context.Context, req TestRequest, res *TestResponse) error
func Func ¶
func Func(fn interface{}) (UseCaseRunnerFn, error)
Func is a helper function that converts a function with the appropriate signature into a UseCaseRunnerFn.
The function must have the following signature:
Have 3 arguments:
- ctx context.Context,
- req a struct which implements Request interface,
- res a pointer to a struct which implements Response interface.
Return an error
An example signature may look like as follows:
func(ctx context.Context, req TestRequest, res *TestResponse) error
func Must ¶
func Must(fn UseCaseRunnerFn, err error) UseCaseRunnerFn
Must is a wrapper around Func which panics if an error occurs.
It is useful for tests and for cases where you are sure that the signature is valid. You can use it like this:
interactor.Must(interactor.Func(userCaseRunner.run))
func MustAdapt ¶
func MustAdapt(fn interface{}) UseCaseRunnerFn
MustAdapt is a wrapper around Adapt which panics if an error occurs.
It is useful for tests and for cases where you are sure that the runner has a `Ru`n method with a valid signature. You can use it like this:
interactor.MustAdapt(userCaseRunner)