generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
74 lines (58 loc) · 1.6 KB
/
aggregate.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
package configkit
import (
"context"
"reflect"
"github.com/dogmatiq/configkit/message"
"github.com/dogmatiq/dogma"
)
// Aggregate is an interface that represents the configuration of a Dogma
// aggregate message handler.
type Aggregate interface {
Handler
}
// RichAggregate is a specialization of Aggregate that exposes information
// about the Go types used to implement the underlying Dogma handler.
type RichAggregate interface {
RichHandler
// Handler returns the underlying message handler.
Handler() dogma.AggregateMessageHandler
}
// FromAggregate returns the configuration for an aggregate message handler.
//
// It panics if the handler is configured incorrectly. Use Recover() to convert
// configuration related panic values to errors.
func FromAggregate(h dogma.AggregateMessageHandler) RichAggregate {
cfg := &aggregate{
entity: entity{
rt: reflect.TypeOf(h),
},
impl: h,
}
c := &handlerConfigurer{
entityConfigurer: entityConfigurer{
entity: &cfg.entity,
},
}
h.Configure(c)
c.validate()
c.mustConsume(message.CommandRole)
c.mustProduce(message.EventRole)
return cfg
}
// aggregate is an implementation of RichAggregate.
type aggregate struct {
entity
impl dogma.AggregateMessageHandler
}
func (h *aggregate) AcceptVisitor(ctx context.Context, v Visitor) error {
return v.VisitAggregate(ctx, h)
}
func (h *aggregate) AcceptRichVisitor(ctx context.Context, v RichVisitor) error {
return v.VisitRichAggregate(ctx, h)
}
func (h *aggregate) HandlerType() HandlerType {
return AggregateHandlerType
}
func (h *aggregate) Handler() dogma.AggregateMessageHandler {
return h.impl
}