-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation.go
79 lines (70 loc) · 1.6 KB
/
mutation.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
package entity
import (
"context"
"time"
cvxcontext "github.com/cevixe/sdk/context"
)
type Mutation interface {
SetEvent(
eventType string,
eventVersion uint64,
eventData interface{},
) Mutation
Execute() Entity
}
type mutationImpl struct {
Author string
Target Entity
Trigger string
Transaction string
NewEventType string
NewEventVersion uint64
NewEventData interface{}
NewEntityData interface{}
}
func newMutation(
ctx context.Context,
target *entityImpl,
newState interface{},
) Mutation {
cvx := cvxcontext.GetExecutionContenxt(ctx)
return &mutationImpl{
Author: cvx.Author,
Trigger: cvx.Trigger,
Transaction: cvx.Transaction,
Target: target,
NewEntityData: newState,
}
}
func (m *mutationImpl) SetEvent(
eventType string,
eventVersion uint64,
eventData interface{},
) Mutation {
m.NewEventType = eventType
if eventVersion == 0 {
m.NewEventVersion = 1
} else {
m.NewEventVersion = eventVersion
}
m.NewEventData = eventData
return m
}
func (m *mutationImpl) Execute() Entity {
return &entityImpl{
EntityID: m.Target.ID(),
EntityType: m.Target.Type(),
EntityVersion: m.Target.Version() + 1,
EntityStatus: EntityStatus_Alive,
EntityData: m.NewEntityData,
EntityUpdatedBy: m.Author,
EntityUpdatedAt: time.Now(),
EntityCreatedAt: m.Target.CreatedAt(),
EntityCreatedBy: m.Target.CreatedBy(),
LastTransaction: m.Transaction,
LastEventTrigger: m.Trigger,
LastEventType: m.NewEventType,
LastEventVersion: m.NewEventVersion,
LastEventData: m.NewEventData,
}
}