-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.go
108 lines (96 loc) · 2.89 KB
/
processor.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
package stream
import (
"errors"
"reflect"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// State of the entity.
type State interface {
Process(event InboundEvent) (outbound []OutboundEvent, err error)
}
// InboundEvents are received from external systems.
type InboundEvent interface {
EventName() string
IsInbound()
}
// OutboundEvents are send via EventBridge.
type OutboundEvent interface {
EventName() string
IsOutbound()
}
// Store is the interface that describes database operations.
type Store interface {
Get(id string, state State) (sequence int64, err error)
Put(id string, atSequence int64, state State, inbound []InboundEvent, outbound []OutboundEvent) error
Prepare(id string, atSequence int64, state State, inbound []InboundEvent, outbound []OutboundEvent) (items []types.TransactWriteItem, err error)
Execute(items []types.TransactWriteItem) error
}
// Processor of events.
type Processor struct {
store Store
id string
state State
sequence int64
}
// New creates a new, empty stream processor.
func New(store Store, id string, state State) (p *Processor, err error) {
if reflect.ValueOf(state).Kind() != reflect.Ptr {
err = errors.New("the state parameter must be a pointer")
return
}
p = &Processor{
store: store,
id: id,
state: state,
sequence: 0,
}
return
}
// Load the state from the data store. Pass a pointer to the state.
func Load(store Store, id string, state State) (p *Processor, err error) {
if reflect.ValueOf(state).Kind() != reflect.Ptr {
err = errors.New("the state parameter must be a pointer")
return
}
sequence, err := store.Get(id, state)
if err != nil {
return
}
p = &Processor{
store: store,
id: id,
state: state,
sequence: sequence,
}
return
}
// Process inbound events, then store the updated state and outbound events.
func (p *Processor) Process(events ...InboundEvent) error {
items, err := p.Prepare(events...)
if err != nil {
return err
}
return p.Execute(items)
}
// Prepare the transaction. Usually, you'd want to use the Process method, this method
// is for if you want to customise the underlying database transaction, e.g. by adding
// additional records.
func (p *Processor) Prepare(events ...InboundEvent) (items []types.TransactWriteItem, err error) {
var inbound []InboundEvent
var outbound []OutboundEvent
for i := 0; i < len(events); i++ {
inbound = append(inbound, events[i])
var outboundEvents []OutboundEvent
outboundEvents, err = p.state.Process(events[i])
if err != nil {
return
}
outbound = append(outbound, outboundEvents...)
}
return p.store.Prepare(p.id, p.sequence, p.state, inbound, outbound)
}
// Execute the database transaction. Usually, you'd want to use the Process method,
// this method is used if you need to customise the database transaction.
func (p *Processor) Execute(items []types.TransactWriteItem) error {
return p.store.Execute(items)
}