/
entity.go
121 lines (106 loc) · 2.97 KB
/
entity.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
package impl
import (
"context"
"github.com/cevixe/aws-sdk-go/aws/model"
"github.com/cevixe/aws-sdk-go/aws/util"
"github.com/cevixe/core-sdk-go/core"
"github.com/pkg/errors"
"strconv"
"time"
)
type EntityImpl struct {
Context context.Context
EventRecord *model.AwsEventRecord
StateRecord *model.AwsStateRecord
}
func NewEntity(ctx context.Context,
stateRecord *model.AwsStateRecord,
eventRecord *model.AwsEventRecord) core.Entity {
if eventRecord != nil {
entityVersion, err := strconv.ParseUint(*eventRecord.EventID, 10, 64)
if err != nil {
panic(errors.Wrap(err, "cannot get entity version"))
}
entityState := make(map[string]interface{})
if eventRecord.EntityState != nil {
entityState = *eventRecord.EntityState
}
return &EntityImpl{
Context: ctx,
StateRecord: &model.AwsStateRecord{
Type: *eventRecord.EntityType,
ID: *eventRecord.EntityID,
Version: entityVersion,
Deleted: eventRecord.EntityDeleted,
State: entityState,
UpdatedAt: *eventRecord.EventTime,
UpdatedBy: *eventRecord.EventAuthor,
CreatedAt: *eventRecord.EntityCreatedAt,
CreatedBy: *eventRecord.EntityCreatedBy,
ContentLocation: eventRecord.ContentLocation,
ContentType: eventRecord.ContentType,
ContentEncoding: eventRecord.ContentEncoding,
Content: eventRecord.Content,
},
EventRecord: nil,
}
}
if stateRecord != nil {
return &EntityImpl{
Context: ctx,
StateRecord: stateRecord,
EventRecord: nil,
}
}
return nil
}
func (e EntityImpl) ID() string {
return e.StateRecord.ID
}
func (e EntityImpl) Type() string {
return e.StateRecord.Type
}
func (e EntityImpl) Version() uint64 {
return e.StateRecord.Version
}
func (e *EntityImpl) State(v interface{}) {
if e.StateRecord.State != nil && len(e.StateRecord.State) > 0 {
json := util.MarshalJson(e.StateRecord.State)
util.UnmarshalJson(json, v)
} else {
if e.EventRecord == nil {
e.EventRecord = GetEventContent(
e.Context,
e.StateRecord.ContentLocation,
e.StateRecord.ContentEncoding,
e.StateRecord.ContentType,
e.StateRecord.Content)
} else if e.EventRecord.EventData == nil {
e.EventRecord = GetEventContent(
e.Context,
e.EventRecord.ContentLocation,
e.EventRecord.ContentEncoding,
e.EventRecord.ContentType,
e.EventRecord.Content)
}
json := util.MarshalJson(e.EventRecord.EntityState)
util.UnmarshalJson(json, v)
}
}
func (e EntityImpl) UpdatedAt() time.Time {
nanoseconds := e.StateRecord.UpdatedAt * int64(time.Millisecond)
return time.Unix(0, nanoseconds)
}
func (e EntityImpl) UpdatedBy() string {
return e.StateRecord.UpdatedBy
}
func (e EntityImpl) CreatedAt() time.Time {
nanoseconds := e.StateRecord.CreatedAt * int64(time.Millisecond)
return time.Unix(0, nanoseconds)
}
func (e EntityImpl) CreatedBy() string {
return e.StateRecord.CreatedBy
}
func (e EntityImpl) IsDeleted() bool {
return e.StateRecord.Deleted
}