forked from hellofresh/goengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projection.go
64 lines (54 loc) · 1.83 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
package goengine
import "context"
type (
// MessageHandler is a func that can do state changes based on a message
MessageHandler func(ctx context.Context, state interface{}, message Message) (interface{}, error)
// Query contains the information of a query
//
// Example when querying the total the amount of deposits the query could be as follows.
// type TotalDepositState struct {
// deposited int
// times int
// }
//
// type TotalDepositQuery struct {}
// func (q *TotalDepositQuery) Init(ctx context.Context) (interface{}, error) {
// return TotalDepositState{}, nil
// }
// func (q *TotalDepositQuery) Handlers() interface{} {
// return map[string]MessageHandler{
// "deposited": func(ctx context.Context, state interface{}, message goengine.Message) (interface{}, error) {
// depositState := state.(TotalDepositState)
//
// switch event := message.Payload().(type) {
// case AccountDebited:
// depositState.deposited += event.Amount
// }
//
// return depositState, nil
// },
// }
// }
Query interface {
// Init initializes the state of the Query
Init(ctx context.Context) (interface{}, error)
// Handlers return the handlers for a set of messages
Handlers() map[string]MessageHandler
}
// Projection contains the information of a projection
Projection interface {
Query
// Name returns the name of the projection
Name() string
// FromStream returns the stream this projection is based on
FromStream() StreamName
}
// ProjectionSaga is a projection that contains state data
ProjectionSaga interface {
Projection
// DecodeState reconstitute the projection state based on the provided state data
DecodeState(data []byte) (interface{}, error)
// EncodeState encode the given object for storage
EncodeState(obj interface{}) ([]byte, error)
}
)