generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
116 lines (92 loc) · 2.99 KB
/
process.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package configkit
import (
"context"
"reflect"
"github.com/dogmatiq/configkit/internal/typename/goreflect"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/enginekit/message"
)
// Process is an interface that represents the configuration of a Dogma process
// message handler.
type Process interface {
Handler
}
// RichProcess is a specialization of Process that exposes information about the
// Go types used to implement the underlying Dogma handler.
type RichProcess interface {
RichHandler
// Handler returns the underlying message handler.
Handler() dogma.ProcessMessageHandler
}
// FromProcess returns the configuration for a process message handler.
//
// It panics if the handler is configured incorrectly. Use Recover() to convert
// configuration related panic values to errors.
func FromProcess(h dogma.ProcessMessageHandler) RichProcess {
cfg := fromProcessUnvalidated(h)
cfg.mustValidate()
return cfg
}
func fromProcessUnvalidated(h dogma.ProcessMessageHandler) *richProcess {
cfg := &richProcess{handler: h}
h.Configure(&processConfigurer{cfg})
return cfg
}
// richProcess is the default implementation of [RichProcess].
type richProcess struct {
ident Identity
types EntityMessages[message.Type]
isDisabled bool
handler dogma.ProcessMessageHandler
}
func (h *richProcess) Identity() Identity {
return h.ident
}
func (h *richProcess) MessageNames() EntityMessages[message.Name] {
return asMessageNames(h.types)
}
func (h *richProcess) MessageTypes() EntityMessages[message.Type] {
return h.types
}
func (h *richProcess) TypeName() string {
return goreflect.NameOf(h.ReflectType())
}
func (h *richProcess) ReflectType() reflect.Type {
return reflect.TypeOf(h.handler)
}
func (h *richProcess) IsDisabled() bool {
return h.isDisabled
}
func (h *richProcess) AcceptVisitor(ctx context.Context, v Visitor) error {
return v.VisitProcess(ctx, h)
}
func (h *richProcess) AcceptRichVisitor(ctx context.Context, v RichVisitor) error {
return v.VisitRichProcess(ctx, h)
}
func (h *richProcess) HandlerType() HandlerType {
return ProcessHandlerType
}
func (h *richProcess) Handler() dogma.ProcessMessageHandler {
return h.handler
}
func (h *richProcess) isConfigured() bool {
return !h.ident.IsZero() || len(h.types) != 0
}
func (h *richProcess) mustValidate() {
mustHaveValidIdentity(h.Identity(), h.ReflectType())
mustHaveConsumerRoute(&h.types, message.EventKind, h.Identity(), h.ReflectType())
mustHaveProducerRoute(&h.types, message.CommandKind, h.Identity(), h.ReflectType())
}
// processConfigurer is the default implementation of [dogma.ProcessConfigurer].
type processConfigurer struct {
config *richProcess
}
func (c *processConfigurer) Identity(name, key string) {
configureIdentity(&c.config.ident, name, key, c.config.ReflectType())
}
func (c *processConfigurer) Routes(routes ...dogma.ProcessRoute) {
configureRoutes(&c.config.types, routes, c.config.ident, c.config.ReflectType())
}
func (c *processConfigurer) Disable(...dogma.DisableOption) {
c.config.isDisabled = true
}