Documentation
¶
Overview ¶
Package interactor runs use cases and dispatches requests to them by request type.
A use case is any type with a Run method taking a request and returning a response:
type Greet struct{}
func (Greet) Run(ctx context.Context, req Hello) (Greeting, error) { ... }
Register it on a Dispatcher, then dispatch requests of that type:
var d interactor.Dispatcher
d.MustRegister(Greet{}) // or Register, to handle the error
greeting, err := d.Dispatch[Greeting](ctx, Hello{Name: "Gopher"})
Types are checked by the compiler; only the wiring (a request type nobody registered, or a response type other than the registered one) is checked when dispatching.
Example ¶
package main
import (
"context"
"fmt"
"github.com/screwyprof/interactor/v3"
)
type Hello struct{ Name string }
type Greeting struct{ Text string }
// Greet is a use case: a type with a Run method from a request to a response.
type Greet struct{}
func (Greet) Run(_ context.Context, req Hello) (Greeting, error) {
return Greeting{Text: "Hello, " + req.Name + "!"}, nil
}
func main() {
var d interactor.Dispatcher
d.MustRegister(Greet{})
greeting, err := d.Dispatch[Greeting](context.Background(), Hello{Name: "Gopher"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(greeting.Text)
}
Output: Hello, Gopher!
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUseCaseNotFound = errors.New("interactor: no use case registered for request type") ErrAlreadyRegistered = errors.New("interactor: use case already registered for request type") ErrNilUseCase = errors.New("interactor: use case must not be nil") ErrResponseTypeMismatch = errors.New("interactor: registered use case returns a different response type") )
Errors returned by Dispatcher.Register and Dispatcher.Dispatch. They come back wrapped with the types involved, so match them with errors.Is.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
// contains filtered or unexported fields
}
Dispatcher routes a request to the use case registered for its type.
The zero value is ready to use. A Dispatcher is safe for concurrent use and must not be copied after first use.
func (*Dispatcher) Dispatch ¶
func (d *Dispatcher) Dispatch[Res, Req any](ctx context.Context, req Req) (Res, error)
Dispatch runs the use case registered for req's type and returns its response.
Res comes first so callers name only the response type and the request type is inferred:
greeting, err := d.Dispatch[Greeting](ctx, Hello{})
The use case is found by Req, the request's static type: a request held in an interface variable is looked up as that interface type, not its dynamic type. Res must be exactly the registered use case's response type.
It returns an error wrapping ErrUseCaseNotFound if nothing is registered for Req, or ErrResponseTypeMismatch if the registered use case returns a type other than Res. ctx is passed to the use case as given. Errors from the use case itself are returned unchanged, including any it gets from dispatching further requests, together with whatever response it returned alongside the error.
Example (NotFound) ¶
package main
import (
"context"
"errors"
"fmt"
"github.com/screwyprof/interactor/v3"
)
type Hello struct{ Name string }
type Greeting struct{ Text string }
func main() {
var d interactor.Dispatcher
_, err := d.Dispatch[Greeting](context.Background(), Hello{Name: "Gopher"})
fmt.Println(errors.Is(err, interactor.ErrUseCaseNotFound))
}
Output: true
func (*Dispatcher) MustRegister ¶
func (d *Dispatcher) MustRegister[Req, Res any](uc UseCase[Req, Res])
MustRegister is like Dispatcher.Register but panics if registration fails. Use it when wiring an application at startup, where a duplicate or nil use case is a programming mistake.
func (*Dispatcher) Register ¶
func (d *Dispatcher) Register[Req, Res any](uc UseCase[Req, Res]) error
Register adds uc as the use case for requests of type Req.
Request types match exactly: Req and *Req are different types. For a nil use case the error wraps ErrNilUseCase, and if Req already has one it wraps ErrAlreadyRegistered; both name the request type.
Register is meant for wiring at startup: each call copies the registrations, so that Dispatch can read them without locking.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"github.com/screwyprof/interactor/v3"
)
type Hello struct{ Name string }
type Greeting struct{ Text string }
// Greet is a use case: a type with a Run method from a request to a response.
type Greet struct{}
func (Greet) Run(_ context.Context, req Hello) (Greeting, error) {
return Greeting{Text: "Hello, " + req.Name + "!"}, nil
}
func main() {
var d interactor.Dispatcher
d.MustRegister(Greet{})
// Register returns the error instead, for wiring that is decided at runtime.
err := d.Register(Greet{})
fmt.Println(errors.Is(err, interactor.ErrAlreadyRegistered))
}
Output: true
type UseCaseFunc ¶
UseCaseFunc lets a plain function be a UseCase, like net/http.HandlerFunc.
Example ¶
package main
import (
"context"
"fmt"
"github.com/screwyprof/interactor/v3"
)
type Hello struct{ Name string }
type Greeting struct{ Text string }
func main() {
shout := interactor.UseCaseFunc[Hello, Greeting](func(_ context.Context, req Hello) (Greeting, error) {
return Greeting{Text: "HELLO, " + req.Name + "!"}, nil
})
var d interactor.Dispatcher
d.MustRegister(shout)
greeting, err := d.Dispatch[Greeting](context.Background(), Hello{Name: "Gopher"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(greeting.Text)
}
Output: HELLO, Gopher!