forked from jeroenvanmaanen/dendrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projection.go
92 lines (77 loc) · 2.77 KB
/
projection.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
package trusted
import (
log "log"
axon_utils "github.com/dendrite2go/dendrite/src/pkg/axon_utils"
grpc_config "github.com/dendrite2go/dendrite/src/pkg/grpc/configuration"
)
// Redeclare event types, so that they can be extended with event handler methods.
type TrustedKeyAddedEvent struct {
grpc_config.TrustedKeyAddedEvent
}
type TrustedKeyRemovedEvent struct {
grpc_config.TrustedKeyRemovedEvent
}
type KeyManagerAddedEvent struct {
grpc_config.KeyManagerAddedEvent
}
type KeyManagerRemovedEvent struct {
grpc_config.KeyManagerRemovedEvent
}
// Projection
type Projection struct {
TrustedKeys map[string]string
KeyManagers map[string]string
AggregateState axon_utils.AggregateState
}
func (projection *Projection) GetAggregateState() axon_utils.AggregateState {
return projection.AggregateState
}
func RestoreProjection(aggregateIdentifier string, clientConnection *axon_utils.ClientConnection) *Projection {
return axon_utils.RestoreProjection("Trusted Keys", aggregateIdentifier, createInitialProjection, clientConnection, prepareUnmarshal).(*Projection)
}
func createInitialProjection() interface{} {
return &Projection{
TrustedKeys: make(map[string]string),
KeyManagers: make(map[string]string),
AggregateState: axon_utils.NewAggregateState(),
}
}
func (projection *Projection) Apply(event axon_utils.Event) {
event.ApplyTo(projection)
}
// Map an event name as stored in AxonServer to an object that has two aspects:
// 1. It is a proto.Message, so it can be unmarshalled from the Axon event
// 2. It is an axon_utils.Event, so it can be applied to a projection
func prepareUnmarshal(payloadType string) (event axon_utils.Event) {
log.Printf("Configuration Projection: Payload type: %v", payloadType)
switch payloadType {
case "TrustedKeyAddedEvent":
event = &TrustedKeyAddedEvent{}
case "TrustedKeyRemovedEvent":
event = &TrustedKeyRemovedEvent{}
case "KeyManagerAddedEvent":
event = &KeyManagerAddedEvent{}
case "KeyManagerRemovedEvent":
event = &KeyManagerRemovedEvent{}
default:
event = nil
}
return event
}
// Event Handlers
func (event *TrustedKeyAddedEvent) ApplyTo(projectionWrapper interface{}) {
projection := projectionWrapper.(*Projection)
projection.TrustedKeys[event.PublicKey.Name] = event.PublicKey.PublicKey
}
func (event *TrustedKeyRemovedEvent) ApplyTo(projectionWrapper interface{}) {
projection := projectionWrapper.(*Projection)
projection.TrustedKeys[event.Name] = ""
}
func (event *KeyManagerAddedEvent) ApplyTo(projectionWrapper interface{}) {
projection := projectionWrapper.(*Projection)
projection.KeyManagers[event.PublicKey.Name] = event.PublicKey.PublicKey
}
func (event *KeyManagerRemovedEvent) ApplyTo(projectionWrapper interface{}) {
projection := projectionWrapper.(*Projection)
projection.KeyManagers[event.Name] = ""
}