-
Notifications
You must be signed in to change notification settings - Fork 7
/
model.go
146 lines (127 loc) · 3.15 KB
/
model.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package events
import (
"encoding/json"
"time"
uuid "github.com/gofrs/uuid"
)
// EventType ...
type EventType string
const (
// EventTypeCreated ...
EventTypeCreated = "CREATED"
// EventTypeUpdated ...
EventTypeUpdated = "UPDATED"
// EventTypeDeleted ...
EventTypeDeleted = "DELETED"
)
// EventDataValue ...
type EventDataValue interface{}
// EventChange ...
type EventChange struct {
Name string `json:"name"`
OldValue string `json:"oldValue"`
NewValue string `json:"newValue"`
}
// SetOldValue ...
func (ec *EventChange) SetOldValue(value interface{}) error {
data, err := json.Marshal(value)
if err == nil {
ec.OldValue = string(data)
}
return err
}
// OldValueAs ...
func (ec *EventChange) OldValueAs(data interface{}) error {
return json.Unmarshal([]byte(ec.OldValue), data)
}
func (ec *EventChange) SetNewValue(value interface{}) error {
data, err := json.Marshal(value)
if err == nil {
ec.NewValue = string(data)
}
return err
}
// NewValueAs ...
func (ec *EventChange) NewValueAs(data interface{}) error {
return json.Unmarshal([]byte(ec.NewValue), data)
}
// EventMetadata ...
type EventMetadata struct {
Type EventType `json:"type"`
Entity string `json:"entity"`
EntityID string `json:"entityId"`
Date time.Time `json:"date"`
PrincipalID *string `json:"principalId"`
}
// Event ...
type Event struct {
EventMetadata
ID string `json:"id"`
Changes []*EventChange `json:"changes"`
OldValues map[string]EventDataValue `json:"oldValues"`
NewValues map[string]EventDataValue `json:"newValues"`
}
// NewEvent ...
func NewEvent(meta EventMetadata) Event {
return Event{
EventMetadata: meta,
ID: uuid.Must(uuid.NewV4()).String(),
Changes: []*EventChange{},
OldValues: map[string]EventDataValue{},
NewValues: map[string]EventDataValue{},
}
}
// HasChangedColumn check if given event has changes on specific column
func (e Event) HasChangedColumn(c string) bool {
for _, col := range e.ChangedColumns() {
if col == c {
return true
}
}
return false
}
// ChangedColumns returns list of names of changed columns
func (e Event) ChangedColumns() []string {
columns := []string{}
for _, change := range e.Changes {
columns = append(columns, change.Name)
}
return columns
}
// Change ...
func (e *Event) Change(column string) (ec *EventChange) {
for _, c := range e.Changes {
if c.Name == column {
ec = c
break
}
}
return
}
// AddNewValue ...
func (e *Event) AddNewValue(column string, v EventDataValue) {
change := e.Change(column)
if change == nil {
c := EventChange{Name: column}
change = &c
change.SetOldValue(nil)
e.Changes = append(e.Changes, change)
}
if err := change.SetNewValue(v); err != nil {
panic("failed to set new value" + err.Error())
}
e.NewValues[column] = v
}
// AddOldValue ...
func (e *Event) AddOldValue(column string, v EventDataValue) {
change := e.Change(column)
if change == nil {
c := EventChange{Name: column}
change = &c
e.Changes = append(e.Changes, change)
}
if err := change.SetOldValue(v); err != nil {
panic("failed to set new value" + err.Error())
}
e.OldValues[column] = v
}