generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
302 lines (249 loc) · 7.89 KB
/
application.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package configkit
import (
"context"
"reflect"
"github.com/dogmatiq/configkit/internal/typename/goreflect"
"github.com/dogmatiq/configkit/internal/validation"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
)
// Application is an interface that represents the configuration of a Dogma
// application.
type Application interface {
Entity
// Handlers returns the handlers within this application.
Handlers() HandlerSet
}
// RichApplication is a specialization of Application that exposes information
// about the Go types used to implement the Dogma application.
type RichApplication interface {
RichEntity
// Handlers returns the handlers within this application.
Handlers() HandlerSet
// RichHandlers returns the handlers within this application.
RichHandlers() RichHandlerSet
// Application returns the underlying application.
Application() dogma.Application
}
// FromApplication returns the configuration for an application.
//
// It panics if the application is configured incorrectly. Use Recover() to
// convert configuration related panic values to errors.
func FromApplication(a dogma.Application) RichApplication {
cfg := &richApplication{app: a}
a.Configure(&applicationConfigurer{cfg})
mustHaveValidIdentity(
cfg.Identity(),
cfg.ReflectType(),
)
return cfg
}
// IsApplicationEqual compares two applications for equality.
//
// It returns true if both applications:
//
// 1. have the same identity
// 2. produce and consume the same messages, with the same roles
// 3. are implemented using the same Go types
// 4. contain equivalent handlers
//
// Point 3. refers to the type used to implement the dogma.Application interface
// (not the type used to implement the configkit.Application interface).
func IsApplicationEqual(a, b Application) bool {
return a.Identity() == b.Identity() &&
a.TypeName() == b.TypeName() &&
a.MessageNames().IsEqual(b.MessageNames()) &&
a.Handlers().IsEqual(b.Handlers())
}
// richApplication is the default implementation of [RichApplication].
type richApplication struct {
ident Identity
types EntityMessageTypes
handlers RichHandlerSet
app dogma.Application
}
func (a *richApplication) Identity() Identity {
return a.ident
}
func (a *richApplication) MessageNames() EntityMessageNames {
return a.types.asNames()
}
func (a *richApplication) MessageTypes() EntityMessageTypes {
return a.types
}
func (a *richApplication) TypeName() string {
return goreflect.NameOf(a.ReflectType())
}
func (a *richApplication) ReflectType() reflect.Type {
return reflect.TypeOf(a.app)
}
func (a *richApplication) AcceptVisitor(ctx context.Context, v Visitor) error {
return v.VisitApplication(ctx, a)
}
func (a *richApplication) AcceptRichVisitor(ctx context.Context, v RichVisitor) error {
return v.VisitRichApplication(ctx, a)
}
func (a *richApplication) Handlers() HandlerSet {
return a.handlers.asHandlerSet()
}
func (a *richApplication) RichHandlers() RichHandlerSet {
return a.handlers
}
func (a *richApplication) Application() dogma.Application {
return a.app
}
// applicationConfigurer is the default implementation of
// [dogma.ApplicationConfigurer].
type applicationConfigurer struct {
config *richApplication
}
func (c *applicationConfigurer) Identity(name, key string) {
if h, ok := c.config.handlers.ByKey(key); ok {
validation.Panicf(
`%s can not use the application key "%s", because it is already used by %s`,
c.config.ReflectType(),
key,
h.ReflectType(),
)
}
configureIdentity(&c.config.ident, name, key, c.config.ReflectType())
}
func (c *applicationConfigurer) RegisterAggregate(h dogma.AggregateMessageHandler, _ ...dogma.RegisterAggregateOption) {
c.registerIfConfigured(fromAggregateUnvalidated(h))
}
func (c *applicationConfigurer) RegisterProcess(h dogma.ProcessMessageHandler, _ ...dogma.RegisterProcessOption) {
c.registerIfConfigured(fromProcessUnvalidated(h))
}
func (c *applicationConfigurer) RegisterIntegration(h dogma.IntegrationMessageHandler, _ ...dogma.RegisterIntegrationOption) {
c.registerIfConfigured(fromIntegrationUnvalidated(h))
}
func (c *applicationConfigurer) RegisterProjection(h dogma.ProjectionMessageHandler, _ ...dogma.RegisterProjectionOption) {
c.registerIfConfigured(fromProjectionUnvalidated(h))
}
type validatableHandler interface {
RichHandler
// isConfigured returns true if the handler has been configured in any way
// beyond being disabled, even if the configuration is invalid.
isConfigured() bool
// mustValidate panics if the handler is not configured correctly.
mustValidate()
}
func (c *applicationConfigurer) registerIfConfigured(
h validatableHandler,
) {
if h.IsDisabled() && !h.isConfigured() {
return
}
h.mustValidate()
c.guardAgainstConflictingIdentities(h)
c.guardAgainstConflictingRoles(h)
c.guardAgainstConflictingRoutes(h)
if c.config.handlers == nil {
c.config.handlers = RichHandlerSet{}
}
c.config.handlers.Add(h)
types := h.MessageTypes()
for mt, r := range types.Produced {
if c.config.types.Produced == nil {
c.config.types.Produced = message.TypeRoles{}
}
c.config.types.Produced.Add(mt, r)
}
for mt, r := range types.Consumed {
if c.config.types.Consumed == nil {
c.config.types.Consumed = message.TypeRoles{}
}
c.config.types.Consumed.Add(mt, r)
}
}
// guardAgainstConflictingIdentities panics if h's identity conflicts with the
// application or any other handlers.
func (c *applicationConfigurer) guardAgainstConflictingIdentities(h RichHandler) {
appIdent := c.config.Identity()
handlerIdent := h.Identity()
if handlerIdent.Key == appIdent.Key {
validation.Panicf(
`%s can not use the handler key "%s", because it is already used by %s`,
h.ReflectType(),
handlerIdent.Key,
c.config.ReflectType(),
)
}
if x, ok := c.config.handlers.ByName(handlerIdent.Name); ok {
validation.Panicf(
`%s can not use the handler name "%s", because it is already used by %s`,
h.ReflectType(),
handlerIdent.Name,
x.ReflectType(),
)
}
if x, ok := c.config.handlers.ByKey(handlerIdent.Key); ok {
validation.Panicf(
`%s can not use the handler key "%s", because it is already used by %s`,
h.ReflectType(),
handlerIdent.Key,
x.ReflectType(),
)
}
}
// guardAgainstConflictingRoles panics if h configures any messages in roles
// contrary to the way they are configured by any other handler.
func (c *applicationConfigurer) guardAgainstConflictingRoles(h RichHandler) {
for mt, r := range h.MessageTypes().All() {
xr, ok := c.config.types.RoleOf(mt)
if !ok || xr == r {
continue
}
// we know there's a conflict, now we just need to find a handler that
// refers to this message type as some other role.
xh, _ := c.config.handlers.Find(func(h RichHandler) bool {
x, ok := h.MessageTypes().RoleOf(mt)
return ok && x != r
})
validation.Panicf(
`%s (%s) configures %s as a %s but %s (%s) configures it as a %s`,
h.ReflectType(),
h.Identity().Name,
mt,
r,
xh.ReflectType(),
xh.Identity().Name,
xr,
)
}
}
// guardAgainstConflictingRoutes panics if an h consumes the same commands or
// produces the same events as some existing handler.
func (c *applicationConfigurer) guardAgainstConflictingRoutes(h RichHandler) {
types := h.MessageTypes()
for mt, r := range types.Consumed {
if r != message.CommandRole {
continue
}
for _, x := range c.config.handlers.ConsumersOf(mt) {
validation.Panicf(
`%s (%s) can not handle %s commands because they are already configured to be handled by %s (%s)`,
h.ReflectType(),
h.Identity().Name,
mt,
x.ReflectType(),
x.Identity().Name,
)
}
}
for mt, r := range types.Produced {
if r != message.EventRole {
continue
}
for _, x := range c.config.handlers.ProducersOf(mt) {
validation.Panicf(
`%s (%s) can not record %s events because they are already configured to be recorded by %s (%s)`,
h.ReflectType(),
h.Identity().Name,
mt,
x.ReflectType(),
x.Identity().Name,
)
}
}
}