-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity.go
208 lines (172 loc) · 4.41 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gocqrs
import (
"encoding/json"
"errors"
"log"
"reflect"
"strings"
"time"
)
const (
AllRole = "all"
)
type EntityConf struct {
Name string `json:"name"`
CRUD bool `json:"crud"`
Description string `json:"desc"`
// basic entity prefix
StreamPrefix string `json:"stream_prefix"`
CorrelationStream string `json:"correlation_stream"`
// Entity ID references
EntityReferences []EntityReference `json:"references"`
// Event Handlers/Aggregators
EventHandlers map[string]EventHandler `json:"handlers"`
Validators map[string]Validator `json:"validators"`
BaseStruct interface{} `json:"base,omitempty"`
BaseSeted bool
ReadRoles []string `json:"roles,omitempty"`
}
type BasicEntity struct {
AccountID string `json:"accID"`
CreatedBy string `json:"createBy"`
Created time.Time `json:"created,omitempty"`
UpdatedBy string `json:"createBy"`
Updated time.Time `json:"updated,omitempty"`
}
type EntityReference struct {
Entity string `json:"entity"`
Key string `json:"key"`
Null bool `json:"null"`
}
func NewEntityConf(name string) *EntityConf {
var e EntityConf
name = strings.ToLower(name)
e.Name = name
e.Validators = make(map[string]Validator)
e.EventHandlers = make(map[string]EventHandler)
return &e
}
func (e *EntityConf) Reference(en, k string, null bool) {
e.EntityReferences = append(e.EntityReferences, EntityReference{en, k, null})
}
func (e *EntityConf) AddValidator(v ...Validator) error {
var err error
for _, validator := range v {
_, exist := e.Validators[validator.GetName()]
if exist {
log.Fatal("Could not add validator, already set:" + validator.GetName())
} else {
e.Validators[validator.GetName()] = validator
}
}
return err
}
func (e *EntityConf) SetBaseStruct(i interface{}) {
t := reflect.TypeOf(i)
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Interface {
t = t.Elem()
}
switch t.Kind() {
case reflect.Struct:
default:
panic("Invalid type to be base struct")
}
e.BaseStruct = i
e.BaseSeted = true
}
func (e *EntityConf) AddEventHandler(eh ...EventHandler) error {
var err error
if e.EventHandlers == nil {
e.EventHandlers = make(map[string]EventHandler)
}
for _, h := range eh {
for _, event := range h.EventName() {
// replace handler if needed
e.EventHandlers[event] = h
}
}
return err
}
func (e *EntityConf) AddCRUD(checkVersion bool) *EntityConf {
e.CRUD = true
// TODO add basic CRUD
ch := NewCRUDHandler(e.Name)
ch.CheckVersion = checkVersion
e.AddEventHandler(ch)
return e
}
func (ec *EntityConf) Aggregate(id string, events chan Eventer) (*Entity, error) {
var err error
var entity Entity
entity.Data = make(map[string]interface{})
for e := range events {
eventHandler, has := ec.EventHandlers[e.GetType()]
if !has {
return &entity, errors.New("Event " + e.GetType() + " not handled")
}
if e.GetVersion() == entity.Version+1 || e.GetVersion() == 0 {
// replay do not have userid or role
_, err = eventHandler.Handle(id, "", "", "", e, &entity, true)
} else {
return &entity, errors.New("Failed to aggregate entity " + id + " , unorder events")
}
}
entity.ID = id
return &entity, err
}
func (ev EntityConf) checkBase(data map[string]interface{}) error {
i := ev.BaseStruct
t := reflect.TypeOf(i)
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Interface {
t = t.Elem()
}
n := t.NumField()
for k, _ := range data {
has := false
for f := 0; f < n; f++ {
v, hasTag := t.Field(f).Tag.Lookup("json")
if !hasTag {
v = t.Field(f).Name
}
if v == k {
has = true
break
}
}
if !has {
return errors.New("Invalid field, do not exist in base struct: " + k)
}
// check json decode
b, _ := json.Marshal(data)
log.Println(string(b), i)
err := json.Unmarshal(b, &i)
if err != nil {
return errors.New("Invalid type:" + err.Error())
}
}
return nil
}
type Entity struct {
ID string `json:"id"`
Group string `json:"group"`
Version uint64 `json:"version"`
Deleted bool `json:"deleted"`
Data map[string]interface{} `json:"data"`
}
func (e *Entity) Decode(i interface{}) error {
b, err := json.Marshal(e.Data)
if err != nil {
return err
}
err = json.Unmarshal(b, &i)
if err != nil {
return err
}
return err
}
func ToMap(i interface{}) map[string]interface{} {
m := make(map[string]interface{})
d, _ := json.Marshal(i)
json.Unmarshal(d, &m)
return m
}