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.
Register a use case for its request type, then run requests through the dispatcher:
dispatcher := interactor.NewDispatcher()
dispatcher.Register(MyRequest{}, interactor.MustAdapt(&MyUseCase{}))
var res MyResponse
err := dispatcher.Run(ctx, MyRequest{}, &res)
See the examples for complete, runnable code.
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("use case runner must have 3 input params") ErrUseCaseRunnerIsNotAFunction = errors.New("use case runner is not a function") ErrUseCaseRunnerHasNoRunMethod = errors.New("use case runner 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") ErrRequestTypeMismatch = errors.New("request type mismatch") ErrInvalidReturnType = errors.New("use case runner must return a single error") ErrNilContext = errors.New("context must not be nil") )
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.
The zero value is ready to use.
It is safe for concurrent use.
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 Response ¶
type Response any
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 any) (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 any) (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(useCaseRunner.Run))
func MustAdapt ¶
func MustAdapt(fn any) 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 `Run` method with a valid signature. You can use it like this:
interactor.MustAdapt(useCaseRunner)
Example ¶
package main
import (
"context"
"fmt"
"log"
"github.com/screwyprof/interactor/v2"
)
// ReadmeRequest represents a test request.
type ReadmeRequest struct{}
// ReadmeResponse represents a test response.
type ReadmeResponse struct {
Result int
}
// ReadmeUseCase is an example use case implementation.
type ReadmeUseCase struct {
Res int
}
// Run runs the use case with the given request.
func (uc ReadmeUseCase) Run(_ context.Context, _ ReadmeRequest, res *ReadmeResponse) error {
res.Result = uc.Res
return nil
}
func main() {
// Create a use case.
useCaseRunner := &ReadmeUseCase{Res: 42}
// Create a new dispatcher and register the use case runner.
dispatcher := interactor.NewDispatcher()
dispatcher.Register(ReadmeRequest{}, interactor.MustAdapt(useCaseRunner))
// Run the use case
var res ReadmeResponse
if err := dispatcher.Run(context.Background(), ReadmeRequest{}, &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