-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractor.go
48 lines (42 loc) · 1.58 KB
/
interactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 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
// }
package interactor
import "context"
// Request is an interface representing the input for the use case.
type Request interface{}
// Response is an interface representing the output from the use case.
type Response interface{}
// UseCaseRunner is an interface that defines a contract for running a use case.
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
}
// UseCaseRunnerFn allows using pure functions as a UseCaseRunner.
type UseCaseRunnerFn func(ctx context.Context, req Request, resp Response) error
// Run executes the given request and writes the result to the provided response.
func (fn UseCaseRunnerFn) Run(ctx context.Context, req Request, resp Response) error {
return fn(ctx, req, resp)
}