-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity.go
330 lines (264 loc) · 8.74 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package gizmo
import (
"errors"
"fmt"
)
// Entity is the most basic structure in the library. It represents an
// arbitrary piece of data that can be represented in different Views and is
// automatically versioned on each save.
type Entity interface {
// Identifier is the unique ID of the Entity object across all Views.
Identifier() int64
// CommitID is the ID of the commit with the specific data in this object.
CommitID() int64
// ViewID is the ID of the Entity that this exists in.
ViewID() int64
// Kind is an identifier of the type of Entity.
Kind() string
// Attributes gets all of the custom attributes.
Attributes() map[string]interface{}
// Attribute gets the value of a custom attribute. If the attribute is not
// found, nil is returned.
Attribute(key string) (interface{}, error)
// Relations gets all of the relations to other Entities.
Relations() map[string][]int64
// RelationsByEntity gets all of the associations between this Entity and an
// Entity of a specified type.
RelationsByEntity(entityType string) ([]int64, error)
}
// EntityUpdater is the interface for modifying the contents of an Entity.
type EntityUpdater interface {
// SetIdentifier sets the unique ID for the Entity object.
SetIdentifier(id int64) error
// SetCommitID sets the commit ID for the Entity object.
SetCommitID(id int64) error
// SetViewID sets the ViewID for the Entity object.
SetViewID(id int64) error
// SetKind sets the type of Entity.
SetKind(str string) error
// SetAttribute sets the value of a custom attribute. It can be used to
// either create or update the attribute.
SetAttribute(key string, value interface{}) error
// RemoveAttribute removes the attribute from the list of custom attributes.
// If the key does not exist, the function is a no-op.
RemoveAttribute(key string) error
// SetRelation creates a mapping between this Entity and another existing
// Entity. If the mapping already exists, nothing is changed.
SetRelation(entityType string, entityID int64) error
// SetRelations overwrites an Entity's relations.
SetRelations(relations map[string][]int64)
// UpdateRelation updates the ID of a mapping between this Entity and another
// previously associated Entity.
UpdateRelation(entityType string, oldID int64, newID int64) error
// RemoveRelation deletes a mapping between this Entity and another Entity.
// If the mapping did not previously exist, an error is thrown.
RemoveRelation(entityType string, entityID int64) error
}
// EntityObject is the default implementation of the Entity interface.
// Its general purpose is to be embedded in the other Entity objects.
type EntityObject struct {
id int64
commitID int64
viewID int64
kind string
attributes map[string]interface{}
relations map[string][]int64
}
// Identifier is the unique ID of the Entity object across all Views.
func (c EntityObject) Identifier() int64 {
return c.id
}
// SetIdentifier sets the unique ID for the Entity object.
func (c *EntityObject) SetIdentifier(id int64) error {
if id == 0 {
return errors.New("Identifier must be greater than 0")
}
c.id = id
return nil
}
// CommitID is the ID of the commit with the specific data in this object.
func (c EntityObject) CommitID() int64 {
return c.commitID
}
// SetCommitID sets the commit ID for the Entity object.
func (c *EntityObject) SetCommitID(commitID int64) error {
if commitID == 0 {
return errors.New("CommitID must be greater than 0")
}
c.commitID = commitID
return nil
}
// ViewID is the ID of the Entity that this exists in.
func (c EntityObject) ViewID() int64 {
return c.viewID
}
// SetViewID sets the ViewID for the Entity object.
func (c *EntityObject) SetViewID(viewID int64) error {
if viewID == 0 {
return errors.New("ViewID must be greater than 0")
}
c.viewID = viewID
return nil
}
// Kind is an identifier of the type of Entity.
func (c EntityObject) Kind() string {
return c.kind
}
// SetKind sets the type of Entity.
func (c *EntityObject) SetKind(kind string) error {
if kind == "" {
return errors.New("Kind must be non-empty")
}
c.kind = kind
return nil
}
// Attributes gets the set of custom attributes.
func (c EntityObject) Attributes() map[string]interface{} {
return c.attributes
}
// Attribute gets the value of a custom attribute. If the attribute is not
// found, nil is returned.
func (c EntityObject) Attribute(key string) (interface{}, error) {
if key == "" {
return nil, errors.New("Attribute key must be non-empty")
}
if c.attributes == nil {
return nil, nil
}
value, found := c.attributes[key]
if !found {
return nil, nil
}
return value, nil
}
// SetAttribute sets the value of a custom attribute. It can be used to
// either create or update the attribute.
func (c *EntityObject) SetAttribute(key string, value interface{}) error {
if key == "" {
return errors.New("Attribute key must be non-empty")
}
if c.attributes == nil {
c.attributes = map[string]interface{}{}
}
c.attributes[key] = value
return nil
}
// RemoveAttribute removes the attribute from the list of custom attributes.
// If the key does not exist, the function is a no-op.
func (c *EntityObject) RemoveAttribute(key string) error {
if key == "" {
return errors.New("Attribute key must be non-empty")
}
if c.attributes == nil {
return nil
}
delete(c.attributes, key)
return nil
}
// Relations gets all of the relations to other Entities.
func (c EntityObject) Relations() map[string][]int64 {
if c.relations == nil {
return map[string][]int64{}
}
return c.relations
}
// RelationsByEntity gets all of the associations between this Entity and an
// Entity of a specified type.
func (c EntityObject) RelationsByEntity(entityType string) ([]int64, error) {
if entityType == "" {
return nil, errors.New("Entity type must be non-empty")
}
if c.relations == nil {
return []int64{}, nil
}
ids, ok := c.relations[entityType]
if !ok {
return []int64{}, nil
}
return ids, nil
}
// SetRelation creates a mapping between this Entity and another existing
// Entity. If the mapping already exists, nothing is changed.
func (c *EntityObject) SetRelation(entityType string, entityID int64) error {
if entityType == "" {
return errors.New("Entity type must be non-empty")
} else if entityID < 1 {
return errors.New("Entity ID must be greater than 0")
}
if c.relations == nil {
c.relations = map[string][]int64{}
}
ids, ok := c.relations[entityType]
if !ok {
ids = []int64{}
}
for _, id := range ids {
if id == entityID {
// The ID is already in the list, meaning the mapping exists.
// Return with no error and no change.
return nil
}
}
c.relations[entityType] = append(ids, entityID)
return nil
}
// SetRelations overwrites an Entity's relations.
func (c *EntityObject) SetRelations(relations map[string][]int64) {
c.relations = relations
}
// UpdateRelation updates the ID of a mapping between this Entity and another
// previously associated Entity.
func (c *EntityObject) UpdateRelation(entityType string, oldID int64, newID int64) error {
if entityType == "" {
return errors.New("Entity type must be non-empty")
} else if oldID < 1 {
return errors.New("Old ID must be greater than 0")
} else if newID < 1 {
return errors.New("New ID must be greater than 0")
} else if c.relations == nil {
return fmt.Errorf("Mapping to %d of type %s is not found", oldID, entityType)
}
ids, ok := c.relations[entityType]
if !ok {
return fmt.Errorf("Mapping to %d of type %s is not found", oldID, entityType)
}
var updatedIDs []int64
for index, id := range ids {
if id == oldID {
updatedIDs = make([]int64, len(ids))
copy(updatedIDs, ids)
updatedIDs[index] = newID
} else if id == newID {
return fmt.Errorf("New mapping to %d of type %s is already an existing mapping", newID, entityType)
}
}
if updatedIDs == nil {
return fmt.Errorf("Mapping to %d of type %s is not found", oldID, entityType)
}
c.relations[entityType] = updatedIDs
return nil
}
// RemoveRelation deletes a mapping between this Entity and another Entity.
// If the mapping did not previously exist, an error is thrown.
func (c *EntityObject) RemoveRelation(entityType string, entityID int64) error {
if entityType == "" {
return errors.New("Entity type must be non-empty")
} else if entityID < 1 {
return errors.New("Entity ID must be greater than 0")
} else if c.relations == nil {
return fmt.Errorf("Mapping to %d of type %s is not found", entityID, entityType)
}
ids, ok := c.relations[entityType]
if !ok {
return fmt.Errorf("Mapping to %d of type %s is not found", entityID, entityType)
}
for index, id := range ids {
if id == entityID {
copy(ids[index:], ids[index+1:])
ids = ids[:len(ids)-1]
c.relations[entityType] = ids
return nil
}
}
return fmt.Errorf("Mapping to %d of type %s is not found", entityID, entityType)
}