/
entity.go
52 lines (43 loc) · 1.33 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
package flow
type IDEntity interface {
// ID returns a unique id for this entity using a hash of the immutable
// fields of the entity.
ID() Identifier
}
// Entity defines how flow entities should be defined
// Entities are flat data structures holding multiple data fields.
// Entities don't include nested entities, they only include pointers to
// other entities. For example, they keep a slice of entity commits instead
// of keeping a slice of entity object itself. This simplifies storage, signature and validation
// of entities.
type Entity interface {
IDEntity
// Checksum returns a unique checksum for the entity, including the mutable
// data such as signatures.
Checksum() Identifier
}
func EntitiesToIDs[T Entity](entities []T) []Identifier {
ids := make([]Identifier, 0, len(entities))
for _, entity := range entities {
ids = append(ids, entity.ID())
}
return ids
}
// Deduplicate entities in a slice by the ID method
// The original order of the entities is preserved.
func Deduplicate[T IDEntity](entities []T) []T {
if entities == nil {
return nil
}
seen := make(map[Identifier]struct{}, len(entities))
result := make([]T, 0, len(entities))
for _, entity := range entities {
id := entity.ID()
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
result = append(result, entity)
}
return result
}