-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.dispatch.go
89 lines (72 loc) · 1.96 KB
/
action.dispatch.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package testkit
import (
"context"
"fmt"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/enginekit/message"
"github.com/dogmatiq/testkit/internal/inflect"
"github.com/dogmatiq/testkit/internal/validation"
"github.com/dogmatiq/testkit/location"
)
// ExecuteCommand returns an Action that executes a command message.
func ExecuteCommand(m dogma.Command) Action {
if m == nil {
panic("ExecuteCommand(<nil>): message must not be nil")
}
mt := message.TypeOf(m)
if err := m.Validate(validation.CommandValidationScope()); err != nil {
panic(fmt.Sprintf("ToRecordEvent(%s): %s", mt, err))
}
return dispatchAction{
m,
location.OfCall(),
}
}
// RecordEvent returns an Action that records an event message.
func RecordEvent(m dogma.Event) Action {
if m == nil {
panic("RecordEvent(<nil>): message must not be nil")
}
mt := message.TypeOf(m)
if err := m.Validate(validation.EventValidationScope()); err != nil {
panic(fmt.Sprintf("RecordEvent(%s): %s", mt, err))
}
return dispatchAction{
m,
location.OfCall(),
}
}
// dispatchAction is an implementation of Action that dispatches a message to
// the engine.
type dispatchAction struct {
m dogma.Message
loc location.Location
}
func (a dispatchAction) Caption() string {
mt := message.TypeOf(a.m)
return inflect.Sprintf(
mt.Kind(),
"<producing> %s <message>",
mt,
)
}
func (a dispatchAction) Location() location.Location {
return a.loc
}
func (a dispatchAction) ConfigurePredicate(*PredicateOptions) {
}
func (a dispatchAction) Do(ctx context.Context, s ActionScope) error {
mt := message.TypeOf(a.m)
// TODO: These checks should result in information being added to the
// report, not just returning an error.
//
// See https://github.com/dogmatiq/testkit/issues/162
if _, ok := s.App.MessageTypes()[mt]; !ok {
return inflect.Errorf(
mt.Kind(),
"cannot <produce> <message>, %s is a not a recognized message type",
mt,
)
}
return s.Engine.Dispatch(ctx, a.m, s.OperationOptions...)
}